determine if instances are InService across a given load balancer and array of load balancers
This commit is contained in:
		
							parent
							
								
									20b59fb132
								
							
						
					
					
						commit
						9273f748c2
					
				@ -24,4 +24,20 @@ class AwsHaRelease
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @group.update(desired_capacity: @group.desired_capacity + 1)
 | 
					    @group.update(desired_capacity: @group.desired_capacity + 1)
 | 
				
			||||||
  end
 | 
					  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
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
@ -59,5 +59,46 @@ describe 'aws-ha-release' do
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      expect(@group.desired_capacity).to eq 2
 | 
					      expect(@group.desired_capacity).to eq 2
 | 
				
			||||||
    end
 | 
					    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
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										11
									
								
								spec/support/fake_ec2.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								spec/support/fake_ec2.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					module AWS
 | 
				
			||||||
 | 
					  class FakeEC2
 | 
				
			||||||
 | 
					    def initialize
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class Instance
 | 
				
			||||||
 | 
					      def initialize
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
							
								
								
									
										37
									
								
								spec/support/fake_elb.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								spec/support/fake_elb.rb
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user