github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/upgrade_cluster_operation_test.go (about) 1 package process 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/kyma-project/kyma-environment-broker/internal" 9 "github.com/kyma-project/kyma-environment-broker/internal/fixture" 10 "github.com/kyma-project/kyma-environment-broker/internal/storage" 11 "github.com/pivotal-cf/brokerapi/v8/domain" 12 "github.com/sirupsen/logrus" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestUpgradeClusterOperationManager_OperationSucceeded(t *testing.T) { 18 // given 19 memory := storage.NewMemoryStorage() 20 operations := memory.Operations() 21 opManager := NewUpgradeClusterOperationManager(operations) 22 op := fixUpgradeClusterOperation() 23 err := operations.InsertUpgradeClusterOperation(op) 24 require.NoError(t, err) 25 26 // when 27 op, when, err := opManager.OperationSucceeded(op, "task succeeded", logrus.New()) 28 29 // then 30 assert.NoError(t, err) 31 assert.Equal(t, domain.Succeeded, op.State) 32 assert.Equal(t, time.Duration(0), when) 33 } 34 35 func TestUpgradeClusterOperationManager_OperationFailed(t *testing.T) { 36 // given 37 memory := storage.NewMemoryStorage() 38 operations := memory.Operations() 39 opManager := NewUpgradeClusterOperationManager(operations) 40 op := fixUpgradeClusterOperation() 41 err := operations.InsertUpgradeClusterOperation(op) 42 require.NoError(t, err) 43 44 errMsg := "task failed miserably" 45 errOut := fmt.Errorf("error occurred") 46 47 // when 48 op, when, err := opManager.OperationFailed(op, errMsg, errOut, logrus.New()) 49 50 // then 51 assert.Error(t, err) 52 assert.EqualError(t, err, "task failed miserably: error occurred") 53 assert.Equal(t, domain.Failed, op.State) 54 assert.Equal(t, time.Duration(0), when) 55 56 // when 57 _, _, err = opManager.OperationFailed(op, errMsg, nil, logrus.New()) 58 59 // then 60 assert.Error(t, err) 61 assert.EqualError(t, err, "task failed miserably") 62 } 63 64 func TestUpgradeClusterOperationManager_RetryOperation(t *testing.T) { 65 // given 66 memory := storage.NewMemoryStorage() 67 operations := memory.Operations() 68 opManager := NewUpgradeClusterOperationManager(operations) 69 op := internal.UpgradeClusterOperation{} 70 op.UpdatedAt = time.Now() 71 retryInterval := time.Hour 72 errorMessage := "task failed" 73 errOut := fmt.Errorf("error occurred") 74 maxtime := time.Hour * 3 // allow 2 retries 75 76 // this is required to avoid storage retries (without this statement there will be an error => retry) 77 err := operations.InsertUpgradeClusterOperation(op) 78 require.NoError(t, err) 79 80 // then - first call 81 op, when, err := opManager.RetryOperation(op, errorMessage, errOut, retryInterval, maxtime, fixLogger()) 82 83 // when - first retry 84 assert.True(t, when > 0) 85 assert.Nil(t, err) 86 87 // then - second call 88 t.Log(op.UpdatedAt.String()) 89 op.UpdatedAt = op.UpdatedAt.Add(-retryInterval - time.Second) // simulate wait of first retry 90 t.Log(op.UpdatedAt.String()) 91 op, when, err = opManager.RetryOperation(op, errorMessage, errOut, retryInterval, maxtime, fixLogger()) 92 93 // when - second call => retry 94 assert.True(t, when > 0) 95 assert.Nil(t, err) 96 97 } 98 99 func fixUpgradeClusterOperation() internal.UpgradeClusterOperation { 100 upgradeOperation := fixture.FixUpgradeClusterOperation( 101 "2c538027-d1c4-41ef-a26c-c9604483cb6d", 102 "2b6645a1-87e7-491d-bce3-cc0fbe16b6c0", 103 ) 104 upgradeOperation.State = domain.InProgress 105 106 return upgradeOperation 107 }