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

     1  package provisioning
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/edp"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/logger"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  const (
    17  	edpName        = "cd4b333c-97fb-4894-bb20-7874f5833e8d"
    18  	edpEnvironment = "test"
    19  	edpRegion      = "cf-eu10"
    20  	edpPlan        = "standard"
    21  )
    22  
    23  func TestEDPRegistration_Run(t *testing.T) {
    24  	// given
    25  	memoryStorage := storage.NewMemoryStorage()
    26  	client := edp.NewFakeClient()
    27  
    28  	step := NewEDPRegistrationStep(memoryStorage.Operations(), client, edp.Config{
    29  		Environment: edpEnvironment,
    30  		Required:    true,
    31  	})
    32  	operation := internal.Operation{
    33  		ProvisioningParameters: internal.ProvisioningParameters{
    34  			PlanID:         broker.AzurePlanID,
    35  			PlatformRegion: edpRegion,
    36  			ErsContext: internal.ERSContext{
    37  				SubAccountID: edpName,
    38  			},
    39  		},
    40  	}
    41  	memoryStorage.Operations().InsertOperation(operation)
    42  
    43  	// when
    44  	_, repeat, err := step.Run(operation, logger.NewLogDummy())
    45  
    46  	// then
    47  	assert.Equal(t, 0*time.Second, repeat)
    48  	assert.NoError(t, err)
    49  
    50  	dataTenant, dataTenantExists := client.GetDataTenantItem(edpName, edpEnvironment)
    51  	assert.True(t, dataTenantExists)
    52  	assert.Equal(t, edp.DataTenantItem{
    53  		Name:        edpName,
    54  		Environment: edpEnvironment,
    55  	}, dataTenant)
    56  
    57  	for key, value := range map[string]string{
    58  		edp.MaasConsumerEnvironmentKey: step.selectEnvironmentKey(edpRegion, logger.NewLogDummy()),
    59  		edp.MaasConsumerRegionKey:      edpRegion,
    60  		edp.MaasConsumerSubAccountKey:  edpName,
    61  		edp.MaasConsumerServicePlan:    edpPlan,
    62  	} {
    63  		metadataTenant, metadataTenantExists := client.GetMetadataItem(edpName, edpEnvironment, key)
    64  		assert.True(t, metadataTenantExists)
    65  		assert.Equal(t, edp.MetadataItem{
    66  			DataTenant: edp.DataTenantItem{
    67  				Name:        edpName,
    68  				Environment: edpEnvironment,
    69  			},
    70  			Key:   key,
    71  			Value: value,
    72  		}, metadataTenant)
    73  	}
    74  
    75  }
    76  
    77  func TestEDPRegistrationStep_selectEnvironmentKey(t *testing.T) {
    78  	for name, tc := range map[string]struct {
    79  		region   string
    80  		expected string
    81  	}{
    82  		"kubernetes region": {
    83  			region:   "k8s-as34",
    84  			expected: "KUBERNETES",
    85  		},
    86  		"cf region": {
    87  			region:   "cf-eu10",
    88  			expected: "CF",
    89  		},
    90  		"neo region": {
    91  			region:   "neo-us13",
    92  			expected: "NEO",
    93  		},
    94  		"default region": {
    95  			region:   "undefined",
    96  			expected: "CF",
    97  		},
    98  		"empty region": {
    99  			region:   "",
   100  			expected: "CF",
   101  		},
   102  	} {
   103  		t.Run(name, func(t *testing.T) {
   104  			// given
   105  			step := NewEDPRegistrationStep(nil, nil, edp.Config{})
   106  
   107  			// when
   108  			envKey := step.selectEnvironmentKey(tc.region, logger.NewLogDummy())
   109  
   110  			// then
   111  			assert.Equal(t, tc.expected, envKey)
   112  		})
   113  	}
   114  }
   115  
   116  func TestEDPRegistrationStep_selectServicePlan(t *testing.T) {
   117  	for name, tc := range map[string]struct {
   118  		planID   string
   119  		expected string
   120  	}{
   121  		"GCP": {
   122  			planID:   broker.GCPPlanID,
   123  			expected: "standard",
   124  		},
   125  		"AWS": {
   126  			planID:   broker.AWSPlanID,
   127  			expected: "standard",
   128  		},
   129  		"Azure": {
   130  			planID:   broker.AzurePlanID,
   131  			expected: "standard",
   132  		},
   133  		"Azure Lite": {
   134  			planID:   broker.AzureLitePlanID,
   135  			expected: "tdd",
   136  		},
   137  		"Trial": {
   138  			planID:   broker.TrialPlanID,
   139  			expected: "standard",
   140  		},
   141  		"OpenStack": {
   142  			planID:   broker.OpenStackPlanID,
   143  			expected: "standard",
   144  		},
   145  		"Freemium": {
   146  			planID:   broker.FreemiumPlanID,
   147  			expected: "free",
   148  		},
   149  	} {
   150  		t.Run(name, func(t *testing.T) {
   151  			// given
   152  			step := NewEDPRegistrationStep(nil, nil, edp.Config{})
   153  
   154  			// when
   155  			envKey := step.selectServicePlan(tc.planID)
   156  
   157  			// then
   158  			assert.Equal(t, tc.expected, envKey)
   159  		})
   160  	}
   161  }