github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/provider/aws_provider_test.go (about)

     1  package provider
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
     7  
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/ptr"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestAWSZones(t *testing.T) {
    15  	regions := broker.AWSRegions(false)
    16  	for _, region := range regions {
    17  		_, exists := awsZones[region]
    18  		assert.True(t, exists)
    19  	}
    20  	_, exists := awsZones[DefaultAWSRegion]
    21  	assert.True(t, exists)
    22  }
    23  
    24  func TestAWSZonesWithCustomNodeIPRange(t *testing.T) {
    25  	svc := AWSInput{
    26  		MultiZone: true,
    27  	}
    28  
    29  	clusterConfigInput := svc.Defaults()
    30  	svc.ApplyParameters(clusterConfigInput, internal.ProvisioningParameters{
    31  		Parameters: internal.ProvisioningParametersDTO{
    32  			Networking: &internal.NetworkingDTO{
    33  				NodesCidr: "10.180.0.0/16",
    34  			},
    35  		},
    36  	})
    37  
    38  	assert.Equal(t, "10.180.0.0/16", clusterConfigInput.GardenerConfig.WorkerCidr)
    39  	assert.Equal(t, "10.180.0.0/16", clusterConfigInput.GardenerConfig.ProviderSpecificConfig.AwsConfig.VpcCidr)
    40  
    41  	for tname, tcase := range map[string]struct {
    42  		givenNodesCidr   string
    43  		expectedAwsZones []gqlschema.AWSZoneInput
    44  	}{
    45  		"Regular 10.250.0.0/16": {
    46  			givenNodesCidr: "10.250.0.0/16",
    47  			expectedAwsZones: []gqlschema.AWSZoneInput{
    48  				{
    49  					WorkerCidr:   "10.250.0.0/19",
    50  					PublicCidr:   "10.250.32.0/20",
    51  					InternalCidr: "10.250.48.0/20",
    52  				},
    53  				{
    54  					WorkerCidr:   "10.250.64.0/19",
    55  					PublicCidr:   "10.250.96.0/20",
    56  					InternalCidr: "10.250.112.0/20",
    57  				},
    58  				{
    59  					WorkerCidr:   "10.250.128.0/19",
    60  					PublicCidr:   "10.250.160.0/20",
    61  					InternalCidr: "10.250.176.0/20",
    62  				},
    63  			},
    64  		},
    65  		"Regular 10.180.0.0/23": {
    66  			givenNodesCidr: "10.180.0.0/23",
    67  			expectedAwsZones: []gqlschema.AWSZoneInput{
    68  				{
    69  					WorkerCidr:   "10.180.0.0/26",
    70  					PublicCidr:   "10.180.0.64/27",
    71  					InternalCidr: "10.180.0.96/27",
    72  				},
    73  				{
    74  					WorkerCidr:   "10.180.0.128/26",
    75  					PublicCidr:   "10.180.0.192/27",
    76  					InternalCidr: "10.180.0.224/27",
    77  				},
    78  				{
    79  					WorkerCidr:   "10.180.1.0/26",
    80  					PublicCidr:   "10.180.1.64/27",
    81  					InternalCidr: "10.180.1.96/27",
    82  				},
    83  			},
    84  		},
    85  	} {
    86  		t.Run(tname, func(t *testing.T) {
    87  			// given
    88  			svc := AWSInput{
    89  				MultiZone: true,
    90  			}
    91  
    92  			// when
    93  			clusterConfigInput := svc.Defaults()
    94  			svc.ApplyParameters(clusterConfigInput, internal.ProvisioningParameters{
    95  				Parameters: internal.ProvisioningParametersDTO{
    96  					Networking: &internal.NetworkingDTO{
    97  						NodesCidr: tcase.givenNodesCidr,
    98  					},
    99  				},
   100  			})
   101  
   102  			// then
   103  			assert.Equal(t, tcase.givenNodesCidr, clusterConfigInput.GardenerConfig.WorkerCidr)
   104  			assert.Equal(t, tcase.givenNodesCidr, clusterConfigInput.GardenerConfig.ProviderSpecificConfig.AwsConfig.VpcCidr)
   105  
   106  			for i, expectedZone := range tcase.expectedAwsZones {
   107  				assertAWSIpRanges(t, expectedZone, clusterConfigInput.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones[i])
   108  			}
   109  		})
   110  	}
   111  
   112  }
   113  
   114  func assertAWSIpRanges(t *testing.T, zone gqlschema.AWSZoneInput, input *gqlschema.AWSZoneInput) {
   115  	assert.Equal(t, zone.InternalCidr, input.InternalCidr)
   116  	assert.Equal(t, zone.WorkerCidr, input.WorkerCidr)
   117  	assert.Equal(t, zone.PublicCidr, input.PublicCidr)
   118  }
   119  
   120  func TestAWSZonesForEuAccess(t *testing.T) {
   121  	regions := broker.AWSRegions(true)
   122  	for _, region := range regions {
   123  		_, exists := awsZones[region]
   124  		assert.True(t, exists)
   125  	}
   126  	_, exists := awsZones[DefaultEuAccessAWSRegion]
   127  	assert.True(t, exists)
   128  }
   129  
   130  func TestMultipleZonesForAWSRegion(t *testing.T) {
   131  	t.Run("for valid zonesCount", func(t *testing.T) {
   132  		// given
   133  		region := "us-east-1"
   134  
   135  		// when
   136  		generatedZones := MultipleZonesForAWSRegion(region, 3)
   137  
   138  		// then
   139  		for _, zone := range generatedZones {
   140  			regionFromZone := zone[:len(zone)-1]
   141  			assert.Equal(t, region, regionFromZone)
   142  		}
   143  		assert.Equal(t, 3, len(generatedZones))
   144  		// check if all zones are unique
   145  		assert.Condition(t, func() (success bool) {
   146  			var zones []string
   147  			for _, zone := range generatedZones {
   148  				for _, z := range zones {
   149  					if zone == z {
   150  						return false
   151  					}
   152  				}
   153  				zones = append(zones, zone)
   154  			}
   155  			return true
   156  		})
   157  	})
   158  
   159  	t.Run("for zonesCount exceeding maximum zones for region", func(t *testing.T) {
   160  		// given
   161  		region := "us-east-1"
   162  		zonesCountExceedingMaximum := 20
   163  		maximumZonesForRegion := len(awsZones[region])
   164  		// "us-east-1" region has maximum 6 zones, user request 20
   165  
   166  		// when
   167  		generatedZones := MultipleZonesForAWSRegion(region, zonesCountExceedingMaximum)
   168  
   169  		// then
   170  		for _, zone := range generatedZones {
   171  			regionFromZone := zone[:len(zone)-1]
   172  			assert.Equal(t, region, regionFromZone)
   173  		}
   174  		assert.Equal(t, maximumZonesForRegion, len(generatedZones))
   175  	})
   176  }
   177  
   178  func TestAWSInput_SingleZone_ApplyParameters(t *testing.T) {
   179  	// given
   180  	svc := AWSInput{}
   181  
   182  	// when
   183  	t.Run("use default region and default zones count", func(t *testing.T) {
   184  		// given
   185  		input := svc.Defaults()
   186  
   187  		// when
   188  		svc.ApplyParameters(input, internal.ProvisioningParameters{})
   189  
   190  		//then
   191  		assert.Equal(t, DefaultAWSRegion, input.GardenerConfig.Region)
   192  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones, 1)
   193  
   194  		for _, zone := range input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones {
   195  			regionFromZone := zone.Name[:len(zone.Name)-1]
   196  			assert.Equal(t, DefaultAWSRegion, regionFromZone)
   197  		}
   198  	})
   199  
   200  	t.Run("use default region and default zones count for EU Access", func(t *testing.T) {
   201  		// given
   202  		input := svc.Defaults()
   203  
   204  		// when
   205  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   206  			PlatformRegion: "cf-ch20",
   207  		})
   208  
   209  		//then
   210  		assert.Equal(t, DefaultEuAccessAWSRegion, input.GardenerConfig.Region)
   211  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones, 1)
   212  
   213  		for _, zone := range input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones {
   214  			regionFromZone := zone.Name[:len(zone.Name)-1]
   215  			assert.Equal(t, DefaultEuAccessAWSRegion, regionFromZone)
   216  		}
   217  	})
   218  
   219  	// when
   220  	t.Run("use region input parameter", func(t *testing.T) {
   221  		// given
   222  		input := svc.Defaults()
   223  		inputRegion := "us-east-1"
   224  
   225  		// when
   226  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   227  			Parameters: internal.ProvisioningParametersDTO{
   228  				Region: ptr.String(inputRegion),
   229  			},
   230  		})
   231  
   232  		//then
   233  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones, 1)
   234  
   235  		for _, zone := range input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones {
   236  			regionFromZone := zone.Name[:len(zone.Name)-1]
   237  			assert.Equal(t, inputRegion, regionFromZone)
   238  		}
   239  	})
   240  
   241  	// when
   242  	t.Run("use zones list input parameter", func(t *testing.T) {
   243  		// given
   244  		input := svc.Defaults()
   245  		zones := []string{"eu-central-1a", "eu-central-1b"}
   246  
   247  		// when
   248  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   249  			Parameters: internal.ProvisioningParametersDTO{
   250  				Zones: zones,
   251  			},
   252  		})
   253  
   254  		//then
   255  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones, len(zones))
   256  
   257  		for i, zone := range input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones {
   258  			assert.Equal(t, zones[i], zone.Name)
   259  		}
   260  	})
   261  }
   262  
   263  func TestAWSInput_MultiZone_ApplyParameters(t *testing.T) {
   264  	// given
   265  	svc := AWSInput{MultiZone: true, ControlPlaneFailureTolerance: "zone"}
   266  
   267  	// when
   268  	t.Run("use default region and default zones count", func(t *testing.T) {
   269  		// given
   270  		input := svc.Defaults()
   271  
   272  		// when
   273  		svc.ApplyParameters(input, internal.ProvisioningParameters{})
   274  
   275  		//then
   276  		assert.Equal(t, DefaultAWSRegion, input.GardenerConfig.Region)
   277  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones, DefaultAWSMultiZoneCount)
   278  
   279  		for _, zone := range input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones {
   280  			regionFromZone := zone.Name[:len(zone.Name)-1]
   281  			assert.Equal(t, DefaultAWSRegion, regionFromZone)
   282  		}
   283  
   284  		assert.Equal(t, "zone", *input.GardenerConfig.ControlPlaneFailureTolerance)
   285  	})
   286  
   287  	// when
   288  	t.Run("use region input parameter", func(t *testing.T) {
   289  		// given
   290  		input := svc.Defaults()
   291  		inputRegion := "us-east-1"
   292  
   293  		// when
   294  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   295  			Parameters: internal.ProvisioningParametersDTO{
   296  				Region: ptr.String(inputRegion),
   297  			},
   298  		})
   299  
   300  		//then
   301  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones, DefaultAWSMultiZoneCount)
   302  
   303  		for _, zone := range input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones {
   304  			regionFromZone := zone.Name[:len(zone.Name)-1]
   305  			assert.Equal(t, inputRegion, regionFromZone)
   306  		}
   307  		assert.Equal(t, "zone", *input.GardenerConfig.ControlPlaneFailureTolerance)
   308  	})
   309  
   310  	// when
   311  	t.Run("use zones list input parameter", func(t *testing.T) {
   312  		// given
   313  		input := svc.Defaults()
   314  		zones := []string{"eu-central-1a", "eu-central-1b"}
   315  
   316  		// when
   317  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   318  			Parameters: internal.ProvisioningParametersDTO{
   319  				Zones: zones,
   320  			},
   321  		})
   322  
   323  		//then
   324  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones, len(zones))
   325  
   326  		for i, zone := range input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones {
   327  			assert.Equal(t, zones[i], zone.Name)
   328  		}
   329  		assert.Equal(t, "zone", *input.GardenerConfig.ControlPlaneFailureTolerance)
   330  	})
   331  }
   332  
   333  func TestAWSTrialInput_ApplyParameters(t *testing.T) {
   334  	t.Run("AWS trial", func(t *testing.T) {
   335  		svc := AWSTrialInput{PlatformRegionMapping: map[string]string{
   336  			"cf-eu10": "europe",
   337  			"cf-us10": "us",
   338  		}}
   339  		input := svc.Defaults()
   340  
   341  		// when
   342  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   343  			PlatformRegion: "cf-us10",
   344  		})
   345  
   346  		// then
   347  		assert.Contains(t, input.GardenerConfig.ProviderSpecificConfig.AwsConfig.AwsZones[0].Name, input.GardenerConfig.Region)
   348  	})
   349  
   350  	t.Run("AWS trial with EU Access restrictions", func(t *testing.T) {
   351  		svc := AWSTrialInput{PlatformRegionMapping: map[string]string{
   352  			"cf-eu10": "europe",
   353  			"cf-us10": "us",
   354  		}}
   355  		input := svc.Defaults()
   356  
   357  		// when
   358  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   359  			PlatformRegion: "cf-eu11",
   360  		})
   361  
   362  		// then
   363  		assert.Contains(t, DefaultEuAccessAWSRegion, input.GardenerConfig.Region)
   364  	})
   365  }