github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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 "sort" 11 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 "gopkg.in/goose.v1/client" 15 "gopkg.in/goose.v1/identity" 16 "gopkg.in/goose.v1/nova" 17 18 "github.com/juju/juju/environs/jujutest" 19 "github.com/juju/juju/environs/storage" 20 envtesting "github.com/juju/juju/environs/testing" 21 jujutesting "github.com/juju/juju/juju/testing" 22 "github.com/juju/juju/provider/openstack" 23 coretesting "github.com/juju/juju/testing" 24 ) 25 26 // generate a different bucket name for each config instance, so that 27 // we are not polluted by previous test state. 28 func randomName() string { 29 buf := make([]byte, 8) 30 _, err := io.ReadFull(rand.Reader, buf) 31 if err != nil { 32 panic(fmt.Sprintf("error from crypto rand: %v", err)) 33 } 34 return fmt.Sprintf("%x", buf) 35 } 36 37 func makeTestConfig(cred *identity.Credentials) map[string]interface{} { 38 // The following attributes hold the environment configuration 39 // for running the OpenStack integration tests. 40 // 41 // This is missing keys for security reasons; set the following 42 // environment variables to make the OpenStack testing work: 43 // access-key: $OS_USERNAME 44 // secret-key: $OS_PASSWORD 45 // 46 attrs := coretesting.FakeConfig().Merge(coretesting.Attrs{ 47 "name": "sample-" + randomName(), 48 "type": "openstack", 49 "auth-mode": "userpass", 50 "username": cred.User, 51 "password": cred.Secrets, 52 "region": cred.Region, 53 "auth-url": cred.URL, 54 "tenant-name": cred.TenantName, 55 }) 56 return attrs 57 } 58 59 // Register tests to run against a real Openstack instance. 60 func registerLiveTests(cred *identity.Credentials) { 61 config := makeTestConfig(cred) 62 gc.Suite(&LiveTests{ 63 cred: cred, 64 LiveTests: jujutest.LiveTests{ 65 TestConfig: config, 66 Attempt: *openstack.ShortAttempt, 67 CanOpenState: true, 68 HasProvisioner: true, 69 }, 70 }) 71 } 72 73 // LiveTests contains tests that can be run against OpenStack deployments. 74 // The deployment can be a real live instance or service doubles. 75 // Each test runs using the same connection. 76 type LiveTests struct { 77 coretesting.BaseSuite 78 jujutest.LiveTests 79 cred *identity.Credentials 80 metadataStorage storage.Storage 81 } 82 83 func (t *LiveTests) SetUpSuite(c *gc.C) { 84 t.BaseSuite.SetUpSuite(c) 85 // Update some Config items now that we have services running. 86 // This is setting the simplestreams urls and auth-url because that 87 // information is set during startup of the localLiveSuite 88 cl := client.NewClient(t.cred, identity.AuthUserPass, nil) 89 err := cl.Authenticate() 90 c.Assert(err, jc.ErrorIsNil) 91 containerURL, err := cl.MakeServiceURL("object-store", nil) 92 c.Assert(err, jc.ErrorIsNil) 93 t.TestConfig = t.TestConfig.Merge(coretesting.Attrs{ 94 "agent-metadata-url": containerURL + "/juju-dist-test/tools", 95 "image-metadata-url": containerURL + "/juju-dist-test", 96 "auth-url": t.cred.URL, 97 }) 98 t.LiveTests.SetUpSuite(c) 99 // For testing, we create a storage instance to which is uploaded tools and image metadata. 100 t.PrepareOnce(c) 101 t.metadataStorage = openstack.MetadataStorage(t.Env) 102 // Put some fake tools metadata in place so that tests that are simply 103 // starting instances without any need to check if those instances 104 // are running can find the metadata. 105 envtesting.UploadFakeTools(c, t.metadataStorage, t.Env.Config().AgentStream(), t.Env.Config().AgentStream()) 106 } 107 108 func (t *LiveTests) TearDownSuite(c *gc.C) { 109 if t.Env == nil { 110 // This can happen if SetUpSuite fails. 111 return 112 } 113 if t.metadataStorage != nil { 114 envtesting.RemoveFakeToolsMetadata(c, t.metadataStorage) 115 } 116 t.LiveTests.TearDownSuite(c) 117 t.BaseSuite.TearDownSuite(c) 118 } 119 120 func (t *LiveTests) SetUpTest(c *gc.C) { 121 t.BaseSuite.SetUpTest(c) 122 t.LiveTests.SetUpTest(c) 123 } 124 125 func (t *LiveTests) TearDownTest(c *gc.C) { 126 t.LiveTests.TearDownTest(c) 127 t.BaseSuite.TearDownTest(c) 128 } 129 130 func (t *LiveTests) TestEnsureGroupSetsGroupId(c *gc.C) { 131 t.PrepareOnce(c) 132 rules := []nova.RuleInfo{ 133 { // First group explicitly asks for all services 134 IPProtocol: "tcp", 135 FromPort: 22, 136 ToPort: 22, 137 Cidr: "0.0.0.0/0", 138 }, 139 { // Second group should only allow access from within the group 140 IPProtocol: "tcp", 141 FromPort: 1, 142 ToPort: 65535, 143 }, 144 } 145 groupName := "juju-test-group-" + randomName() 146 // Make sure things are clean before we start, and clean when we are done 147 cleanup := func() { 148 c.Check(openstack.DiscardSecurityGroup(t.Env, groupName), gc.IsNil) 149 } 150 cleanup() 151 defer cleanup() 152 group, err := openstack.EnsureGroup(t.Env, groupName, rules) 153 c.Assert(err, jc.ErrorIsNil) 154 c.Check(group.Rules, gc.HasLen, 2) 155 c.Check(*group.Rules[0].IPProtocol, gc.Equals, "tcp") 156 c.Check(*group.Rules[0].FromPort, gc.Equals, 22) 157 c.Check(*group.Rules[0].ToPort, gc.Equals, 22) 158 c.Check(group.Rules[0].IPRange["cidr"], gc.Equals, "0.0.0.0/0") 159 c.Check(group.Rules[0].Group.Name, gc.Equals, "") 160 c.Check(group.Rules[0].Group.TenantId, gc.Equals, "") 161 c.Check(*group.Rules[1].IPProtocol, gc.Equals, "tcp") 162 c.Check(*group.Rules[1].FromPort, gc.Equals, 1) 163 c.Check(*group.Rules[1].ToPort, gc.Equals, 65535) 164 c.Check(group.Rules[1].IPRange, gc.HasLen, 0) 165 c.Check(group.Rules[1].Group.Name, gc.Equals, groupName) 166 c.Check(group.Rules[1].Group.TenantId, gc.Equals, group.TenantId) 167 } 168 169 func (t *LiveTests) TestSetupGlobalGroupExposesCorrectPorts(c *gc.C) { 170 t.PrepareOnce(c) 171 groupName := "juju-test-group-" + randomName() 172 // Make sure things are clean before we start, and will be clean when we finish 173 cleanup := func() { 174 c.Check(openstack.DiscardSecurityGroup(t.Env, groupName), gc.IsNil) 175 } 176 cleanup() 177 defer cleanup() 178 apiPort := 34567 // Default 17070 179 group, err := openstack.SetUpGlobalGroup(t.Env, groupName, apiPort) 180 c.Assert(err, jc.ErrorIsNil) 181 // We default to exporting 22, apiPort, and icmp/udp/tcp on 182 // all ports to other machines inside the same group 183 // TODO(jam): 2013-09-18 http://pad.lv/1227142 184 // We shouldn't be exposing the API port on all the machines 185 // that *aren't* hosting the controller. 186 stringRules := make([]string, 0, len(group.Rules)) 187 for _, rule := range group.Rules { 188 ruleStr := fmt.Sprintf("%s %d %d %q %q", 189 *rule.IPProtocol, 190 *rule.FromPort, 191 *rule.ToPort, 192 rule.IPRange["cidr"], 193 rule.Group.Name, 194 ) 195 stringRules = append(stringRules, ruleStr) 196 } 197 // We don't care about the ordering, so we sort the result, and compare it. 198 expectedRules := []string{ 199 `tcp 22 22 "0.0.0.0/0" ""`, 200 fmt.Sprintf(`tcp %d %d "0.0.0.0/0" ""`, apiPort, apiPort), 201 fmt.Sprintf(`tcp 1 65535 "" "%s"`, groupName), 202 fmt.Sprintf(`udp 1 65535 "" "%s"`, groupName), 203 fmt.Sprintf(`icmp -1 -1 "" "%s"`, groupName), 204 } 205 sort.Strings(stringRules) 206 sort.Strings(expectedRules) 207 c.Check(stringRules, gc.DeepEquals, expectedRules) 208 } 209 210 func (s *LiveTests) assertStartInstanceDefaultSecurityGroup(c *gc.C, useDefault bool) { 211 s.LiveTests.PatchValue(&s.TestConfig, s.TestConfig.Merge(coretesting.Attrs{ 212 "use-default-secgroup": useDefault, 213 })) 214 s.Destroy(c) 215 s.BootstrapOnce(c) 216 217 inst, _ := jujutesting.AssertStartInstance(c, s.Env, s.ControllerUUID, "100") 218 // Check whether the instance has the default security group assigned. 219 novaClient := openstack.GetNovaClient(s.Env) 220 groups, err := novaClient.GetServerSecurityGroups(string(inst.Id())) 221 c.Assert(err, jc.ErrorIsNil) 222 defaultGroupFound := false 223 for _, group := range groups { 224 if group.Name == "default" { 225 defaultGroupFound = true 226 break 227 } 228 } 229 c.Assert(defaultGroupFound, gc.Equals, useDefault) 230 } 231 232 func (s *LiveTests) TestStartInstanceWithDefaultSecurityGroup(c *gc.C) { 233 s.assertStartInstanceDefaultSecurityGroup(c, true) 234 } 235 236 func (s *LiveTests) TestStartInstanceWithoutDefaultSecurityGroup(c *gc.C) { 237 s.assertStartInstanceDefaultSecurityGroup(c, false) 238 }