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