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

     1  package process
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/kyma-project/kyma-environment-broker/internal"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    14  )
    15  
    16  func Test_Deprovision_RetryOperationOnce(t *testing.T) {
    17  	// given
    18  	memory := storage.NewMemoryStorage()
    19  	operations := memory.Operations()
    20  	opManager := NewDeprovisionOperationManager(operations)
    21  	op := internal.DeprovisioningOperation{}
    22  	op.UpdatedAt = time.Now()
    23  	retryInterval := time.Hour
    24  	errMsg := fmt.Errorf("ups ... ")
    25  
    26  	// this is required to avoid storage retries (without this statement there will be an error => retry)
    27  	err := operations.InsertDeprovisioningOperation(op)
    28  	require.NoError(t, err)
    29  
    30  	// then - first call
    31  	op, when, err := opManager.RetryOperationOnce(op, errMsg.Error(), errMsg, retryInterval, fixLogger())
    32  
    33  	// when - first retry
    34  	assert.True(t, when > 0)
    35  	assert.Nil(t, err)
    36  
    37  	// then - second call
    38  	t.Log(op.UpdatedAt.String())
    39  	op.UpdatedAt = op.UpdatedAt.Add(-retryInterval - time.Second) // simulate wait of first retry
    40  	t.Log(op.UpdatedAt.String())
    41  	op, when, err = opManager.RetryOperationOnce(op, errMsg.Error(), errMsg, retryInterval, fixLogger())
    42  
    43  	// when - second call => no retry
    44  	assert.True(t, when == 0)
    45  	assert.NotNil(t, err)
    46  }
    47  
    48  func Test_Deprovision_RetryOperationWithoutFail(t *testing.T) {
    49  	// given
    50  	memory := storage.NewMemoryStorage()
    51  	operations := memory.Operations()
    52  	opManager := NewDeprovisionOperationManager(operations)
    53  	op := internal.DeprovisioningOperation{}
    54  	op.UpdatedAt = time.Now()
    55  	retryInterval := time.Hour
    56  	errorMessage := fmt.Sprintf("ups ... ")
    57  
    58  	// this is required to avoid storage retries (without this statement there will be an error => retry)
    59  	err := operations.InsertDeprovisioningOperation(op)
    60  	require.NoError(t, err)
    61  
    62  	// then - first call
    63  	op, when, err := opManager.RetryOperationWithoutFail(op, errorMessage, retryInterval, retryInterval+1, fixLogger())
    64  
    65  	// when - first retry
    66  	assert.True(t, when > 0)
    67  	assert.Nil(t, err)
    68  
    69  	// then - second call
    70  	t.Log(op.UpdatedAt.String())
    71  	op.UpdatedAt = op.UpdatedAt.Add(-retryInterval - time.Second) // simulate wait of first retry
    72  	t.Log(op.UpdatedAt.String())
    73  	op, when, err = opManager.RetryOperationWithoutFail(op, errorMessage, retryInterval, retryInterval+1, fixLogger())
    74  
    75  	// when - second call => no retry
    76  	assert.True(t, when == 0)
    77  	assert.NoError(t, err)
    78  }
    79  
    80  func Test_Deprovision_RetryOperation(t *testing.T) {
    81  	// given
    82  	memory := storage.NewMemoryStorage()
    83  	operations := memory.Operations()
    84  	opManager := NewDeprovisionOperationManager(operations)
    85  	op := internal.DeprovisioningOperation{}
    86  	op.UpdatedAt = time.Now()
    87  	retryInterval := time.Hour
    88  	errorMessage := fmt.Sprintf("ups ... ")
    89  	errOut := fmt.Errorf("error occurred")
    90  	maxtime := time.Hour * 3 // allow 2 retries
    91  
    92  	// this is required to avoid storage retries (without this statement there will be an error => retry)
    93  	err := operations.InsertDeprovisioningOperation(op)
    94  	require.NoError(t, err)
    95  
    96  	// then - first call
    97  	op, when, err := opManager.RetryOperation(op, errorMessage, errOut, retryInterval, maxtime, fixLogger())
    98  
    99  	// when - first retry
   100  	assert.True(t, when > 0)
   101  	assert.Nil(t, err)
   102  
   103  	// then - second call
   104  	t.Log(op.UpdatedAt.String())
   105  	op.UpdatedAt = op.UpdatedAt.Add(-retryInterval - time.Second) // simulate wait of first retry
   106  	t.Log(op.UpdatedAt.String())
   107  	op, when, err = opManager.RetryOperation(op, errorMessage, errOut, retryInterval, maxtime, fixLogger())
   108  
   109  	// when - second call => retry
   110  	assert.True(t, when > 0)
   111  	assert.Nil(t, err)
   112  }
   113  
   114  func fixLogger() logrus.FieldLogger {
   115  	return logrus.StandardLogger()
   116  }