~hduran-8/juju-core/trunk

« back to all changes in this revision

Viewing changes to cmd/juju/adduser_test.go

  • Committer: Tarmac
  • Author(s): Andrew Wilkins
  • Date: 2014-05-13 01:48:02 UTC
  • mfrom: (2713.1.5 envcmd-inversion)
  • Revision ID: tarmac-20140513014802-d1n958os00gw27zf
[r=axwalk] Invert envcmd relationship

Previously we embedded EnvCommandBase in all commands
requring an environment, and EnvCommandBase included
everything (SetFlags, and Init). The problem with this
is that it was easy to miss initialisation of the
EnvCommandBase type (this happened a few times), which
leads to bad things happening like sync-tools destroying
environments.

I have inverted the relationship so that we now have
envcmd.EnvironCommand, an interface that extends Command
with a SetEnvName method, and EnvCommandBase, which
implements EnvironCommand. A new method, envcmd.Wrap takes
an EnvironCommand and creates a Command that calls
SetEnvName prior to the wrapped method's Init method. If
the environment name cannot be determined, the wrapping
command will error out early.

https://codereview.appspot.com/94350045/

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
        "launchpad.net/goyaml"
12
12
        jujutesting "launchpad.net/juju-core/juju/testing"
13
13
 
 
14
        "launchpad.net/juju-core/cmd"
 
15
        "launchpad.net/juju-core/cmd/envcmd"
14
16
        "launchpad.net/juju-core/testing"
15
17
)
16
18
 
22
24
 
23
25
var _ = gc.Suite(&AddUserSuite{})
24
26
 
 
27
func newAddUserCommand() cmd.Command {
 
28
        return envcmd.Wrap(&AddUserCommand{})
 
29
}
 
30
 
25
31
func (s *AddUserSuite) TestAddUser(c *gc.C) {
26
32
 
27
 
        _, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password"})
 
33
        _, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password"})
28
34
        c.Assert(err, gc.IsNil)
29
35
 
30
 
        _, err = testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "newpassword"})
 
36
        _, err = testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "newpassword"})
31
37
        c.Assert(err, gc.ErrorMatches, "Failed to create user: user already exists")
32
38
}
33
39
 
34
40
func (s *AddUserSuite) TestTooManyArgs(c *gc.C) {
35
 
        _, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password", "whoops"})
 
41
        _, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password", "whoops"})
36
42
        c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
37
43
}
38
44
 
39
45
func (s *AddUserSuite) TestNotEnoughArgs(c *gc.C) {
40
 
        _, err := testing.RunCommand(c, &AddUserCommand{}, []string{})
 
46
        _, err := testing.RunCommand(c, newAddUserCommand(), []string{})
41
47
        c.Assert(err, gc.ErrorMatches, `no username supplied`)
42
48
}
43
49
 
50
56
        tempFile, err := ioutil.TempFile("", "adduser-test")
51
57
        tempFile.Close()
52
58
        c.Assert(err, gc.IsNil)
53
 
        _, err = testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password", "-o", tempFile.Name()})
 
59
        _, err = testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password", "-o", tempFile.Name()})
54
60
        c.Assert(err, gc.IsNil)
55
61
        data, err := ioutil.ReadFile(tempFile.Name())
56
62
        result := map[string]interface{}{}
65
71
                "password":      "password",
66
72
                "state-servers": []interface{}{},
67
73
                "ca-cert":       ""}
68
 
        ctx, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password"})
 
74
        ctx, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password"})
69
75
        c.Assert(err, gc.IsNil)
70
76
        stdout := ctx.Stdout.(*bytes.Buffer).Bytes()
71
77
        result := map[string]interface{}{}
80
86
        tempFile, err := ioutil.TempFile("", "adduser-test")
81
87
        tempFile.Close()
82
88
        c.Assert(err, gc.IsNil)
83
 
        _, err = testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password", "-o", tempFile.Name(), "--format", "json"})
 
89
        _, err = testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password", "-o", tempFile.Name(), "--format", "json"})
84
90
        c.Assert(err, gc.IsNil)
85
91
        data, err := ioutil.ReadFile(tempFile.Name())
86
92
        c.Assert(string(data), gc.DeepEquals, expected)
89
95
func (s *AddUserSuite) TestJenvJsonFileOutput(c *gc.C) {
90
96
        expected := `{"User":"foobar","Password":"password","state-servers":null,"ca-cert":""}
91
97
`
92
 
        ctx, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar", "password", "--format", "json"})
 
98
        ctx, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar", "password", "--format", "json"})
93
99
        c.Assert(err, gc.IsNil)
94
100
        stdout := ctx.Stdout.(*bytes.Buffer).String()
95
101
        c.Assert(stdout, gc.DeepEquals, expected)
96
102
}
97
103
 
98
104
func (s *AddUserSuite) TestGeneratePassword(c *gc.C) {
99
 
        ctx, err := testing.RunCommand(c, &AddUserCommand{}, []string{"foobar"})
 
105
        ctx, err := testing.RunCommand(c, newAddUserCommand(), []string{"foobar"})
100
106
        c.Assert(err, gc.IsNil)
101
107
        stdout := ctx.Stdout.(*bytes.Buffer).Bytes()
102
108
        var d map[string]interface{}