~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/model/grantrevoke_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package model_test
 
5
 
 
6
import (
 
7
        "github.com/juju/cmd"
 
8
        jc "github.com/juju/testing/checkers"
 
9
        gc "gopkg.in/check.v1"
 
10
 
 
11
        "github.com/juju/juju/apiserver/params"
 
12
        "github.com/juju/juju/cmd/juju/model"
 
13
        "github.com/juju/juju/jujuclient"
 
14
        "github.com/juju/juju/jujuclient/jujuclienttesting"
 
15
        "github.com/juju/juju/testing"
 
16
)
 
17
 
 
18
type grantRevokeSuite struct {
 
19
        testing.FakeJujuXDGDataHomeSuite
 
20
        fake       *fakeGrantRevokeAPI
 
21
        cmdFactory func(*fakeGrantRevokeAPI) cmd.Command
 
22
        store      *jujuclienttesting.MemStore
 
23
}
 
24
 
 
25
const (
 
26
        fooModelUUID    = "0701e916-3274-46e4-bd12-c31aff89cee3"
 
27
        barModelUUID    = "0701e916-3274-46e4-bd12-c31aff89cee4"
 
28
        bazModelUUID    = "0701e916-3274-46e4-bd12-c31aff89cee5"
 
29
        model1ModelUUID = "0701e916-3274-46e4-bd12-c31aff89cee6"
 
30
        model2ModelUUID = "0701e916-3274-46e4-bd12-c31aff89cee7"
 
31
)
 
32
 
 
33
func (s *grantRevokeSuite) SetUpTest(c *gc.C) {
 
34
        s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
 
35
        s.fake = &fakeGrantRevokeAPI{}
 
36
 
 
37
        // Set up the current controller, and write just enough info
 
38
        // so we don't try to refresh
 
39
        controllerName := "test-master"
 
40
 
 
41
        s.store = jujuclienttesting.NewMemStore()
 
42
        s.store.CurrentControllerName = controllerName
 
43
        s.store.Controllers[controllerName] = jujuclient.ControllerDetails{}
 
44
        s.store.Accounts[controllerName] = jujuclient.AccountDetails{
 
45
                User: "bob@local",
 
46
        }
 
47
        s.store.Models = map[string]*jujuclient.ControllerModels{
 
48
                controllerName: {
 
49
                        Models: map[string]jujuclient.ModelDetails{
 
50
                                "bob@local/foo":    jujuclient.ModelDetails{fooModelUUID},
 
51
                                "bob@local/bar":    jujuclient.ModelDetails{barModelUUID},
 
52
                                "bob@local/baz":    jujuclient.ModelDetails{bazModelUUID},
 
53
                                "bob@local/model1": jujuclient.ModelDetails{model1ModelUUID},
 
54
                                "bob@local/model2": jujuclient.ModelDetails{model2ModelUUID},
 
55
                        },
 
56
                },
 
57
        }
 
58
}
 
59
 
 
60
func (s *grantRevokeSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
 
61
        command := s.cmdFactory(s.fake)
 
62
        return testing.RunCommand(c, command, args...)
 
63
}
 
64
 
 
65
func (s *grantRevokeSuite) TestPassesValues(c *gc.C) {
 
66
        user := "sam"
 
67
        models := []string{fooModelUUID, barModelUUID, bazModelUUID}
 
68
        _, err := s.run(c, "sam", "foo", "bar", "baz")
 
69
        c.Assert(err, jc.ErrorIsNil)
 
70
        c.Assert(s.fake.user, jc.DeepEquals, user)
 
71
        c.Assert(s.fake.modelUUIDs, jc.DeepEquals, models)
 
72
        c.Assert(s.fake.access, gc.Equals, "read")
 
73
}
 
74
 
 
75
func (s *grantRevokeSuite) TestAccess(c *gc.C) {
 
76
        sam := "sam"
 
77
        _, err := s.run(c, "--acl", "write", "sam", "model1", "model2")
 
78
        c.Assert(err, jc.ErrorIsNil)
 
79
        c.Assert(s.fake.user, jc.DeepEquals, sam)
 
80
        c.Assert(s.fake.modelUUIDs, jc.DeepEquals, []string{model1ModelUUID, model2ModelUUID})
 
81
        c.Assert(s.fake.access, gc.Equals, "write")
 
82
}
 
83
 
 
84
func (s *grantRevokeSuite) TestBlockGrant(c *gc.C) {
 
85
        s.fake.err = &params.Error{Code: params.CodeOperationBlocked}
 
86
        _, err := s.run(c, "sam", "foo")
 
87
        c.Assert(err, gc.Equals, cmd.ErrSilent)
 
88
        c.Check(c.GetTestLog(), jc.Contains, "To unblock changes")
 
89
}
 
