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

« back to all changes in this revision

Viewing changes to src/launchpad.net/goamz/ec2/ec2i_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 ec2_test
 
2
 
 
3
import (
 
4
        "crypto/rand"
 
5
        "fmt"
 
6
        "launchpad.net/goamz/aws"
 
7
        "launchpad.net/goamz/ec2"
 
8
        "launchpad.net/goamz/testutil"
 
9
        . "launchpad.net/gocheck"
 
10
)
 
11
 
 
12
// AmazonServer represents an Amazon EC2 server.
 
13
type AmazonServer struct {
 
14
        auth aws.Auth
 
15
}
 
16
 
 
17
func (s *AmazonServer) SetUp(c *C) {
 
18
        auth, err := aws.EnvAuth()
 
19
        if err != nil {
 
20
                c.Fatal(err.Error())
 
21
        }
 
22
        s.auth = auth
 
23
}
 
24
 
 
25
// Suite cost per run: 0.02 USD
 
26
var _ = Suite(&AmazonClientSuite{})
 
27
 
 
28
// AmazonClientSuite tests the client against a live EC2 server.
 
29
type AmazonClientSuite struct {
 
30
        srv AmazonServer
 
31
        ClientTests
 
32
}
 
33
 
 
34
func (s *AmazonClientSuite) SetUpSuite(c *C) {
 
35
        if !testutil.Amazon {
 
36
                c.Skip("AmazonClientSuite tests not enabled")
 
37
        }
 
38
        s.srv.SetUp(c)
 
39
        s.ec2 = ec2.New(s.srv.auth, aws.USEast)
 
40
}
 
41
 
 
42
// ClientTests defines integration tests designed to test the client.
 
43
// It is not used as a test suite in itself, but embedded within
 
44
// another type.
 
45
type ClientTests struct {
 
46
        ec2 *ec2.EC2
 
47
}
 
48
 
 
49
var imageId = "ami-ccf405a5" // Ubuntu Maverick, i386, EBS store
 
50
 
 
51
// Cost: 0.00 USD
 
52
func (s *ClientTests) TestRunInstancesError(c *C) {
 
53
        options := ec2.RunInstances{
 
54
                ImageId:      "ami-a6f504cf", // Ubuntu Maverick, i386, instance store
 
55
                InstanceType: "t1.micro",     // Doesn't work with micro, results in 400.
 
56
        }
 
57
 
 
58
        resp, err := s.ec2.RunInstances(&options)
 
59
 
 
60
        c.Assert(resp, IsNil)
 
61
        c.Assert(err, ErrorMatches, "AMI.*root device.*not supported.*")
 
62
 
 
63
        ec2err, ok := err.(*ec2.Error)
 
64
        c.Assert(ok, Equals, true)
 
65
        c.Assert(ec2err.StatusCode, Equals, 400)
 
66
        c.Assert(ec2err.Code, Equals, "UnsupportedOperation")
 
67
        c.Assert(ec2err.Message, Matches, "AMI.*root device.*not supported.*")
 
68
        c.Assert(ec2err.RequestId, Matches, ".+")
 
69
}
 
70
 
 
71
// Cost: 0.02 USD
 
72
func (s *ClientTests) TestRunAndTerminate(c *C) {
 
73
        options := ec2.RunInstances{
 
74
                ImageId:      imageId,
 
75
                InstanceType: "t1.micro",
 
76
        }
 
77
        resp1, err := s.ec2.RunInstances(&options)
 
78
        c.Assert(err, IsNil)
 
79
        c.Check(resp1.ReservationId, Matches, "r-[0-9a-f]*")
 
80
        c.Check(resp1.OwnerId, Matches, "[0-9]+")
 
81
        c.Check(resp1.Instances, HasLen, 1)
 
82
        c.Check(resp1.Instances[0].InstanceType, Equals, "t1.micro")
 
83
 
 
84
        instId := resp1.Instances[0].InstanceId
 
85
 
 
86
        resp2, err := s.ec2.Instances([]string{instId}, nil)
 
87
        c.Assert(err, IsNil)
 
88
        if c.Check(resp2.Reservations, HasLen, 1) && c.Check(len(resp2.Reservations[0].Instances), Equals, 1) {
 
89
                inst := resp2.Reservations[0].Instances[0]
 
90
                c.Check(inst.InstanceId, Equals, instId)
 
91
        }
 
92
 
 
93
        resp3, err := s.ec2.TerminateInstances([]string{instId})
 
94
        c.Assert(err, IsNil)
 
95
        c.Check(resp3.StateChanges, HasLen, 1)
 
96
        c.Check(resp3.StateChanges[0].InstanceId, Equals, instId)
 
97
        c.Check(resp3.StateChanges[0].CurrentState.Name, Equals, "shutting-down")
 
98
        c.Check(resp3.StateChanges[0].CurrentState.Code, Equals, 32)
 
99
}
 
100
 
 
101
// Cost: 0.00 USD
 
