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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/pivotal-cf/brokerapi/v8/domain"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestClusterUpgrade_UpgradeAfterUpdateWithNetworkPolicy(t *testing.T) {
    14  	// given
    15  	suite := NewBrokerSuiteTest(t)
    16  	mockBTPOperatorClusterID()
    17  	defer suite.TearDown()
    18  	id := "InstanceID-UpgradeAfterUpdate"
    19  
    20  	// provision Kyma 2.0
    21  	resp := suite.CallAPI("PUT", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true", id), `
    22  {
    23  	"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
    24  	"plan_id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
    25  	"context": {
    26  		"sm_operator_credentials": {
    27  			"clientid": "testClientID",
    28  			"clientsecret": "testClientSecret",
    29  			"sm_url": "https://service-manager.kyma.com",
    30  			"url": "https://test.auth.com",
    31  			"xsappname": "testXsappname"
    32  		},
    33  		"globalaccount_id": "g-account-id",
    34  		"subaccount_id": "sub-id",
    35  		"user_id": "john.smith@email.com"
    36  	},
    37  	"parameters": {
    38  		"name": "testing-cluster",
    39  		"region": "eastus"
    40  	}
    41  }`)
    42  	opID := suite.DecodeOperationID(resp)
    43  	suite.processProvisioningAndReconcilingByOperationID(opID)
    44  	suite.WaitForOperationState(opID, domain.Succeeded)
    45  
    46  	// provide license_type
    47  	resp = suite.CallAPI("PATCH", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true", id), `
    48  {
    49  	"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
    50  	"context": {
    51  		"license_type": "CUSTOMER"
    52  	}
    53  }`)
    54  
    55  	// finish the update operation
    56  	assert.Equal(t, http.StatusAccepted, resp.StatusCode)
    57  	updateOperationID := suite.DecodeOperationID(resp)
    58  	suite.FinishUpdatingOperationByProvisioner(updateOperationID)
    59  	suite.WaitForOperationState(updateOperationID, domain.Succeeded)
    60  	i, err := suite.db.Instances().GetByID(id)
    61  	rsu1, err := suite.db.RuntimeStates().GetLatestWithReconcilerInputByRuntimeID(i.RuntimeID)
    62  
    63  	// ensure license type is persisted and network filter enabled
    64  	instance2 := suite.GetInstance(id)
    65  	enabled := true
    66  	suite.AssertDisabledNetworkFilterRuntimeState(i.RuntimeID, updateOperationID, &enabled)
    67  	assert.Equal(suite.t, "CUSTOMER", *instance2.Parameters.ErsContext.LicenseType)
    68  
    69  	// run upgrade
    70  	orchestrationResp := suite.CallAPI("POST", "upgrade/cluster", `
    71  {
    72  	"strategy": {
    73  		"type": "parallel",
    74  		"schedule": "immediate",
    75  		"parallel": {
    76  			"workers": 1
    77  		}
    78  	},
    79  	"dryRun": false,
    80  	"targets": {
    81  		"include": [
    82  			{
    83  				"subAccount": "sub-id"
    84  			}
    85  		]
    86  	},
    87  	"kubernetes": {
    88  		"kubernetesVersion": "1.25.0"
    89  	}
    90  }`)
    91  	oID := suite.DecodeOrchestrationID(orchestrationResp)
    92  	upgradeClusterOperationID, err := suite.DecodeLastUpgradeClusterOperationIDFromOrchestration(oID)
    93  	require.NoError(t, err)
    94  
    95  	suite.WaitForOperationState(upgradeClusterOperationID, domain.InProgress)
    96  	suite.FinishUpgradeClusterOperationByProvisioner(upgradeClusterOperationID)
    97  	suite.WaitForOperationState(upgradeClusterOperationID, domain.Succeeded)
    98  
    99  	_, err = suite.db.Operations().GetUpgradeClusterOperationByID(upgradeClusterOperationID)
   100  	require.NoError(t, err)
   101  
   102  	// ensure component list after upgrade didn't get changed
   103  	i, err = suite.db.Instances().GetByID(id)
   104  	assert.NoError(t, err, "getting instance after upgrade")
   105  	rsu2, err := suite.db.RuntimeStates().GetLatestWithReconcilerInputByRuntimeID(i.RuntimeID)
   106  	assert.NoError(t, err, "getting runtime after upgrade")
   107  	assert.Equal(t, rsu1.ClusterConfig.Name, rsu2.ClusterConfig.Name)
   108  
   109  	// ensure license type still persisted and network filter still disabled after upgrade
   110  	disabled := true
   111  	suite.AssertDisabledNetworkFilterRuntimeState(i.RuntimeID, upgradeClusterOperationID, &disabled)
   112  	assert.Equal(suite.t, "CUSTOMER", *instance2.Parameters.ErsContext.LicenseType)
   113  
   114  	resp = suite.CallAPI("PATCH", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true", id), `
   115  {
   116  	"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
   117  	"plan_id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
   118  	"context": {
   119  		"globalaccount_id": "g-account-id",
   120  		"user_id": "jack.anvil@email.com"
   121  	},
   122  	"parameters": {
   123  		"autoScalerMin":15,
   124  		"autoScalerMax":25,
   125  		"maxSurge":13,
   126  		"maxUnavailable":6
   127  	}
   128  }`)
   129  	assert.Equal(t, http.StatusAccepted, resp.StatusCode)
   130  	upgradeOperationID := suite.DecodeOperationID(resp)
   131  	suite.FinishUpdatingOperationByProvisioner(upgradeOperationID)
   132  
   133  	resp = suite.CallAPI("PATCH", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true", id), `
   134  {
   135  	"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
   136  	"plan_id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
   137  	"context": {
   138  		"globalaccount_id": "g-account-id",
   139  		"user_id": "jack.anvil@email.com"
   140  	},
   141  	"parameters": {
   142  		"autoScalerMin":14,
   143  		"autoScalerMax":25,
   144  		"maxSurge":13,
   145  		"maxUnavailable":6
   146  	}
   147  }`)
   148  	assert.Equal(t, http.StatusAccepted, resp.StatusCode)
   149  	upgradeOperationID = suite.DecodeOperationID(resp)
   150  	suite.FinishUpdatingOperationByProvisioner(upgradeOperationID)
   151  
   152  	suite.AssertKymaResourceExists(upgradeOperationID)
   153  	suite.AssertKymaLabelsExist(upgradeOperationID, map[string]string{
   154  		"kyma-project.io/region":          "eastus",
   155  		"kyma-project.io/platform-region": "cf-eu10",
   156  	})
   157  }