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

     1  package provider
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestAzureTrialInput_ApplyParametersWithRegion(t *testing.T) { //TODO apply EU Access for trials
    13  	// given
    14  	svc := AzureTrialInput{
    15  		PlatformRegionMapping: map[string]string{
    16  			"cf-asia": "asia",
    17  		},
    18  	}
    19  
    20  	// when
    21  	t.Run("use platform region mapping", func(t *testing.T) {
    22  		// given
    23  		input := svc.Defaults()
    24  
    25  		// when
    26  		svc.ApplyParameters(input, internal.ProvisioningParameters{
    27  			PlatformRegion: "cf-asia",
    28  		})
    29  
    30  		//then
    31  		assert.Equal(t, "southeastasia", input.GardenerConfig.Region)
    32  	})
    33  
    34  	// when
    35  	t.Run("use customer mapping", func(t *testing.T) {
    36  		// given
    37  		input := svc.Defaults()
    38  		us := "us"
    39  
    40  		// when
    41  		svc.ApplyParameters(input, internal.ProvisioningParameters{
    42  			PlatformRegion: "cf-asia",
    43  			Parameters: internal.ProvisioningParametersDTO{
    44  				Region: &us,
    45  			},
    46  		})
    47  
    48  		//then
    49  		assert.Equal(t, "eastus", input.GardenerConfig.Region)
    50  	})
    51  
    52  	// when
    53  	t.Run("forget customer empty region", func(t *testing.T) {
    54  		// given
    55  		input := svc.Defaults()
    56  		r := ""
    57  
    58  		// when
    59  		svc.ApplyParameters(input, internal.ProvisioningParameters{
    60  			Parameters: internal.ProvisioningParametersDTO{
    61  				Region: &r,
    62  			},
    63  		})
    64  
    65  		//then
    66  		assert.Equal(t, DefaultAzureRegion, input.GardenerConfig.Region)
    67  	})
    68  
    69  	// when
    70  	t.Run("forget customer empty region for EU Access", func(t *testing.T) {
    71  		// given
    72  		input := svc.Defaults()
    73  		r := ""
    74  
    75  		// when
    76  		svc.ApplyParameters(input, internal.ProvisioningParameters{
    77  			PlatformRegion: "cf-ch20",
    78  			Parameters: internal.ProvisioningParametersDTO{
    79  				Region: &r,
    80  			},
    81  		})
    82  
    83  		//then
    84  		assert.Equal(t, DefaultEuAccessAzureRegion, input.GardenerConfig.Region)
    85  	})
    86  
    87  	// when
    88  	t.Run("use default region", func(t *testing.T) {
    89  		// given
    90  		input := svc.Defaults()
    91  
    92  		// when
    93  		svc.ApplyParameters(input, internal.ProvisioningParameters{})
    94  
    95  		//then
    96  		assert.Equal(t, DefaultAzureRegion, input.GardenerConfig.Region)
    97  	})
    98  
    99  	// when
   100  	t.Run("use default region for EU Access", func(t *testing.T) {
   101  		// given
   102  		input := svc.Defaults()
   103  
   104  		// when
   105  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   106  			PlatformRegion: "cf-ch20"},
   107  		)
   108  
   109  		//then
   110  		assert.Equal(t, DefaultEuAccessAzureRegion, input.GardenerConfig.Region)
   111  	})
   112  
   113  	// when
   114  	t.Run("use random zone", func(t *testing.T) {
   115  		// given
   116  		input := svc.Defaults()
   117  
   118  		// when
   119  		svc.ApplyParameters(input, internal.ProvisioningParameters{})
   120  
   121  		zone := input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones[0].Name
   122  
   123  		//then
   124  		assert.LessOrEqual(t, zone, 3)
   125  		assert.GreaterOrEqual(t, zone, 1)
   126  	})
   127  
   128  	// when
   129  	t.Run("use default region for not defined mapping", func(t *testing.T) {
   130  		// given
   131  		input := svc.Defaults()
   132  
   133  		// when
   134  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   135  			PlatformRegion: "cf-southamerica",
   136  		})
   137  
   138  		//then
   139  		assert.Equal(t, DefaultAzureRegion, input.GardenerConfig.Region)
   140  	})
   141  
   142  	// when
   143  	t.Run("use default with NAT gateway", func(t *testing.T) {
   144  		// given
   145  		input := svc.Defaults()
   146  
   147  		//then
   148  		assert.Equal(t, false, *input.GardenerConfig.ProviderSpecificConfig.AzureConfig.EnableNatGateway)
   149  	})
   150  }
   151  
   152  func TestAzureInput_SingleZone_ApplyParameters(t *testing.T) {
   153  	// given
   154  	svc := AzureInput{}
   155  
   156  	// when
   157  	t.Run("defaults use one zone with dedicated subnet", func(t *testing.T) {
   158  		// given
   159  		input := svc.Defaults()
   160  
   161  		// when
   162  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   163  			Parameters: internal.ProvisioningParametersDTO{},
   164  		})
   165  
   166  		//then
   167  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones, 1)
   168  		assert.Subset(t, []int{1, 2, 3}, azureZoneNames(input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones))
   169  		for i, zone := range input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones {
   170  			assert.Equal(t, fmt.Sprintf("10.250.%d.0/25", 32*i), zone.Cidr)
   171  		}
   172  	})
   173  
   174  	// when
   175  	t.Run("use zones parameter", func(t *testing.T) {
   176  		// given
   177  		input := svc.Defaults()
   178  
   179  		// when
   180  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   181  			Parameters: internal.ProvisioningParametersDTO{
   182  				Zones: []string{"2", "3"},
   183  			},
   184  		})
   185  
   186  		//then
   187  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones, 2)
   188  		assert.Equal(t, []int{2, 3}, azureZoneNames(input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones))
   189  
   190  		assertAzureZoneCidrs(t, []string{"10.250.0.0/25", "10.250.0.128/25"}, input)
   191  	})
   192  }
   193  
   194  func TestAzureInput_MultiZone_ApplyParameters(t *testing.T) {
   195  	// given
   196  	svc := AzureInput{
   197  		MultiZone:                    true,
   198  		ControlPlaneFailureTolerance: "zone",
   199  	}
   200  
   201  	// when
   202  	t.Run("defaults use three zones with dedicated subnet", func(t *testing.T) {
   203  		// given
   204  		input := svc.Defaults()
   205  
   206  		// when
   207  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   208  			Parameters: internal.ProvisioningParametersDTO{},
   209  		})
   210  
   211  		//then
   212  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones, DefaultAzureMultiZoneCount)
   213  		assert.ElementsMatch(t, []int{1, 2, 3}, azureZoneNames(input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones))
   214  
   215  		assertAzureZoneCidrs(t, []string{"10.250.0.0/25", "10.250.0.128/25", "10.250.1.0/25"}, input)
   216  		assert.Equal(t, "zone", *input.GardenerConfig.ControlPlaneFailureTolerance)
   217  	})
   218  
   219  	// when
   220  	t.Run("use provided nodes CIDR", func(t *testing.T) {
   221  		// given
   222  		input := svc.Defaults()
   223  
   224  		// when
   225  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   226  			Parameters: internal.ProvisioningParametersDTO{
   227  				Networking: &internal.NetworkingDTO{
   228  					NodesCidr: "10.180.0.0/17",
   229  				},
   230  			},
   231  		})
   232  
   233  		//then
   234  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones, DefaultAzureMultiZoneCount)
   235  		assert.ElementsMatch(t, []int{1, 2, 3}, azureZoneNames(input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones))
   236  		assertAzureZoneCidrs(t, []string{"10.180.0.0/20", "10.180.16.0/20", "10.180.32.0/20"}, input)
   237  		assert.Equal(t, "zone", *input.GardenerConfig.ControlPlaneFailureTolerance)
   238  
   239  	})
   240  
   241  	// when
   242  	t.Run("use zones parameter", func(t *testing.T) {
   243  		// given
   244  		input := svc.Defaults()
   245  
   246  		// when
   247  		svc.ApplyParameters(input, internal.ProvisioningParameters{
   248  			Parameters: internal.ProvisioningParametersDTO{
   249  				Zones: []string{"2", "3"},
   250  			},
   251  		})
   252  
   253  		//then
   254  		assert.Len(t, input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones, 2)
   255  		assert.Equal(t, []int{2, 3}, azureZoneNames(input.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones))
   256  		assertAzureZoneCidrs(t, []string{"10.250.0.0/25", "10.250.0.128/25"}, input)
   257  		assert.Equal(t, "zone", *input.GardenerConfig.ControlPlaneFailureTolerance)
   258  	})
   259  }
   260  
   261  func azureZoneNames(zones []*gqlschema.AzureZoneInput) []int {
   262  	zoneNames := []int{}
   263  
   264  	for _, zone := range zones {
   265  		zoneNames = append(zoneNames, zone.Name)
   266  	}
   267  
   268  	return zoneNames
   269  }
   270  
   271  func assertAzureZoneCidrs(t *testing.T, expected []string, givenInput *gqlschema.ClusterConfigInput) {
   272  	t.Helper()
   273  	assert.Equal(t, len(expected), len(givenInput.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones))
   274  
   275  	for i, cidr := range expected {
   276  		assert.Equal(t, cidr, givenInput.GardenerConfig.ProviderSpecificConfig.AzureConfig.AzureZones[i].Cidr)
   277  	}
   278  }