~axwalk/goose/nova-availability-zones

« back to all changes in this revision

Viewing changes to tools/secgroup-delete-all/main_test.go

  • Committer: Martin Packman
  • Date: 2013-01-23 11:34:23 UTC
  • mfrom: (39.3.8 secgroup-delete-all)
  • Revision ID: martin.packman@canonical.com-20130123113423-6b3czjq48p7c6r3c
Add simple tool for deleting security groups

Basic test of using the current api for doing some actual task. As the
live tests create lots of security groups without ever deleting them
it's also sort of useful to have around.

R=wallyworld, jameinel, rog, dimitern
CC=
https://codereview.appspot.com/6948051

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main_test
 
2
 
 
3
import (
 
4
        "bytes"
 
5
        . "launchpad.net/gocheck"
 
6
        "launchpad.net/goose/client"
 
7
        "launchpad.net/goose/identity"
 
8
        "launchpad.net/goose/nova"
 
9
        "launchpad.net/goose/testing/httpsuite"
 
10
        "launchpad.net/goose/testservices/identityservice"
 
11
        "launchpad.net/goose/testservices/novaservice"
 
12
        tool "launchpad.net/goose/tools/secgroup-delete-all"
 
13
        "testing"
 
14
)
 
15
 
 
16
func Test(t *testing.T) {
 
17
        TestingT(t)
 
18
}
 
19
 
 
20
const (
 
21
        username = "auser"
 
22
        password = "apass"
 
23
        region   = "aregion"
 
24
        tenant   = "1"
 
25
)
 
26
 
 
27
type ToolSuite struct {
 
28
        httpsuite.HTTPSuite
 
29
}
 
30
 
 
31
var _ = Suite(&ToolSuite{})
 
32
 
 
33
// GZ 2013-01-21: Should require EnvSuite for this, but clashes with HTTPSuite
 
34
func createNovaClient(auth_url string) *nova.Client {
 
35
        creds := identity.Credentials{
 
36
                URL:        auth_url,
 
37
                User:       username,
 
38
                Secrets:    password,
 
39
                Region:     region,
 
40
                TenantName: tenant,
 
41
        }
 
42
        osc := client.NewClient(&creds, identity.AuthUserPass, nil)
 
43
        return nova.New(osc)
 
44
}
 
45
 
 
46
func (s *ToolSuite) makeServices(c *C) *nova.Client {
 
47
        ident := identityservice.NewUserPass()
 
48
        token := ident.AddUser(username, password)
 
49
        // GZ 2013-01-21: Current novaservice double requires magic url like so
 
50
        computeurl := s.Server.URL + "/v2.0/" + tenant
 
51
        ident.AddService(identityservice.Service{
 
52
                "nova",
 
53
                "compute",
 
54
                []identityservice.Endpoint{
 
55
                        {
 
56
                                AdminURL:    computeurl,
 
57
                                InternalURL: computeurl,
 
58
                                PublicURL:   computeurl,
 
59
                                Region:      region,
 
60
                        },
 
61
                }})
 
62
        s.Mux.Handle("/tokens", ident)
 
63
        comp := novaservice.New("unused.invalid", "v2.0", token, tenant)
 
64
        comp.SetupHTTP(s.Mux)
 
65
        return createNovaClient(s.Server.URL)
 
66
}
 
67
 
 
68
func (s *ToolSuite) TestNoGroups(c *C) {
 
69
        nova := s.makeServices(c)
 
70
        var buf bytes.Buffer
 
71
        err := tool.DeleteAll(&buf, nova)
 
72
        c.Assert(err, IsNil)
 
73
        c.Assert(string(buf.Bytes()), Equals, "No security groups to delete.\n")
 
74
}
 
75
 
 
76
func (s *ToolSuite) TestTwoGroups(c *C) {
 
77
        nova := s.makeServices(c)
 
78
        nova.CreateSecurityGroup("group-a", "A group")
 
79
        nova.CreateSecurityGroup("group-b", "Another group")
 
80
        var buf bytes.Buffer
 
81
        err := tool.DeleteAll(&buf, nova)
 
82
        c.Assert(err, IsNil)
 
83
        c.Assert(string(buf.Bytes()), Equals, "2 security groups deleted.\n")
 
84
}
 
85
 
 
86
// GZ 2013-01-21: Should also test undeleteable groups, but can't induce
 
87
//                novaservice errors currently.