90
 
 
91
type grantSuite struct {
 
92
        grantRevokeSuite
 
93
}
 
94
 
 
95
var _ = gc.Suite(&grantSuite{})
 
96
 
 
97
func (s *grantSuite) SetUpTest(c *gc.C) {
 
98
        s.grantRevokeSuite.SetUpTest(c)
 
99
        s.cmdFactory = func(fake *fakeGrantRevokeAPI) cmd.Command {
 
100
                c, _ := model.NewGrantCommandForTest(fake, s.store)
 
101
                return c
 
102
        }
 
103
}
 
104
 
 
105
func (s *grantSuite) TestInit(c *gc.C) {
 
106
        wrappedCmd, grantCmd := model.NewGrantCommandForTest(s.fake, s.store)
 
107
        err := testing.InitCommand(wrappedCmd, []string{})
 
108
        c.Assert(err, gc.ErrorMatches, "no user specified")
 
109
 
 
110
        err = testing.InitCommand(wrappedCmd, []string{"bob", "model1", "model2"})
 
111
        c.Assert(err, jc.ErrorIsNil)
 
112
 
 
113
        c.Assert(grantCmd.User, gc.Equals, "bob")
 
114
        c.Assert(grantCmd.ModelNames, jc.DeepEquals, []string{"model1", "model2"})
 
115
 
 
116
        err = testing.InitCommand(wrappedCmd, []string{})
 
117
        c.Assert(err, gc.ErrorMatches, `no user specified`)
 
118
 
 
119
        err = testing.InitCommand(wrappedCmd, []string{"nomodel"})
 
120
        c.Assert(err, gc.ErrorMatches, `no model specified`)
 
121
}
 
122
 
 
123
type revokeSuite struct {
 
124
        grantRevokeSuite
 
125
}
 
126
 
 
127
var _ = gc.Suite(&revokeSuite{})
 
128
 
 
129
func (s *revokeSuite) SetUpTest(c *gc.C) {
 
130
        s.grantRevokeSuite.SetUpTest(c)
 
131
        s.cmdFactory = func(fake *fakeGrantRevokeAPI) cmd.Command {
 
132
                c, _ := model.NewRevokeCommandForTest(fake, s.store)
 
133
                return c
 
134
        }
 
135
}
 
136
 
 
137
func (s *revokeSuite) TestInit(c *gc.C) {
 
138
        wrappedCmd, revokeCmd := model.NewRevokeCommandForTest(s.fake, s.store)
 
139
        err := testing.InitCommand(wrappedCmd, []string{})
 
140
        c.Assert(err, gc.ErrorMatches, "no user specified")
 
141
 
 
142
        err = testing.InitCommand(wrappedCmd, []string{"bob", "model1", "model2"})
 
143
        c.Assert(err, jc.ErrorIsNil)
 
144
 
 
145
        c.Assert(revokeCmd.User, gc.Equals, "bob")
 
146
        c.Assert(revokeCmd.ModelNames, jc.DeepEquals, []string{"model1", "model2"})
 
147
 
 
148
        err = testing.InitCommand(wrappedCmd, []string{})
 
149
        c.Assert(err, gc.ErrorMatches, `no user specified`)
 
150
 
 
151
        err = testing.InitCommand(wrappedCmd, []string{"nomodel"})
 
152
        c.Assert(err, gc.ErrorMatches, `no model specified`)
 
153
}
 
154
 
 
155
type fakeGrantRevokeAPI struct {
 
156
        err        error
 
157
        user       string
 
158
        access     string
 
159
        modelUUIDs []string
 
160
}
 
161
 
 
162
func (f *fakeGrantRevokeAPI) Close() error { return nil }
 
163
 
 
164
func (f *fakeGrantRevokeAPI) GrantModel(user, access string, modelUUIDs ...string) error {
 
165
        return f.fake(user, access, modelUUIDs...)
 
166
}
 
167
 
 
168
func (f *fakeGrantRevokeAPI) RevokeModel(user, access string, modelUUIDs ...string) error {
 
169
        return f.fake(user, access, modelUUIDs...)
 
170
}
 
171
 
 
172
func (f *fakeGrantRevokeAPI) fake(user, access string, modelUUIDs ...string) error {
 
173
        f.user = user
 
174
        f.access = access
 
175
        f.modelUUIDs = modelUUIDs
 
176
        return f.err
 
177
}