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

     1  package deprovisioning
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRemoveInstanceStep_HappyPathForPermanentRemoval(t *testing.T) {
    14  	// given
    15  	log := logrus.New()
    16  	memoryStorage := storage.NewMemoryStorage()
    17  
    18  	operation := fixture.FixDeprovisioningOperationAsOperation(operationID, instanceID)
    19  	instance := fixture.FixInstance(instanceID)
    20  
    21  	err := memoryStorage.Instances().Insert(instance)
    22  	assert.NoError(t, err)
    23  
    24  	err = memoryStorage.Operations().InsertOperation(operation)
    25  	assert.NoError(t, err)
    26  
    27  	step := NewRemoveInstanceStep(memoryStorage.Instances(), memoryStorage.Operations())
    28  
    29  	// when
    30  	operation, backoff, err := step.Run(operation, log)
    31  
    32  	assert.NoError(t, err)
    33  
    34  	// then
    35  	operationFromStorage, err := memoryStorage.Operations().GetOperationByID(operationID)
    36  	assert.NoError(t, err)
    37  	assert.Equal(t, 0, len(operationFromStorage.ProvisioningParameters.ErsContext.UserID))
    38  
    39  	_, err = memoryStorage.Instances().GetByID(instanceID)
    40  	assert.ErrorContains(t, err, "not exist")
    41  
    42  	assert.Equal(t, time.Duration(0), backoff)
    43  }
    44  
    45  func TestRemoveInstanceStep_UpdateOperationFailsForPermanentRemoval(t *testing.T) {
    46  	// given
    47  	log := logrus.New()
    48  	memoryStorage := storage.NewMemoryStorage()
    49  
    50  	operation := fixture.FixDeprovisioningOperationAsOperation(operationID, instanceID)
    51  	instance := fixture.FixInstance(instanceID)
    52  
    53  	err := memoryStorage.Instances().Insert(instance)
    54  	assert.NoError(t, err)
    55  
    56  	step := NewRemoveInstanceStep(memoryStorage.Instances(), memoryStorage.Operations())
    57  
    58  	// when
    59  	operation, backoff, err := step.Run(operation, log)
    60  
    61  	assert.NoError(t, err)
    62  
    63  	// then
    64  	assert.Equal(t, time.Minute, backoff)
    65  }
    66  
    67  func TestRemoveInstanceStep_HappyPathForSuspension(t *testing.T) {
    68  	// given
    69  	log := logrus.New()
    70  	memoryStorage := storage.NewMemoryStorage()
    71  
    72  	operation := fixture.FixSuspensionOperationAsOperation(operationID, instanceID)
    73  	instance := fixture.FixInstance(instanceID)
    74  	instance.DeletedAt = time.Time{}
    75  
    76  	err := memoryStorage.Instances().Insert(instance)
    77  	assert.NoError(t, err)
    78  
    79  	err = memoryStorage.Operations().InsertOperation(operation)
    80  	assert.NoError(t, err)
    81  
    82  	step := NewRemoveInstanceStep(memoryStorage.Instances(), memoryStorage.Operations())
    83  
    84  	// when
    85  	operation, backoff, err := step.Run(operation, log)
    86  
    87  	assert.NoError(t, err)
    88  
    89  	// then
    90  	operationFromStorage, err := memoryStorage.Operations().GetOperationByID(operationID)
    91  	assert.NoError(t, err)
    92  	assert.Equal(t, 0, len(operationFromStorage.RuntimeID))
    93  
    94  	instanceFromStorage, err := memoryStorage.Instances().GetByID(instanceID)
    95  	assert.Equal(t, 0, len(instanceFromStorage.RuntimeID))
    96  	assert.Equal(t, time.Time{}, instanceFromStorage.DeletedAt)
    97  
    98  	assert.Equal(t, time.Duration(0), backoff)
    99  }
   100  
   101  func TestRemoveInstanceStep_InstanceHasExecutedButNotCompletedOperationSteps(t *testing.T) {
   102  	// given
   103  	log := logrus.New()
   104  	memoryStorage := storage.NewMemoryStorage()
   105  
   106  	operation := fixture.FixDeprovisioningOperationAsOperation(operationID, instanceID)
   107  	operation.ExcutedButNotCompleted = append(operation.ExcutedButNotCompleted, "De-provision_AVS_Evaluations")
   108  	instance := fixture.FixInstance(instanceID)
   109  	instance.DeletedAt = time.Time{}
   110  
   111  	err := memoryStorage.Instances().Insert(instance)
   112  	assert.NoError(t, err)
   113  
   114  	err = memoryStorage.Operations().InsertOperation(operation)
   115  	assert.NoError(t, err)
   116  
   117  	step := NewRemoveInstanceStep(memoryStorage.Instances(), memoryStorage.Operations())
   118  
   119  	// when
   120  	_, backoff, err := step.Run(operation, log)
   121  
   122  	assert.NoError(t, err)
   123  
   124  	// then
   125  	operationFromStorage, err := memoryStorage.Operations().GetOperationByID(operationID)
   126  	assert.NoError(t, err)
   127  	assert.Equal(t, false, operationFromStorage.Temporary)
   128  
   129  	instanceFromStorage, err := memoryStorage.Instances().GetByID(instanceID)
   130  	assert.NoError(t, err)
   131  	assert.NotEqual(t, time.Time{}, instanceFromStorage.DeletedAt)
   132  
   133  	assert.Equal(t, time.Duration(0), backoff)
   134  }
   135  
   136  func TestRemoveInstanceStep_InstanceDeleted(t *testing.T) {
   137  	// given
   138  	log := logrus.New()
   139  	memoryStorage := storage.NewMemoryStorage()
   140  
   141  	operation := fixture.FixDeprovisioningOperationAsOperation(operationID, instanceID)
   142  
   143  	err := memoryStorage.Operations().InsertOperation(operation)
   144  	assert.NoError(t, err)
   145  
   146  	step := NewRemoveInstanceStep(memoryStorage.Instances(), memoryStorage.Operations())
   147  
   148  	// when
   149  	_, backoff, err := step.Run(operation, log)
   150  
   151  	assert.NoError(t, err)
   152  
   153  	// then
   154  	assert.Equal(t, time.Duration(0), backoff)
   155  }