uniter/upgrade: Added steps for setting installed #31

Merged
merged 1 commit into from Aug 20, 2015
Jump to file or symbol
Failed to load files and symbols.
+163 −3
Split
View
@@ -72,6 +72,10 @@ var upgradeOperations = func() []Operation {
version.MustParse("1.25.0"),
stepsFor125(),
},
+ upgradeToVersion{
+ version.MustParse("1.26.0"),
+ stepsFor126(),
+ },
}
return steps
}
View
@@ -0,0 +1,28 @@
+// Copyright 2015 Canonical Ltd.
+// Licensed under the AGPLv3, see LICENCE file for details.
+
+package upgrades
+
+import (
+ "github.com/juju/juju/worker/uniter"
+ "github.com/juju/names"
+)
+
+// stepsFor126 returns upgrade steps for Juju 1.26.
+func stepsFor126() []Step {
+ return []Step{
+ &upgradeStep{
+ description: "installed boolean needs to be set in the uniter local state",
+ targets: []Target{AllMachines},
+ run: func(context Context) error {
+ config := context.AgentConfig()
+ tag, ok := config.Tag().(names.UnitTag)
+ if !ok {
+ // not a Unit; skipping
+ return nil
+ }
+ return uniter.AddInstalledToUniterState(tag, config.DataDir())
+ },
+ },
+ }
+}
@@ -0,0 +1,24 @@
+// Copyright 2015 Canonical Ltd.
+// Licensed under the AGPLv3, see LICENCE file for details.
+
+package upgrades_test
+
+import (
+ gc "gopkg.in/check.v1"
+
+ "github.com/juju/juju/testing"
+ "github.com/juju/juju/version"
+)
+
+type steps126Suite struct {
+ testing.BaseSuite
+}
+
+var _ = gc.Suite(&steps126Suite{})
+
+func (s *steps126Suite) TestStepsFor126(c *gc.C) {
+ expected := []string{
+ "installed boolean needs to be set in the uniter local state",
+ }
+ assertSteps(c, version.MustParse("1.26.0"), expected)
+}
@@ -678,7 +678,7 @@ func (s *upgradeSuite) TestStateUpgradeOperationsVersions(c *gc.C) {
func (s *upgradeSuite) TestUpgradeOperationsVersions(c *gc.C) {
versions := extractUpgradeVersions(c, (*upgrades.UpgradeOperations)())
- c.Assert(versions, gc.DeepEquals, []string{"1.18.0", "1.22.0", "1.23.0", "1.24.0", "1.25.0"})
+ c.Assert(versions, gc.DeepEquals, []string{"1.18.0", "1.22.0", "1.23.0", "1.24.0", "1.25.0", "1.26.0"})
}
func extractUpgradeVersions(c *gc.C, ops []upgrades.Operation) []string {
@@ -223,7 +223,7 @@ func (f *StateFile) Read() (*State, error) {
// Write stores the supplied state to the file.
func (f *StateFile) Write(st *State) error {
if err := st.validate(); err != nil {
- panic(err)
+ return errors.Trace(err)
}
return utils.WriteYaml(f.path, st)
}
@@ -28,7 +28,6 @@ func AddStoppedFieldToUniterState(tag names.UnitTag, dataDir string) error {
default:
return err
}
-
}
func getUniterStateFile(dataDir string, tag names.UnitTag) string {
@@ -0,0 +1,44 @@
+// Copyright 2015 Canonical Ltd.
+// Licensed under the AGPLv3, see LICENCE file for details.
+
+package uniter
+
+import (
+ "github.com/juju/names"
+ "gopkg.in/juju/charm.v5/hooks"
+
+ "github.com/juju/juju/worker/uniter/operation"
+)
+
+// AddInstalledToUniterState sets the Installed boolean in state to true
+// if the charm has been installed. The only occasion where this is not
+// true is if we are currently installing.
+func AddInstalledToUniterState(tag names.UnitTag, dataDir string) error {
+ logger.Tracef("entering upgrade step AddInstalledToUniterState")
+ defer logger.Tracef("leaving upgrade step AddInstalledToUniterState")
+
+ opsFile := getUniterStateFile(dataDir, tag)
+ state, err := readUnsafe(opsFile)
+ switch err {
+ case nil:
+ return addInstalled(opsFile, state)
+ case operation.ErrNoStateFile:
+ logger.Warningf("no uniter state file found for unit %s, skipping uniter upgrade step", tag)
+ return nil
+ default:
+ return err
+ }
+}
+
+func addInstalled(opsFile string, state *operation.State) error {
+ statefile := operation.NewStateFile(opsFile)
+ if state.Kind == operation.Install {
+ return nil
+ }
+ if state.Kind == operation.RunHook && state.Hook.Kind == hooks.Install {
+ return nil
+ }
+ state.Installed = true
+ return statefile.Write(state)
+ return nil
+}
@@ -0,0 +1,61 @@
+// Copyright 2015 Canonical Ltd.
+// Licensed under the AGPLv3, see LICENCE file for details.
+
+package uniter_test
+
+import (
+ jc "github.com/juju/testing/checkers"
+ gc "gopkg.in/check.v1"
+ "gopkg.in/juju/charm.v5"
+ "gopkg.in/juju/charm.v5/hooks"
+
+ "github.com/juju/juju/worker/uniter"
+ "github.com/juju/juju/worker/uniter/hook"
+ "github.com/juju/juju/worker/uniter/operation"
+)
+
+func (s *upgradeStateContextSuite) TestInstalledBooleanFalseIfInstalling(c *gc.C) {
@wallyworld

wallyworld Aug 20, 2015

Owner

this doesn't check both conditions from the code

+ oldState := &operation.State{
+ Kind: operation.Install,
+ Step: operation.Pending,
+ CharmURL: charm.MustParseURL("local:quantal/charm"),
+ }
+ err := s.statefile.Write(oldState)
+ c.Assert(err, jc.ErrorIsNil)
+ err = uniter.AddInstalledToUniterState(s.unitTag, s.datadir)
+ c.Assert(err, jc.ErrorIsNil)
+ newState := s.readState(c)
+ c.Assert(newState.Installed, gc.Equals, false)
+}
+
+func (s *upgradeStateContextSuite) TestInstalledBooleanFalseIfRunHookInstalling(c *gc.C) {
+ oldState := &operation.State{
+ Kind: operation.RunHook,
+ Step: operation.Pending,
+ Hook: &hook.Info{
+ Kind: hooks.Install,
+ },
+ }
+ err := s.statefile.Write(oldState)
+ c.Assert(err, jc.ErrorIsNil)
+ err = uniter.AddInstalledToUniterState(s.unitTag, s.datadir)
+ c.Assert(err, jc.ErrorIsNil)
+ newState := s.readState(c)
+ c.Assert(newState.Installed, gc.Equals, false)
+}
+
+func (s *upgradeStateContextSuite) TestInstalledBooleanTrueIfInstalled(c *gc.C) {
+ oldState := &operation.State{
+ Kind: operation.Continue,
+ Step: operation.Pending,
+ Hook: &hook.Info{
+ Kind: hooks.Start,
+ },
+ }
+ err := s.statefile.Write(oldState)
+ c.Assert(err, jc.ErrorIsNil)
+ err = uniter.AddInstalledToUniterState(s.unitTag, s.datadir)
+ c.Assert(err, jc.ErrorIsNil)
+ newState := s.readState(c)
+ c.Assert(newState.Installed, gc.Equals, true)
+}