sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/endpoints/endpoints_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package endpoints
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  
    23  	"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope"
    24  )
    25  
    26  func TestParseFlags(t *testing.T) {
    27  	testCases := []struct {
    28  		name           string
    29  		flagToParse    string
    30  		expectedOutput []scope.ServiceEndpoint
    31  		expectedError  error
    32  	}{
    33  		{
    34  			name:           "no configuration",
    35  			flagToParse:    "",
    36  			expectedOutput: nil,
    37  			expectedError:  nil,
    38  		},
    39  		{
    40  			name:        "single region, single service",
    41  			flagToParse: "us-iso:ec2=https://localhost:8080",
    42  			expectedOutput: []scope.ServiceEndpoint{
    43  				{
    44  					ServiceID:     "ec2",
    45  					URL:           "https://localhost:8080",
    46  					SigningRegion: "us-iso",
    47  				},
    48  			},
    49  			expectedError: nil,
    50  		},
    51  		{
    52  			name:        "single region, multiple services",
    53  			flagToParse: "us-iso:ec2=https://localhost:8080,sts=https://elbhost:8080",
    54  			expectedOutput: []scope.ServiceEndpoint{
    55  				{
    56  					ServiceID:     "ec2",
    57  					URL:           "https://localhost:8080",
    58  					SigningRegion: "us-iso",
    59  				},
    60  				{
    61  					ServiceID:     "sts",
    62  					URL:           "https://elbhost:8080",
    63  					SigningRegion: "us-iso",
    64  				},
    65  			},
    66  			expectedError: nil,
    67  		},
    68  		{
    69  			name:           "single region, duplicate service",
    70  			flagToParse:    "us-iso:ec2=https://localhost:8080,ec2=https://elbhost:8080",
    71  			expectedOutput: nil,
    72  			expectedError:  errServiceEndpointDuplicateServiceID,
    73  		},
    74  		{
    75  			name:           "single region, non-valid URI",
    76  			flagToParse:    "us-iso:ec2=fdsfs",
    77  			expectedOutput: nil,
    78  			expectedError:  errServiceEndpointURL,
    79  		},
    80  		{
    81  			name:        "multiples regions",
    82  			flagToParse: "us-iso:ec2=https://localhost:8080,sts=https://elbhost:8080;gb-iso:ec2=https://localhost:8080,sts=https://elbhost:8080",
    83  			expectedOutput: []scope.ServiceEndpoint{
    84  				{
    85  					ServiceID:     "ec2",
    86  					URL:           "https://localhost:8080",
    87  					SigningRegion: "us-iso",
    88  				},
    89  				{
    90  					ServiceID:     "sts",
    91  					URL:           "https://elbhost:8080",
    92  					SigningRegion: "us-iso",
    93  				},
    94  				{
    95  					ServiceID:     "ec2",
    96  					URL:           "https://localhost:8080",
    97  					SigningRegion: "gb-iso",
    98  				},
    99  				{
   100  					ServiceID:     "sts",
   101  					URL:           "https://elbhost:8080",
   102  					SigningRegion: "gb-iso",
   103  				},
   104  			},
   105  			expectedError: nil,
   106  		},
   107  		{
   108  			name:           "invalid config",
   109  			flagToParse:    "us-isoec2=localhost",
   110  			expectedOutput: nil,
   111  			expectedError:  errServiceEndpointSigningRegion,
   112  		},
   113  	}
   114  
   115  	for _, tc := range testCases {
   116  		t.Run(tc.name, func(t *testing.T) {
   117  			out, err := ParseFlag(tc.flagToParse)
   118  
   119  			if !errors.Is(err, tc.expectedError) {
   120  				t.Fatalf("did not expect correct error: got %v, expected %v", err, tc.expectedError)
   121  			}
   122  
   123  			if !endpointsEqual(out, tc.expectedOutput) {
   124  				t.Fatalf("did not expect correct output: got %v, expected %v", out, tc.expectedOutput)
   125  			}
   126  		})
   127  	}
   128  }
   129  
   130  func endpointsEqual(a, b []scope.ServiceEndpoint) bool {
   131  	if len(a) != len(b) {
   132  		return false
   133  	}
   134  
   135  	for i, v := range a {
   136  		if v != b[i] {
   137  			return false
   138  		}
   139  	}
   140  	return true
   141  }