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

     1  package broker
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"path"
     8  	"testing"
     9  
    10  	"github.com/kyma-project/kyma-environment-broker/internal"
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestSchemaGenerator(t *testing.T) {
    17  	modulesEnabled := true
    18  	tests := []struct {
    19  		name                string
    20  		generator           func(map[string]string, []string, bool, bool) *map[string]interface{}
    21  		machineTypes        []string
    22  		machineTypesDisplay map[string]string
    23  		file                string
    24  		updateFile          string
    25  		fileOIDC            string
    26  		updateFileOIDC      string
    27  	}{
    28  		{
    29  			name: "AWS schema is correct",
    30  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
    31  				return AWSSchema(machinesDisplay, machines, additionalParams, update, false, false, modulesEnabled)
    32  			},
    33  			machineTypes:   []string{"m5.xlarge", "m5.2xlarge", "m5.4xlarge", "m5.8xlarge", "m5.12xlarge", "m6i.xlarge", "m6i.2xlarge", "m6i.4xlarge", "m6i.8xlarge", "m6i.12xlarge"},
    34  			file:           "aws-schema.json",
    35  			updateFile:     "update-aws-schema.json",
    36  			fileOIDC:       "aws-schema-additional-params.json",
    37  			updateFileOIDC: "update-aws-schema-additional-params.json",
    38  		},
    39  		{
    40  			name: "AWS schema with region required is correct",
    41  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
    42  				return AWSSchema(machinesDisplay, machines, additionalParams, update, false, true, modulesEnabled)
    43  			},
    44  			machineTypes:   []string{"m5.xlarge", "m5.2xlarge", "m5.4xlarge", "m5.8xlarge", "m5.12xlarge", "m6i.xlarge", "m6i.2xlarge", "m6i.4xlarge", "m6i.8xlarge", "m6i.12xlarge"},
    45  			file:           "aws-schema-region-required.json",
    46  			updateFile:     "update-aws-schema.json",
    47  			fileOIDC:       "aws-schema-additional-params-region-required.json",
    48  			updateFileOIDC: "update-aws-schema-additional-params.json",
    49  		},
    50  		{
    51  			name: "AWS schema with EU access restriction is correct",
    52  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
    53  				return AWSSchema(machinesDisplay, machines, additionalParams, update, true, false, modulesEnabled)
    54  			},
    55  			machineTypes:   []string{"m5.xlarge", "m5.2xlarge", "m5.4xlarge", "m5.8xlarge", "m5.12xlarge", "m6i.xlarge", "m6i.2xlarge", "m6i.4xlarge", "m6i.8xlarge", "m6i.12xlarge"},
    56  			file:           "aws-schema-eu.json",
    57  			updateFile:     "update-aws-schema.json",
    58  			fileOIDC:       "aws-schema-additional-params-eu.json",
    59  			updateFileOIDC: "update-aws-schema-additional-params.json",
    60  		},
    61  		{
    62  			name: "AWS schema with region required and EU access restriction is correct",
    63  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
    64  				return AWSSchema(machinesDisplay, machines, additionalParams, update, true, true, modulesEnabled)
    65  			},
    66  			machineTypes:   []string{"m5.xlarge", "m5.2xlarge", "m5.4xlarge", "m5.8xlarge", "m5.12xlarge", "m6i.xlarge", "m6i.2xlarge", "m6i.4xlarge", "m6i.8xlarge", "m6i.12xlarge"},
    67  			file:           "aws-schema-eu-region-required.json",
    68  			updateFile:     "update-aws-schema.json",
    69  			fileOIDC:       "aws-schema-additional-params-eu-region-required.json",
    70  			updateFileOIDC: "update-aws-schema-additional-params.json",
    71  		},
    72  		{
    73  			name: "Azure schema is correct",
    74  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
    75  				return AzureSchema(machinesDisplay, machines, additionalParams, update, false, false, modulesEnabled)
    76  			},
    77  			machineTypes:   []string{"Standard_D4_v3", "Standard_D8_v3", "Standard_D16_v3", "Standard_D32_v3", "Standard_D48_v3", "Standard_D64_v3"},
    78  			file:           "azure-schema.json",
    79  			updateFile:     "update-azure-schema.json",
    80  			fileOIDC:       "azure-schema-additional-params.json",
    81  			updateFileOIDC: "update-azure-schema-additional-params.json",
    82  		},
    83  		{
    84  			name: "Azure schema with region required is correct",
    85  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
    86  				return AzureSchema(machinesDisplay, machines, additionalParams, update, false, true, modulesEnabled)
    87  			},
    88  			machineTypes:   []string{"Standard_D4_v3", "Standard_D8_v3", "Standard_D16_v3", "Standard_D32_v3", "Standard_D48_v3", "Standard_D64_v3"},
    89  			file:           "azure-schema-region-required.json",
    90  			updateFile:     "update-azure-schema.json",
    91  			fileOIDC:       "azure-schema-additional-params-region-required.json",
    92  			updateFileOIDC: "update-azure-schema-additional-params.json",
    93  		},
    94  		{
    95  			name: "Azure schema with EU access restriction is correct",
    96  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
    97  				return AzureSchema(machinesDisplay, machines, additionalParams, update, true, false, modulesEnabled)
    98  			},
    99  			machineTypes:   []string{"Standard_D4_v3", "Standard_D8_v3", "Standard_D16_v3", "Standard_D32_v3", "Standard_D48_v3", "Standard_D64_v3"},
   100  			file:           "azure-schema-eu.json",
   101  			updateFile:     "update-azure-schema.json",
   102  			fileOIDC:       "azure-schema-additional-params-eu.json",
   103  			updateFileOIDC: "update-azure-schema-additional-params.json",
   104  		},
   105  		{
   106  			name: "Azure schema with region required and EU access restriction is correct",
   107  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   108  				return AzureSchema(machinesDisplay, machines, additionalParams, update, true, true, modulesEnabled)
   109  			},
   110  			machineTypes:   []string{"Standard_D4_v3", "Standard_D8_v3", "Standard_D16_v3", "Standard_D32_v3", "Standard_D48_v3", "Standard_D64_v3"},
   111  			file:           "azure-schema-eu-region-required.json",
   112  			updateFile:     "update-azure-schema.json",
   113  			fileOIDC:       "azure-schema-additional-params-eu-region-required.json",
   114  			updateFileOIDC: "update-azure-schema-additional-params.json",
   115  		},
   116  		{
   117  			name: "AzureLite schema is correct",
   118  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   119  				return AzureLiteSchema(machinesDisplay, machines, additionalParams, update, false, false, modulesEnabled)
   120  			},
   121  			machineTypes:        []string{"Standard_D4_v3"},
   122  			machineTypesDisplay: map[string]string{"Standard_D4_v3": "Standard_D4_v3 (4vCPU, 16GB RAM)"},
   123  			file:                "azure-lite-schema.json",
   124  			updateFile:          "update-azure-lite-schema.json",
   125  			fileOIDC:            "azure-lite-schema-additional-params.json",
   126  			updateFileOIDC:      "update-azure-lite-schema-additional-params.json",
   127  		},
   128  		{
   129  			name: "AzureLite schema with region required is correct",
   130  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   131  				return AzureLiteSchema(machinesDisplay, machines, additionalParams, update, false, true, modulesEnabled)
   132  			},
   133  			machineTypes:        []string{"Standard_D4_v3"},
   134  			machineTypesDisplay: map[string]string{"Standard_D4_v3": "Standard_D4_v3 (4vCPU, 16GB RAM)"},
   135  			file:                "azure-lite-schema-region-required.json",
   136  			updateFile:          "update-azure-lite-schema.json",
   137  			fileOIDC:            "azure-lite-schema-additional-params-region-required.json",
   138  			updateFileOIDC:      "update-azure-lite-schema-additional-params.json",
   139  		},
   140  		{
   141  			name: "AzureLite schema with EU access restriction is correct",
   142  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   143  				return AzureLiteSchema(machinesDisplay, machines, additionalParams, update, true, false, modulesEnabled)
   144  			},
   145  			machineTypes:        []string{"Standard_D4_v3"},
   146  			machineTypesDisplay: map[string]string{"Standard_D4_v3": "Standard_D4_v3 (4vCPU, 16GB RAM)"},
   147  			file:                "azure-lite-schema-eu.json",
   148  			updateFile:          "update-azure-lite-schema.json",
   149  			fileOIDC:            "azure-lite-schema-additional-params-eu.json",
   150  			updateFileOIDC:      "update-azure-lite-schema-additional-params.json",
   151  		},
   152  		{
   153  			name: "AzureLite schema with region required and EU access restriction is correct",
   154  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   155  				return AzureLiteSchema(machinesDisplay, machines, additionalParams, update, true, true, modulesEnabled)
   156  			},
   157  			machineTypes:        []string{"Standard_D4_v3"},
   158  			machineTypesDisplay: map[string]string{"Standard_D4_v3": "Standard_D4_v3 (4vCPU, 16GB RAM)"},
   159  			file:                "azure-lite-schema-eu-region-required.json",
   160  			updateFile:          "update-azure-lite-schema.json",
   161  			fileOIDC:            "azure-lite-schema-additional-params-eu-region-required.json",
   162  			updateFileOIDC:      "update-azure-lite-schema-additional-params.json",
   163  		},
   164  		{
   165  			name: "Freemium schema is correct",
   166  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   167  				return FreemiumSchema(internal.Azure, additionalParams, update, false, false, modulesEnabled)
   168  			},
   169  			machineTypes:   []string{},
   170  			file:           "free-azure-schema.json",
   171  			updateFile:     "update-free-azure-schema.json",
   172  			fileOIDC:       "free-azure-schema-additional-params.json",
   173  			updateFileOIDC: "update-free-azure-schema-additional-params.json",
   174  		},
   175  		{
   176  			name: "Freemium schema with region required is correct",
   177  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   178  				return FreemiumSchema(internal.Azure, additionalParams, update, false, true, modulesEnabled)
   179  			},
   180  			machineTypes:   []string{},
   181  			file:           "free-azure-schema-region-required.json",
   182  			updateFile:     "update-free-azure-schema.json",
   183  			fileOIDC:       "free-azure-schema-additional-params-region-required.json",
   184  			updateFileOIDC: "update-free-azure-schema-additional-params.json",
   185  		},
   186  		{
   187  			name: " Freemium schema is correct",
   188  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   189  				return FreemiumSchema(internal.AWS, additionalParams, update, false, false, modulesEnabled)
   190  			},
   191  			machineTypes:   []string{},
   192  			file:           "free-aws-schema.json",
   193  			updateFile:     "update-free-aws-schema.json",
   194  			fileOIDC:       "free-aws-schema-additional-params.json",
   195  			updateFileOIDC: "update-free-aws-schema-additional-params.json",
   196  		},
   197  		{
   198  			name: " Freemium schema with region required is correct",
   199  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   200  				return FreemiumSchema(internal.AWS, additionalParams, update, false, true, modulesEnabled)
   201  			},
   202  			machineTypes:   []string{},
   203  			file:           "free-aws-schema-region-required.json",
   204  			updateFile:     "update-free-aws-schema.json",
   205  			fileOIDC:       "free-aws-schema-additional-params-region-required.json",
   206  			updateFileOIDC: "update-free-aws-schema-additional-params.json",
   207  		},
   208  		{
   209  			name: "Freemium schema with EU access restriction is correct",
   210  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   211  				return FreemiumSchema(internal.Azure, additionalParams, update, true, false, modulesEnabled)
   212  			},
   213  			machineTypes:   []string{},
   214  			file:           "free-azure-schema-eu.json",
   215  			updateFile:     "update-free-azure-schema.json",
   216  			fileOIDC:       "free-azure-schema-additional-params-eu.json",
   217  			updateFileOIDC: "update-free-azure-schema-additional-params.json",
   218  		},
   219  		{
   220  			name: "Freemium schema with region required and EU access restriction is correct",
   221  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   222  				return FreemiumSchema(internal.Azure, additionalParams, update, true, true, modulesEnabled)
   223  			},
   224  			machineTypes:   []string{},
   225  			file:           "free-azure-schema-eu-region-required.json",
   226  			updateFile:     "update-free-azure-schema.json",
   227  			fileOIDC:       "free-azure-schema-additional-params-eu-region-required.json",
   228  			updateFileOIDC: "update-free-azure-schema-additional-params.json",
   229  		},
   230  		{
   231  			name: " Freemium schema with EU access restriction is correct",
   232  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   233  				return FreemiumSchema(internal.AWS, additionalParams, update, true, false, modulesEnabled)
   234  			},
   235  			machineTypes:   []string{},
   236  			file:           "free-aws-schema-eu.json",
   237  			updateFile:     "update-free-aws-schema.json",
   238  			fileOIDC:       "free-aws-schema-additional-params-eu.json",
   239  			updateFileOIDC: "update-free-aws-schema-additional-params.json",
   240  		},
   241  		{
   242  			name: " Freemium schema with region required and EU access restriction is correct",
   243  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   244  				return FreemiumSchema(internal.AWS, additionalParams, update, true, true, modulesEnabled)
   245  			},
   246  			machineTypes:   []string{},
   247  			file:           "free-aws-schema-eu-region-required.json",
   248  			updateFile:     "update-free-aws-schema.json",
   249  			fileOIDC:       "free-aws-schema-additional-params-eu-region-required.json",
   250  			updateFileOIDC: "update-free-aws-schema-additional-params.json",
   251  		},
   252  		{
   253  			name: "GCP schema is correct",
   254  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   255  				return GCPSchema(machinesDisplay, machines, additionalParams, update, false, modulesEnabled)
   256  			},
   257  			machineTypes:   []string{"n2-standard-4", "n2-standard-8", "n2-standard-16", "n2-standard-32", "n2-standard-48"},
   258  			file:           "gcp-schema.json",
   259  			updateFile:     "update-gcp-schema.json",
   260  			fileOIDC:       "gcp-schema-additional-params.json",
   261  			updateFileOIDC: "update-gcp-schema-additional-params.json",
   262  		},
   263  		{
   264  			name: "GCP schema with region required is correct",
   265  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   266  				return GCPSchema(machinesDisplay, machines, additionalParams, update, true, modulesEnabled)
   267  			},
   268  			machineTypes:   []string{"n2-standard-4", "n2-standard-8", "n2-standard-16", "n2-standard-32", "n2-standard-48"},
   269  			file:           "gcp-schema-region-required.json",
   270  			updateFile:     "update-gcp-schema.json",
   271  			fileOIDC:       "gcp-schema-additional-params-region-required.json",
   272  			updateFileOIDC: "update-gcp-schema-additional-params.json",
   273  		},
   274  		{
   275  			name: "OpenStack schema is correct",
   276  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   277  				return OpenStackSchema(machinesDisplay, machines, additionalParams, update, false, modulesEnabled)
   278  			},
   279  			machineTypes:   []string{"g_c4_m16", "g_c8_m32"},
   280  			file:           "openstack-schema.json",
   281  			updateFile:     "update-openstack-schema.json",
   282  			fileOIDC:       "openstack-schema-additional-params.json",
   283  			updateFileOIDC: "update-openstack-schema-additional-params.json",
   284  		},
   285  		{
   286  			name: "OpenStack schema with region required is correct",
   287  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   288  				return OpenStackSchema(machinesDisplay, machines, additionalParams, update, true, modulesEnabled)
   289  			},
   290  			machineTypes:   []string{"g_c4_m16", "g_c8_m32"},
   291  			file:           "openstack-schema-region-required.json",
   292  			updateFile:     "update-openstack-schema.json",
   293  			fileOIDC:       "openstack-schema-additional-params-region-required.json",
   294  			updateFileOIDC: "update-openstack-schema-additional-params.json",
   295  		},
   296  		{
   297  			name: "Trial schema is correct",
   298  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   299  				return TrialSchema(additionalParams, update, modulesEnabled)
   300  			},
   301  			machineTypes:   []string{},
   302  			file:           "azure-trial-schema.json",
   303  			updateFile:     "update-azure-trial-schema.json",
   304  			fileOIDC:       "azure-trial-schema-additional-params.json",
   305  			updateFileOIDC: "update-azure-trial-schema-additional-params.json",
   306  		},
   307  		{
   308  			name: "Own cluster schema is correct",
   309  			generator: func(machinesDisplay map[string]string, machines []string, additionalParams, update bool) *map[string]interface{} {
   310  				return OwnClusterSchema(update, modulesEnabled)
   311  			},
   312  			machineTypes:   []string{},
   313  			file:           "own-cluster-schema.json",
   314  			updateFile:     "update-own-cluster-schema.json",
   315  			fileOIDC:       "own-cluster-schema-additional-params.json",
   316  			updateFileOIDC: "update-own-cluster-schema-additional-params.json",
   317  		},
   318  	}
   319  	for _, tt := range tests {
   320  		t.Run(tt.name, func(t *testing.T) {
   321  			got := tt.generator(tt.machineTypesDisplay, tt.machineTypes, false, false)
   322  			validateSchema(t, Marshal(got), tt.file)
   323  
   324  			got = tt.generator(tt.machineTypesDisplay, tt.machineTypes, false, true)
   325  			validateSchema(t, Marshal(got), tt.updateFile)
   326  
   327  			got = tt.generator(tt.machineTypesDisplay, tt.machineTypes, true, false)
   328  			validateSchema(t, Marshal(got), tt.fileOIDC)
   329  
   330  			got = tt.generator(tt.machineTypesDisplay, tt.machineTypes, true, true)
   331  			validateSchema(t, Marshal(got), tt.updateFileOIDC)
   332  		})
   333  	}
   334  }
   335  
   336  func validateSchema(t *testing.T, got []byte, file string) {
   337  	var prettyWant bytes.Buffer
   338  	want := readJsonFile(t, file)
   339  	if len(want) > 0 {
   340  		err := json.Indent(&prettyWant, []byte(want), "", "  ")
   341  		if err != nil {
   342  			t.Error(err)
   343  			t.Fail()
   344  		}
   345  	}
   346  
   347  	var prettyGot bytes.Buffer
   348  	if len(got) > 0 {
   349  		err := json.Indent(&prettyGot, got, "", "  ")
   350  		if err != nil {
   351  			t.Error(err)
   352  			t.Fail()
   353  		}
   354  	}
   355  	if !assert.JSONEq(t, prettyGot.String(), prettyWant.String()) {
   356  		t.Errorf("%v Schema() = \n######### GOT ###########%v\n######### ENDGOT ########, want \n##### WANT #####%v\n##### ENDWANT #####", file, prettyGot.String(), prettyWant.String())
   357  	}
   358  }
   359  
   360  func readJsonFile(t *testing.T, file string) string {
   361  	t.Helper()
   362  
   363  	filename := path.Join("testdata", file)
   364  	yamlFile, err := ioutil.ReadFile(filename)
   365  	require.NoError(t, err)
   366  
   367  	return string(yamlFile)
   368  }