You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

205 lines
5.7 KiB

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