~hduran-8/+junk/caddylegacy

« back to all changes in this revision

Viewing changes to debian/gocode/src/github.com/mitchellh/goamz/ec2/ec2i_test.go

  • Committer: Horacio Durán
  • Date: 2016-10-14 14:33:43 UTC
  • Revision ID: horacio.duran@canonical.com-20161014143343-ytyhz5sx7d1cje4q
Added new upstream version

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
 
        "github.com/mitchellh/goamz/aws"
7
 
        "github.com/mitchellh/goamz/ec2"
8
 
        "github.com/mitchellh/goamz/testutil"
9
 
        . "github.com/motain/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.NewWithClient(s.srv.auth, aws.USEast, testutil.DefaultClient)
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(ec2.SecurityGroup{Name: name, Description: 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(ec2.SecurityGroup{Name: name, Description: 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.EUCentral,
170
 
        aws.APSoutheast,
171
 
        aws.APNortheast,
172
 
}
173
 
 
174
 
// Communicate with all EC2 endpoints to see if they are alive.
175
 
func (s *ClientTests) TestRegions(c *C) {
176
 
        name := sessionName("goamz-region-test")
177
 
        perms := []ec2.IPPerm{{
178
 
                Protocol:  "tcp",
179
 
                FromPort:  80,
180
 
                ToPort:    80,
181
 
                SourceIPs: []string{"127.0.0.1/32"},
182
 
        }}
183
 
        errs := make(chan error, len(allRegions))
184
 
        for _, region := range allRegions {
185
 
                go func(r aws.Region) {
186
 
                        e := ec2.NewWithClient(s.ec2.Auth, r, testutil.DefaultClient)
187
 
                        _, err := e.AuthorizeSecurityGroup(ec2.SecurityGroup{Name: name}, perms)
188
 
                        errs <- err
189
 
                }(region)
190
 
        }
191
 
        for _ = range allRegions {
192
 
                err := <-errs
193
 
                if err != nil {
194
 
                        ec2_err, ok := err.(*ec2.Error)
195
 
                        if ok {
196
 
                                c.Check(ec2_err.Code, Matches, "InvalidGroup.NotFound")
197
 
                        } else {
198
 
                                c.Errorf("Non-EC2 error: %s", err)
199
 
                        }
200
 
                } else {
201
 
                        c.Errorf("Test should have errored but it seems to have succeeded")
202
 
                }
203
 
        }
204
 
}