~go-bot/goose/trunk

« back to all changes in this revision

Viewing changes to testservices/swiftservice/service_test.go

Goose test infrastructure improvements

This branch improves the usabilty of the goose test infrastructure.

A full Openstack service test double is provided. A bunch of manual coding which was required in each test suite to set up various Openstack module test doubles is replaced by a few lines of code.

New code:

        cred := &identity.Credentials{...}
        openstack := openstack.New(cred)
        openstack.SetupHTTP(s.Mux)

Old code:

        // Create the identity service.
        s.identityDouble = identityservice.NewUserPass()
        token := s.identityDouble.AddUser(s.cred.User, s.cred.Secrets)
        s.Mux.Handle(baseIdentityURL, s.identityDouble)

        // Register Swift endpoints with identity service.
        ep := identityservice.Endpoint{
                AdminURL:    s.Server.URL + baseSwiftURL,
                InternalURL: s.Server.URL + baseSwiftURL,
                PublicURL:   s.Server.URL + baseSwiftURL,
                Region:      s.cred.Region,
        }
        service := identityservice.Service{"swift", "object-store", []identityservice.Endpoint{ep}}
        s.identityDouble.AddService(service)
        s.swiftDouble = swiftservice.New("localhost", baseSwiftURL+"/", token)
        s.Mux.Handle(baseSwiftURL+"/", s.swiftDouble)

        // Register Nova endpoints with identity service.
        ep = identityservice.Endpoint{
                AdminURL:    s.Server.URL + baseNovaURL,
                InternalURL: s.Server.URL + baseNovaURL,
                PublicURL:   s.Server.URL + baseNovaURL,
                Region:      s.cred.Region,
        }
        service = identityservice.Service{"nova", "compute", []identityservice.Endpoint{ep}}
        s.identityDouble.AddService(service)
        s.novaDouble = novaservice.New("localhost", "V1", token, "1")
        s.novaDouble.SetupHTTP(s.Mux)
        
        
Other changes include:

- fix the identity service double to remove the hard coded userId and tenantId.
- do not hard code a fixed token against a service double - each user is assigned their own token just like a real instance, allowing multi-user tests to be written.
- improvements to the Swift service double to make it use URLs which are compliant with how a real Swift instance would do it.
- factor out a common base class for the legacy and userpass double implementations.
- use a SetupHTTP() for all test doubles instead of requiring the coder to know the magic URL paths to use.

R=dimitern
CC=
https://codereview.appspot.com/7194043

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
package swiftservice
4
4
 
5
5
import (
 
6
        "fmt"
6
7
        . "launchpad.net/gocheck"
7
8
)
8
9
 
9
10
type SwiftServiceSuite struct {
10
 
        service SwiftService
 
11
        service *Swift
11
12
}
12
13
 
13
 
var baseURL = "/v1/AUTH_tenant/"
14
 
var token = "token"
15
 
var hostname = "localhost" // not really used here
 
14
var region = "region"             // not really used here
 
15
var hostname = "http://localhost" // not really used here
 
16
var versionPath = "v2"            // not really used here
 
17
var tenantId = "tenant"           // not really used here
16
18
 
17
19
var _ = Suite(&SwiftServiceSuite{})
18
20
 
19
21
func (s *SwiftServiceSuite) SetUpSuite(c *C) {
20
 
        s.service = New(hostname, baseURL, token)
 
22
        s.service = New(hostname, versionPath, tenantId, region, nil)
21
23
}
22
24
 
23
25
func (s *SwiftServiceSuite) TestAddHasRemoveContainer(c *C) {
76
78
        err = s.service.AddObject("test", "obj", data)
77
79
        c.Assert(err, IsNil)
78
80
        url, err := s.service.GetURL("test", "obj")
79
 
        path := baseURL + "test/obj"
80
81
        c.Assert(err, IsNil)
81
 
        c.Assert(url, Equals, hostname+path)
 
82
        c.Assert(url, Equals, fmt.Sprintf("%s/%s/%s/test/obj", hostname, versionPath, tenantId))
82
83
        err = s.service.RemoveContainer("test")
83
84
        c.Assert(err, IsNil)
84
85
        ok = s.service.HasContainer("test")