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

     1  package deprovisioning
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal"
     8  
     9  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/provisioner"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    12  	"github.com/pivotal-cf/brokerapi/v8/domain"
    13  	"github.com/sirupsen/logrus"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestCheckRuntimeRemovalStep(t *testing.T) {
    19  	for _, tc := range []struct {
    20  		givenState       gqlschema.OperationState
    21  		expectedDuration bool
    22  	}{
    23  		{givenState: gqlschema.OperationStatePending, expectedDuration: true},
    24  		{givenState: gqlschema.OperationStateInProgress, expectedDuration: true},
    25  		{givenState: gqlschema.OperationStateSucceeded, expectedDuration: false},
    26  	} {
    27  		t.Run(string(tc.givenState), func(t *testing.T) {
    28  			// given
    29  			log := logrus.New()
    30  			memoryStorage := storage.NewMemoryStorage()
    31  			provisionerClient := provisioner.NewFakeClient()
    32  			svc := NewCheckRuntimeRemovalStep(memoryStorage.Operations(), memoryStorage.Instances(), provisionerClient, time.Minute)
    33  			dOp := fixDeprovisioningOperation().Operation
    34  			memoryStorage.Instances().Insert(internal.Instance{
    35  				GlobalAccountID: "global-acc",
    36  				InstanceID:      dOp.InstanceID,
    37  			})
    38  			provisionerOp, _ := provisionerClient.DeprovisionRuntime(dOp.GlobalAccountID, dOp.RuntimeID)
    39  			provisionerClient.FinishProvisionerOperation(provisionerOp, tc.givenState)
    40  			dOp.ProvisionerOperationID = provisionerOp
    41  
    42  			// when
    43  			_, d, err := svc.Run(dOp, log)
    44  
    45  			// then
    46  			require.NoError(t, err)
    47  			assert.Equal(t, tc.expectedDuration, d > 0)
    48  		})
    49  	}
    50  }
    51  
    52  func TestCheckRuntimeRemovalStep_ProvisionerFailed(t *testing.T) {
    53  	// given
    54  	log := logrus.New()
    55  	memoryStorage := storage.NewMemoryStorage()
    56  	provisionerClient := provisioner.NewFakeClient()
    57  	svc := NewCheckRuntimeRemovalStep(memoryStorage.Operations(), memoryStorage.Instances(), provisionerClient, time.Minute)
    58  	dOp := fixDeprovisioningOperation().Operation
    59  	memoryStorage.Operations().InsertOperation(dOp)
    60  	memoryStorage.Instances().Insert(internal.Instance{
    61  		GlobalAccountID: "global-acc",
    62  		InstanceID:      dOp.InstanceID,
    63  	})
    64  	provisionerOp, _ := provisionerClient.DeprovisionRuntime(dOp.GlobalAccountID, dOp.RuntimeID)
    65  	provisionerClient.FinishProvisionerOperation(provisionerOp, gqlschema.OperationStateFailed)
    66  	dOp.ProvisionerOperationID = provisionerOp
    67  
    68  	// when
    69  	op, _, err := svc.Run(dOp, log)
    70  
    71  	// then
    72  	require.Error(t, err)
    73  	assert.Equal(t, domain.Failed, op.State)
    74  }
    75  
    76  func TestCheckRuntimeRemovalStep_InstanceDeleted(t *testing.T) {
    77  	// given
    78  	log := logrus.New()
    79  	memoryStorage := storage.NewMemoryStorage()
    80  	provisionerClient := provisioner.NewFakeClient()
    81  	svc := NewCheckRuntimeRemovalStep(memoryStorage.Operations(), memoryStorage.Instances(), provisionerClient, time.Minute)
    82  	dOp := fixDeprovisioningOperation().Operation
    83  	memoryStorage.Operations().InsertOperation(dOp)
    84  
    85  	// when
    86  	_, backoff, err := svc.Run(dOp, log)
    87  
    88  	// then
    89  	require.NoError(t, err)
    90  	assert.Zero(t, backoff)
    91  }
    92  
    93  func TestCheckRuntimeRemovalStep_NoProvisionerOperationID(t *testing.T) {
    94  	// given
    95  	log := logrus.New()
    96  	memoryStorage := storage.NewMemoryStorage()
    97  	svc := NewCheckRuntimeRemovalStep(memoryStorage.Operations(), memoryStorage.Instances(), nil, time.Minute)
    98  	dOp := fixDeprovisioningOperation().Operation
    99  	memoryStorage.Instances().Insert(internal.Instance{
   100  		GlobalAccountID: "global-acc",
   101  		InstanceID:      dOp.InstanceID,
   102  	})
   103  	dOp.ProvisionerOperationID = ""
   104  
   105  	// when
   106  	_, d, err := svc.Run(dOp, log)
   107  
   108  	// then
   109  	require.NoError(t, err)
   110  	assert.Zero(t, d)
   111  }