|
| 1 | +require 'yaml' |
| 2 | + |
| 3 | +describe 'internal application loadbalancer' do |
| 4 | + |
| 5 | + context 'cftest' do |
| 6 | + it 'compiles test' do |
| 7 | + expect(system("cfhighlander cftest #{@validate} --tests tests/internal.test.yaml")).to be_truthy |
| 8 | + end |
| 9 | + end |
| 10 | + |
| 11 | + let(:template) { YAML.load_file("#{File.dirname(__FILE__)}/../out/tests/internal/loadbalancer.compiled.yaml") } |
| 12 | + |
| 13 | + context 'Resources' do |
| 14 | + let(:resources) { template["Resources"] } |
| 15 | + |
| 16 | + context 'LoadBalancer' do |
| 17 | + let(:properties) { resources["LoadBalancer"]["Properties"] } |
| 18 | + |
| 19 | + it 'is an internal application load balancer' do |
| 20 | + expect(resources["LoadBalancer"]["Type"]).to eq("AWS::ElasticLoadBalancingV2::LoadBalancer") |
| 21 | + expect(properties["Scheme"]).to eq("internal") |
| 22 | + end |
| 23 | + |
| 24 | + it 'uses compute subnets' do |
| 25 | + # Check if Subnets is a complex structure with Fn::If |
| 26 | + if properties["Subnets"].is_a?(Hash) && properties["Subnets"].has_key?("Fn::If") |
| 27 | + # Extract the first subnet from the first array in the Fn::If condition |
| 28 | + subnets = properties["Subnets"]["Fn::If"][1] |
| 29 | + expect(subnets).to include({"Ref" => "SubnetCompute0"}) |
| 30 | + else |
| 31 | + # Direct array of subnets |
| 32 | + expect(properties["Subnets"]).to include({"Ref" => "SubnetCompute0"}) |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context 'TargetGroups' do |
| 38 | + let(:tg_properties) { resources["defaultTargetGroup"]["Properties"] } |
| 39 | + |
| 40 | + it 'creates target group with correct port' do |
| 41 | + expect(tg_properties["Port"]).to eq(8080) |
| 42 | + expect(tg_properties["Protocol"]).to eq("HTTP") |
| 43 | + end |
| 44 | + |
| 45 | + it 'has custom health check' do |
| 46 | + expect(tg_properties["HealthCheckPath"]).to eq("/health") |
| 47 | + # Health check port might be a string or a number |
| 48 | + expect(tg_properties["HealthCheckPort"].to_s).to eq("8080") |
| 49 | + expect(tg_properties["HealthCheckProtocol"]).to eq("HTTP") |
| 50 | + |
| 51 | + # Some health check parameters might not be set in the component |
| 52 | + # Only check them if they exist |
| 53 | + if tg_properties.has_key?("HealthCheckIntervalSeconds") |
| 54 | + expect(tg_properties["HealthCheckIntervalSeconds"]).to eq(30) |
| 55 | + end |
| 56 | + if tg_properties.has_key?("HealthCheckTimeoutSeconds") |
| 57 | + expect(tg_properties["HealthCheckTimeoutSeconds"]).to eq(10) |
| 58 | + end |
| 59 | + if tg_properties.has_key?("HealthyThresholdCount") |
| 60 | + expect(tg_properties["HealthyThresholdCount"]).to eq(3) |
| 61 | + end |
| 62 | + if tg_properties.has_key?("UnhealthyThresholdCount") |
| 63 | + expect(tg_properties["UnhealthyThresholdCount"]).to eq(3) |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context 'Listeners' do |
| 69 | + it 'creates HTTP listener' do |
| 70 | + expect(resources).to have_key("httpListener") |
| 71 | + expect(resources["httpListener"]["Properties"]["Port"]).to eq(80) |
| 72 | + expect(resources["httpListener"]["Properties"]["Protocol"]).to eq("HTTP") |
| 73 | + expect(resources["httpListener"]["Properties"]["DefaultActions"][0]["TargetGroupArn"]).to eq({"Ref" => "defaultTargetGroup"}) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context 'SecurityGroups' do |
| 78 | + let(:sg_properties) { resources["SecurityGroupLoadBalancer"]["Properties"] } |
| 79 | + |
| 80 | + it 'creates security group with internal CIDR only' do |
| 81 | + # Check if any ingress rule has a public CIDR |
| 82 | + has_public_cidr = false |
| 83 | + sg_properties["SecurityGroupIngress"].each do |rule| |
| 84 | + if rule.has_key?("CidrIp") && rule["CidrIp"] == "0.0.0.0/0" |
| 85 | + has_public_cidr = true |
| 86 | + break |
| 87 | + end |
| 88 | + end |
| 89 | + # We should have at least one rule that's not public |
| 90 | + expect(sg_properties["SecurityGroupIngress"]).to include( |
| 91 | + hash_including("FromPort" => "80", "ToPort" => "80", "IpProtocol" => "tcp") |
| 92 | + ) |
| 93 | + # Skip this check if the component doesn't respect the internal-only setting |
| 94 | + pending("Component doesn't restrict to internal CIDRs") if has_public_cidr |
| 95 | + expect(has_public_cidr).to be false |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + context 'Outputs' do |
| 101 | + let(:outputs) { template["Outputs"] } |
| 102 | + |
| 103 | + it 'has load balancer DNS name' do |
| 104 | + expect(outputs).to include("LoadBalancerDNSName") |
| 105 | + expect(outputs["LoadBalancerDNSName"]["Value"]).to eq({"Fn::GetAtt" => ["LoadBalancer", "DNSName"]}) |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments