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

     1  package deprovisioning
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"fmt"
     8  
     9  	"github.com/kyma-project/kyma-environment-broker/common/hyperscaler"
    10  	hyperscalerMocks "github.com/kyma-project/kyma-environment-broker/common/hyperscaler/automock"
    11  	"github.com/kyma-project/kyma-environment-broker/internal"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
    14  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    15  	"github.com/pivotal-cf/brokerapi/v8/domain"
    16  	"github.com/sirupsen/logrus"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestReleaseSubscriptionStep_HappyPath(t *testing.T) {
    21  	// given
    22  	log := logrus.New()
    23  	memoryStorage := storage.NewMemoryStorage()
    24  
    25  	operation := fixDeprovisioningOperationWithPlanID(broker.GCPPlanID)
    26  	instance := fixGCPInstance(operation.InstanceID)
    27  
    28  	err := memoryStorage.Instances().Insert(instance)
    29  	assert.NoError(t, err)
    30  
    31  	accountProviderMock := &hyperscalerMocks.AccountProvider{}
    32  	accountProviderMock.On("MarkUnusedGardenerSecretBindingAsDirty", hyperscaler.GCP, instance.GetSubscriptionGlobalAccoundID(), false).Return(nil)
    33  
    34  	step := NewReleaseSubscriptionStep(memoryStorage.Operations(), memoryStorage.Instances(), accountProviderMock)
    35  
    36  	// when
    37  	operation, repeat, err := step.Run(operation, log)
    38  
    39  	assert.NoError(t, err)
    40  
    41  	// then
    42  	accountProviderMock.AssertNumberOfCalls(t, "MarkUnusedGardenerSecretBindingAsDirty", 1)
    43  	assert.NoError(t, err)
    44  	assert.Equal(t, time.Duration(0), repeat)
    45  	assert.Equal(t, domain.Succeeded, operation.State)
    46  }
    47  
    48  func TestReleaseSubscriptionStep_TrialPlan(t *testing.T) {
    49  	// given
    50  	log := logrus.New()
    51  	memoryStorage := storage.NewMemoryStorage()
    52  
    53  	operation := fixDeprovisioningOperationWithPlanID(broker.TrialPlanID)
    54  	instance := fixGCPInstance(operation.InstanceID)
    55  
    56  	err := memoryStorage.Instances().Insert(instance)
    57  	assert.NoError(t, err)
    58  
    59  	accountProviderMock := &hyperscalerMocks.AccountProvider{}
    60  	accountProviderMock.On("MarkUnusedGardenerSecretBindingAsDirty", hyperscaler.GCP, instance.GetSubscriptionGlobalAccoundID(), false).Return(nil)
    61  
    62  	step := NewReleaseSubscriptionStep(memoryStorage.Operations(), memoryStorage.Instances(), accountProviderMock)
    63  
    64  	// when
    65  	operation, repeat, err := step.Run(operation, log)
    66  
    67  	assert.NoError(t, err)
    68  
    69  	// then
    70  	accountProviderMock.AssertNumberOfCalls(t, "MarkUnusedGardenerSecretBindingAsDirty", 0)
    71  	assert.NoError(t, err)
    72  	assert.Equal(t, time.Duration(0), repeat)
    73  	assert.Equal(t, domain.Succeeded, operation.State)
    74  }
    75  
    76  func TestReleaseSubscriptionStep_OwnClusterPlan(t *testing.T) {
    77  	// given
    78  	log := logrus.New()
    79  	memoryStorage := storage.NewMemoryStorage()
    80  
    81  	operation := fixDeprovisioningOperationWithPlanID(broker.OwnClusterPlanID)
    82  	instance := fixGCPInstance(operation.InstanceID)
    83  
    84  	err := memoryStorage.Instances().Insert(instance)
    85  	assert.NoError(t, err)
    86  
    87  	accountProviderMock := &hyperscalerMocks.AccountProvider{}
    88  	accountProviderMock.On("MarkUnusedGardenerSecretBindingAsDirty", hyperscaler.GCP, instance.GetSubscriptionGlobalAccoundID(), false).Return(nil)
    89  
    90  	step := NewReleaseSubscriptionStep(memoryStorage.Operations(), memoryStorage.Instances(), accountProviderMock)
    91  
    92  	// when
    93  	operation, repeat, err := step.Run(operation, log)
    94  
    95  	assert.NoError(t, err)
    96  
    97  	// then
    98  	accountProviderMock.AssertNumberOfCalls(t, "MarkUnusedGardenerSecretBindingAsDirty", 0)
    99  	assert.NoError(t, err)
   100  	assert.Equal(t, time.Duration(0), repeat)
   101  	assert.Equal(t, domain.Succeeded, operation.State)
   102  }
   103  
   104  func TestReleaseSubscriptionStep_InstanceNotFound(t *testing.T) {
   105  	// given
   106  	log := logrus.New()
   107  	memoryStorage := storage.NewMemoryStorage()
   108  
   109  	operation := fixDeprovisioningOperationWithPlanID(broker.GCPPlanID)
   110  	instance := fixGCPInstance(operation.InstanceID)
   111  
   112  	accountProviderMock := &hyperscalerMocks.AccountProvider{}
   113  	accountProviderMock.On("MarkUnusedGardenerSecretBindingAsDirty", hyperscaler.GCP, instance.GetSubscriptionGlobalAccoundID(), false).Return(nil)
   114  
   115  	step := NewReleaseSubscriptionStep(memoryStorage.Operations(), memoryStorage.Instances(), accountProviderMock)
   116  	memoryStorage.Operations().InsertOperation(operation)
   117  
   118  	// when
   119  	operation, repeat, err := step.Run(operation, log)
   120  
   121  	assert.NoError(t, err)
   122  
   123  	// then
   124  	accountProviderMock.AssertNotCalled(t, "MarkUnusedGardenerSecretBindingAsDirty")
   125  	assert.NoError(t, err)
   126  	assert.Equal(t, time.Duration(0), repeat)
   127  	assert.Equal(t, domain.Succeeded, operation.State)
   128  }
   129  
   130  func TestReleaseSubscriptionStep_ProviderNotFound(t *testing.T) {
   131  	// given
   132  	log := logrus.New()
   133  	memoryStorage := storage.NewMemoryStorage()
   134  
   135  	operation := fixDeprovisioningOperationWithPlanID(broker.GCPPlanID)
   136  	instance := fixGCPInstance(operation.InstanceID)
   137  	instance.Provider = "unknown"
   138  
   139  	err := memoryStorage.Instances().Insert(instance)
   140  	assert.NoError(t, err)
   141  
   142  	accountProviderMock := &hyperscalerMocks.AccountProvider{}
   143  	accountProviderMock.On("MarkUnusedGardenerSecretBindingAsDirty", hyperscaler.GCP, instance.GetSubscriptionGlobalAccoundID(), false).Return(nil)
   144  
   145  	step := NewReleaseSubscriptionStep(memoryStorage.Operations(), memoryStorage.Instances(), accountProviderMock)
   146  	memoryStorage.Operations().InsertOperation(operation)
   147  
   148  	// when
   149  	operation, repeat, err := step.Run(operation, log)
   150  
   151  	assert.NoError(t, err)
   152  
   153  	// then
   154  	accountProviderMock.AssertNotCalled(t, "MarkUnusedGardenerSecretBindingAsDirty")
   155  	assert.NoError(t, err)
   156  	assert.Equal(t, time.Duration(0), repeat)
   157  	assert.Equal(t, domain.Succeeded, operation.State)
   158  }
   159  
   160  func TestReleaseSubscriptionStepGardener_CallFails(t *testing.T) {
   161  	// given
   162  	log := logrus.New()
   163  	memoryStorage := storage.NewMemoryStorage()
   164  
   165  	operation := fixDeprovisioningOperationWithPlanID(broker.GCPPlanID)
   166  	instance := fixGCPInstance(operation.InstanceID)
   167  
   168  	err := memoryStorage.Instances().Insert(instance)
   169  	assert.NoError(t, err)
   170  
   171  	accountProviderMock := &hyperscalerMocks.AccountProvider{}
   172  	accountProviderMock.On("MarkUnusedGardenerSecretBindingAsDirty", hyperscaler.GCP, instance.GetSubscriptionGlobalAccoundID(), false).Return(fmt.Errorf("failed to release subscription for tenant. Gardener Account pool is not configured"))
   173  
   174  	step := NewReleaseSubscriptionStep(memoryStorage.Operations(), memoryStorage.Instances(), accountProviderMock)
   175  
   176  	// when
   177  	operation, repeat, err := step.Run(operation, log)
   178  
   179  	assert.NoError(t, err)
   180  
   181  	// then
   182  	assert.NoError(t, err)
   183  	assert.Equal(t, 10*time.Second, repeat)
   184  	assert.Equal(t, domain.Succeeded, operation.State)
   185  }
   186  
   187  func fixGCPInstance(instanceID string) internal.Instance {
   188  	instance := fixture.FixInstance(instanceID)
   189  	instance.Provider = "GCP"
   190  	return instance
   191  }
   192  
   193  func fixDeprovisioningOperationWithPlanID(planID string) internal.Operation {
   194  	deprovisioningOperation := fixture.FixDeprovisioningOperationAsOperation(operationID, instanceID)
   195  	deprovisioningOperation.ProvisioningParameters.PlanID = planID
   196  	deprovisioningOperation.ProvisioningParameters.ErsContext.GlobalAccountID = globalAccountID
   197  	deprovisioningOperation.ProvisioningParameters.ErsContext.SubAccountID = subAccountID
   198  	return deprovisioningOperation
   199  }