Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
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/goose/nova" | |
11 "launchpad.net/juju-core/environs" | |
12 "launchpad.net/juju-core/environs/jujutest" | |
13 coretesting "launchpad.net/juju-core/testing" | |
14 ) | |
15 | |
16 // uniqueName is generated afresh for every test run, so that | |
17 // we are not polluted by previous test state. | |
18 var uniqueName = randomName() | |
19 | |
20 func randomName() string { | |
21 buf := make([]byte, 8) | |
22 _, err := io.ReadFull(rand.Reader, buf) | |
23 if err != nil { | |
24 panic(fmt.Sprintf("error from crypto rand: %v", err)) | |
25 } | |
26 return fmt.Sprintf("%x", buf) | |
27 } | |
28 | |
29 func registerOpenStackTests() { | |
30 // The following attributes hold the environment configuration | |
31 // for running the OpenStack integration tests. | |
32 // | |
33 // This is missing keys for security reasons; set the following | |
34 // environment variables to make the OpenStack testing work: | |
35 // access-key: $OS_USERNAME | |
36 // secret-key: $OS_PASSWORD | |
37 attrs := map[string]interface{}{ | |
38 "name": "sample-" + uniqueName, | |
39 "type": "openstack", | |
40 "admin-secret": "for real", | |
41 } | |
42 Suite(&LiveTests{ | |
43 LiveTests: jujutest.LiveTests{ | |
44 Config: attrs, | |
45 }, | |
46 }) | |
47 } | |
48 | |
49 // LiveTests contains tests that can be run against OpenStack deployments. | |
50 // Each test runs using the same connection. | |
51 type LiveTests struct { | |
52 coretesting.LoggingSuite | |
53 jujutest.LiveTests | |
54 novaClient *nova.Client | |
55 testServers []nova.Entity | |
56 } | |
57 | |
58 func (t *LiveTests) SetUpSuite(c *C) { | |
59 t.LoggingSuite.SetUpSuite(c) | |
60 _, err := environs.NewFromAttrs(t.Config) | |
61 c.Assert(err, IsNil) | |
62 | |
63 // Get a nova client and start some test service instances. | |
64 cred, err := identity.CompleteCredentialsFromEnv() | |
65 c.Assert(err, IsNil) | |
66 client := client.NewClient(cred, identity.AuthUserPass) | |
67 t.novaClient = nova.New(client) | |
68 // Not all of the provider APIs are implemented yet so we'll use a helpe r | |
69 // method to create some test server instances until the implementation catches up. | |
70 t.testServers, err = t.createInstances(2) | |
71 c.Assert(err, IsNil) | |
72 | |
73 // TODO: Put some fake tools in place so that tests that are simply | |
74 // starting instances without any need to check if those instances | |
75 // are running will find them in the public bucket. | |
76 // putFakeTools(c, e.PublicStorage().(environs.Storage)) | |
77 t.LiveTests.SetUpSuite(c) | |
78 } | |
79 | |
80 func (t *LiveTests) TearDownSuite(c *C) { | |
81 if t.Env == nil { | |
82 // This can happen if SetUpSuite fails. | |
83 return | |
84 } | |
85 // Delete any test servers started during suite setup. | |
86 for _, inst := range t.testServers { | |
87 err := t.novaClient.DeleteServer(inst.Id) | |
88 c.Check(err, IsNil) | |
89 } | |
90 // TODO: delete any content put into swift | |
91 t.LiveTests.TearDownSuite(c) | |
92 t.LoggingSuite.TearDownSuite(c) | |
93 } | |
94 | |
95 func (t *LiveTests) SetUpTest(c *C) { | |
96 t.LoggingSuite.SetUpTest(c) | |
97 t.LiveTests.SetUpTest(c) | |
98 } | |
99 | |
100 func (t *LiveTests) TearDownTest(c *C) { | |
101 t.LiveTests.TearDownTest(c) | |
102 t.LoggingSuite.TearDownTest(c) | |
103 } | |
104 | |
105 // The OpenStack provider is being developed a few methods at a time. The juju t ests exercise the whole stack and so | |
106 // currently fail because not everything is implemented yet. So below we add som e tests for those methods which have | |
107 // so far been completed. | |
108 | |
109 // createInstances runs some test servers using a known pre-existing image. | |
110 func (t *LiveTests) createInstances(numInstances int) (instances []nova.Entity, err error) { | |
111 for n := 1; n <= numInstances; n++ { | |
112 opts := nova.RunServerOpts{ | |
113 Name: fmt.Sprintf("test_server%d", n), | |
114 FlavorId: "1", // m1.tiny | |
115 ImageId: "0f602ea9-c09e-440c-9e29-cfae5635afa3", | |
116 UserData: nil, | |
117 } | |
118 entity, err := t.novaClient.RunServer(opts) | |
119 if err != nil { | |
120 return nil, err | |
121 } | |
122 instances = append(t.testServers, *entity) | |
123 } | |
124 return instances, nil | |
125 } | |
126 | |
127 func (t *LiveTests) TestAllInstances(c *C) { | |
niemeyer
2012/12/04 16:37:07
// FIXME These instances were not started by the e
| |
128 observedInst, err := t.Env.AllInstances() | |
129 c.Assert(err, IsNil) | |
130 idSet := make(map[string]bool) | |
131 for _, inst := range observedInst { | |
132 idSet[inst.Id()] = true | |
133 } | |
134 for _, inst := range t.testServers { | |
135 _, ok := idSet[inst.Id] | |
136 if !ok { | |
137 c.Logf("Server id '%s' was not listed in AllInstances %v ", inst.Id, observedInst) | |
138 c.Fail() | |
139 } | |
140 } | |
141 } | |
142 | |
143 func (t *LiveTests) TestInstances(c *C) { | |
niemeyer
2012/12/04 16:37:07
// FIXME These instances were not started by the e
| |
144 observedInst, err := t.Env.Instances([]string{t.testServers[0].Id}) | |
145 c.Assert(err, IsNil) | |
146 c.Assert(len(observedInst), Equals, 1) | |
147 c.Assert(observedInst[0].Id(), Equals, t.testServers[0].Id) | |
148 } | |
OLD | NEW |