25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

244 lines
8.6 KiB

  1. package elb_test
  2. import (
  3. "github.com/goamz/goamz/aws"
  4. "github.com/goamz/goamz/elb"
  5. "github.com/goamz/goamz/elb/elbtest"
  6. . "gopkg.in/check.v1"
  7. )
  8. // LocalServer represents a local elbtest fake server.
  9. type LocalServer struct {
  10. auth aws.Auth
  11. region aws.Region
  12. srv *elbtest.Server
  13. }
  14. func (s *LocalServer) SetUp(c *C) {
  15. srv, err := elbtest.NewServer()
  16. c.Assert(err, IsNil)
  17. c.Assert(srv, NotNil)
  18. s.srv = srv
  19. s.region = aws.Region{ELBEndpoint: srv.URL()}
  20. }
  21. // LocalServerSuite defines tests that will run
  22. // against the local elbtest server. It includes
  23. // selected tests from ClientTests;
  24. // when the elbtest functionality is sufficient, it should
  25. // include all of them, and ClientTests can be simply embedded.
  26. type LocalServerSuite struct {
  27. srv LocalServer
  28. ServerTests
  29. clientTests ClientTests
  30. }
  31. // ServerTests defines a set of tests designed to test
  32. // the elbtest local fake elb server.
  33. // It is not used as a test suite in itself, but embedded within
  34. // another type.
  35. type ServerTests struct {
  36. elb *elb.ELB
  37. }
  38. // AmazonServerSuite runs the elbtest server tests against a live ELB server.
  39. // It will only be activated if the -all flag is specified.
  40. type AmazonServerSuite struct {
  41. srv AmazonServer
  42. ServerTests
  43. }
  44. var _ = Suite(&AmazonServerSuite{})
  45. func (s *AmazonServerSuite) SetUpSuite(c *C) {
  46. if !*amazon {
  47. c.Skip("AmazonServerSuite tests not enabled")
  48. }
  49. s.srv.SetUp(c)
  50. s.ServerTests.elb = elb.New(s.srv.auth, aws.USEast)
  51. }
  52. var _ = Suite(&LocalServerSuite{})
  53. func (s *LocalServerSuite) SetUpSuite(c *C) {
  54. s.srv.SetUp(c)
  55. s.ServerTests.elb = elb.New(s.srv.auth, s.srv.region)
  56. s.clientTests.elb = elb.New(s.srv.auth, s.srv.region)
  57. }
  58. func (s *LocalServerSuite) TestCreateLoadBalancer(c *C) {
  59. s.clientTests.TestCreateAndDeleteLoadBalancer(c)
  60. }
  61. func (s *LocalServerSuite) TestCreateLoadBalancerError(c *C) {
  62. s.clientTests.TestCreateLoadBalancerError(c)
  63. }
  64. func (s *LocalServerSuite) TestDescribeLoadBalancer(c *C) {
  65. s.clientTests.TestDescribeLoadBalancers(c)
  66. }
  67. func (s *LocalServerSuite) TestDescribeLoadBalancerListsAddedByNewLoadbalancerFunc(c *C) {
  68. srv := s.srv.srv
  69. srv.NewLoadBalancer("wierdlb")
  70. defer srv.RemoveLoadBalancer("wierdlb")
  71. resp, err := s.clientTests.elb.DescribeLoadBalancers()
  72. c.Assert(err, IsNil)
  73. isPresent := false
  74. for _, desc := range resp.LoadBalancerDescriptions {
  75. if desc.LoadBalancerName == "wierdlb" {
  76. isPresent = true
  77. }
  78. }
  79. c.Assert(isPresent, Equals, true)
  80. }
  81. func (s *LocalServerSuite) TestDescribeLoadBalancerListsInstancesAddedByRegisterInstancesFunc(c *C) {
  82. srv := s.srv.srv
  83. lbName := "somelb"
  84. srv.NewLoadBalancer(lbName)
  85. defer srv.RemoveLoadBalancer(lbName)
  86. instId := srv.NewInstance()
  87. defer srv.RemoveInstance(instId)
  88. srv.RegisterInstance(instId, lbName) // no need to deregister, since we're removing the lb
  89. resp, err := s.clientTests.elb.DescribeLoadBalancers()
  90. c.Assert(err, IsNil)
  91. c.Assert(len(resp.LoadBalancerDescriptions) > 0, Equals, true)
  92. c.Assert(len(resp.LoadBalancerDescriptions[0].Instances) > 0, Equals, true)
  93. c.Assert(resp.LoadBalancerDescriptions[0].Instances, DeepEquals, []elb.Instance{{InstanceId: instId}})
  94. srv.DeregisterInstance(instId, lbName)
  95. resp, err = s.clientTests.elb.DescribeLoadBalancers()
  96. c.Assert(err, IsNil)
  97. c.Assert(resp.LoadBalancerDescriptions[0].Instances, DeepEquals, []elb.Instance(nil))
  98. }
  99. func (s *LocalServerSuite) TestDescribeLoadBalancersBadRequest(c *C) {
  100. s.clientTests.TestDescribeLoadBalancersBadRequest(c)
  101. }
  102. func (s *LocalServerSuite) TestRegisterInstanceWithLoadBalancer(c *C) {
  103. srv := s.srv.srv
  104. instId := srv.NewInstance()
  105. defer srv.RemoveInstance(instId)
  106. srv.NewLoadBalancer("testlb")
  107. defer srv.RemoveLoadBalancer("testlb")
  108. resp, err := s.clientTests.elb.RegisterInstancesWithLoadBalancer([]string{instId}, "testlb")
  109. c.Assert(err, IsNil)
  110. c.Assert(resp.InstanceIds, DeepEquals, []string{instId})
  111. }
  112. func (s *LocalServerSuite) TestRegisterInstanceWithLoadBalancerWithAbsentInstance(c *C) {
  113. srv := s.srv.srv
  114. srv.NewLoadBalancer("testlb")
  115. defer srv.RemoveLoadBalancer("testlb")
  116. resp, err := s.clientTests.elb.RegisterInstancesWithLoadBalancer([]string{"i-212"}, "testlb")
  117. c.Assert(err, NotNil)
  118. c.Assert(err, ErrorMatches, `^InvalidInstance found in \[i-212\]. Invalid id: "i-212" \(InvalidInstance\)$`)
  119. c.Assert(resp, IsNil)
  120. }
  121. func (s *LocalServerSuite) TestRegisterInstanceWithLoadBalancerWithAbsentLoadBalancer(c *C) {
  122. // the verification if the lb exists is done before the instances, so there is no need to create
  123. // fixture instances for this test, it'll never get that far
  124. resp, err := s.clientTests.elb.RegisterInstancesWithLoadBalancer([]string{"i-212"}, "absentlb")
  125. c.Assert(err, NotNil)
  126. c.Assert(err, ErrorMatches, `^There is no ACTIVE Load Balancer named 'absentlb' \(LoadBalancerNotFound\)$`)
  127. c.Assert(resp, IsNil)
  128. }
  129. func (s *LocalServerSuite) TestDeregisterInstanceWithLoadBalancer(c *C) {
  130. // there is no need to register the instance first, amazon returns the same response
  131. // in both cases (instance registered or not)
  132. srv := s.srv.srv
  133. instId := srv.NewInstance()
  134. defer srv.RemoveInstance(instId)
  135. srv.NewLoadBalancer("testlb")
  136. defer srv.RemoveLoadBalancer("testlb")
  137. resp, err := s.clientTests.elb.DeregisterInstancesFromLoadBalancer([]string{instId}, "testlb")
  138. c.Assert(err, IsNil)
  139. c.Assert(resp.RequestId, Not(Equals), "")
  140. }
  141. func (s *LocalServerSuite) TestDeregisterInstanceWithLoadBalancerWithAbsentLoadBalancer(c *C) {
  142. resp, err := s.clientTests.elb.DeregisterInstancesFromLoadBalancer([]string{"i-212"}, "absentlb")
  143. c.Assert(resp, IsNil)
  144. c.Assert(err, NotNil)
  145. c.Assert(err, ErrorMatches, `^There is no ACTIVE Load Balancer named 'absentlb' \(LoadBalancerNotFound\)$`)
  146. }
  147. func (s *LocalServerSuite) TestDeregisterInstancewithLoadBalancerWithAbsentInstance(c *C) {
  148. srv := s.srv.srv
  149. srv.NewLoadBalancer("testlb")
  150. defer srv.RemoveLoadBalancer("testlb")
  151. resp, err := s.clientTests.elb.DeregisterInstancesFromLoadBalancer([]string{"i-212"}, "testlb")
  152. c.Assert(resp, IsNil)
  153. c.Assert(err, NotNil)
  154. c.Assert(err, ErrorMatches, `^InvalidInstance found in \[i-212\]. Invalid id: "i-212" \(InvalidInstance\)$`)
  155. }
  156. func (s *LocalServerSuite) TestDescribeInstanceHealth(c *C) {
  157. srv := s.srv.srv
  158. instId := srv.NewInstance()
  159. defer srv.RemoveInstance(instId)
  160. srv.NewLoadBalancer("testlb")
  161. defer srv.RemoveLoadBalancer("testlb")
  162. resp, err := s.clientTests.elb.DescribeInstanceHealth("testlb", instId)
  163. c.Assert(err, IsNil)
  164. c.Assert(len(resp.InstanceStates) > 0, Equals, true)
  165. c.Assert(resp.InstanceStates[0].Description, Equals, "Instance is in pending state.")
  166. c.Assert(resp.InstanceStates[0].InstanceId, Equals, instId)
  167. c.Assert(resp.InstanceStates[0].State, Equals, "OutOfService")
  168. c.Assert(resp.InstanceStates[0].ReasonCode, Equals, "Instance")
  169. }
  170. func (s *LocalServerSuite) TestDescribeInstanceHealthBadRequest(c *C) {
  171. s.clientTests.TestDescribeInstanceHealthBadRequest(c)
  172. }
  173. func (s *LocalServerSuite) TestDescribeInstanceHealthWithoutSpecifyingInstances(c *C) {
  174. srv := s.srv.srv
  175. instId := srv.NewInstance()
  176. defer srv.RemoveInstance(instId)
  177. srv.NewLoadBalancer("testlb")
  178. defer srv.RemoveLoadBalancer("testlb")
  179. srv.RegisterInstance(instId, "testlb")
  180. resp, err := s.clientTests.elb.DescribeInstanceHealth("testlb")
  181. c.Assert(err, IsNil)
  182. c.Assert(len(resp.InstanceStates) > 0, Equals, true)
  183. c.Assert(resp.InstanceStates[0].Description, Equals, "Instance is in pending state.")
  184. c.Assert(resp.InstanceStates[0].InstanceId, Equals, instId)
  185. c.Assert(resp.InstanceStates[0].State, Equals, "OutOfService")
  186. c.Assert(resp.InstanceStates[0].ReasonCode, Equals, "Instance")
  187. }
  188. func (s *LocalServerSuite) TestDescribeInstanceHealthChangingIt(c *C) {
  189. srv := s.srv.srv
  190. instId := srv.NewInstance()
  191. defer srv.RemoveInstance(instId)
  192. srv.NewLoadBalancer("somelb")
  193. defer srv.RemoveLoadBalancer("somelb")
  194. srv.RegisterInstance(instId, "somelb")
  195. state := elb.InstanceState{
  196. Description: "Instance has failed at least the UnhealthyThreshold number of health checks consecutively",
  197. InstanceId: instId,
  198. State: "OutOfService",
  199. ReasonCode: "Instance",
  200. }
  201. srv.ChangeInstanceState("somelb", state)
  202. resp, err := s.clientTests.elb.DescribeInstanceHealth("somelb")
  203. c.Assert(err, IsNil)
  204. c.Assert(len(resp.InstanceStates) > 0, Equals, true)
  205. c.Assert(resp.InstanceStates[0].Description, Equals, "Instance has failed at least the UnhealthyThreshold number of health checks consecutively")
  206. c.Assert(resp.InstanceStates[0].InstanceId, Equals, instId)
  207. c.Assert(resp.InstanceStates[0].State, Equals, "OutOfService")
  208. c.Assert(resp.InstanceStates[0].ReasonCode, Equals, "Instance")
  209. }
  210. func (s *LocalServerSuite) TestConfigureHealthCheck(c *C) {
  211. s.clientTests.TestConfigureHealthCheck(c)
  212. }
  213. func (s *LocalServerSuite) TestConfigureHealthCheckBadRequest(c *C) {
  214. s.clientTests.TestConfigureHealthCheckBadRequest(c)
  215. }