github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/model/show_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for info. 3 4 package model_test 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "github.com/juju/names" 11 gitjujutesting "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/cmd/juju/model" 17 "github.com/juju/juju/cmd/modelcmd" 18 "github.com/juju/juju/jujuclient" 19 "github.com/juju/juju/jujuclient/jujuclienttesting" 20 "github.com/juju/juju/status" 21 "github.com/juju/juju/testing" 22 ) 23 24 type ShowCommandSuite struct { 25 testing.FakeJujuXDGDataHomeSuite 26 fake fakeModelShowClient 27 store *jujuclienttesting.MemStore 28 expectedOutput attrs 29 } 30 31 var _ = gc.Suite(&ShowCommandSuite{}) 32 33 type fakeModelShowClient struct { 34 gitjujutesting.Stub 35 info params.ModelInfo 36 err *params.Error 37 } 38 39 func (f *fakeModelShowClient) Close() error { 40 f.MethodCall(f, "Close") 41 return f.NextErr() 42 } 43 44 func (f *fakeModelShowClient) ModelInfo(tags []names.ModelTag) ([]params.ModelInfoResult, error) { 45 f.MethodCall(f, "ModelInfo", tags) 46 if len(tags) != 1 { 47 return nil, errors.Errorf("expected 1 tag, got %d", len(tags)) 48 } 49 if tags[0] != testing.ModelTag { 50 return nil, errors.Errorf("expected %s, got %s", testing.ModelTag.Id(), tags[0].Id()) 51 } 52 return []params.ModelInfoResult{{Result: &f.info, Error: f.err}}, f.NextErr() 53 } 54 55 type attrs map[string]interface{} 56 57 func (s *ShowCommandSuite) SetUpTest(c *gc.C) { 58 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 59 lastConnection := time.Date(2015, 3, 20, 0, 0, 0, 0, time.UTC) 60 statusSince := time.Date(2016, 4, 5, 0, 0, 0, 0, time.UTC) 61 62 users := []params.ModelUserInfo{{ 63 UserName: "admin@local", 64 LastConnection: &lastConnection, 65 Access: "write", 66 }, { 67 UserName: "bob@local", 68 DisplayName: "Bob", 69 Access: "read", 70 }} 71 72 s.fake.ResetCalls() 73 s.fake.err = nil 74 s.fake.info = params.ModelInfo{ 75 Name: "mymodel", 76 UUID: testing.ModelTag.Id(), 77 ControllerUUID: "1ca2293b-fdb9-4299-97d6-55583bb39364", 78 OwnerTag: "user-admin@local", 79 ProviderType: "openstack", 80 Life: params.Alive, 81 Status: params.EntityStatus{ 82 Status: status.StatusActive, 83 Since: &statusSince, 84 }, 85 Users: users, 86 } 87 88 s.expectedOutput = attrs{ 89 "mymodel": attrs{ 90 "name": "mymodel", 91 "model-uuid": "deadbeef-0bad-400d-8000-4b1d0d06f00d", 92 "controller-uuid": "1ca2293b-fdb9-4299-97d6-55583bb39364", 93 "owner": "admin@local", 94 "type": "openstack", 95 "life": "alive", 96 "status": attrs{ 97 "current": "active", 98 "since": "2016-04-05", 99 }, 100 "users": attrs{ 101 "admin@local": attrs{ 102 "access": "write", 103 "last-connection": "2015-03-20", 104 }, 105 "bob@local": attrs{ 106 "display-name": "Bob", 107 "access": "read", 108 "last-connection": "never connected", 109 }, 110 }, 111 }, 112 } 113 114 err := modelcmd.WriteCurrentController("testing") 115 c.Assert(err, jc.ErrorIsNil) 116 s.store = jujuclienttesting.NewMemStore() 117 s.store.Controllers["testing"] = jujuclient.ControllerDetails{} 118 s.store.Accounts["testing"] = &jujuclient.ControllerAccounts{ 119 CurrentAccount: "admin@local", 120 } 121 err = s.store.UpdateModel("testing", "admin@local", "mymodel", jujuclient.ModelDetails{ 122 testing.ModelTag.Id(), 123 }) 124 c.Assert(err, jc.ErrorIsNil) 125 s.store.Models["testing"].AccountModels["admin@local"].CurrentModel = "mymodel" 126 } 127 128 func (s *ShowCommandSuite) TestShow(c *gc.C) { 129 _, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store)) 130 c.Assert(err, jc.ErrorIsNil) 131 s.fake.CheckCalls(c, []gitjujutesting.StubCall{ 132 {"ModelInfo", []interface{}{[]names.ModelTag{testing.ModelTag}}}, 133 {"Close", nil}, 134 }) 135 } 136 137 func (s *ShowCommandSuite) TestShowFormatYaml(c *gc.C) { 138 ctx, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "--format", "yaml") 139 c.Assert(err, jc.ErrorIsNil) 140 c.Assert(testing.Stdout(ctx), jc.YAMLEquals, s.expectedOutput) 141 } 142 143 func (s *ShowCommandSuite) TestShowFormatJson(c *gc.C) { 144 ctx, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "--format", "json") 145 c.Assert(err, jc.ErrorIsNil) 146 c.Assert(testing.Stdout(ctx), jc.JSONEquals, s.expectedOutput) 147 } 148 149 func (s *ShowCommandSuite) TestUnrecognizedArg(c *gc.C) { 150 _, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "-m", "admin", "whoops") 151 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`) 152 }