~rogpeppe/goose/state-of-the-world

« back to all changes in this revision

Viewing changes to client/client_test.go

  • Committer: Ian Booth
  • Date: 2012-11-21 07:56:19 UTC
  • Revision ID: ian.booth@canonical.com-20121121075619-4fh6i9yq6fj6cwct
Extract identity functionality from client, and also extract common HTTP methods

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
        "flag"
5
5
        . "launchpad.net/gocheck"
6
6
        "launchpad.net/goose/client"
 
7
        "launchpad.net/goose/identity"
7
8
        "testing"
8
9
        "time"
9
10
)
15
16
 
16
17
type ClientSuite struct {
17
18
        client       *client.OpenStackClient
18
 
        username     string
19
 
        password     string
20
 
        tenant       string
21
19
        testServerId string
22
20
        skipAuth     bool
23
21
}
27
25
                c.Skip("-live not provided")
28
26
        }
29
27
 
30
 
        username, password, tenant, region, auth_url := client.GetEnvVars()
31
 
        for i, p := range []string{username, password, tenant, region, auth_url} {
 
28
        cred := identity.CredentialsFromEnv()
 
29
        for i, p := range []string{cred.User, cred.Secrets, cred.TenantName, cred.Region, cred.URL} {
32
30
                if p == "" {
33
31
                        c.Fatalf("required environment variable not set: %d", i)
34
32
                }
35
33
        }
36
34
 
37
 
        s.client = &client.OpenStackClient{IdentityEndpoint: auth_url, Region: region}
38
 
        s.username = username
39
 
        s.password = password
40
 
        s.tenant = tenant
 
35
        s.client = client.NewOpenStackClient(cred, identity.AUTH_USERPASS)
41
36
        s.skipAuth = true // set after TestAuthenticate
42
37
 
43
38
}
54
49
                ImageId:  "3fc0ef0b-82a9-4f44-a797-a43f0f73b20e", // smoser-cloud-images/ubuntu-precise-12.04-i386-server-20120424.manifest.xml
55
50
        }
56
51
        err := s.client.RunServer(ro)
57
 
        c.Check(err, IsNil)
 
52
        if err != nil {
 
53
                c.Fatal("Error starting test server: " + err.Error())
 
54
        }
58
55
 
59
56
        // Now find it and save its ID
60
57
        servers, err := s.client.ListServers()
82
79
 
