github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  	"time"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cmd/juju/application"
    13  	jujutesting "github.com/juju/juju/juju/testing"
    14  	"github.com/juju/juju/state"
    15  	"github.com/juju/juju/status"
    16  	"github.com/juju/juju/testcharms"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type ResolvedSuite struct {
    21  	jujutesting.RepoSuite
    22  	testing.CmdBlockHelper
    23  }
    24  
    25  func (s *ResolvedSuite) SetUpTest(c *gc.C) {
    26  	s.RepoSuite.SetUpTest(c)
    27  	s.CmdBlockHelper = testing.NewCmdBlockHelper(s.APIState)
    28  	c.Assert(s.CmdBlockHelper, gc.NotNil)
    29  	s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() })
    30  }
    31  
    32  var _ = gc.Suite(&ResolvedSuite{})
    33  
    34  func runResolved(c *gc.C, args []string) error {
    35  	_, err := testing.RunCommand(c, newResolvedCommand(), args...)
    36  	return err
    37  }
    38  
    39  func runDeploy(c *gc.C, args ...string) error {
    40  	_, err := testing.RunCommand(c, application.NewDefaultDeployCommand(), args...)
    41  	return err
    42  }
    43  
    44  var resolvedTests = []struct {
    45  	args []string
    46  	err  string
    47  	unit string
    48  	mode state.ResolvedMode
    49  }{
    50  	{
    51  		err: `no unit specified`,
    52  	}, {
    53  		args: []string{"jeremy-fisher"},
    54  		err:  `invalid unit name "jeremy-fisher"`,
    55  	}, {
    56  		args: []string{"jeremy-fisher/99"},
    57  		err:  `unit "jeremy-fisher/99" not found \(not found\)`,
    58  	}, {
    59  		args: []string{"dummy/0"},
    60  		err:  `unit "dummy/0" is not in an error state`,
    61  		unit: "dummy/0",
    62  		mode: state.ResolvedNone,
    63  	}, {
    64  		args: []string{"dummy/1", "--no-retry"},
    65  		err:  `unit "dummy/1" is not in an error state`,
    66  		unit: "dummy/1",
    67  		mode: state.ResolvedNone,
    68  	}, {
    69  		args: []string{"dummy/2", "--no-retry"},
    70  		unit: "dummy/2",
    71  		mode: state.ResolvedNoHooks,
    72  	}, {
    73  		args: []string{"dummy/2", "--no-retry"},
    74  		err:  `cannot set resolved mode for unit "dummy/2": already resolved`,
    75  		unit: "dummy/2",
    76  		mode: state.ResolvedNoHooks,
    77  	}, {
    78  		args: []string{"dummy/3"},
    79  		unit: "dummy/3",
    80  		mode: state.ResolvedRetryHooks,
    81  	}, {
    82  		args: []string{"dummy/3"},
    83  		err:  `cannot set resolved mode for unit "dummy/3": already resolved`,
    84  		unit: "dummy/3",
    85  		mode: state.ResolvedRetryHooks,
    86  	}, {
    87  		args: []string{"dummy/4", "roflcopter"},
    88  		err:  `unrecognized args: \["roflcopter"\]`,
    89  	},
    90  }
    91  
    92  func (s *ResolvedSuite) TestResolved(c *gc.C) {
    93  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
    94  	err := runDeploy(c, "-n", "5", ch, "dummy", "--series", "quantal")
    95  	c.Assert(err, jc.ErrorIsNil)
    96  
    97  	// lp:1558657
    98  	now := time.Now()
    99  	for _, name := range []string{"dummy/2", "dummy/3", "dummy/4"} {
   100  		u, err := s.State.Unit(name)
   101  		c.Assert(err, jc.ErrorIsNil)
   102  		sInfo := status.StatusInfo{
   103  			Status:  status.Error,
   104  			Message: "lol borken",
   105  			Since:   &now,
   106  		}
   107  		err = u.SetAgentStatus(sInfo)
   108  		c.Assert(err, jc.ErrorIsNil)
   109  	}
   110  
   111  	for i, t := range resolvedTests {
   112  		c.Logf("test %d: %v", i, t.args)
   113  		err := runResolved(c, t.args)
   114  		if t.err != "" {
   115  			c.Assert(err, gc.ErrorMatches, t.err)
   116  		} else {
   117  			c.Assert(err, jc.ErrorIsNil)
   118  		}
   119  		if t.unit != "" {
   120  			unit, err := s.State.Unit(t.unit)
   121  			c.Assert(err, jc.ErrorIsNil)
   122  			c.Assert(unit.Resolved(), gc.Equals, t.mode)
   123  		}
   124  	}
   125  }
   126  
   127  func (s *ResolvedSuite) TestBlockResolved(c *gc.C) {
   128  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
   129  	err := runDeploy(c, "-n", "5", ch, "dummy", "--series", "quantal")
   130  	c.Assert(err, jc.ErrorIsNil)
   131  
   132  	// lp:1558657
   133  	now := time.Now()
   134  	for _, name := range []string{"dummy/2", "dummy/3", "dummy/4"} {
   135  		u, err := s.State.Unit(name)
   136  		c.Assert(err, jc.ErrorIsNil)
   137  		sInfo := status.StatusInfo{
   138  			Status:  status.Error,
   139  			Message: "lol borken",
   140  			Since:   &now,
   141  		}
   142  		err = u.SetAgentStatus(sInfo)
   143  		c.Assert(err, jc.ErrorIsNil)
   144  	}
   145  
   146  	// Block operation
   147  	s.BlockAllChanges(c, "TestBlockResolved")
   148  	err = runResolved(c, []string{"dummy/2"})
   149  	testing.AssertOperationWasBlocked(c, err, ".*TestBlockResolved.*")
   150  }