github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/cmd_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package service 5 6 import ( 7 "os" 8 9 "github.com/juju/cmd" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cmd/modelcmd" 14 "github.com/juju/juju/juju/testing" 15 "github.com/juju/juju/jujuclient" 16 coretesting "github.com/juju/juju/testing" 17 ) 18 19 type CmdSuite struct { 20 testing.JujuConnSuite 21 } 22 23 var _ = gc.Suite(&CmdSuite{}) 24 25 var deployTests = []struct { 26 args []string 27 com *DeployCommand 28 }{ 29 { 30 []string{"charm-name"}, 31 &DeployCommand{}, 32 }, { 33 []string{"charm-name", "service-name"}, 34 &DeployCommand{ServiceName: "service-name"}, 35 }, { 36 []string{"--num-units", "33", "charm-name"}, 37 &DeployCommand{UnitCommandBase: UnitCommandBase{NumUnits: 33}}, 38 }, { 39 []string{"-n", "104", "charm-name"}, 40 &DeployCommand{UnitCommandBase: UnitCommandBase{NumUnits: 104}}, 41 }, 42 } 43 44 func initExpectations(com *DeployCommand, store jujuclient.ClientStore) { 45 if com.CharmOrBundle == "" { 46 com.CharmOrBundle = "charm-name" 47 } 48 if com.NumUnits == 0 { 49 com.NumUnits = 1 50 } 51 com.SetClientStore(store) 52 com.SetModelName("admin") 53 } 54 55 func initDeployCommand(store jujuclient.ClientStore, args ...string) (*DeployCommand, error) { 56 com := &DeployCommand{} 57 com.SetClientStore(store) 58 return com, coretesting.InitCommand(modelcmd.Wrap(com), args) 59 } 60 61 func (s *CmdSuite) TestDeployCommandInit(c *gc.C) { 62 for i, t := range deployTests { 63 c.Logf("\ntest %d: args %q", i, t.args) 64 initExpectations(t.com, s.ControllerStore) 65 com, err := initDeployCommand(s.ControllerStore, t.args...) 66 // Testing that the flag set is populated is good enough for the scope 67 // of this test. 68 c.Assert(com.flagSet, gc.NotNil) 69 com.flagSet = nil 70 c.Assert(err, jc.ErrorIsNil) 71 c.Assert(com, jc.DeepEquals, t.com) 72 } 73 74 // test relative --config path 75 ctx := coretesting.Context(c) 76 expected := []byte("test: data") 77 path := ctx.AbsPath("testconfig.yaml") 78 file, err := os.Create(path) 79 c.Assert(err, jc.ErrorIsNil) 80 _, err = file.Write(expected) 81 c.Assert(err, jc.ErrorIsNil) 82 file.Close() 83 84 com, err := initDeployCommand(s.ControllerStore, "--config", "testconfig.yaml", "charm-name") 85 c.Assert(err, jc.ErrorIsNil) 86 actual, err := com.Config.Read(ctx) 87 c.Assert(err, jc.ErrorIsNil) 88 c.Assert(expected, gc.DeepEquals, actual) 89 90 // missing args 91 _, err = initDeployCommand(s.ControllerStore) 92 c.Assert(err, gc.ErrorMatches, "no charm or bundle specified") 93 94 // bad unit count 95 _, err = initDeployCommand(s.ControllerStore, "charm-name", "--num-units", "0") 96 c.Assert(err, gc.ErrorMatches, "--num-units must be a positive integer") 97 _, err = initDeployCommand(s.ControllerStore, "charm-name", "-n", "0") 98 c.Assert(err, gc.ErrorMatches, "--num-units must be a positive integer") 99 100 // environment tested elsewhere 101 } 102 103 func initExposeCommand(args ...string) (*exposeCommand, error) { 104 com := &exposeCommand{} 105 return com, coretesting.InitCommand(modelcmd.Wrap(com), args) 106 } 107 108 func (*CmdSuite) TestExposeCommandInit(c *gc.C) { 109 // missing args 110 _, err := initExposeCommand() 111 c.Assert(err, gc.ErrorMatches, "no service name specified") 112 113 // environment tested elsewhere 114 } 115 116 func initUnexposeCommand(args ...string) (*unexposeCommand, error) { 117 com := &unexposeCommand{} 118 return com, coretesting.InitCommand(modelcmd.Wrap(com), args) 119 } 120 121 func (*CmdSuite) TestUnexposeCommandInit(c *gc.C) { 122 // missing args 123 _, err := initUnexposeCommand() 124 c.Assert(err, gc.ErrorMatches, "no service name specified") 125 126 // environment tested elsewhere 127 } 128 129 func initRemoveUnitCommand(args ...string) (cmd.Command, error) { 130 com := NewRemoveUnitCommand() 131 return com, coretesting.InitCommand(com, args) 132 } 133 134 func (*CmdSuite) TestRemoveUnitCommandInit(c *gc.C) { 135 // missing args 136 _, err := initRemoveUnitCommand() 137 c.Assert(err, gc.ErrorMatches, "no units specified") 138 // not a unit 139 _, err = initRemoveUnitCommand("seven/nine") 140 c.Assert(err, gc.ErrorMatches, `invalid unit name "seven/nine"`) 141 }