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

     1  package broker_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
     8  	"github.com/pivotal-cf/brokerapi/v8/domain"
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestServices_Services(t *testing.T) {
    15  
    16  	t.Run("should get service and plans without OIDC", func(t *testing.T) {
    17  		// given
    18  		var (
    19  			name       = "testServiceName"
    20  			supportURL = "example.com/support"
    21  		)
    22  
    23  		cfg := broker.Config{
    24  			EnablePlans: []string{"gcp", "azure", "openstack", "aws", "free"},
    25  		}
    26  		servicesConfig := map[string]broker.Service{
    27  			broker.KymaServiceName: {
    28  				Metadata: broker.ServiceMetadata{
    29  					DisplayName: name,
    30  					SupportUrl:  supportURL,
    31  				},
    32  			},
    33  		}
    34  		servicesEndpoint := broker.NewServices(cfg, servicesConfig, logrus.StandardLogger())
    35  
    36  		// when
    37  		services, err := servicesEndpoint.Services(context.TODO())
    38  
    39  		// then
    40  		require.NoError(t, err)
    41  		assert.Len(t, services, 1)
    42  		assert.Len(t, services[0].Plans, 5)
    43  
    44  		assert.Equal(t, name, services[0].Metadata.DisplayName)
    45  		assert.Equal(t, supportURL, services[0].Metadata.SupportUrl)
    46  	})
    47  	t.Run("should get service and plans with OIDC & administrators", func(t *testing.T) {
    48  		// given
    49  		var (
    50  			name       = "testServiceName"
    51  			supportURL = "example.com/support"
    52  		)
    53  
    54  		cfg := broker.Config{
    55  			EnablePlans:                     []string{"gcp", "azure", "openstack", "aws", "free"},
    56  			IncludeAdditionalParamsInSchema: true,
    57  		}
    58  		servicesConfig := map[string]broker.Service{
    59  			broker.KymaServiceName: {
    60  				Metadata: broker.ServiceMetadata{
    61  					DisplayName: name,
    62  					SupportUrl:  supportURL,
    63  				},
    64  			},
    65  		}
    66  		servicesEndpoint := broker.NewServices(cfg, servicesConfig, logrus.StandardLogger())
    67  
    68  		// when
    69  		services, err := servicesEndpoint.Services(context.TODO())
    70  
    71  		// then
    72  		require.NoError(t, err)
    73  		assert.Len(t, services, 1)
    74  		assert.Len(t, services[0].Plans, 5)
    75  
    76  		assert.Equal(t, name, services[0].Metadata.DisplayName)
    77  		assert.Equal(t, supportURL, services[0].Metadata.SupportUrl)
    78  
    79  		assertPlansContainPropertyInSchemas(t, services[0], "oidc")
    80  		assertPlansContainPropertyInSchemas(t, services[0], "administrators")
    81  	})
    82  
    83  	t.Run("should return sync control orders", func(t *testing.T) {
    84  		// given
    85  		var (
    86  			name       = "testServiceName"
    87  			supportURL = "example.com/support"
    88  		)
    89  
    90  		cfg := broker.Config{
    91  			EnablePlans:                     []string{"gcp", "azure", "openstack", "aws", "free"},
    92  			IncludeAdditionalParamsInSchema: true,
    93  		}
    94  		servicesConfig := map[string]broker.Service{
    95  			broker.KymaServiceName: {
    96  				Metadata: broker.ServiceMetadata{
    97  					DisplayName: name,
    98  					SupportUrl:  supportURL,
    99  				},
   100  			},
   101  		}
   102  		servicesEndpoint := broker.NewServices(cfg, servicesConfig, logrus.StandardLogger())
   103  
   104  		// when
   105  		services, err := servicesEndpoint.Services(context.TODO())
   106  
   107  		// then
   108  		require.NoError(t, err)
   109  		assert.Len(t, services, 1)
   110  		assert.Len(t, services[0].Plans, 5)
   111  
   112  		assert.Equal(t, name, services[0].Metadata.DisplayName)
   113  		assert.Equal(t, supportURL, services[0].Metadata.SupportUrl)
   114  
   115  		assertPlansContainPropertyInSchemas(t, services[0], "oidc")
   116  		assertPlansContainPropertyInSchemas(t, services[0], "administrators")
   117  	})
   118  
   119  	t.Run("should contain the property 'required' with values [name region] when ExposeSchemaWithRegionRequired is true and RegionParameterIsRequired is false", func(t *testing.T) {
   120  		// given
   121  		var (
   122  			name       = "testServiceName"
   123  			supportURL = "example.com/support"
   124  		)
   125  
   126  		cfg := broker.Config{
   127  			EnablePlans:                     []string{"gcp", "azure", "openstack", "aws", "free"},
   128  			IncludeAdditionalParamsInSchema: true,
   129  			RegionParameterIsRequired:       false,
   130  			ExposeSchemaWithRegionRequired:  true,
   131  		}
   132  		servicesConfig := map[string]broker.Service{
   133  			broker.KymaServiceName: {
   134  				Metadata: broker.ServiceMetadata{
   135  					DisplayName: name,
   136  					SupportUrl:  supportURL,
   137  				},
   138  			},
   139  		}
   140  		servicesEndpoint := broker.NewServices(cfg, servicesConfig, logrus.StandardLogger())
   141  
   142  		// when
   143  		services, err := servicesEndpoint.Services(context.TODO())
   144  
   145  		// then
   146  		require.NoError(t, err)
   147  		assert.Len(t, services, 1)
   148  		assert.Len(t, services[0].Plans, 5)
   149  
   150  		assert.Equal(t, name, services[0].Metadata.DisplayName)
   151  		assert.Equal(t, supportURL, services[0].Metadata.SupportUrl)
   152  
   153  		for _, plan := range services[0].Plans {
   154  			assertPlanContainsPropertyValuesInCreateSchema(t, plan, "required", []string{"name", "region"})
   155  		}
   156  
   157  	})
   158  
   159  	t.Run("should contain the property 'required' with values [name region] when ExposeSchemaWithRegionRequired is true and RegionParameterIsRequired is true", func(t *testing.T) {
   160  		// given
   161  		var (
   162  			name       = "testServiceName"
   163  			supportURL = "example.com/support"
   164  		)
   165  
   166  		cfg := broker.Config{
   167  			EnablePlans:                     []string{"gcp", "azure", "openstack", "aws", "free"},
   168  			IncludeAdditionalParamsInSchema: true,
   169  			RegionParameterIsRequired:       true,
   170  			ExposeSchemaWithRegionRequired:  true,
   171  		}
   172  		servicesConfig := map[string]broker.Service{
   173  			broker.KymaServiceName: {
   174  				Metadata: broker.ServiceMetadata{
   175  					DisplayName: name,
   176  					SupportUrl:  supportURL,
   177  				},
   178  			},
   179  		}
   180  		servicesEndpoint := broker.NewServices(cfg, servicesConfig, logrus.StandardLogger())
   181  
   182  		// when
   183  		services, err := servicesEndpoint.Services(context.TODO())
   184  
   185  		// then
   186  		require.NoError(t, err)
   187  		assert.Len(t, services, 1)
   188  		assert.Len(t, services[0].Plans, 5)
   189  
   190  		assert.Equal(t, name, services[0].Metadata.DisplayName)
   191  		assert.Equal(t, supportURL, services[0].Metadata.SupportUrl)
   192  
   193  		for _, plan := range services[0].Plans {
   194  			assertPlanContainsPropertyValuesInCreateSchema(t, plan, "required", []string{"name", "region"})
   195  		}
   196  
   197  	})
   198  
   199  	t.Run("should contain the property 'required' with values [name region] when ExposeSchemaWithRegionRequired is false and RegionParameterIsRequired is true", func(t *testing.T) {
   200  		// given
   201  		var (
   202  			name       = "testServiceName"
   203  			supportURL = "example.com/support"
   204  		)
   205  
   206  		cfg := broker.Config{
   207  			EnablePlans:                     []string{"gcp", "azure", "openstack", "aws", "free"},
   208  			IncludeAdditionalParamsInSchema: true,
   209  			RegionParameterIsRequired:       true,
   210  			ExposeSchemaWithRegionRequired:  false,
   211  		}
   212  		servicesConfig := map[string]broker.Service{
   213  			broker.KymaServiceName: {
   214  				Metadata: broker.ServiceMetadata{
   215  					DisplayName: name,
   216  					SupportUrl:  supportURL,
   217  				},
   218  			},
   219  		}
   220  		servicesEndpoint := broker.NewServices(cfg, servicesConfig, logrus.StandardLogger())
   221  
   222  		// when
   223  		services, err := servicesEndpoint.Services(context.TODO())
   224  
   225  		// then
   226  		require.NoError(t, err)
   227  		assert.Len(t, services, 1)
   228  		assert.Len(t, services[0].Plans, 5)
   229  
   230  		assert.Equal(t, name, services[0].Metadata.DisplayName)
   231  		assert.Equal(t, supportURL, services[0].Metadata.SupportUrl)
   232  
   233  		for _, plan := range services[0].Plans {
   234  			assertPlanContainsPropertyValuesInCreateSchema(t, plan, "required", []string{"name", "region"})
   235  		}
   236  
   237  	})
   238  
   239  	t.Run("should contain the property 'required' with values [name] when ExposeSchemaWithRegionRequired is false and RegionParameterIsRequired is false", func(t *testing.T) {
   240  		// given
   241  		var (
   242  			name       = "testServiceName"
   243  			supportURL = "example.com/support"
   244  		)
   245  
   246  		cfg := broker.Config{
   247  			EnablePlans:                     []string{"gcp", "azure", "openstack", "aws", "free"},
   248  			IncludeAdditionalParamsInSchema: true,
   249  			RegionParameterIsRequired:       false,
   250  			ExposeSchemaWithRegionRequired:  false,
   251  		}
   252  		servicesConfig := map[string]broker.Service{
   253  			broker.KymaServiceName: {
   254  				Metadata: broker.ServiceMetadata{
   255  					DisplayName: name,
   256  					SupportUrl:  supportURL,
   257  				},
   258  			},
   259  		}
   260  		servicesEndpoint := broker.NewServices(cfg, servicesConfig, logrus.StandardLogger())
   261  
   262  		// when
   263  		services, err := servicesEndpoint.Services(context.TODO())
   264  
   265  		// then
   266  		require.NoError(t, err)
   267  		assert.Len(t, services, 1)
   268  		assert.Len(t, services[0].Plans, 5)
   269  
   270  		assert.Equal(t, name, services[0].Metadata.DisplayName)
   271  		assert.Equal(t, supportURL, services[0].Metadata.SupportUrl)
   272  
   273  		for _, plan := range services[0].Plans {
   274  			assertPlanContainsPropertyValuesInCreateSchema(t, plan, "required", []string{"name"})
   275  		}
   276  
   277  	})
   278  }
   279  
   280  func assertPlansContainPropertyInSchemas(t *testing.T, service domain.Service, property string) {
   281  	for _, plan := range service.Plans {
   282  		assertPlanContainsPropertyInCreateSchema(t, plan, property)
   283  		assertPlanContainsPropertyInUpdateSchema(t, plan, property)
   284  	}
   285  }
   286  
   287  func assertPlanContainsPropertyInCreateSchema(t *testing.T, plan domain.ServicePlan, property string) {
   288  	properties := plan.Schemas.Instance.Create.Parameters[broker.PropertiesKey]
   289  	propertiesMap := properties.(map[string]interface{})
   290  	if _, exists := propertiesMap[property]; !exists {
   291  		t.Errorf("plan %s does not contain %s property in Create schema", plan.Name, property)
   292  	}
   293  }
   294  
   295  func assertPlanContainsPropertyInUpdateSchema(t *testing.T, plan domain.ServicePlan, property string) {
   296  	properties := plan.Schemas.Instance.Update.Parameters[broker.PropertiesKey]
   297  	propertiesMap := properties.(map[string]interface{})
   298  	if _, exists := propertiesMap[property]; !exists {
   299  		t.Errorf("plan %s does not contain %s property in Update schema", plan.Name, property)
   300  	}
   301  }
   302  
   303  func assertPlanContainsPropertyValuesInCreateSchema(t *testing.T, plan domain.ServicePlan, property string, wantedPropertyValues []string) {
   304  	planPropertyValues := plan.Schemas.Instance.Create.Parameters[property]
   305  	var wantedPropVal string
   306  
   307  	if len(wantedPropertyValues) < len(planPropertyValues.([]interface{})) {
   308  		t.Errorf("plan %s has more values (%s) for property '%s' than expected (%s) in Create schema", plan.Name, planPropertyValues.([]interface{}), property, wantedPropertyValues)
   309  	}
   310  
   311  	for _, wantedPropVal = range wantedPropertyValues {
   312  		assert.Contains(t, planPropertyValues, wantedPropVal)
   313  	}
   314  }