From 9273f748c23a936863bb1632e2654bc5ae4f8c04 Mon Sep 17 00:00:00 2001 From: Anuj Biyani Date: Fri, 31 May 2013 18:20:09 -0700 Subject: [PATCH] determine if instances are InService across a given load balancer and array of load balancers --- .../aws-ha-release/aws-ha-release.rb | 16 ++++++++ .../aws-ha-release/aws-ha-release_spec.rb | 41 +++++++++++++++++++ spec/support/fake_ec2.rb | 11 +++++ spec/support/fake_elb.rb | 37 +++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 spec/support/fake_ec2.rb create mode 100644 spec/support/fake_elb.rb diff --git a/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb b/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb index dfd9970..4658080 100755 --- a/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb +++ b/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb @@ -24,4 +24,20 @@ class AwsHaRelease @group.update(desired_capacity: @group.desired_capacity + 1) end + + def instances_inservice?(load_balancer) + load_balancer.instances.health.each do |health| + return false unless health[:state] == 'InService' + end + + true + end + + def all_instances_inservice?(load_balancers) + load_balancers.each do |load_balancer| + return false unless instances_inservice?(load_balancer) + end + + true + end end diff --git a/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb b/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb index 9a0ef06..8f9b0b1 100644 --- a/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb +++ b/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb @@ -59,5 +59,46 @@ describe 'aws-ha-release' do expect(@group.desired_capacity).to eq 2 end + + context 'determining if instances are in service' do + it 'checks all instances across a given load balancer' do + load_balancer = AWS::FakeELB::LoadBalancer.new 'test_load_balancer_01' + + expect(@aws_ha_release.instances_inservice?(load_balancer)).to eq false + + load_balancer.instances.health[1] = { + instance: AWS::FakeEC2::Instance.new, + description: 'N/A', + state: 'InService', + reason_code: 'N/A' + } + + expect(@aws_ha_release.instances_inservice?(load_balancer)).to eq true + end + + it 'checks all instances across an array of load balancers' do + load_balancers = [AWS::FakeELB::LoadBalancer.new('test_load_balancer_01'), AWS::FakeELB::LoadBalancer.new('test_load_balancer_02')] + + expect(@aws_ha_release.all_instances_inservice?(load_balancers)).to eq false + + load_balancers[0].instances.health[1] = { + instance: AWS::FakeEC2::Instance.new, + description: 'N/A', + state: 'InService', + reason_code: 'N/A' + } + + expect(@aws_ha_release.all_instances_inservice?(load_balancers)).to eq false + + load_balancers[1].instances.health[1] = { + instance: AWS::FakeEC2::Instance.new, + description: 'N/A', + state: 'InService', + reason_code: 'N/A' + } + + expect(@aws_ha_release.all_instances_inservice?(load_balancers)).to eq true + end + end end end diff --git a/spec/support/fake_ec2.rb b/spec/support/fake_ec2.rb new file mode 100644 index 0000000..8420241 --- /dev/null +++ b/spec/support/fake_ec2.rb @@ -0,0 +1,11 @@ +module AWS + class FakeEC2 + def initialize + end + + class Instance + def initialize + end + end + end +end diff --git a/spec/support/fake_elb.rb b/spec/support/fake_elb.rb new file mode 100644 index 0000000..b581fa9 --- /dev/null +++ b/spec/support/fake_elb.rb @@ -0,0 +1,37 @@ +module AWS + class FakeELB + def initialize + end + + class LoadBalancer + def initialize(name, options = {}) + end + + def instances + @instances ||= InstanceCollection.new + end + end + + class InstanceCollection + def initialize + end + + def health + @health ||= [ + { + instance: AWS::FakeEC2::Instance.new, + description: 'N/A', + state: 'InService', + reason_code: 'N/A' + }, + { + instance: AWS::FakeEC2::Instance.new, + description: 'Instance has failed at least the UnhealthyThreshold number of health checks consecutively.', + state: 'OutOfService', + reason_code: 'Instance' + } + ] + end + end + end +end