github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/controller/listmodels_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller_test 5 6 import ( 7 "time" 8 9 "github.com/juju/cmd" 10 "github.com/juju/names" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/api/base" 15 "github.com/juju/juju/apiserver/common" 16 "github.com/juju/juju/apiserver/params" 17 "github.com/juju/juju/cmd/juju/controller" 18 "github.com/juju/juju/cmd/modelcmd" 19 "github.com/juju/juju/jujuclient" 20 "github.com/juju/juju/jujuclient/jujuclienttesting" 21 "github.com/juju/juju/status" 22 "github.com/juju/juju/testing" 23 ) 24 25 type ModelsSuite struct { 26 testing.FakeJujuXDGDataHomeSuite 27 api *fakeModelMgrAPIClient 28 store *jujuclienttesting.MemStore 29 } 30 31 var _ = gc.Suite(&ModelsSuite{}) 32 33 type fakeModelMgrAPIClient struct { 34 err error 35 user string 36 models []base.UserModel 37 all bool 38 } 39 40 func (f *fakeModelMgrAPIClient) Close() error { 41 return nil 42 } 43 44 func (f *fakeModelMgrAPIClient) ListModels(user string) ([]base.UserModel, error) { 45 if f.err != nil { 46 return nil, f.err 47 } 48 49 f.user = user 50 return f.models, nil 51 } 52 53 func (f *fakeModelMgrAPIClient) AllModels() ([]base.UserModel, error) { 54 if f.err != nil { 55 return nil, f.err 56 } 57 f.all = true 58 return f.models, nil 59 } 60 61 func (f *fakeModelMgrAPIClient) ModelInfo(tags []names.ModelTag) ([]params.ModelInfoResult, error) { 62 results := make([]params.ModelInfoResult, len(tags)) 63 for i, tag := range tags { 64 for _, model := range f.models { 65 if model.UUID != tag.Id() { 66 continue 67 } 68 result := ¶ms.ModelInfo{ 69 Name: model.Name, 70 UUID: model.UUID, 71 OwnerTag: names.NewUserTag(model.Owner).String(), 72 } 73 switch model.Name { 74 case "test-model1": 75 last1 := time.Date(2015, 3, 20, 0, 0, 0, 0, time.UTC) 76 result.Status.Status = status.StatusActive 77 if f.user != "" { 78 result.Users = []params.ModelUserInfo{{ 79 UserName: f.user, 80 LastConnection: &last1, 81 }} 82 } 83 case "test-model2": 84 last2 := time.Date(2015, 3, 1, 0, 0, 0, 0, time.UTC) 85 result.Status.Status = status.StatusActive 86 if f.user != "" { 87 result.Users = []params.ModelUserInfo{{ 88 UserName: f.user, 89 LastConnection: &last2, 90 }} 91 } 92 case "test-model3": 93 result.Status.Status = status.StatusDestroying 94 } 95 results[i].Result = result 96 } 97 } 98 return results, nil 99 } 100 101 func (s *ModelsSuite) SetUpTest(c *gc.C) { 102 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 103 104 err := modelcmd.WriteCurrentController("fake") 105 c.Assert(err, jc.ErrorIsNil) 106 107 models := []base.UserModel{ 108 { 109 Name: "test-model1", 110 Owner: "user-admin@local", 111 UUID: "test-model1-UUID", 112 }, { 113 Name: "test-model2", 114 Owner: "user-admin@local", 115 UUID: "test-model2-UUID", 116 }, { 117 Name: "test-model3", 118 Owner: "user-admin@local", 119 UUID: "test-model3-UUID", 120 }, 121 } 122 s.api = &fakeModelMgrAPIClient{ 123 models: models, 124 user: "admin@local", 125 } 126 s.store = jujuclienttesting.NewMemStore() 127 s.store.Controllers["fake"] = jujuclient.ControllerDetails{} 128 s.store.Models["fake"] = jujuclient.ControllerAccountModels{ 129 AccountModels: map[string]*jujuclient.AccountModels{ 130 "admin@local": { 131 CurrentModel: "test-model1", 132 }, 133 }, 134 } 135 s.store.Accounts["fake"] = &jujuclient.ControllerAccounts{ 136 Accounts: map[string]jujuclient.AccountDetails{ 137 "admin@local": { 138 User: "admin@local", 139 Password: "password", 140 }, 141 }, 142 CurrentAccount: "admin@local", 143 } 144 } 145 146 func (s *ModelsSuite) newCommand() cmd.Command { 147 return controller.NewListModelsCommandForTest(s.api, s.api, s.store) 148 } 149 150 func (s *ModelsSuite) checkSuccess(c *gc.C, user string, args ...string) { 151 context, err := testing.RunCommand(c, s.newCommand(), args...) 152 c.Assert(err, jc.ErrorIsNil) 153 c.Assert(s.api.user, gc.Equals, user) 154 c.Assert(testing.Stdout(context), gc.Equals, ""+ 155 "NAME OWNER STATUS LAST CONNECTION\n"+ 156 "test-model1* user-admin@local active 2015-03-20\n"+ 157 "test-model2 user-admin@local active 2015-03-01\n"+ 158 "test-model3 user-admin@local destroying never connected\n"+ 159 "\n") 160 } 161 162 func (s *ModelsSuite) TestModels(c *gc.C) { 163 s.checkSuccess(c, "admin@local") 164 s.checkSuccess(c, "bob", "--user", "bob") 165 } 166 167 func (s *ModelsSuite) TestAllModels(c *gc.C) { 168 context, err := testing.RunCommand(c, s.newCommand(), "--all") 169 c.Assert(err, jc.ErrorIsNil) 170 c.Assert(s.api.all, jc.IsTrue) 171 c.Assert(testing.Stdout(context), gc.Equals, ""+ 172 "NAME OWNER STATUS LAST CONNECTION\n"+ 173 "test-model1* user-admin@local active 2015-03-20\n"+ 174 "test-model2 user-admin@local active 2015-03-01\n"+ 175 "test-model3 user-admin@local destroying never connected\n"+ 176 "\n") 177 } 178 179 func (s *ModelsSuite) TestAllModelsNoneCurrent(c *gc.C) { 180 delete(s.store.Models, "fake") 181 context, err := testing.RunCommand(c, s.newCommand()) 182 c.Assert(err, jc.ErrorIsNil) 183 c.Assert(testing.Stdout(context), gc.Equals, ""+ 184 "NAME OWNER STATUS LAST CONNECTION\n"+ 185 "test-model1 user-admin@local active 2015-03-20\n"+ 186 "test-model2 user-admin@local active 2015-03-01\n"+ 187 "test-model3 user-admin@local destroying never connected\n"+ 188 "\n") 189 } 190 191 func (s *ModelsSuite) TestModelsUUID(c *gc.C) { 192 context, err := testing.RunCommand(c, s.newCommand(), "--uuid") 193 c.Assert(err, jc.ErrorIsNil) 194 c.Assert(s.api.user, gc.Equals, "admin@local") 195 c.Assert(testing.Stdout(context), gc.Equals, ""+ 196 "NAME MODEL UUID OWNER STATUS LAST CONNECTION\n"+ 197 "test-model1* test-model1-UUID user-admin@local active 2015-03-20\n"+ 198 "test-model2 test-model2-UUID user-admin@local active 2015-03-01\n"+ 199 "test-model3 test-model3-UUID user-admin@local destroying never connected\n"+ 200 "\n") 201 } 202 203 func (s *ModelsSuite) TestUnrecognizedArg(c *gc.C) { 204 _, err := testing.RunCommand(c, s.newCommand(), "whoops") 205 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`) 206 } 207 208 func (s *ModelsSuite) TestModelsError(c *gc.C) { 209 s.api.err = common.ErrPerm 210 _, err := testing.RunCommand(c, s.newCommand()) 211 c.Assert(err, gc.ErrorMatches, "cannot list models: permission denied") 212 }