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

     1  package upgrade_cluster
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/process/input"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/process/input/automock"
    12  	provisionerAutomock "github.com/kyma-project/kyma-environment-broker/internal/provisioner/automock"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/ptr"
    14  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    15  	"github.com/sirupsen/logrus"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/mock"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  const (
    22  	fixKymaVersion                   = "1.19.0"
    23  	fixKubernetesVersion             = "1.17.16"
    24  	fixMachineImage                  = "gardenlinux"
    25  	fixMachineImageVersion           = "184.0.0"
    26  	fixAutoUpdateKubernetesVersion   = true
    27  	fixAutoUpdateMachineImageVersion = true
    28  )
    29  
    30  func TestUpgradeClusterStep_Run(t *testing.T) {
    31  	// given
    32  	expectedOIDC := fixture.FixOIDCConfigDTO()
    33  	log := logrus.New()
    34  	memoryStorage := storage.NewMemoryStorage()
    35  
    36  	operation := fixUpgradeClusterOperationWithInputCreator(t)
    37  	err := memoryStorage.Operations().InsertUpgradeClusterOperation(operation)
    38  	assert.NoError(t, err)
    39  
    40  	provisioningOperation := fixProvisioningOperation()
    41  	err = memoryStorage.Operations().InsertOperation(provisioningOperation)
    42  	assert.NoError(t, err)
    43  
    44  	runtimeState := fixture.FixRuntimeState("runtimestate-1", fixRuntimeID, provisioningOperation.ID)
    45  	runtimeState.ClusterConfig.OidcConfig = &gqlschema.OIDCConfigInput{
    46  		ClientID:       expectedOIDC.ClientID,
    47  		GroupsClaim:    expectedOIDC.GroupsClaim,
    48  		IssuerURL:      expectedOIDC.IssuerURL,
    49  		SigningAlgs:    expectedOIDC.SigningAlgs,
    50  		UsernameClaim:  expectedOIDC.UsernameClaim,
    51  		UsernamePrefix: expectedOIDC.UsernamePrefix,
    52  	}
    53  	memoryStorage.RuntimeStates().Insert(runtimeState)
    54  	assert.NoError(t, err)
    55  
    56  	// as autoscaler values are not nil in provisioningParameters, the provider values are not used
    57  	provider := fixGetHyperscalerProviderForPlanID(operation.ProvisioningParameters.PlanID)
    58  	assert.NotNil(t, provider)
    59  
    60  	provisionerClient := &provisionerAutomock.Client{}
    61  	disabled := false
    62  	provisionerClient.On("UpgradeShoot", fixGlobalAccountID, fixRuntimeID, gqlschema.UpgradeShootInput{
    63  		GardenerConfig: &gqlschema.GardenerUpgradeInput{
    64  			KubernetesVersion:                   ptr.String(fixKubernetesVersion),
    65  			MachineImage:                        ptr.String(fixMachineImage),
    66  			MachineImageVersion:                 ptr.String(fixMachineImageVersion),
    67  			MaxSurge:                            operation.ProvisioningParameters.Parameters.MaxSurge,
    68  			MaxUnavailable:                      operation.ProvisioningParameters.Parameters.MaxUnavailable,
    69  			EnableKubernetesVersionAutoUpdate:   ptr.Bool(fixAutoUpdateKubernetesVersion),
    70  			EnableMachineImageVersionAutoUpdate: ptr.Bool(fixAutoUpdateMachineImageVersion),
    71  			ShootNetworkingFilterDisabled:       &disabled,
    72  			OidcConfig: &gqlschema.OIDCConfigInput{
    73  				ClientID:       expectedOIDC.ClientID,
    74  				GroupsClaim:    expectedOIDC.GroupsClaim,
    75  				IssuerURL:      expectedOIDC.IssuerURL,
    76  				SigningAlgs:    expectedOIDC.SigningAlgs,
    77  				UsernameClaim:  expectedOIDC.UsernameClaim,
    78  				UsernamePrefix: expectedOIDC.UsernamePrefix,
    79  			},
    80  		},
    81  		Administrators: []string{provisioningOperation.ProvisioningParameters.ErsContext.UserID},
    82  	}).Return(gqlschema.OperationStatus{
    83  		ID:        StringPtr(fixProvisionerOperationID),
    84  		Operation: "",
    85  		State:     "",
    86  		Message:   nil,
    87  		RuntimeID: StringPtr(fixRuntimeID),
    88  	}, nil)
    89  	provisionerClient.On("RuntimeOperationStatus", fixGlobalAccountID, fixProvisionerOperationID).Return(gqlschema.OperationStatus{
    90  		ID:        ptr.String(fixProvisionerOperationID),
    91  		Operation: "",
    92  		State:     "",
    93  		Message:   nil,
    94  		RuntimeID: ptr.String(fixRuntimeID),
    95  	}, nil)
    96  
    97  	step := NewUpgradeClusterStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), provisionerClient, nil)
    98  
    99  	// when
   100  
   101  	operation, repeat, err := step.Run(operation, log.WithFields(logrus.Fields{"step": "TEST"}))
   102  
   103  	// then
   104  	assert.NoError(t, err)
   105  	assert.Equal(t, 5*time.Second, repeat)
   106  	assert.Equal(t, fixProvisionerOperationID, operation.ProvisionerOperationID)
   107  }
   108  
   109  func fixUpgradeClusterOperationWithInputCreator(t *testing.T) internal.UpgradeClusterOperation {
   110  	upgradeOperation := fixture.FixUpgradeClusterOperation(fixUpgradeOperationID, fixInstanceID)
   111  	upgradeOperation.Description = ""
   112  	upgradeOperation.ProvisioningParameters = fixProvisioningParameters()
   113  	upgradeOperation.InstanceDetails.RuntimeID = fixRuntimeID
   114  	upgradeOperation.RuntimeOperation.RuntimeID = fixRuntimeID
   115  	upgradeOperation.RuntimeOperation.GlobalAccountID = fixGlobalAccountID
   116  	upgradeOperation.RuntimeOperation.SubAccountID = fixSubAccountID
   117  	upgradeOperation.InputCreator = fixInputCreator(t)
   118  
   119  	return upgradeOperation
   120  }
   121  
   122  func fixInputCreator(t *testing.T) internal.ProvisionerInputCreator {
   123  	componentsProvider := &automock.ComponentListProvider{}
   124  	defer componentsProvider.AssertExpectations(t)
   125  
   126  	configProvider := &automock.ConfigurationProvider{}
   127  	configProvider.On("ProvideForGivenVersionAndPlan",
   128  		mock.AnythingOfType("string"),
   129  		mock.AnythingOfType("string")).
   130  		Return(&internal.ConfigForPlan{
   131  			AdditionalComponents: []internal.KymaComponent{
   132  				{
   133  					Name:      "kyma-component1",
   134  					Namespace: "kyma-system",
   135  				},
   136  			},
   137  		}, nil)
   138  
   139  	ibf, err := input.NewInputBuilderFactory(nil, nil, componentsProvider,
   140  		configProvider, input.Config{
   141  			KubernetesVersion:             fixKubernetesVersion,
   142  			MachineImage:                  fixMachineImage,
   143  			MachineImageVersion:           fixMachineImageVersion,
   144  			TrialNodesNumber:              1,
   145  			AutoUpdateKubernetesVersion:   fixAutoUpdateKubernetesVersion,
   146  			AutoUpdateMachineImageVersion: fixAutoUpdateMachineImageVersion,
   147  		}, fixKymaVersion, nil, nil, fixture.FixOIDCConfigDTO())
   148  	require.NoError(t, err, "Input factory creation error")
   149  
   150  	ver := internal.RuntimeVersionData{
   151  		Version: fixKymaVersion,
   152  		Origin:  internal.Defaults,
   153  	}
   154  
   155  	creator, err := ibf.CreateUpgradeShootInput(fixProvisioningParameters(), ver)
   156  	require.NoError(t, err, "Input creator creation error")
   157  
   158  	return creator
   159  }