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