6
"github.com/mitchellh/goamz/aws"
7
"github.com/mitchellh/goamz/ec2"
8
"github.com/mitchellh/goamz/testutil"
9
. "github.com/motain/gocheck"
12
// AmazonServer represents an Amazon EC2 server.
13
type AmazonServer struct {
17
func (s *AmazonServer) SetUp(c *C) {
18
auth, err := aws.EnvAuth()
25
// Suite cost per run: 0.02 USD
26
var _ = Suite(&AmazonClientSuite{})
28
// AmazonClientSuite tests the client against a live EC2 server.
29
type AmazonClientSuite struct {
34
func (s *AmazonClientSuite) SetUpSuite(c *C) {
36
c.Skip("AmazonClientSuite tests not enabled")
39
s.ec2 = ec2.NewWithClient(s.srv.auth, aws.USEast, testutil.DefaultClient)
42
// ClientTests defines integration tests designed to test the client.
43
// It is not used as a test suite in itself, but embedded within
45
type ClientTests struct {
49
var imageId = "ami-ccf405a5" // Ubuntu Maverick, i386, EBS store
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.
58
resp, err := s.ec2.RunInstances(&options)
61
c.Assert(err, ErrorMatches, "AMI.*root device.*not supported.*")
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, ".+")
72
func (s *ClientTests) TestRunAndTerminate(c *C) {
73
options := ec2.RunInstances{
75
InstanceType: "t1.micro",
77
resp1, err := s.ec2.RunInstances(&options)
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")
84
instId := resp1.Instances[0].InstanceId
86
resp2, err := s.ec2.Instances([]string{instId}, nil)
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)
93
resp3, err := s.ec2.TerminateInstances([]string{instId})
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)
102
func (s *ClientTests) TestSecurityGroups(c *C) {
104
descr := "goamz security group for tests"
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})
110
resp1, err := s.ec2.CreateSecurityGroup(ec2.SecurityGroup{Name: name, Description: descr})
112
c.Assert(resp1.RequestId, Matches, ".+")
113
c.Assert(resp1.Name, Equals, name)
114
c.Assert(resp1.Id, Matches, ".+")
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")
122
perms := []ec2.IPPerm{{
126
SourceIPs: []string{"127.0.0.1/24"},
129
resp2, err := s.ec2.AuthorizeSecurityGroup(ec2.SecurityGroup{Name: name}, perms)
131
c.Assert(resp2.RequestId, Matches, ".+")
133
resp3, err := s.ec2.SecurityGroups(ec2.SecurityGroupNames(name), nil)
135
c.Assert(resp3.RequestId, Matches, ".+")
136
c.Assert(resp3.Groups, HasLen, 1)
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"})
147
resp2, err = s.ec2.DeleteSecurityGroup(ec2.SecurityGroup{Name: name})
149
c.Assert(resp2.RequestId, Matches, ".+")
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.
156
return fmt.Sprintf("%x", buf)
159
// sessionName reutrns a name that is probably
160
// unique to this test session.
161
func sessionName(prefix string) string {
162
return prefix + "-" + sessionId
165
var allRegions = []aws.Region{
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{{
181
SourceIPs: []string{"127.0.0.1/32"},
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)
191
for _ = range allRegions {
194
ec2_err, ok := err.(*ec2.Error)
196
c.Check(ec2_err.Code, Matches, "InvalidGroup.NotFound")
198
c.Errorf("Non-EC2 error: %s", err)
201
c.Errorf("Test should have errored but it seems to have succeeded")