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