mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #400 from dylanegan/elb_mocks
ELB model_tests with mocking
This commit is contained in:
commit
1496e3e840
8 changed files with 134 additions and 30 deletions
|
@ -55,6 +55,8 @@ module Fog
|
|||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
raise Fog::AWS::ELB::IdentifierTaken if self.data[:load_balancers].has_key? lb_name
|
||||
|
||||
dns_name = Fog::AWS::ELB::Mock.dns_name(lb_name, @region)
|
||||
self.data[:load_balancers][lb_name] = {
|
||||
'AvailabilityZones' => availability_zones,
|
||||
|
@ -63,14 +65,23 @@ module Fog
|
|||
'CreatedTime' => Time.now,
|
||||
'DNSName' => dns_name,
|
||||
'HealthCheck' => {
|
||||
'HealthyThreshold' => 3,
|
||||
'Interval' => 60,
|
||||
'Target' => 'TCP:5000',
|
||||
'Timeout' => 60,
|
||||
'UnhealthyThreshold' => 2
|
||||
'HealthyThreshold' => 10,
|
||||
'Timeout' => 5,
|
||||
'UnhealthyThreshold' => 2,
|
||||
'Interval' => 30,
|
||||
'Target' => 'TCP:80'
|
||||
},
|
||||
'Instances' => [],
|
||||
'ListenerDescriptions' => [],
|
||||
'ListenerDescriptions' => [
|
||||
{
|
||||
'Listener' => {
|
||||
'InstancePort' => 80,
|
||||
'Protocol' => 'HTTP',
|
||||
'LoadBalancerPort' => 80
|
||||
},
|
||||
'PolicyNames' => []
|
||||
}
|
||||
],
|
||||
'LoadBalancerName' => lb_name,
|
||||
'Policies' => {
|
||||
'LBCookieStickinessPolicies' => [],
|
||||
|
|
|
@ -33,15 +33,31 @@ module Fog
|
|||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def deregister_instances_from_load_balancer(instance_ids, lb_name)
|
||||
Fog::Mock.not_implemented
|
||||
raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]
|
||||
instance_ids = [*instance_ids]
|
||||
instance_ids.each do |instance|
|
||||
raise Fog::AWS::ELB::InvalidInstance unless Compute[:aws].servers.get(instance)
|
||||
end
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
load_balancer['Instances'].delete_if { |i| instance_ids.include? i['InstanceId'] }
|
||||
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
},
|
||||
'DeregisterInstancesFromLoadBalancerResult' => {
|
||||
'Instances' => load_balancer['Instances'].dup
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
alias :deregister_instances :deregister_instances_from_load_balancer
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,6 +32,41 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def describe_instance_health(lb_name, instance_ids = [])
|
||||
raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]
|
||||
|
||||
instance_ids = [*instance_ids]
|
||||
instance_ids = load_balancer['Instances'].collect { |i| i['InstanceId'] } unless instance_ids.any?
|
||||
data = instance_ids.map do |id|
|
||||
unless instance = Compute[:aws].servers.get(id)
|
||||
raise Fog::AWS::ELB::InvalidInstance
|
||||
end
|
||||
|
||||
{
|
||||
'Description' => "",
|
||||
'InstanceId' => instance.id,
|
||||
'ReasonCode' => "",
|
||||
'State' => 'OutOfService'
|
||||
}
|
||||
end
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
},
|
||||
'DescribeInstanceHealthResult' => {
|
||||
'InstanceStates' => data
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -57,9 +57,9 @@ module Fog
|
|||
lb_names = [*lb_names]
|
||||
load_balancers = if lb_names.any?
|
||||
lb_names.map do |lb_name|
|
||||
lb = self.data[:load_balancers].find { |name, data| data if name == lb_name }
|
||||
lb = self.data[:load_balancers].find { |name, data| name == lb_name }
|
||||
raise Fog::AWS::ELB::NotFound unless lb
|
||||
lb
|
||||
lb[1]
|
||||
end.compact
|
||||
else
|
||||
self.data[:load_balancers].values
|
||||
|
|
|
@ -17,7 +17,7 @@ module Fog
|
|||
# * 'ResponseMetadata'<~Hash>:
|
||||
# * 'RequestId'<~String> - Id of request
|
||||
# * 'DisableAvailabilityZonesForLoadBalancerResult'<~Hash>:
|
||||
# * 'AvailabilityZones'<~Array> - array of strings describing instances currently enabled
|
||||
# * 'AvailabilityZones'<~Array> - A list of updated Availability Zones for the LoadBalancer.
|
||||
def disable_availability_zones_for_load_balancer(availability_zones, lb_name)
|
||||
params = Fog::AWS.indexed_param('AvailabilityZones.member', [*availability_zones])
|
||||
request({
|
||||
|
@ -32,15 +32,28 @@ module Fog
|
|||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def disable_availability_zones_for_load_balancer(availability_zones, lb_name)
|
||||
Fog::Mock.not_implemented
|
||||
raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
load_balancer['AvailabilityZones'].delete_if { |az| availability_zones.include? az }
|
||||
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
},
|
||||
'DisableAvailabilityZonesForLoadBalancerResult' => {
|
||||
'AvailabilityZones' => load_balancer['AvailabilityZones']
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
alias :disable_zones :disable_availability_zones_for_load_balancer
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -34,13 +34,28 @@ module Fog
|
|||
class Mock
|
||||
|
||||
def enable_availability_zones_for_load_balancer(availability_zones, lb_name)
|
||||
Fog::Mock.not_implemented
|
||||
raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
load_balancer['AvailabilityZones'] << availability_zones
|
||||
load_balancer['AvailabilityZones'].flatten!.uniq!
|
||||
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
},
|
||||
'EnableAvailabilityZonesForLoadBalancerResult' => {
|
||||
'AvailabilityZones' => load_balancer['AvailabilityZones']
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
alias :enable_zones :enable_availability_zones_for_load_balancer
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,15 +33,32 @@ module Fog
|
|||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def register_instances_with_load_balancer(instance_ids, lb_name)
|
||||
Fog::Mock.not_implemented
|
||||
raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name]
|
||||
instance_ids = [*instance_ids]
|
||||
instances = instance_ids.map do |instance|
|
||||
raise Fog::AWS::ELB::InvalidInstance unless Compute[:aws].servers.get(instance)
|
||||
{'InstanceId' => instance}
|
||||
end
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
|
||||
load_balancer['Instances'] = instances.dup
|
||||
|
||||
response.body = {
|
||||
'ResponseMetadata' => {
|
||||
'RequestId' => Fog::AWS::Mock.request_id
|
||||
},
|
||||
'RegisterInstancesWithLoadBalancerResult' => {
|
||||
'Instances' => instances
|
||||
}
|
||||
}
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
alias :register_instances :register_instances_with_load_balancer
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,8 +2,6 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do
|
|||
|
||||
|
||||
tests('success') do
|
||||
pending if Fog.mocking?
|
||||
|
||||
tests('load_balancers') do
|
||||
tests('getting a missing elb') do
|
||||
returns(nil) { AWS[:elb].load_balancers.get('no-such-elb') }
|
||||
|
@ -44,7 +42,7 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do
|
|||
raises(Fog::AWS::ELB::InvalidInstance) { elb.deregister_instances('i-00000000') }
|
||||
end
|
||||
|
||||
server = Compute[:aws].servers.create
|
||||
server = Fog::Compute[:aws].servers.create
|
||||
tests('register instance') do
|
||||
begin
|
||||
elb.register_instances(server.id)
|
||||
|
@ -183,5 +181,4 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do
|
|||
elb.destroy
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue