github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/orchestration/handlers/cancel_test.go (about)

     1  package handlers
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-project/kyma-environment-broker/common/orchestration"
     7  	"github.com/kyma-project/kyma-environment-broker/internal"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    10  	"github.com/sirupsen/logrus"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  const (
    16  	fixOrchestrationID = "test-id"
    17  )
    18  
    19  func TestCanceler_CancelForID(t *testing.T) {
    20  	t.Run("should cancel orchestration", func(t *testing.T) {
    21  		s := storage.NewMemoryStorage()
    22  		err := s.Orchestrations().Insert(fixOrchestration())
    23  		require.NoError(t, err)
    24  
    25  		c := NewCanceler(s.Orchestrations(), logrus.New())
    26  
    27  		err = c.CancelForID(fixOrchestrationID)
    28  		require.NoError(t, err)
    29  
    30  		isCanceling, err := isCanceling(s.Orchestrations())
    31  		require.NoError(t, err)
    32  
    33  		assert.True(t, isCanceling)
    34  	})
    35  	t.Run("already canceling", func(t *testing.T) {
    36  		s := storage.NewMemoryStorage()
    37  		o := fixOrchestration()
    38  		o.State = orchestration.Canceling
    39  		err := s.Orchestrations().Insert(o)
    40  		require.NoError(t, err)
    41  
    42  		c := NewCanceler(s.Orchestrations(), logrus.New())
    43  
    44  		err = c.CancelForID(fixOrchestrationID)
    45  		require.NoError(t, err)
    46  
    47  		isCanceling, err := isCanceling(s.Orchestrations())
    48  		require.NoError(t, err)
    49  
    50  		assert.True(t, isCanceling)
    51  	})
    52  	t.Run("already canceled", func(t *testing.T) {
    53  		s := storage.NewMemoryStorage()
    54  		o := fixOrchestration()
    55  		o.State = orchestration.Canceled
    56  		err := s.Orchestrations().Insert(o)
    57  		require.NoError(t, err)
    58  
    59  		c := NewCanceler(s.Orchestrations(), logrus.New())
    60  
    61  		err = c.CancelForID(fixOrchestrationID)
    62  		require.NoError(t, err)
    63  
    64  		isCanceling, err := isCanceling(s.Orchestrations())
    65  		require.NoError(t, err)
    66  
    67  		assert.False(t, isCanceling)
    68  	})
    69  	t.Run("should return error when orchestration not found", func(t *testing.T) {
    70  		s := storage.NewMemoryStorage()
    71  		c := NewCanceler(s.Orchestrations(), logrus.New())
    72  
    73  		err := c.CancelForID(fixOrchestrationID)
    74  		assert.Error(t, err)
    75  	})
    76  }
    77  
    78  func isCanceling(s storage.Orchestrations) (bool, error) {
    79  	o, err := s.GetByID(fixOrchestrationID)
    80  	if err != nil {
    81  		return false, err
    82  	}
    83  	if o.State == orchestration.Canceling {
    84  		return true, nil
    85  	}
    86  	return false, nil
    87  }
    88  
    89  func fixOrchestration() internal.Orchestration {
    90  	o := fixture.FixOrchestration(fixOrchestrationID)
    91  	o.State = orchestration.InProgress
    92  
    93  	return o
    94  }