github.com/in4it/ecs-deploy@v0.0.42-0.20240508120354-ed77ff16df25/provider/ecs/alb_test.go (about)

     1  package ecs
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/elbv2"
     9  	"github.com/in4it/ecs-deploy/util"
    10  )
    11  
    12  func TestGetHighestRule(t *testing.T) {
    13  	if accountId == nil {
    14  		t.Skip(noAWSMsg)
    15  	}
    16  	a, err := NewALB(util.GetEnv("TEST_CLUSTERNAME", "test-cluster"))
    17  	if err != nil {
    18  		t.Errorf("Error: %v", err)
    19  		return
    20  	}
    21  	highest, err := a.GetHighestRule()
    22  	if err != nil {
    23  		t.Errorf("Error: %v", err)
    24  	}
    25  	fmt.Printf("Highest rule in ALB (%v) is: %d ", a.loadBalancerName, highest)
    26  }
    27  
    28  func TestFindRule(t *testing.T) {
    29  	a := ALB{}
    30  	a.Rules = make(map[string][]*elbv2.Rule)
    31  	a.Rules["listener"] = []*elbv2.Rule{
    32  		{
    33  			RuleArn:  aws.String("1"),
    34  			Priority: aws.String("1"),
    35  			Actions: []*elbv2.Action{
    36  				{
    37  					Type:           aws.String("forward"),
    38  					TargetGroupArn: aws.String("targetGroup"),
    39  				},
    40  			},
    41  			Conditions: []*elbv2.RuleCondition{
    42  				{
    43  					Field:  aws.String("host-header"),
    44  					Values: []*string{aws.String("host.example.com")},
    45  				},
    46  			},
    47  		},
    48  		{
    49  			RuleArn:  aws.String("2"),
    50  			Priority: aws.String("2"),
    51  			Actions: []*elbv2.Action{
    52  				{
    53  					Type:           aws.String("forward"),
    54  					TargetGroupArn: aws.String("targetGroup"),
    55  				},
    56  			},
    57  			Conditions: []*elbv2.RuleCondition{
    58  				{
    59  					Field:  aws.String("host-header"),
    60  					Values: []*string{aws.String("host-2.example.com")},
    61  				},
    62  				{
    63  					Field:  aws.String("path-pattern"),
    64  					Values: []*string{aws.String("/api")},
    65  				},
    66  			},
    67  		},
    68  		{
    69  			RuleArn:  aws.String("3"),
    70  			Priority: aws.String("3"),
    71  			Actions: []*elbv2.Action{
    72  				{
    73  					Type:           aws.String("forward"),
    74  					TargetGroupArn: aws.String("targetGroup"),
    75  				},
    76  			},
    77  			Conditions: []*elbv2.RuleCondition{
    78  				{
    79  					Field:  aws.String("host-header"),
    80  					Values: []*string{aws.String("host.example.com")},
    81  				},
    82  				{
    83  					Field:  aws.String("path-pattern"),
    84  					Values: []*string{aws.String("/api/v1")},
    85  				},
    86  			},
    87  		},
    88  		{
    89  			RuleArn:  aws.String("4"),
    90  			Priority: aws.String("4"),
    91  			Actions: []*elbv2.Action{
    92  				{
    93  					Type:           aws.String("forward"),
    94  					TargetGroupArn: aws.String("targetGroup"),
    95  				},
    96  			},
    97  			Conditions: []*elbv2.RuleCondition{
    98  				{
    99  					Field:  aws.String("host-header"),
   100  					Values: []*string{aws.String("host.example.com")},
   101  				},
   102  				{
   103  					Field:  aws.String("path-pattern"),
   104  					Values: []*string{aws.String("/api")},
   105  				},
   106  			},
   107  		},
   108  	}
   109  	conditionField := []string{"host-header", "path-pattern"}
   110  	conditionValue := []string{"host.example.com", "/api"}
   111  	ruleArn, priority, err := a.FindRule("listener", "targetGroup", conditionField, conditionValue)
   112  	if err != nil {
   113  		t.Errorf("Error: %v", err)
   114  	}
   115  	if *priority != "4" || *ruleArn != "4" {
   116  		t.Errorf("Error: found wrong rule")
   117  	}
   118  	// re-order
   119  	a.Rules["listener"][0], a.Rules["listener"][3] = a.Rules["listener"][3], a.Rules["listener"][0]
   120  	ruleArn, priority, err = a.FindRule("listener", "targetGroup", conditionField, conditionValue)
   121  	if err != nil {
   122  		t.Errorf("Error: %v", err)
   123  	}
   124  	if *priority != "4" || *ruleArn != "4" {
   125  		t.Errorf("Error: found wrong rule")
   126  	}
   127  }
   128  
   129  func TestGetListenersArnForProtocol(t *testing.T) {
   130  	a := ALB{}
   131  	a.Listeners = []*elbv2.Listener{
   132  		{
   133  			Protocol:    aws.String("HTTP"),
   134  			ListenerArn: aws.String("arn:aws:elasticloadbalancing:region:1234567890:listener/app/myapp/abc123"),
   135  		},
   136  		{
   137  			Protocol:    aws.String("HTTPS"),
   138  			ListenerArn: aws.String("arn:aws:elasticloadbalancing:region:1234567890:listener/app/myapp/def456"),
   139  		},
   140  	}
   141  	listeners := []string{"http", "https"}
   142  	retListeners := a.getListenersArnForProtocol(listeners)
   143  	expectedResult := map[string]string{
   144  		"http":  "arn:aws:elasticloadbalancing:region:1234567890:listener/app/myapp/abc123",
   145  		"https": "arn:aws:elasticloadbalancing:region:1234567890:listener/app/myapp/def456",
   146  	}
   147  	if retListeners["http"] != expectedResult["http"] {
   148  		t.Errorf("didn't get expected result: got %s, expected %s", retListeners, expectedResult)
   149  	}
   150  	if retListeners["https"] != expectedResult["https"] {
   151  		t.Errorf("didn't get expected result: got %s, expected %s", retListeners, expectedResult)
   152  	}
   153  }
   154  
   155  func TestGetListenerArnForProtocol(t *testing.T) {
   156  	a := ALB{}
   157  	a.Listeners = []*elbv2.Listener{
   158  		{
   159  			Protocol:    aws.String("HTTP"),
   160  			ListenerArn: aws.String("arn:aws:elasticloadbalancing:region:1234567890:listener/app/myapp/abc123"),
   161  		},
   162  		{
   163  			Protocol:    aws.String("HTTPS"),
   164  			ListenerArn: aws.String("arn:aws:elasticloadbalancing:region:1234567890:listener/app/myapp/def456"),
   165  		},
   166  	}
   167  	retListener := a.GetListenerArnForProtocol("http")
   168  	expectedResult := aws.StringValue(a.Listeners[0].ListenerArn)
   169  	if retListener != expectedResult {
   170  		t.Errorf("didn't get expected result: got %s, expected %s", retListener, expectedResult)
   171  	}
   172  }