github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/user/whoami_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package user_test 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/cmd/juju/user" 13 "github.com/juju/juju/jujuclient" 14 "github.com/juju/juju/jujuclient/jujuclienttesting" 15 "github.com/juju/juju/testing" 16 ) 17 18 type WhoAmITestSuite struct { 19 testing.BaseSuite 20 store jujuclient.ClientStore 21 expectedOutput string 22 expectedErr string 23 } 24 25 var _ = gc.Suite(&WhoAmITestSuite{}) 26 27 func (s *WhoAmITestSuite) TestEmptyStore(c *gc.C) { 28 s.expectedOutput = ` 29 There is no current controller. 30 Run juju list-controllers to see available controllers. 31 `[1:] 32 33 s.store = jujuclienttesting.NewMemStore() 34 s.assertWhoAmI(c) 35 } 36 37 func (s *WhoAmITestSuite) TestNoCurrentController(c *gc.C) { 38 s.expectedOutput = ` 39 There is no current controller. 40 Run juju list-controllers to see available controllers. 41 `[1:] 42 43 s.store = &jujuclienttesting.MemStore{ 44 Controllers: map[string]jujuclient.ControllerDetails{ 45 "controller": {}, 46 }, 47 } 48 s.assertWhoAmI(c) 49 } 50 51 func (s *WhoAmITestSuite) TestNoCurrentModel(c *gc.C) { 52 s.expectedOutput = ` 53 Controller: controller 54 Model: <no-current-model> 55 User: admin 56 `[1:] 57 58 s.store = &jujuclienttesting.MemStore{ 59 CurrentControllerName: "controller", 60 Controllers: map[string]jujuclient.ControllerDetails{ 61 "controller": {}, 62 }, 63 Models: map[string]*jujuclient.ControllerModels{ 64 "controller": { 65 Models: map[string]jujuclient.ModelDetails{ 66 "admin/model": {"model-uuid"}, 67 }, 68 }, 69 }, 70 Accounts: map[string]jujuclient.AccountDetails{ 71 "controller": { 72 User: "admin", 73 }, 74 }, 75 } 76 s.assertWhoAmI(c) 77 } 78 79 func (s *WhoAmITestSuite) TestNoCurrentUser(c *gc.C) { 80 s.expectedOutput = ` 81 You are not logged in to controller "controller" and model "admin/model". 82 Run juju login if you want to login. 83 `[1:] 84 85 s.store = &jujuclienttesting.MemStore{ 86 CurrentControllerName: "controller", 87 Controllers: map[string]jujuclient.ControllerDetails{ 88 "controller": {}, 89 }, 90 Models: map[string]*jujuclient.ControllerModels{ 91 "controller": { 92 Models: map[string]jujuclient.ModelDetails{ 93 "admin/model": {"model-uuid"}, 94 }, 95 CurrentModel: "admin/model", 96 }, 97 }, 98 } 99 s.assertWhoAmI(c) 100 } 101 102 func (s *WhoAmITestSuite) assertWhoAmIForUser(c *gc.C, user, format string) { 103 s.store = &jujuclienttesting.MemStore{ 104 CurrentControllerName: "controller", 105 Controllers: map[string]jujuclient.ControllerDetails{ 106 "controller": {}, 107 }, 108 Models: map[string]*jujuclient.ControllerModels{ 109 "controller": { 110 Models: map[string]jujuclient.ModelDetails{ 111 "admin/model": {"model-uuid"}, 112 }, 113 CurrentModel: "admin/model", 114 }, 115 }, 116 Accounts: map[string]jujuclient.AccountDetails{ 117 "controller": { 118 User: user, 119 }, 120 }, 121 } 122 s.assertWhoAmI(c, "--format", format) 123 } 124 125 func (s *WhoAmITestSuite) TestWhoAmISameUser(c *gc.C) { 126 s.expectedOutput = ` 127 Controller: controller 128 Model: model 129 User: admin 130 `[1:] 131 s.assertWhoAmIForUser(c, "admin", "tabular") 132 } 133 134 func (s *WhoAmITestSuite) TestWhoAmIYaml(c *gc.C) { 135 s.expectedOutput = ` 136 controller: controller 137 model: model 138 user: admin 139 `[1:] 140 s.assertWhoAmIForUser(c, "admin", "yaml") 141 } 142 143 func (s *WhoAmITestSuite) TestWhoAmIJson(c *gc.C) { 144 s.expectedOutput = ` 145 {"controller":"controller","model":"model","user":"admin"} 146 `[1:] 147 s.assertWhoAmIForUser(c, "admin", "json") 148 } 149 150 func (s *WhoAmITestSuite) TestWhoAmIDifferentUsersModel(c *gc.C) { 151 s.expectedOutput = ` 152 Controller: controller 153 Model: admin/model 154 User: bob 155 `[1:] 156 s.assertWhoAmIForUser(c, "bob", "tabular") 157 } 158 159 func (s *WhoAmITestSuite) TestFromStoreErr(c *gc.C) { 160 msg := "fail getting current controller" 161 errStore := jujuclienttesting.NewStubStore() 162 errStore.SetErrors(errors.New(msg)) 163 s.store = errStore 164 s.expectedErr = msg 165 s.assertWhoAmIFailed(c) 166 errStore.CheckCallNames(c, "CurrentController") 167 } 168 169 func (s *WhoAmITestSuite) runWhoAmI(c *gc.C, args ...string) (*cmd.Context, error) { 170 return testing.RunCommand(c, user.NewWhoAmICommandForTest(s.store), args...) 171 } 172 173 func (s *WhoAmITestSuite) assertWhoAmIFailed(c *gc.C, args ...string) { 174 _, err := s.runWhoAmI(c, args...) 175 c.Assert(err, gc.ErrorMatches, s.expectedErr) 176 } 177 178 func (s *WhoAmITestSuite) assertWhoAmI(c *gc.C, args ...string) string { 179 context, err := s.runWhoAmI(c, args...) 180 c.Assert(err, jc.ErrorIsNil) 181 output := testing.Stdout(context) 182 if output == "" { 183 output = testing.Stderr(context) 184 } 185 if s.expectedOutput != "" { 186 c.Assert(output, gc.Equals, s.expectedOutput) 187 } 188 return output 189 }