Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
uniter/upgrade: Added steps for setting installed #31
Merged
wallyworld
merged 1 commit into
wallyworld:uniter-solver
from
mattyw:install-bool-upgrade-step
Aug 20, 2015
Jump to file or symbol
Failed to load files and symbols.
| @@ -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) | ||
| +} |
| @@ -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) { | ||
|
|
||
| + 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) | ||
| +} | ||
this doesn't check both conditions from the code