github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/model/grantrevoke_test.go (about) 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/common" 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", "read", "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, "sam", "write", "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 = common.OperationBlockedError("TestBlockGrant") 86 _, err := s.run(c, "sam", "read", "foo") 87 testing.AssertOperationWasBlocked(c, err, ".*TestBlockGrant.*") 88 } 89 90 type grantSuite struct { 91 grantRevokeSuite 92 } 93 94 var _ = gc.Suite(&grantSuite{}) 95 96 func (s *grantSuite) SetUpTest(c *gc.C) { 97 s.grantRevokeSuite.SetUpTest(c) 98 s.cmdFactory = func(fake *fakeGrantRevokeAPI) cmd.Command { 99 c, _ := model.NewGrantCommandForTest(fake, s.store) 100 return c 101 } 102 } 103 104 func (s *grantSuite) TestInit(c *gc.C) { 105 wrappedCmd, grantCmd := model.NewGrantCommandForTest(s.fake, s.store) 106 err := testing.InitCommand(wrappedCmd, []string{}) 107 c.Assert(err, gc.ErrorMatches, "no user specified") 108 109 err = testing.InitCommand(wrappedCmd, []string{"bob", "read", "model1", "model2"}) 110 c.Assert(err, jc.ErrorIsNil) 111 112 c.Assert(grantCmd.User, gc.Equals, "bob") 113 c.Assert(grantCmd.ModelNames, jc.DeepEquals, []string{"model1", "model2"}) 114 115 err = testing.InitCommand(wrappedCmd, []string{}) 116 c.Assert(err, gc.ErrorMatches, `no user specified`) 117 } 118 119 type revokeSuite struct { 120 grantRevokeSuite 121 } 122 123 var _ = gc.Suite(&revokeSuite{}) 124 125 func (s *revokeSuite) SetUpTest(c *gc.C) { 126 s.grantRevokeSuite.SetUpTest(c) 127 s.cmdFactory = func(fake *fakeGrantRevokeAPI) cmd.Command { 128 c, _ := model.NewRevokeCommandForTest(fake, s.store) 129 return c 130 } 131 } 132 133 func (s *revokeSuite) TestInit(c *gc.C) { 134 wrappedCmd, revokeCmd := model.NewRevokeCommandForTest(s.fake, s.store) 135 err := testing.InitCommand(wrappedCmd, []string{}) 136 c.Assert(err, gc.ErrorMatches, "no user specified") 137 138 err = testing.InitCommand(wrappedCmd, []string{"bob", "read", "model1", "model2"}) 139 c.Assert(err, jc.ErrorIsNil) 140 141 c.Assert(revokeCmd.User, gc.Equals, "bob") 142 c.Assert(revokeCmd.ModelNames, jc.DeepEquals, []string{"model1", "model2"}) 143 144 err = testing.InitCommand(wrappedCmd, []string{}) 145 c.Assert(err, gc.ErrorMatches, `no user specified`) 146 147 } 148 149 type fakeGrantRevokeAPI struct { 150 err error 151 user string 152 access string 153 modelUUIDs []string 154 } 155 156 func (f *fakeGrantRevokeAPI) Close() error { return nil } 157 158 func (f *fakeGrantRevokeAPI) GrantModel(user, access string, modelUUIDs ...string) error { 159 return f.fake(user, access, modelUUIDs...) 160 } 161 162 func (f *fakeGrantRevokeAPI) RevokeModel(user, access string, modelUUIDs ...string) error { 163 return f.fake(user, access, modelUUIDs...) 164 } 165 166 func (f *fakeGrantRevokeAPI) fake(user, access string, modelUUIDs ...string) error { 167 f.user = user 168 f.access = access 169 f.modelUUIDs = modelUUIDs 170 return f.err 171 }