github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/credentialmodels_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state_test 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 "github.com/juju/names/v5" 11 jc "github.com/juju/testing/checkers" 12 "github.com/juju/utils/v3" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/cloud" 16 "github.com/juju/juju/core/permission" 17 "github.com/juju/juju/state" 18 "github.com/juju/juju/storage" 19 "github.com/juju/juju/testing" 20 ) 21 22 type CredentialModelsSuite struct { 23 ConnSuite 24 25 credentialTag names.CloudCredentialTag 26 abcModelTag names.ModelTag 27 } 28 29 var _ = gc.Suite(&CredentialModelsSuite{}) 30 31 func (s *CredentialModelsSuite) SetUpTest(c *gc.C) { 32 s.ConnSuite.SetUpTest(c) 33 34 s.credentialTag = s.createCloudCredential(c, "foobar") 35 s.abcModelTag = s.addModel(c, "abcmodel", s.credentialTag) 36 } 37 38 func (s *CredentialModelsSuite) createCloudCredential(c *gc.C, credentialName string) names.CloudCredentialTag { 39 // Cloud name is always "dummy" as deep within the testing infrastructure, 40 // we create a testing controller on a cloud "dummy". 41 // Test cloud "dummy" only allows credentials with an empty auth type. 42 tag := names.NewCloudCredentialTag(fmt.Sprintf("%s/%s/%s", "dummy", s.Owner.Id(), credentialName)) 43 err := s.State.UpdateCloudCredential(tag, cloud.NewEmptyCredential()) 44 c.Assert(err, jc.ErrorIsNil) 45 return tag 46 } 47 48 func (s *CredentialModelsSuite) addModel(c *gc.C, modelName string, tag names.CloudCredentialTag) names.ModelTag { 49 uuid, err := utils.NewUUID() 50 c.Assert(err, jc.ErrorIsNil) 51 cfg := testing.CustomModelConfig(c, testing.Attrs{ 52 "name": modelName, 53 "uuid": uuid.String(), 54 }) 55 _, st, err := s.Controller.NewModel(state.ModelArgs{ 56 Type: state.ModelTypeIAAS, 57 CloudName: "dummy", 58 CloudRegion: "dummy-region", 59 Config: cfg, 60 Owner: tag.Owner(), 61 CloudCredential: tag, 62 StorageProviderRegistry: storage.StaticProviderRegistry{}, 63 }) 64 c.Assert(err, jc.ErrorIsNil) 65 defer st.Close() 66 return names.NewModelTag(uuid.String()) 67 } 68 69 func (s *CredentialModelsSuite) TestCredentialModelsAndOwnerAccess(c *gc.C) { 70 out, err := s.State.CredentialModelsAndOwnerAccess(s.credentialTag) 71 c.Assert(err, jc.ErrorIsNil) 72 c.Assert(out, gc.DeepEquals, []state.CredentialOwnerModelAccess{ 73 {ModelName: "abcmodel", OwnerAccess: permission.AdminAccess, ModelUUID: s.abcModelTag.Id()}, 74 }) 75 } 76 77 func (s *CredentialModelsSuite) TestCredentialModelsAndOwnerAccessMany(c *gc.C) { 78 // add another model with the same credential 79 xyzModelTag := s.addModel(c, "xyzmodel", s.credentialTag) 80 81 // add another model with a different credential - should not be in the output. 82 anotherCredential := s.createCloudCredential(c, "another") 83 s.addModel(c, "dontshow", anotherCredential) 84 85 out, err := s.State.CredentialModelsAndOwnerAccess(s.credentialTag) 86 c.Assert(err, jc.ErrorIsNil) 87 c.Assert(out, jc.SameContents, []state.CredentialOwnerModelAccess{ 88 {ModelName: "abcmodel", OwnerAccess: permission.AdminAccess, ModelUUID: s.abcModelTag.Id()}, 89 {ModelName: "xyzmodel", OwnerAccess: permission.AdminAccess, ModelUUID: xyzModelTag.Id()}, 90 }) 91 } 92 93 func (s *CredentialModelsSuite) TestCredentialModelsAndOwnerAccessNoModels(c *gc.C) { 94 anotherCredential := s.createCloudCredential(c, "another") 95 96 out, err := s.State.CredentialModelsAndOwnerAccess(anotherCredential) 97 c.Assert(err, jc.Satisfies, errors.IsNotFound) 98 c.Assert(out, gc.HasLen, 0) 99 } 100 101 func (s *CredentialModelsSuite) TestCredentialModels(c *gc.C) { 102 out, err := s.State.CredentialModels(s.credentialTag) 103 c.Assert(err, jc.ErrorIsNil) 104 c.Assert(out, gc.DeepEquals, map[string]string{s.abcModelTag.Id(): "abcmodel"}) 105 } 106 107 func (s *CredentialModelsSuite) TestCredentialModelsExcludesDeadModels(c *gc.C) { 108 checkModels := func(expected ...string) { 109 out, err := s.State.CredentialModels(s.credentialTag) 110 c.Assert(err, jc.ErrorIsNil) 111 112 var obtained []string 113 for k := range out { 114 obtained = append(obtained, k) 115 } 116 c.Assert(obtained, jc.SameContents, expected) 117 } 118 119 // Add another model with the same credential. 120 xyzModelTag := s.addModel(c, "xyzmodel", s.credentialTag) 121 checkModels(s.abcModelTag.Id(), xyzModelTag.Id()) 122 123 // Set one of the models to Dead. 124 m, r, err := s.StatePool.GetModel(s.abcModelTag.Id()) 125 c.Assert(err, jc.ErrorIsNil) 126 defer r.Release() 127 128 err = m.SetDead() 129 c.Assert(err, jc.ErrorIsNil) 130 131 checkModels(xyzModelTag.Id()) 132 } 133 134 func (s *CredentialModelsSuite) TestCredentialNoModels(c *gc.C) { 135 anotherCredential := s.createCloudCredential(c, "another") 136 137 out, err := s.State.CredentialModels(anotherCredential) 138 c.Assert(err, jc.Satisfies, errors.IsNotFound) 139 c.Assert(out, gc.HasLen, 0) 140 }