github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/commands/resolved_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/cmd/juju/common"
    11  	"github.com/juju/juju/cmd/juju/service"
    12  	jujutesting "github.com/juju/juju/juju/testing"
    13  	"github.com/juju/juju/state"
    14  	"github.com/juju/juju/status"
    15  	"github.com/juju/juju/testcharms"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type ResolvedSuite struct {
    20  	jujutesting.RepoSuite
    21  	common.CmdBlockHelper
    22  }
    23  
    24  func (s *ResolvedSuite) SetUpTest(c *gc.C) {
    25  	s.RepoSuite.SetUpTest(c)
    26  	s.CmdBlockHelper = common.NewCmdBlockHelper(s.APIState)
    27  	c.Assert(s.CmdBlockHelper, gc.NotNil)
    28  	s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() })
    29  }
    30  
    31  var _ = gc.Suite(&ResolvedSuite{})
    32  
    33  func runResolved(c *gc.C, args []string) error {
    34  	_, err := testing.RunCommand(c, newResolvedCommand(), args...)
    35  	return err
    36  }
    37  
    38  func runDeploy(c *gc.C, args ...string) error {
    39  	_, err := testing.RunCommand(c, service.NewDeployCommand(), args...)
    40  	return err
    41  }
    42  
    43  var resolvedTests = []struct {
    44  	args []string
    45  	err  string
    46  	unit string
    47  	mode state.ResolvedMode
    48  }{
    49  	{
    50  		err: `no unit specified`,
    51  	}, {
    52  		args: []string{"jeremy-fisher"},
    53  		err:  `invalid unit name "jeremy-fisher"`,
    54  	}, {
    55  		args: []string{"jeremy-fisher/99"},
    56  		err:  `unit "jeremy-fisher/99" not found \(not found\)`,
    57  	}, {
    58  		args: []string{"dummy/0"},
    59  		err:  `unit "dummy/0" is not in an error state`,
    60  		unit: "dummy/0",
    61  		mode: state.ResolvedNone,
    62  	}, {
    63  		args: []string{"dummy/1", "--retry"},
    64  		err:  `unit "dummy/1" is not in an error state`,
    65  		unit: "dummy/1",
    66  		mode: state.ResolvedNone,
    67  	}, {
    68  		args: []string{"dummy/2"},
    69  		unit: "dummy/2",
    70  		mode: state.ResolvedNoHooks,
    71  	}, {
    72  		args: []string{"dummy/2", "--retry"},
    73  		err:  `cannot set resolved mode for unit "dummy/2": already resolved`,
    74  		unit: "dummy/2",
    75  		mode: state.ResolvedNoHooks,
    76  	}, {
    77  		args: []string{"dummy/3", "--retry"},
    78  		unit: "dummy/3",
    79  		mode: state.ResolvedRetryHooks,
    80  	}, {
    81  		args: []string{"dummy/3"},
    82  		err:  `cannot set resolved mode for unit "dummy/3": already resolved`,
    83  		unit: "dummy/3",
    84  		mode: state.ResolvedRetryHooks,
    85  	}, {
    86  		args: []string{"dummy/4", "roflcopter"},
    87  		err:  `unrecognized args: \["roflcopter"\]`,
    88  	},
    89  }
    90  
    91  func (s *ResolvedSuite) TestResolved(c *gc.C) {
    92  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
    93  	err := runDeploy(c, "-n", "5", ch, "dummy", "--series", "quantal")
    94  	c.Assert(err, jc.ErrorIsNil)
    95  
    96  	for _, name := range []string{"dummy/2", "dummy/3", "dummy/4"} {
    97  		u, err := s.State.Unit(name)
    98  		c.Assert(err, jc.ErrorIsNil)
    99  		err = u.SetAgentStatus(status.StatusError, "lol borken", nil)
   100  		c.Assert(err, jc.ErrorIsNil)
   101  	}
   102  
   103  	for i, t := range resolvedTests {
   104  		c.Logf("test %d: %v", i, t.args)
   105  		err := runResolved(c, t.args)
   106  		if t.err != "" {
   107  			c.Assert(err, gc.ErrorMatches, t.err)
   108  		} else {
   109  			c.Assert(err, jc.ErrorIsNil)
   110  		}
   111  		if t.unit != "" {
   112  			unit, err := s.State.Unit(t.unit)
   113  			c.Assert(err, jc.ErrorIsNil)
   114  			c.Assert(unit.Resolved(), gc.Equals, t.mode)
   115  		}
   116  	}
   117  }
   118  
   119  func (s *ResolvedSuite) TestBlockResolved(c *gc.C) {
   120  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
   121  	err := runDeploy(c, "-n", "5", ch, "dummy", "--series", "quantal")
   122  	c.Assert(err, jc.ErrorIsNil)
   123  
   124  	for _, name := range []string{"dummy/2", "dummy/3", "dummy/4"} {
   125  		u, err := s.State.Unit(name)
   126  		c.Assert(err, jc.ErrorIsNil)
   127  		err = u.SetAgentStatus(status.StatusError, "lol borken", nil)
   128  		c.Assert(err, jc.ErrorIsNil)
   129  	}
   130  
   131  	// Block operation
   132  	s.BlockAllChanges(c, "TestBlockResolved")
   133  	err = runResolved(c, []string{"dummy/2"})
   134  	s.AssertBlocked(c, err, ".*TestBlockResolved.*")
   135  }