83
80
func (s *ClientSuite) SetUpTest(c *C) {
84
81
        if !s.skipAuth && !s.client.IsAuthenticated() {
85
 
                err := s.client.Authenticate(s.username, s.password, s.tenant)
 
82
                err := s.client.Authenticate()
86
83
                c.Assert(err, IsNil)
87
84
                c.Logf("authenticated")
88
85
        }
90
87
 
91
88
var suite = Suite(&ClientSuite{})
92
89
 
93
 
var authTests = []struct {
94
 
        summary string
95
 
        inputs  []string
96
 
        err     string
97
 
}{
98
 
        {
99
 
                summary: "empty args",
100
 
                inputs:  []string{"", "", ""},
101
 
                err:     "required arg.*missing",
102
 
        },
103
 
        {
104
 
                summary: "dummy args",
105
 
                inputs:  []string{"phony", "fake", "dummy"},
106
 
                err:     "authentication failed.*",
107
 
        },
108
 
        {
109
 
                summary: "valid args",
110
 
                inputs:  []string{"!", "", ""},
111
 
                err:     "",
112
 
        },
 
90
func (s *ClientSuite) TestAuthenticateFail(c *C) {
 
91
        cred := identity.CredentialsFromEnv()
 
92
        cred.User = "fred"
 
93
        cred.Secrets = "broken"
 
94
        cred.Region = ""
 
95
        var osclient *client.OpenStackClient = client.NewOpenStackClient(cred, identity.AUTH_USERPASS)
 
96
        c.Assert(osclient.IsAuthenticated(), Equals, false)
 
97
        var err error
 
98
        err = osclient.Authenticate()
 
99
        c.Assert(err, ErrorMatches, "authentication failed.*")
113
100
}
114
101
 
115
102
func (s *ClientSuite) TestAuthenticate(c *C) {
116
 
        c.Assert(s.client.IsAuthenticated(), Equals, false)
117
 
        for _, t := range authTests {
118
 
                c.Logf("test: %s", t.summary)
119
 
                var err error
120
 
                if t.inputs[0] == "!" {
121
 
                        err = s.client.Authenticate(s.username, s.password, s.tenant)
122
 
                } else {
123
 
                        err = s.client.Authenticate(t.inputs[0], t.inputs[1], t.inputs[2])
124
 
                }
125
 
                if t.err == "" {
126
 
                        c.Assert(err, IsNil)
127
 
                        c.Assert(s.client.IsAuthenticated(), Equals, true)
128
 
                } else {
129
 
                        c.Assert(err, ErrorMatches, t.err)
130
 
                }
131
 
        }
 
103
        var err error
 
104
        err = s.client.Authenticate()
 
105
        c.Assert(err, IsNil)
 
106
        c.Assert(s.client.IsAuthenticated(), Equals, true)
132
107
 
133
108
        // Check service endpoints are discovered
134
 
        c.Assert(s.client.Services["compute"], NotNil)
135
 
        c.Assert(s.client.Services["swift"], NotNil)
 
109
        c.Assert(s.client.ServiceURLs["compute"], NotNil)
 
110
        c.Assert(s.client.ServiceURLs["swift"], NotNil)
136
111
 
137
112
        s.skipAuth = false
138
113
 
204
179
                c.Assert(sr.Updated, Matches, `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.*`)
205
180
                c.Assert(sr.Id, Not(Equals), "")
206
181
                c.Assert(sr.HostId, Not(Equals), "")
207
 
                c.Assert(sr.TenantId, Equals, s.client.Token.Tenant.Id)
208
 
                c.Assert(sr.UserId, Equals, s.client.User.Id)
 
182
                c.Assert(sr.TenantId, Equals, s.client.TenantId)
 
183
                c.Assert(sr.UserId, Equals, s.client.UserId)
209
184
                c.Assert(sr.Status, Not(Equals), "")
210
185
                c.Assert(sr.Name, Not(Equals), "")
211
186
                if sr.Id == s.testServerId {
241
216
                c.Fatalf("no security groups found (expected at least 1)")
242
217
        }
243
218
        for _, g := range groups {
244
 
                c.Assert(g.TenantId, Equals, s.client.Token.Tenant.Id)
 
219
                c.Assert(g.TenantId, Equals, s.client.TenantId)
245
220
                c.Assert(g.Name, Not(Equals), "")
246
221
                c.Assert(g.Description, Not(Equals), "")
247
222
                c.Assert(g.Rules, NotNil)
302
277
        rule, err = s.client.CreateSecurityGroupRule(ri)
303
278
        c.Check(err, IsNil)
304
279
        c.Check(rule.ParentGroupId, Equals, group1.Id)
305
 
        c.Check(rule.Group["tenant_id"], Equals, s.client.Token.Tenant.Id)
 
280
        c.Check(rule.Group["tenant_id"], Equals, s.client.TenantId)
306
281
        c.Check(rule.Group["name"], Equals, "test_secgroup2")
307
282
        err = s.client.DeleteSecurityGroupRule(rule.Id)
308
283
        c.Check(err, IsNil)
324
299
 
325
300
func (s *ClientSuite) waitTestServerToStart(c *C) {
326
301
        // Wait until the test server is actually running
327
 
        c.Logf("waiting the test server to start...")
 
302
        c.Logf("waiting the test server %s to start...", s.testServerId)
328
303
        for {
329
304
                server, err := s.client.GetServer(s.testServerId)
330
305
                c.Check(err, IsNil)