github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/openstack/live_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package openstack_test 5 6 import ( 7 "crypto/rand" 8 "fmt" 9 "io" 10 11 "github.com/go-goose/goose/v5/client" 12 "github.com/go-goose/goose/v5/identity" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/environs/context" 17 "github.com/juju/juju/environs/jujutest" 18 "github.com/juju/juju/environs/storage" 19 envtesting "github.com/juju/juju/environs/testing" 20 jujutesting "github.com/juju/juju/juju/testing" 21 "github.com/juju/juju/provider/openstack" 22 coretesting "github.com/juju/juju/testing" 23 ) 24 25 // generate a different bucket name for each config instance, so that 26 // we are not polluted by previous test state. 27 func randomName() string { 28 buf := make([]byte, 8) 29 _, err := io.ReadFull(rand.Reader, buf) 30 if err != nil { 31 panic(fmt.Sprintf("error from crypto rand: %v", err)) 32 } 33 return fmt.Sprintf("%x", buf) 34 } 35 36 func makeTestConfig(cred *identity.Credentials) map[string]interface{} { 37 // The following attributes hold the environment configuration 38 // for running the OpenStack integration tests. 39 // 40 // This is missing keys for security reasons; set the following 41 // environment variables to make the OpenStack testing work: 42 // access-key: $OS_USERNAME 43 // secret-key: $OS_PASSWORD 44 // 45 attrs := coretesting.FakeConfig().Merge(coretesting.Attrs{ 46 "name": "sample-" + randomName(), 47 "type": "openstack", 48 "auth-mode": "userpass", 49 "username": cred.User, 50 "password": cred.Secrets, 51 "region": cred.Region, 52 "auth-url": cred.URL, 53 "tenant-name": cred.TenantName, 54 }) 55 return attrs 56 } 57 58 // Register tests to run against a real Openstack instance. 59 func registerLiveTests(cred *identity.Credentials) { 60 config := makeTestConfig(cred) 61 gc.Suite(&LiveTests{ 62 cred: cred, 63 LiveTests: jujutest.LiveTests{ 64 TestConfig: config, 65 Attempt: *openstack.ShortAttempt, 66 CanOpenState: true, 67 HasProvisioner: true, 68 }, 69 }) 70 } 71 72 // LiveTests contains tests that can be run against OpenStack deployments. 73 // The deployment can be a real live instance or service doubles. 74 // Each test runs using the same connection. 75 type LiveTests struct { 76 coretesting.BaseSuite 77 jujutest.LiveTests 78 cred *identity.Credentials 79 metadataStorage storage.Storage 80 } 81 82 func (t *LiveTests) SetUpSuite(c *gc.C) { 83 t.BaseSuite.SetUpSuite(c) 84 // Update some Config items now that we have services running. 85 // This is setting the simplestreams urls and auth-url because that 86 // information is set during startup of the localLiveSuite 87 cl := client.NewClient(t.cred, identity.AuthUserPass, nil) 88 err := cl.Authenticate() 89 c.Assert(err, jc.ErrorIsNil) 90 containerURL, err := cl.MakeServiceURL("object-store", "", nil) 91 c.Assert(err, jc.ErrorIsNil) 92 t.TestConfig = t.TestConfig.Merge(coretesting.Attrs{ 93 "agent-metadata-url": containerURL + "/juju-dist-test/tools", 94 "image-metadata-url": containerURL + "/juju-dist-test", 95 "auth-url": t.cred.URL, 96 }) 97 t.LiveTests.SetUpSuite(c) 98 // For testing, we create a storage instance to which is uploaded tools and image metadata. 99 t.PrepareOnce(c) 100 t.metadataStorage = openstack.MetadataStorage(t.Env) 101 // Put some fake tools metadata in place so that tests that are simply 102 // starting instances without any need to check if those instances 103 // are running can find the metadata. 104 envtesting.UploadFakeTools(c, t.metadataStorage, t.Env.Config().AgentStream(), t.Env.Config().AgentStream()) 105 } 106 107 func (t *LiveTests) TearDownSuite(c *gc.C) { 108 if t.Env == nil { 109 // This can happen if SetUpSuite fails. 110 return 111 } 112 if t.metadataStorage != nil { 113 envtesting.RemoveFakeToolsMetadata(c, t.metadataStorage) 114 } 115 t.LiveTests.TearDownSuite(c) 116 t.BaseSuite.TearDownSuite(c) 117 } 118 119 func (t *LiveTests) SetUpTest(c *gc.C) { 120 t.BaseSuite.SetUpTest(c) 121 t.LiveTests.SetUpTest(c) 122 } 123 124 func (t *LiveTests) TearDownTest(c *gc.C) { 125 t.LiveTests.TearDownTest(c) 126 t.BaseSuite.TearDownTest(c) 127 } 128 129 func (s *LiveTests) assertStartInstanceDefaultSecurityGroup(c *gc.C, useDefault bool) { 130 s.LiveTests.PatchValue(&s.TestConfig, s.TestConfig.Merge(coretesting.Attrs{ 131 "use-default-secgroup": useDefault, 132 })) 133 s.Destroy(c) 134 s.BootstrapOnce(c) 135 136 inst, _ := jujutesting.AssertStartInstance(c, s.Env, context.NewEmptyCloudCallContext(), s.ControllerUUID, "100") 137 // Check whether the instance has the default security group assigned. 138 novaClient := openstack.GetNovaClient(s.Env) 139 groups, err := novaClient.GetServerSecurityGroups(string(inst.Id())) 140 c.Assert(err, jc.ErrorIsNil) 141 defaultGroupFound := false 142 for _, group := range groups { 143 if group.Name == "default" { 144 defaultGroupFound = true 145 break 146 } 147 } 148 c.Assert(defaultGroupFound, gc.Equals, useDefault) 149 } 150 151 func (s *LiveTests) TestStartInstanceWithDefaultSecurityGroup(c *gc.C) { 152 s.assertStartInstanceDefaultSecurityGroup(c, true) 153 } 154 155 func (s *LiveTests) TestStartInstanceWithoutDefaultSecurityGroup(c *gc.C) { 156 s.assertStartInstanceDefaultSecurityGroup(c, false) 157 }