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

     1  package provisioning
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/kyma-project/kyma-environment-broker/internal/euaccess"
    10  
    11  	reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb"
    12  	"github.com/kyma-project/kyma-environment-broker/common/gardener"
    13  	"github.com/kyma-project/kyma-environment-broker/internal"
    14  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    15  	kebConfig "github.com/kyma-project/kyma-environment-broker/internal/config"
    16  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
    17  	"github.com/kyma-project/kyma-environment-broker/internal/logger"
    18  	"github.com/kyma-project/kyma-environment-broker/internal/process/input"
    19  	inputAutomock "github.com/kyma-project/kyma-environment-broker/internal/process/input/automock"
    20  	"github.com/kyma-project/kyma-environment-broker/internal/ptr"
    21  	"github.com/kyma-project/kyma-environment-broker/internal/reconciler"
    22  	"github.com/kyma-project/kyma-environment-broker/internal/runtime"
    23  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    24  	"github.com/pivotal-cf/brokerapi/v8/domain"
    25  	"github.com/sirupsen/logrus"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/mock"
    28  	"github.com/stretchr/testify/require"
    29  	coreV1 "k8s.io/api/core/v1"
    30  	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	k8sruntime "k8s.io/apimachinery/pkg/runtime"
    32  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    33  )
    34  
    35  const (
    36  	kymaVersion                   = "1.10"
    37  	k8sVersion                    = "1.16.9"
    38  	shootName                     = "c-1234567"
    39  	instanceID                    = "58f8c703-1756-48ab-9299-a847974d1fee"
    40  	operationID                   = "fd5cee4d-0eeb-40d0-a7a7-0708e5eba470"
    41  	globalAccountID               = "80ac17bd-33e8-4ffa-8d56-1d5367755723"
    42  	subAccountID                  = "12df5747-3efb-4df6-ad6f-4414bb661ce3"
    43  	provisionerOperationID        = "1a0ed09b-9bb9-4e6f-a88c-01955c5f1129"
    44  	runtimeID                     = "2498c8ee-803a-43c2-8194-6d6dd0354c30"
    45  	autoUpdateKubernetesVersion   = true
    46  	autoUpdateMachineImageVersion = true
    47  )
    48  
    49  var shootPurpose = "evaluation"
    50  
    51  func TestCheckClusterConfigurationStep_ClusterReady(t *testing.T) {
    52  	st := storage.NewMemoryStorage()
    53  	operation := fixture.FixProvisioningOperation("op-id", "inst-id")
    54  	operation.ClusterConfigurationVersion = 1
    55  	recClient := reconciler.NewFakeClient()
    56  	recClient.ApplyClusterConfig(reconcilerApi.Cluster{
    57  		RuntimeID:    operation.RuntimeID,
    58  		RuntimeInput: reconcilerApi.RuntimeInput{},
    59  		KymaConfig:   reconcilerApi.KymaConfig{},
    60  		Metadata:     reconcilerApi.Metadata{},
    61  		Kubeconfig:   "kubeconfig",
    62  	})
    63  	recClient.ChangeClusterState(operation.RuntimeID, 1, reconcilerApi.StatusReady)
    64  
    65  	step := NewCheckClusterConfigurationStep(st.Operations(), recClient, time.Minute)
    66  	st.Operations().InsertOperation(operation)
    67  
    68  	// when
    69  	_, d, err := step.Run(operation, logger.NewLogSpy().Logger)
    70  
    71  	// then
    72  	require.NoError(t, err)
    73  	assert.Zero(t, d)
    74  }
    75  
    76  func TestCheckClusterConfigurationStep_InProgress(t *testing.T) {
    77  	for _, state := range []reconcilerApi.Status{reconcilerApi.StatusReconciling, reconcilerApi.StatusReconcilePending} {
    78  		t.Run(fmt.Sprintf("shopuld repeat for state %s", state), func(t *testing.T) {
    79  			st := storage.NewMemoryStorage()
    80  			operation := fixture.FixProvisioningOperation("op-id", "inst-id")
    81  			operation.ClusterConfigurationVersion = 1
    82  			recClient := reconciler.NewFakeClient()
    83  			recClient.ApplyClusterConfig(reconcilerApi.Cluster{
    84  				RuntimeID:    operation.RuntimeID,
    85  				RuntimeInput: reconcilerApi.RuntimeInput{},
    86  				KymaConfig:   reconcilerApi.KymaConfig{},
    87  				Metadata:     reconcilerApi.Metadata{},
    88  				Kubeconfig:   "kubeconfig",
    89  			})
    90  			recClient.ChangeClusterState(operation.RuntimeID, 1, state)
    91  
    92  			step := NewCheckClusterConfigurationStep(st.Operations(), recClient, time.Minute)
    93  			st.Operations().InsertOperation(operation)
    94  
    95  			// when
    96  			_, d, err := step.Run(operation, logger.NewLogSpy().Logger)
    97  
    98  			// then
    99  			require.NoError(t, err)
   100  			assert.True(t, d > 0)
   101  		})
   102  	}
   103  }
   104  
   105  func TestCheckClusterConfigurationStep_ClusterFailed(t *testing.T) {
   106  	st := storage.NewMemoryStorage()
   107  	operation := fixture.FixProvisioningOperation("op-id", "inst-id")
   108  	operation.ClusterConfigurationVersion = 1
   109  	recClient := reconciler.NewFakeClient()
   110  	recClient.ApplyClusterConfig(reconcilerApi.Cluster{
   111  		RuntimeID:    operation.RuntimeID,
   112  		RuntimeInput: reconcilerApi.RuntimeInput{},
   113  		KymaConfig:   reconcilerApi.KymaConfig{},
   114  		Metadata:     reconcilerApi.Metadata{},
   115  		Kubeconfig:   "kubeconfig",
   116  	})
   117  	recClient.ChangeClusterState(operation.RuntimeID, 1, reconcilerApi.StatusError)
   118  
   119  	step := NewCheckClusterConfigurationStep(st.Operations(), recClient, time.Minute)
   120  	st.Operations().InsertOperation(operation)
   121  
   122  	// when
   123  	op, d, _ := step.Run(operation, logger.NewLogSpy().Logger)
   124  
   125  	// then
   126  	assert.Equal(t, domain.Failed, op.State)
   127  	assert.Zero(t, d)
   128  }
   129  
   130  func fixOperationCreateRuntime(t *testing.T, planID, region string) internal.Operation {
   131  	return fixOperationCreateRuntimeWithPlatformRegion(t, planID, region, "")
   132  }
   133  
   134  func fixOperationCreateRuntimeWithPlatformRegion(t *testing.T, planID, region, platformRegion string) internal.Operation {
   135  	provisioningOperation := fixture.FixProvisioningOperation(operationID, instanceID)
   136  	provisioningOperation.State = domain.InProgress
   137  	provisioningOperation.InputCreator = fixInputCreator(t)
   138  	provisioningOperation.InstanceDetails.ShootName = shootName
   139  	provisioningOperation.InstanceDetails.ShootDNSProviders = gardener.DNSProvidersData{
   140  		Providers: []gardener.DNSProviderData{
   141  			{
   142  				DomainsInclude: []string{"devtest.kyma.ondemand.com"},
   143  				Primary:        true,
   144  				SecretName:     "aws_dns_domain_secrets_test_intest",
   145  				Type:           "route53_type_test",
   146  			},
   147  		},
   148  	}
   149  	provisioningOperation.InstanceDetails.EuAccess = euaccess.IsEURestrictedAccess(platformRegion)
   150  	provisioningOperation.ProvisioningParameters = FixProvisioningParameters(planID, region, platformRegion)
   151  	provisioningOperation.RuntimeID = ""
   152  
   153  	return provisioningOperation
   154  }
   155  
   156  func fixInstance() internal.Instance {
   157  	instance := fixture.FixInstance(instanceID)
   158  	instance.GlobalAccountID = globalAccountID
   159  
   160  	return instance
   161  }
   162  
   163  func FixProvisioningParameters(planID, region, platformRegion string) internal.ProvisioningParameters {
   164  	return fixProvisioningParametersWithPlanID(planID, region, platformRegion)
   165  }
   166  
   167  func fixProvisioningParametersWithPlanID(planID, region string, platformRegion string) internal.ProvisioningParameters {
   168  	return internal.ProvisioningParameters{
   169  		PlanID:    planID,
   170  		ServiceID: "",
   171  		ErsContext: internal.ERSContext{
   172  			GlobalAccountID: globalAccountID,
   173  			SubAccountID:    subAccountID,
   174  		},
   175  		PlatformRegion: platformRegion,
   176  		Parameters: internal.ProvisioningParametersDTO{
   177  			Region: ptr.String(region),
   178  			Name:   "dummy",
   179  			Zones:  []string{"europe-west3-b", "europe-west3-c"},
   180  		},
   181  	}
   182  }
   183  
   184  func fixInputCreator(t *testing.T) internal.ProvisionerInputCreator {
   185  	optComponentsSvc := &inputAutomock.OptionalComponentService{}
   186  
   187  	optComponentsSvc.On("ComputeComponentsToDisable", []string{}).Return([]string{})
   188  	optComponentsSvc.On("ExecuteDisablers", internal.ComponentConfigurationInputList{
   189  		{
   190  			Component:     "to-remove-component",
   191  			Namespace:     "kyma-system",
   192  			Configuration: nil,
   193  		},
   194  		{
   195  			Component:     "keb",
   196  			Namespace:     "kyma-system",
   197  			Configuration: nil,
   198  		},
   199  	}).Return(internal.ComponentConfigurationInputList{
   200  		{
   201  			Component:     "keb",
   202  			Namespace:     "kyma-system",
   203  			Configuration: nil,
   204  		},
   205  	}, nil)
   206  
   207  	kymaComponentList := []internal.KymaComponent{
   208  		{
   209  			Name:      "to-remove-component",
   210  			Namespace: "kyma-system",
   211  		},
   212  		{
   213  			Name:      "keb",
   214  			Namespace: "kyma-system",
   215  		},
   216  	}
   217  	componentsProvider := &inputAutomock.ComponentListProvider{}
   218  	componentsProvider.On("AllComponents", mock.AnythingOfType("internal.RuntimeVersionData"), mock.AnythingOfType("*internal.ConfigForPlan")).Return(kymaComponentList, nil)
   219  	defer componentsProvider.AssertExpectations(t)
   220  
   221  	cli := fake.NewClientBuilder().WithRuntimeObjects(fixConfigMap(kymaVersion)).Build()
   222  	configProvider := kebConfig.NewConfigProvider(
   223  		kebConfig.NewConfigMapReader(context.TODO(), cli, logrus.New(), kymaVersion),
   224  		kebConfig.NewConfigMapKeysValidator(),
   225  		kebConfig.NewConfigMapConverter())
   226  	ibf, err := input.NewInputBuilderFactory(optComponentsSvc, runtime.NewDisabledComponentsProvider(), componentsProvider,
   227  		configProvider, input.Config{
   228  			KubernetesVersion:             k8sVersion,
   229  			DefaultGardenerShootPurpose:   shootPurpose,
   230  			AutoUpdateKubernetesVersion:   autoUpdateKubernetesVersion,
   231  			AutoUpdateMachineImageVersion: autoUpdateMachineImageVersion,
   232  			MultiZoneCluster:              true,
   233  			ControlPlaneFailureTolerance:  "zone",
   234  		}, kymaVersion, fixTrialRegionMapping(), fixFreemiumProviders(), fixture.FixOIDCConfigDTO())
   235  	assert.NoError(t, err)
   236  
   237  	pp := internal.ProvisioningParameters{
   238  		PlanID: broker.GCPPlanID,
   239  		Parameters: internal.ProvisioningParametersDTO{
   240  			KymaVersion: "",
   241  		},
   242  	}
   243  	version := internal.RuntimeVersionData{Version: kymaVersion, Origin: internal.Parameters}
   244  	creator, err := ibf.CreateProvisionInput(pp, version)
   245  	if err != nil {
   246  		t.Errorf("cannot create input creator for %q plan", broker.GCPPlanID)
   247  	}
   248  
   249  	return creator
   250  }
   251  
   252  func fixTrialRegionMapping() map[string]string {
   253  	return map[string]string{}
   254  }
   255  
   256  func fixFreemiumProviders() []string {
   257  	return []string{"azure", "aws"}
   258  }
   259  
   260  func fixConfigMap(defaultKymaVersion string) k8sruntime.Object {
   261  	kebCfg := &coreV1.ConfigMap{
   262  		ObjectMeta: metaV1.ObjectMeta{
   263  			Name:      "keb-config",
   264  			Namespace: "kcp-system",
   265  			Labels: map[string]string{
   266  				"keb-config": "true",
   267  				fmt.Sprintf("runtime-version-%s", defaultKymaVersion): "true",
   268  			},
   269  		},
   270  		Data: map[string]string{
   271  			"default": `additional-components:
   272    - name: "additional-component1"
   273      namespace: "kyma-system"`,
   274  		},
   275  	}
   276  
   277  	return kebCfg
   278  }