github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/operation/factory_test.go (about)

     1  // Copyright 2014-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package operation_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	utilexec "github.com/juju/utils/exec"
    10  	gc "gopkg.in/check.v1"
    11  	corecharm "gopkg.in/juju/charm.v6"
    12  	"gopkg.in/juju/charm.v6/hooks"
    13  
    14  	"github.com/juju/juju/worker/uniter/hook"
    15  	"github.com/juju/juju/worker/uniter/operation"
    16  )
    17  
    18  type FactorySuite struct {
    19  	testing.IsolationSuite
    20  	factory operation.Factory
    21  }
    22  
    23  var _ = gc.Suite(&FactorySuite{})
    24  
    25  func (s *FactorySuite) SetUpTest(c *gc.C) {
    26  	s.IsolationSuite.SetUpTest(c)
    27  	// Yes, this factory will produce useless ops; this suite is just for
    28  	// verifying that inadequate args to the factory methods will produce
    29  	// the expected errors; and that the results of same get a string
    30  	// representation that does not depend on the factory attributes.
    31  	s.factory = operation.NewFactory(operation.FactoryParams{})
    32  }
    33  
    34  func (s *FactorySuite) testNewDeployError(c *gc.C, newDeploy newDeploy) {
    35  	op, err := newDeploy(s.factory, nil)
    36  	c.Check(op, gc.IsNil)
    37  	c.Check(err, gc.ErrorMatches, "charm url required")
    38  }
    39  
    40  func (s *FactorySuite) TestNewInstallError(c *gc.C) {
    41  	s.testNewDeployError(c, (operation.Factory).NewInstall)
    42  }
    43  
    44  func (s *FactorySuite) TestNewUpgradeError(c *gc.C) {
    45  	s.testNewDeployError(c, (operation.Factory).NewUpgrade)
    46  }
    47  
    48  func (s *FactorySuite) TestNewRevertUpgradeError(c *gc.C) {
    49  	s.testNewDeployError(c, (operation.Factory).NewRevertUpgrade)
    50  }
    51  
    52  func (s *FactorySuite) TestNewResolvedUpgradeError(c *gc.C) {
    53  	s.testNewDeployError(c, (operation.Factory).NewResolvedUpgrade)
    54  }
    55  
    56  func (s *FactorySuite) testNewDeployString(c *gc.C, newDeploy newDeploy, expectPrefix string) {
    57  	op, err := newDeploy(s.factory, corecharm.MustParseURL("cs:quantal/wordpress-1"))
    58  	c.Check(err, jc.ErrorIsNil)
    59  	c.Check(op.String(), gc.Equals, expectPrefix+" cs:quantal/wordpress-1")
    60  }
    61  
    62  func (s *FactorySuite) TestNewInstallString(c *gc.C) {
    63  	s.testNewDeployString(c, (operation.Factory).NewInstall, "install")
    64  }
    65  
    66  func (s *FactorySuite) TestNewUpgradeString(c *gc.C) {
    67  	s.testNewDeployString(c, (operation.Factory).NewUpgrade, "upgrade to")
    68  }
    69  
    70  func (s *FactorySuite) TestNOOpUpgradeString(c *gc.C) {
    71  	s.testNewDeployString(c, (operation.Factory).NewNoOpUpgrade, "skip no-op upgrade operation to")
    72  }
    73  
    74  func (s *FactorySuite) TestNewRevertUpgradeString(c *gc.C) {
    75  	s.testNewDeployString(c,
    76  		(operation.Factory).NewRevertUpgrade,
    77  		"switch upgrade to",
    78  	)
    79  }
    80  
    81  func (s *FactorySuite) TestNewResolvedUpgradeString(c *gc.C) {
    82  	s.testNewDeployString(c,
    83  		(operation.Factory).NewResolvedUpgrade,
    84  		"continue upgrade to",
    85  	)
    86  }
    87  
    88  func (s *FactorySuite) TestNewActionError(c *gc.C) {
    89  	op, err := s.factory.NewAction("lol-something")
    90  	c.Check(op, gc.IsNil)
    91  	c.Check(err, gc.ErrorMatches, `invalid action id "lol-something"`)
    92  }
    93  
    94  func (s *FactorySuite) TestNewActionString(c *gc.C) {
    95  	op, err := s.factory.NewAction(someActionId)
    96  	c.Check(err, jc.ErrorIsNil)
    97  	c.Check(op.String(), gc.Equals, "run action "+someActionId)
    98  }
    99  
   100  func panicSendResponse(*utilexec.ExecResponse, error) {
   101  	panic("don't call this")
   102  }
   103  
   104  func commandArgs(commands string, relationId int, remoteUnit string) operation.CommandArgs {
   105  	return operation.CommandArgs{
   106  		Commands:       commands,
   107  		RelationId:     relationId,
   108  		RemoteUnitName: remoteUnit,
   109  	}
   110  }
   111  
   112  func (s *FactorySuite) TestNewCommandsSendResponseError(c *gc.C) {
   113  	op, err := s.factory.NewCommands(commandArgs("anything", -1, ""), nil)
   114  	c.Check(op, gc.IsNil)
   115  	c.Check(err, gc.ErrorMatches, "response sender required")
   116  }
   117  
   118  func (s *FactorySuite) testNewCommandsArgsError(
   119  	c *gc.C, args operation.CommandArgs, expect string,
   120  ) {
   121  	op, err := s.factory.NewCommands(args, panicSendResponse)
   122  	c.Check(op, gc.IsNil)
   123  	c.Check(err, gc.ErrorMatches, expect)
   124  }
   125  
   126  func (s *FactorySuite) TestNewCommandsArgsError_NoCommand(c *gc.C) {
   127  	s.testNewCommandsArgsError(c,
   128  		commandArgs("", -1, ""),
   129  		"commands required",
   130  	)
   131  }
   132  
   133  func (s *FactorySuite) TestNewCommandsArgsError_BadRemoteUnit(c *gc.C) {
   134  	s.testNewCommandsArgsError(c,
   135  		commandArgs("any old thing", -1, "unit/1"),
   136  		"remote unit not valid without relation",
   137  	)
   138  }
   139  
   140  func (s *FactorySuite) TestNewCommandsArgsError_BadRemoteUnitName(c *gc.C) {
   141  	s.testNewCommandsArgsError(c,
   142  		commandArgs("any old thing", 0, "lol"),
   143  		`invalid remote unit name "lol"`,
   144  	)
   145  }
   146  
   147  func (s *FactorySuite) testNewCommandsString(
   148  	c *gc.C, args operation.CommandArgs, expect string,
   149  ) {
   150  	op, err := s.factory.NewCommands(args, panicSendResponse)
   151  	c.Check(err, jc.ErrorIsNil)
   152  	c.Check(op.String(), gc.Equals, expect)
   153  }
   154  
   155  func (s *FactorySuite) TestNewCommandsString_CommandsOnly(c *gc.C) {
   156  	s.testNewCommandsString(c,
   157  		commandArgs("anything", -1, ""),
   158  		"run commands",
   159  	)
   160  }
   161  
   162  func (s *FactorySuite) TestNewCommandsString_WithRelation(c *gc.C) {
   163  	s.testNewCommandsString(c,
   164  		commandArgs("anything", 0, ""),
   165  		"run commands (0)",
   166  	)
   167  }
   168  
   169  func (s *FactorySuite) TestNewCommandsString_WithRelationAndUnit(c *gc.C) {
   170  	s.testNewCommandsString(c,
   171  		commandArgs("anything", 3, "unit/123"),
   172  		"run commands (3; unit/123)",
   173  	)
   174  }
   175  
   176  func (s *FactorySuite) testNewHookError(c *gc.C, newHook newHook) {
   177  	op, err := newHook(s.factory, hook.Info{Kind: hooks.Kind("gibberish")})
   178  	c.Check(op, gc.IsNil)
   179  	c.Check(err, gc.ErrorMatches, `unknown hook kind "gibberish"`)
   180  }
   181  
   182  func (s *FactorySuite) TestNewHookError_Run(c *gc.C) {
   183  	s.testNewHookError(c, (operation.Factory).NewRunHook)
   184  }
   185  
   186  func (s *FactorySuite) TestNewHookError_Skip(c *gc.C) {
   187  	s.testNewHookError(c, (operation.Factory).NewSkipHook)
   188  }
   189  
   190  func (s *FactorySuite) TestNewHookString_Run(c *gc.C) {
   191  	op, err := s.factory.NewRunHook(hook.Info{Kind: hooks.Install})
   192  	c.Check(err, jc.ErrorIsNil)
   193  	c.Check(op.String(), gc.Equals, "run install hook")
   194  }
   195  
   196  func (s *FactorySuite) TestNewHookString_Skip(c *gc.C) {
   197  	op, err := s.factory.NewSkipHook(hook.Info{
   198  		Kind:       hooks.RelationJoined,
   199  		RemoteUnit: "foo/22",
   200  		RelationId: 123,
   201  	})
   202  	c.Check(err, jc.ErrorIsNil)
   203  	c.Check(op.String(), gc.Equals, "skip run relation-joined (123; foo/22) hook")
   204  }
   205  
   206  func (s *FactorySuite) TestNewAcceptLeadershipString(c *gc.C) {
   207  	op, err := s.factory.NewAcceptLeadership()
   208  	c.Assert(err, jc.ErrorIsNil)
   209  	c.Assert(op.String(), gc.Equals, "accept leadership")
   210  }
   211  
   212  func (s *FactorySuite) TestNewResignLeadershipString(c *gc.C) {
   213  	op, err := s.factory.NewResignLeadership()
   214  	c.Assert(err, jc.ErrorIsNil)
   215  	c.Assert(op.String(), gc.Equals, "resign leadership")
   216  }