1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
jc "github.com/juju/testing/checkers"
11
gc "gopkg.in/check.v1"
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"
19
type CmdSuite struct {
20
coretesting.FakeJujuXDGDataHomeSuite
21
ControllerStore *jujuclienttesting.MemStore
24
var _ = gc.Suite(&CmdSuite{})
26
func (s *CmdSuite) SetUpTest(c *gc.C) {
27
s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
28
s.ControllerStore = jujuclienttesting.NewMemStore()
31
var deployTests = []struct {
36
[]string{"charm-name"},
39
[]string{"charm-name", "application-name"},
40
&DeployCommand{ApplicationName: "application-name"},
42
[]string{"--num-units", "33", "charm-name"},
43
&DeployCommand{UnitCommandBase: UnitCommandBase{NumUnits: 33}},
45
[]string{"-n", "104", "charm-name"},
46
&DeployCommand{UnitCommandBase: UnitCommandBase{NumUnits: 104}},
50
func initExpectations(com *DeployCommand, store jujuclient.ClientStore) {
51
if com.CharmOrBundle == "" {
52
com.CharmOrBundle = "charm-name"
54
if com.NumUnits == 0 {
57
com.SetClientStore(modelcmd.QualifyingClientStore{store})
58
com.SetModelName("controller")
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)
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
74
c.Assert(com.flagSet, gc.NotNil)
76
c.Assert(err, jc.ErrorIsNil)
77
c.Assert(com, jc.DeepEquals, t.com)
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)
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)
97
_, err = initDeployCommand(s.ControllerStore)
98
c.Assert(err, gc.ErrorMatches, "no charm or bundle specified")
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")
106
// environment tested elsewhere
109
func initExposeCommand(args ...string) (*exposeCommand, error) {
110
com := &exposeCommand{}
111
return com, coretesting.InitCommand(modelcmd.Wrap(com), args)
114
func (*CmdSuite) TestExposeCommandInit(c *gc.C) {
116
_, err := initExposeCommand()
117
c.Assert(err, gc.ErrorMatches, "no application name specified")
119
// environment tested elsewhere
122
func initUnexposeCommand(args ...string) (*unexposeCommand, error) {
123
com := &unexposeCommand{}
124
return com, coretesting.InitCommand(modelcmd.Wrap(com), args)
127
func (*CmdSuite) TestUnexposeCommandInit(c *gc.C) {
129
_, err := initUnexposeCommand()
130
c.Assert(err, gc.ErrorMatches, "no application name specified")
132
// environment tested elsewhere
135
func initRemoveUnitCommand(args ...string) (cmd.Command, error) {
136
com := NewRemoveUnitCommand()
137
return com, coretesting.InitCommand(com, args)
140
func (*CmdSuite) TestRemoveUnitCommandInit(c *gc.C) {
142
_, err := initRemoveUnitCommand()
143
c.Assert(err, gc.ErrorMatches, "no units specified")
145
_, err = initRemoveUnitCommand("seven/nine")
146
c.Assert(err, gc.ErrorMatches, `invalid unit name "seven/nine"`)