102
func (s *ClientTests) TestSecurityGroups(c *C) {
 
103
        name := "goamz-test"
 
104
        descr := "goamz security group for tests"
 
105
 
 
106
        // Clean it up, if a previous test left it around and avoid leaving it around.
 
107
        s.ec2.DeleteSecurityGroup(ec2.SecurityGroup{Name: name})
 
108
        defer s.ec2.DeleteSecurityGroup(ec2.SecurityGroup{Name: name})
 
109
 
 
110
        resp1, err := s.ec2.CreateSecurityGroup(name, descr)
 
111
        c.Assert(err, IsNil)
 
112
        c.Assert(resp1.RequestId, Matches, ".+")
 
113
        c.Assert(resp1.Name, Equals, name)
 
114
        c.Assert(resp1.Id, Matches, ".+")
 
115
 
 
116
        resp1, err = s.ec2.CreateSecurityGroup(name, descr)
 
117
        ec2err, _ := err.(*ec2.Error)
 
118
        c.Assert(resp1, IsNil)
 
119
        c.Assert(ec2err, NotNil)
 
120
        c.Assert(ec2err.Code, Equals, "InvalidGroup.Duplicate")
 
121
 
 
122
        perms := []ec2.IPPerm{{
 
123
                Protocol:  "tcp",
 
124
                FromPort:  0,
 
125
                ToPort:    1024,
 
126
                SourceIPs: []string{"127.0.0.1/24"},
 
127
        }}
 
128
 
 
129
        resp2, err := s.ec2.AuthorizeSecurityGroup(ec2.SecurityGroup{Name: name}, perms)
 
130
        c.Assert(err, IsNil)
 
131
        c.Assert(resp2.RequestId, Matches, ".+")
 
132
 
 
133
        resp3, err := s.ec2.SecurityGroups(ec2.SecurityGroupNames(name), nil)
 
134
        c.Assert(err, IsNil)
 
135
        c.Assert(resp3.RequestId, Matches, ".+")
 
136
        c.Assert(resp3.Groups, HasLen, 1)
 
137
 
 
138
        g0 := resp3.Groups[0]
 
139
        c.Assert(g0.Name, Equals, name)
 
140
        c.Assert(g0.Description, Equals, descr)
 
141
        c.Assert(g0.IPPerms, HasLen, 1)
 
142
        c.Assert(g0.IPPerms[0].Protocol, Equals, "tcp")
 
143
        c.Assert(g0.IPPerms[0].FromPort, Equals, 0)
 
144
        c.Assert(g0.IPPerms[0].ToPort, Equals, 1024)
 
145
        c.Assert(g0.IPPerms[0].SourceIPs, DeepEquals, []string{"127.0.0.1/24"})
 
146
 
 
147
        resp2, err = s.ec2.DeleteSecurityGroup(ec2.SecurityGroup{Name: name})
 
148
        c.Assert(err, IsNil)
 
149
        c.Assert(resp2.RequestId, Matches, ".+")
 
150
}
 
151
 
 
152
var sessionId = func() string {
 
153
        buf := make([]byte, 8)
 
154
        // if we have no randomness, we'll just make do, so ignore the error.
 
155
        rand.Read(buf)
 
156
        return fmt.Sprintf("%x", buf)
 
157
}()
 
158
 
 
159
// sessionName reutrns a name that is probably
 
160
// unique to this test session.
 
161
func sessionName(prefix string) string {
 
162
        return prefix + "-" + sessionId
 
163
}
 
164
 
 
165
var allRegions = []aws.Region{
 
166
        aws.USEast,
 
167
        aws.USWest,
 
168
        aws.EUWest,
 
169
        aws.APSoutheast,
 
170
        aws.APNortheast,
 
171
}
 
172
 
 
173
// Communicate with all EC2 endpoints to see if they are alive.
 
174
func (s *ClientTests) TestRegions(c *C) {
 
175
        name := sessionName("goamz-region-test")
 
176
        perms := []ec2.IPPerm{{
 
177
                Protocol:  "tcp",
 
178
                FromPort:  80,
 
179
                ToPort:    80,
 
180
                SourceIPs: []string{"127.0.0.1/32"},
 
181
        }}
 
182
        errs := make(chan error, len(allRegions))
 
183
        for _, region := range allRegions {
 
184
                go func(r aws.Region) {
 
185
                        e := ec2.New(s.ec2.Auth, r)
 
186
                        _, err := e.AuthorizeSecurityGroup(ec2.SecurityGroup{Name: name}, perms)
 
187
                        errs <- err
 
188
                }(region)
 
189
        }
 
190
        for _ = range allRegions {
 
191
                err := <-errs
 
192
                if err != nil {
 
193
                        ec2_err, ok := err.(*ec2.Error)
 
194
                        if ok {
 
195
                                c.Check(ec2_err.Code, Matches, "InvalidGroup.NotFound")
 
196
                        } else {
 
197
                                c.Errorf("Non-EC2 error: %s", err)
 
198
                        }
 
199
                } else {
 
200
                        c.Errorf("Test should have errored but it seems to have succeeded")
 
201
                }
 
202
        }
 
203
}