github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/featuretests/api_model_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package featuretests 5 6 import ( 7 "bytes" 8 "time" 9 10 "github.com/juju/errors" 11 "github.com/juju/names" 12 jc "github.com/juju/testing/checkers" 13 "github.com/juju/version" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/api" 17 "github.com/juju/juju/api/modelmanager" 18 "github.com/juju/juju/apiserver/params" 19 jujunames "github.com/juju/juju/juju/names" 20 "github.com/juju/juju/juju/testing" 21 "github.com/juju/juju/state" 22 coretesting "github.com/juju/juju/testing" 23 "github.com/juju/juju/testing/factory" 24 ) 25 26 type apiEnvironmentSuite struct { 27 testing.JujuConnSuite 28 client *api.Client 29 } 30 31 func (s *apiEnvironmentSuite) SetUpTest(c *gc.C) { 32 s.JujuConnSuite.SetUpTest(c) 33 s.client = s.APIState.Client() 34 c.Assert(s.client, gc.NotNil) 35 s.AddCleanup(func(*gc.C) { 36 s.client.ClientFacade.Close() 37 }) 38 } 39 40 func (s *apiEnvironmentSuite) TestGrantModel(c *gc.C) { 41 username := "foo@ubuntuone" 42 model, err := s.State.Model() 43 c.Assert(err, jc.ErrorIsNil) 44 mm := modelmanager.NewClient(s.APIState) 45 err = mm.GrantModel(username, "read", model.UUID()) 46 c.Assert(err, jc.ErrorIsNil) 47 48 user := names.NewUserTag(username) 49 modelUser, err := s.State.ModelUser(user) 50 c.Assert(err, jc.ErrorIsNil) 51 c.Assert(modelUser.UserName(), gc.Equals, user.Canonical()) 52 lastConn, err := modelUser.LastConnection() 53 c.Assert(err, jc.Satisfies, state.IsNeverConnectedError) 54 c.Assert(lastConn.IsZero(), jc.IsTrue) 55 } 56 57 func (s *apiEnvironmentSuite) TestRevokeModel(c *gc.C) { 58 // First share an environment with a user. 59 user := s.Factory.MakeModelUser(c, &factory.ModelUserParams{User: "foo@ubuntuone"}) 60 model, err := s.State.Model() 61 c.Assert(err, jc.ErrorIsNil) 62 mm := modelmanager.NewClient(s.APIState) 63 64 modelUser, err := s.State.ModelUser(user.UserTag()) 65 c.Assert(err, jc.ErrorIsNil) 66 c.Assert(modelUser, gc.NotNil) 67 68 // Then test unsharing the environment. 69 err = mm.RevokeModel(user.UserName(), "read", model.UUID()) 70 c.Assert(err, jc.ErrorIsNil) 71 72 modelUser, err = s.State.ModelUser(user.UserTag()) 73 c.Assert(errors.IsNotFound(err), jc.IsTrue) 74 c.Assert(modelUser, gc.IsNil) 75 } 76 77 func (s *apiEnvironmentSuite) TestEnvironmentUserInfo(c *gc.C) { 78 modelUser := s.Factory.MakeModelUser(c, &factory.ModelUserParams{User: "bobjohns@ubuntuone", DisplayName: "Bob Johns"}) 79 env, err := s.State.Model() 80 c.Assert(err, jc.ErrorIsNil) 81 owner, err := s.State.ModelUser(env.Owner()) 82 c.Assert(err, jc.ErrorIsNil) 83 84 obtained, err := s.client.ModelUserInfo() 85 c.Assert(err, jc.ErrorIsNil) 86 c.Assert(obtained, gc.DeepEquals, []params.ModelUserInfo{ 87 { 88 UserName: owner.UserName(), 89 DisplayName: owner.DisplayName(), 90 Access: "write", 91 LastConnection: lastConnPointer(c, owner), 92 }, { 93 UserName: "bobjohns@ubuntuone", 94 DisplayName: "Bob Johns", 95 Access: "write", 96 LastConnection: lastConnPointer(c, modelUser), 97 }, 98 }) 99 } 100 101 func lastConnPointer(c *gc.C, modelUser *state.ModelUser) *time.Time { 102 lastConn, err := modelUser.LastConnection() 103 if err != nil { 104 if state.IsNeverConnectedError(err) { 105 return nil 106 } 107 c.Fatal(err) 108 } 109 return &lastConn 110 } 111 112 func (s *apiEnvironmentSuite) TestUploadToolsOtherEnvironment(c *gc.C) { 113 // setup other environment 114 otherState := s.Factory.MakeModel(c, nil) 115 defer otherState.Close() 116 info := s.APIInfo(c) 117 info.ModelTag = otherState.ModelTag() 118 otherAPIState, err := api.Open(info, api.DefaultDialOpts()) 119 c.Assert(err, jc.ErrorIsNil) 120 defer otherAPIState.Close() 121 otherClient := otherAPIState.Client() 122 defer otherClient.ClientFacade.Close() 123 124 newVersion := version.MustParseBinary("5.4.3-quantal-amd64") 125 vers := newVersion.String() 126 127 // build fake tools 128 tgz, checksum := coretesting.TarGz( 129 coretesting.NewTarFile(jujunames.Jujud, 0777, "jujud contents "+vers)) 130 131 toolsList, err := otherClient.UploadTools(bytes.NewReader(tgz), newVersion) 132 c.Assert(err, jc.ErrorIsNil) 133 c.Assert(toolsList, gc.HasLen, 1) 134 c.Assert(toolsList[0].SHA256, gc.Equals, checksum) 135 136 toolStrg, err := otherState.ToolsStorage() 137 defer toolStrg.Close() 138 c.Assert(err, jc.ErrorIsNil) 139 meta, closer, err := toolStrg.Open(vers) 140 defer closer.Close() 141 c.Assert(err, jc.ErrorIsNil) 142 c.Assert(meta.SHA256, gc.Equals, checksum) 143 c.Assert(meta.Version, gc.Equals, vers) 144 }