~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/openstack/live_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package openstack_test
 
2
 
 
3
import (
 
4
        "crypto/rand"
 
5
        "fmt"
 
6
        "io"
 
7
        . "launchpad.net/gocheck"
 
8
        "launchpad.net/goose/client"
 
9
        "launchpad.net/goose/identity"
 
10
        "launchpad.net/juju-core/environs"
 
11
        "launchpad.net/juju-core/environs/jujutest"
 
12
        "launchpad.net/juju-core/environs/openstack"
 
13
        envtesting "launchpad.net/juju-core/environs/testing"
 
14
        coretesting "launchpad.net/juju-core/testing"
 
15
)
 
16
 
 
17
// generate a different bucket name for each config instance, so that
 
18
// we are not polluted by previous test state.
 
19
func randomName() string {
 
20
        buf := make([]byte, 8)
 
21
        _, err := io.ReadFull(rand.Reader, buf)
 
22
        if err != nil {
 
23
                panic(fmt.Sprintf("error from crypto rand: %v", err))
 
24
        }
 
25
        return fmt.Sprintf("%x", buf)
 
26
}
 
27
 
 
28
func makeTestConfig(cred *identity.Credentials) map[string]interface{} {
 
29
        // The following attributes hold the environment configuration
 
30
        // for running the OpenStack integration tests.
 
31
        //
 
32
        // This is missing keys for security reasons; set the following
 
33
        // environment variables to make the OpenStack testing work:
 
34
        //  access-key: $OS_USERNAME
 
35
        //  secret-key: $OS_PASSWORD
 
36
        //
 
37
        attrs := map[string]interface{}{
 
38
                "name":            "sample-" + randomName(),
 
39
                "type":            "openstack",
 
40
                "auth-mode":       "userpass",
 
41
                "control-bucket":  "juju-test-" + randomName(),
 
42
                "ca-cert":         coretesting.CACert,
 
43
                "ca-private-key":  coretesting.CAKey,
 
44
                "authorized-keys": "fakekey",
 
45
                "admin-secret":    "secret",
 
46
                "username":        cred.User,
 
47
                "password":        cred.Secrets,
 
48
                "region":          cred.Region,
 
49
                "auth-url":        cred.URL,
 
50
                "tenant-name":     cred.TenantName,
 
51
        }
 
52
        return attrs
 
53
}
 
54
 
 
55
// Register tests to run against a real Openstack instance.
 
56
func registerLiveTests(cred *identity.Credentials, testImageDetails openstack.ImageDetails) {
 
57
        config := makeTestConfig(cred)
 
58
        config["default-image-id"] = testImageDetails.ImageId
 
59
        config["default-instance-type"] = testImageDetails.Flavor
 
60
        Suite(&LiveTests{
 
61
                cred: cred,
 
62
                LiveTests: jujutest.LiveTests{
 
63
                        TestConfig: jujutest.TestConfig{config},
 
64
                        Attempt:    *openstack.ShortAttempt,
 
65
                        // TODO: Bug #1133263, once the infrastructure is set up,
 
66
                        //       enable The state tests on openstack
 
67
                        CanOpenState: false,
 
68
                        // TODO: Bug #1133272, enabling this requires mapping from
 
69
                        //       'series' to an image id, when we have support, set
 
70
                        //       this flag to True.
 
71
                        HasProvisioner: false,
 
72
                },
 
73
                testImageId: testImageDetails.ImageId,
 
74
                testFlavor:  testImageDetails.Flavor,
 
75
        })
 
76
}
 
77
 
 
78
// LiveTests contains tests that can be run against OpenStack deployments.
 
79
// The deployment can be a real live instance or service doubles.
 
80
// Each test runs using the same connection.
 
81
type LiveTests struct {
 
82
        coretesting.LoggingSuite
 
83
        jujutest.LiveTests
 
84
        cred                   *identity.Credentials
 
85
        testImageId            string
 
86
        testFlavor             string
 
87
        writeablePublicStorage environs.Storage
 
88
}
 
89
 
 
90
func (t *LiveTests) SetUpSuite(c *C) {
 
91
        t.LoggingSuite.SetUpSuite(c)
 
92
        // Update some Config items now that we have services running.
 
93
        // This is setting the public-bucket-url and auth-url because that
 
94
        // information is set during startup of the localLiveSuite
 
95
        cl := client.NewClient(t.cred, identity.AuthUserPass, nil)
 
96
        err := cl.Authenticate()
 
97
        c.Assert(err, IsNil)
 
98
        publicBucketURL, err := cl.MakeServiceURL("object-store", nil)
 
99
        c.Assert(err, IsNil)
 
100
        t.TestConfig.UpdateConfig(map[string]interface{}{
 
101
                "public-bucket-url": publicBucketURL,
 
102
                "auth-url":          t.cred.URL,
 
103
        })
 
104
        t.LiveTests.SetUpSuite(c)
 
105
        // Environ.PublicStorage() is read only.
 
106
        // For testing, we create a specific storage instance which is authorised to write to
 
107
        // the public storage bucket so that we can upload files for testing.
 
108
        t.writeablePublicStorage = openstack.WritablePublicStorage(t.Env)
 
109
        // Put some fake tools in place so that tests that are simply
 
110
        // starting instances without any need to check if those instances
 
111
        // are running will find them in the public bucket.
 
112
        envtesting.UploadFakeTools(c, t.writeablePublicStorage)
 
113
}
 
114
 
 
115
func (t *LiveTests) TearDownSuite(c *C) {
 
116
        if t.Env == nil {
 
117
                // This can happen if SetUpSuite fails.
 
118
                return
 
119
        }
 
120
        if t.writeablePublicStorage != nil {
 
121
                err := openstack.DeleteStorageContent(t.writeablePublicStorage)
 
122
                c.Check(err, IsNil)
 
123
        }
 
124
        t.LiveTests.TearDownSuite(c)
 
125
        t.LoggingSuite.TearDownSuite(c)
 
126
}
 
127
 
 
128
func (t *LiveTests) SetUpTest(c *C) {
 
129
        t.LoggingSuite.SetUpTest(c)
 
130
        t.LiveTests.SetUpTest(c)
 
131
}
 
132
 
 
133
func (t *LiveTests) TearDownTest(c *C) {
 
134
        t.LiveTests.TearDownTest(c)
 
135
        t.LoggingSuite.TearDownTest(c)
 
136
}