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

     1  package process
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/kyma-project/kyma-environment-broker/internal"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    13  )
    14  
    15  func Test_Provision_RetryOperationOnce(t *testing.T) {
    16  	// given
    17  	memory := storage.NewMemoryStorage()
    18  	operations := memory.Operations()
    19  	opManager := NewProvisionOperationManager(operations)
    20  	op := internal.ProvisioningOperation{}
    21  	op.UpdatedAt = time.Now()
    22  	retryInterval := time.Hour
    23  	errMsg := fmt.Errorf("ups ... ")
    24  
    25  	// this is required to avoid storage retries (without this statement there will be an error => retry)
    26  	err := operations.InsertProvisioningOperation(op)
    27  	require.NoError(t, err)
    28  
    29  	// then - first call
    30  	op, when, err := opManager.RetryOperationOnce(op, errMsg.Error(), errMsg, retryInterval, fixLogger())
    31  
    32  	// when - first retry
    33  	assert.True(t, when > 0)
    34  	assert.Nil(t, err)
    35  
    36  	// then - second call
    37  	t.Log(op.UpdatedAt.String())
    38  	op.UpdatedAt = op.UpdatedAt.Add(-retryInterval - time.Second) // simulate wait of first retry
    39  	t.Log(op.UpdatedAt.String())
    40  	op, when, err = opManager.RetryOperationOnce(op, errMsg.Error(), errMsg, retryInterval, fixLogger())
    41  
    42  	// when - second call => no retry
    43  	assert.True(t, when == 0)
    44  	assert.NotNil(t, err)
    45  }
    46  
    47  func Test_Provision_RetryOperation(t *testing.T) {
    48  	// given
    49  	memory := storage.NewMemoryStorage()
    50  	operations := memory.Operations()
    51  	opManager := NewProvisionOperationManager(operations)
    52  	op := internal.ProvisioningOperation{}
    53  	op.UpdatedAt = time.Now()
    54  	retryInterval := time.Hour
    55  	errorMessage := "ups ... "
    56  	errOut := fmt.Errorf("error occurred")
    57  	maxtime := time.Hour * 3 // allow 2 retries
    58  
    59  	// this is required to avoid storage retries (without this statement there will be an error => retry)
    60  	err := operations.InsertProvisioningOperation(op)
    61  	require.NoError(t, err)
    62  
    63  	// then - first call
    64  	op, when, err := opManager.RetryOperation(op, errorMessage, errOut, retryInterval, maxtime, fixLogger())
    65  
    66  	// when - first retry
    67  	assert.True(t, when > 0)
    68  	assert.Nil(t, err)
    69  
    70  	// then - second call
    71  	t.Log(op.UpdatedAt.String())
    72  	op.UpdatedAt = op.UpdatedAt.Add(-retryInterval - time.Second) // simulate wait of first retry
    73  	t.Log(op.UpdatedAt.String())
    74  	op, when, err = opManager.RetryOperation(op, errorMessage, errOut, retryInterval, maxtime, fixLogger())
    75  
    76  	// when - second call => retry
    77  	assert.True(t, when > 0)
    78  	assert.Nil(t, err)
    79  }