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

     1  package avs
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    10  
    11  	"github.com/sirupsen/logrus"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func newTestParams(t *testing.T) (*Client, *MockAvsServer, Config, *InternalEvalAssistant, *ExternalEvalAssistant, *logrus.Logger) {
    16  	// Given
    17  	server := NewMockAvsServer(t)
    18  	mockServer := FixMockAvsServer(server)
    19  	avsCfg := Config{
    20  		OauthTokenEndpoint: fmt.Sprintf("%s/oauth/token", mockServer.URL),
    21  		ApiEndpoint:        fmt.Sprintf("%s/api/v2/evaluationmetadata", mockServer.URL),
    22  	}
    23  	client, err := NewClient(context.TODO(), avsCfg, logrus.New())
    24  	assert.NoError(t, err)
    25  	iea := NewInternalEvalAssistant(avsCfg)
    26  	eea := NewExternalEvalAssistant(avsCfg)
    27  
    28  	log := logrus.New()
    29  
    30  	return client, server, avsCfg, iea, eea, log
    31  }
    32  
    33  type testParams struct {
    34  	t               *testing.T
    35  	client          *Client
    36  	avsCfg          Config
    37  	internalMonitor *BasicEvaluationCreateResponse
    38  	externalMonitor *BasicEvaluationCreateResponse
    39  }
    40  
    41  func newDelOpsParams(params testParams) (*Delegator, internal.UpgradeKymaOperation) {
    42  	operation := internal.UpgradeKymaOperation{}
    43  
    44  	*params.internalMonitor, *params.externalMonitor = createMonitors(params.client)
    45  
    46  	operation.Avs = internal.AvsLifecycleData{
    47  		AvsEvaluationInternalId: params.internalMonitor.Id,
    48  		AVSEvaluationExternalId: params.externalMonitor.Id,
    49  	}
    50  
    51  	operation.Avs.AvsExternalEvaluationStatus = internal.AvsEvaluationStatus{
    52  		Current:  StatusActive,
    53  		Original: StatusMaintenance,
    54  	}
    55  
    56  	operation.Avs.AvsInternalEvaluationStatus = internal.AvsEvaluationStatus{
    57  		Current:  StatusActive,
    58  		Original: StatusMaintenance,
    59  	}
    60  
    61  	ops := storage.NewMemoryStorage().Operations()
    62  	delegator := NewDelegator(params.client, params.avsCfg, ops)
    63  	err := ops.InsertUpgradeKymaOperation(operation)
    64  	assert.NoError(params.t, err)
    65  	assert.NotEqual(params.t, params.internalMonitor.Id, params.externalMonitor.Id)
    66  
    67  	return delegator, operation
    68  }
    69  
    70  func setOpAvsStatus(op *internal.UpgradeKymaOperation, current string, original string) (string, string) {
    71  	op.Avs.AvsInternalEvaluationStatus.Current = current
    72  	op.Avs.AvsInternalEvaluationStatus.Original = original
    73  
    74  	return current, original
    75  }
    76  
    77  func createMonitors(client *Client) (BasicEvaluationCreateResponse, BasicEvaluationCreateResponse) {
    78  	internalEval, _ := client.CreateEvaluation(&BasicEvaluationCreateRequest{
    79  		Name: "internal-monitor",
    80  	})
    81  
    82  	externalEval, _ := client.CreateEvaluation(&BasicEvaluationCreateRequest{
    83  		Name: "external-monitor",
    84  	})
    85  
    86  	return *internalEval, *externalEval
    87  }
    88  
    89  // Since logic is the same for both internal and external monitors,
    90  // we will only focus on internal monitor (easier to track).
    91  func TestDelegator_SetStatus(t *testing.T) {
    92  	// Given
    93  	client, server, avsCfg, internalEA, _, logger := newTestParams(t)
    94  	params := testParams{
    95  		t:               t,
    96  		client:          client,
    97  		avsCfg:          avsCfg,
    98  		internalMonitor: &BasicEvaluationCreateResponse{},
    99  		externalMonitor: &BasicEvaluationCreateResponse{},
   100  	}
   101  
   102  	t.Run("set for valid fields (requested != current)", func(t *testing.T) {
   103  		delegator, op := newDelOpsParams(params)
   104  
   105  		requested := StatusInactive
   106  		current, _ := setOpAvsStatus(&op, StatusActive, StatusMaintenance)
   107  
   108  		// When
   109  		err := delegator.SetStatus(logger, &op.Avs, internalEA, requested)
   110  
   111  		// Then
   112  		assert.NoError(t, err)
   113  		assert.Equal(t, requested, internalEA.GetEvalStatus(op.Avs))
   114  		assert.Equal(t, current, internalEA.GetOriginalEvalStatus(op.Avs))
   115  	})
   116  
   117  	t.Run("set for valid fields (requested == current)", func(t *testing.T) {
   118  		delegator, op := newDelOpsParams(params)
   119  
   120  		requested := StatusInactive
   121  		current, _ := setOpAvsStatus(&op, requested, StatusMaintenance)
   122  
   123  		// When
   124  		err := delegator.SetStatus(logger, &op.Avs, internalEA, requested)
   125  
   126  		// Then
   127  		assert.NoError(t, err)
   128  		assert.Equal(t, current, internalEA.GetEvalStatus(op.Avs))
   129  		assert.Equal(t, StatusActive, internalEA.GetOriginalEvalStatus(op.Avs))
   130  	})
   131  
   132  	t.Run("set for partial fields (requested != current, original empty)", func(t *testing.T) {
   133  		delegator, op := newDelOpsParams(params)
   134  
   135  		requested := StatusInactive
   136  		current, _ := setOpAvsStatus(&op, StatusActive, "")
   137  
   138  		// When
   139  		err := delegator.SetStatus(logger, &op.Avs, internalEA, requested)
   140  
   141  		// Then
   142  		assert.NoError(t, err)
   143  		assert.Equal(t, requested, internalEA.GetEvalStatus(op.Avs))
   144  		assert.Equal(t, current, internalEA.GetOriginalEvalStatus(op.Avs))
   145  	})
   146  
   147  	t.Run("set for partial fields (requested == current, original empty)", func(t *testing.T) {
   148  		delegator, op := newDelOpsParams(params)
   149  
   150  		requested := StatusInactive
   151  		current, _ := setOpAvsStatus(&op, requested, "")
   152  
   153  		// When
   154  		err := delegator.SetStatus(logger, &op.Avs, internalEA, requested)
   155  
   156  		// Then
   157  		assert.NoError(t, err)
   158  		assert.Equal(t, current, internalEA.GetEvalStatus(op.Avs))
   159  		assert.Equal(t, StatusActive, internalEA.GetOriginalEvalStatus(op.Avs))
   160  	})
   161  
   162  	t.Run("set for partial fields (requested != avs current, current empty)", func(t *testing.T) {
   163  		delegator, op := newDelOpsParams(params)
   164  
   165  		requested := StatusInactive
   166  		setOpAvsStatus(&op, "", StatusMaintenance)
   167  
   168  		// When
   169  		err := delegator.SetStatus(logger, &op.Avs, internalEA, requested)
   170  
   171  		// Then
   172  		assert.NoError(t, err)
   173  		assert.Equal(t, requested, internalEA.GetEvalStatus(op.Avs))
   174  		assert.Equal(t, params.internalMonitor.Status, internalEA.GetOriginalEvalStatus(op.Avs))
   175  	})
   176  
   177  	t.Run("set for partial fields (requested == avs current, current empty)", func(t *testing.T) {
   178  		delegator, op := newDelOpsParams(params)
   179  
   180  		requested := params.internalMonitor.Status
   181  		_, _ = setOpAvsStatus(&op, "", StatusMaintenance)
   182  
   183  		// When
   184  		err := delegator.SetStatus(logger, &op.Avs, internalEA, requested)
   185  
   186  		// Then
   187  		assert.NoError(t, err)
   188  		assert.Equal(t, params.internalMonitor.Status, internalEA.GetEvalStatus(op.Avs))
   189  		assert.Equal(t, params.internalMonitor.Status, internalEA.GetOriginalEvalStatus(op.Avs))
   190  	})
   191  
   192  	t.Run("set for empty fields", func(t *testing.T) {
   193  		delegator, op := newDelOpsParams(params)
   194  
   195  		requested := StatusInactive
   196  		_, _ = setOpAvsStatus(&op, "", "")
   197  
   198  		// When
   199  		err := delegator.SetStatus(logger, &op.Avs, internalEA, requested)
   200  
   201  		// Then
   202  		assert.NoError(t, err)
   203  		assert.Equal(t, requested, internalEA.GetEvalStatus(op.Avs))
   204  		// even though the avs Lifecycle data was initially empty,
   205  		// during SetStatus call it was reloaded from avs backend api
   206  		assert.Equal(t, params.internalMonitor.Status, internalEA.GetOriginalEvalStatus(op.Avs))
   207  	})
   208  
   209  	t.Run("set for invalid request", func(t *testing.T) {
   210  		delegator, op := newDelOpsParams(params)
   211  
   212  		requested := "invalid"
   213  		_, _ = setOpAvsStatus(&op, StatusActive, StatusMaintenance)
   214  
   215  		// When
   216  		err := delegator.SetStatus(logger, &op.Avs, internalEA, requested)
   217  
   218  		// Then
   219  		assert.NotNil(t, err)
   220  		assert.Contains(t, err.Error(), requested)
   221  	})
   222  
   223  	t.Run("disabled monitors", func(t *testing.T) {
   224  		delegator, op := newDelOpsParams(params)
   225  		op.Avs.AvsEvaluationInternalId = 0
   226  
   227  		version := op.Version
   228  
   229  		// When
   230  		err := delegator.SetStatus(logger, &op.Avs, internalEA, StatusActive)
   231  
   232  		// Then
   233  		assert.NoError(t, err)
   234  		assert.Equal(t, version, op.Version)
   235  	})
   236  
   237  	t.Run("deleted monitors", func(t *testing.T) {
   238  		delegator, op := newDelOpsParams(params)
   239  		op.Avs.AVSInternalEvaluationDeleted = true
   240  
   241  		version := op.Version
   242  
   243  		// When
   244  		err := delegator.SetStatus(logger, &op.Avs, internalEA, StatusActive)
   245  
   246  		// Then
   247  		assert.NoError(t, err)
   248  		assert.Equal(t, version, op.Version)
   249  	})
   250  
   251  	t.Run("reset for valid fields (current != original)", func(t *testing.T) {
   252  		delegator, op := newDelOpsParams(params)
   253  
   254  		current, original := setOpAvsStatus(&op, StatusActive, StatusMaintenance)
   255  
   256  		// When
   257  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   258  
   259  		// Then
   260  		assert.NoError(t, err)
   261  		assert.Equal(t, original, internalEA.GetEvalStatus(op.Avs))
   262  		assert.Equal(t, current, internalEA.GetOriginalEvalStatus(op.Avs))
   263  	})
   264  
   265  	t.Run("reset for valid fields (current == original)", func(t *testing.T) {
   266  		delegator, op := newDelOpsParams(params)
   267  
   268  		current, _ := setOpAvsStatus(&op, StatusInactive, StatusInactive)
   269  
   270  		// When
   271  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   272  
   273  		// Then
   274  		assert.NoError(t, err)
   275  		assert.Equal(t, current, internalEA.GetEvalStatus(op.Avs))
   276  		assert.Equal(t, StatusActive, internalEA.GetOriginalEvalStatus(op.Avs))
   277  	})
   278  
   279  	// Since logic is the same for both internal and external monitors,
   280  	// we will only focus on internal monitor (easier to track).
   281  	t.Run("reset for empty fields", func(t *testing.T) {
   282  		delegator, op := newDelOpsParams(params)
   283  
   284  		_, _ = setOpAvsStatus(&op, "", "")
   285  
   286  		// When
   287  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   288  
   289  		// Then
   290  		assert.NoError(t, err)
   291  		// restores current status from default
   292  		assert.Equal(t, StatusActive, internalEA.GetEvalStatus(op.Avs))
   293  		assert.Equal(t, StatusActive, internalEA.GetOriginalEvalStatus(op.Avs))
   294  	})
   295  
   296  	// Since logic is the same for both internal and external monitors,
   297  	// we will only focus on internal monitor (easier to track).
   298  	t.Run("reset for partial fields (original empty)", func(t *testing.T) {
   299  		delegator, op := newDelOpsParams(params)
   300  
   301  		_, _ = setOpAvsStatus(&op, StatusMaintenance, "")
   302  
   303  		// When
   304  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   305  
   306  		// Then
   307  		assert.NoError(t, err)
   308  		// restores current status from default
   309  		assert.Equal(t, StatusActive, internalEA.GetEvalStatus(op.Avs))
   310  		assert.Equal(t, StatusActive, internalEA.GetOriginalEvalStatus(op.Avs))
   311  	})
   312  
   313  	// Since logic is the same for both internal and external monitors,
   314  	// we will only focus on internal monitor (easier to track).
   315  	t.Run("reset for partial fields (current empty)", func(t *testing.T) {
   316  		delegator, op := newDelOpsParams(params)
   317  
   318  		instanceStatus := StatusInactive
   319  		server.Evaluations.BasicEvals[params.internalMonitor.Id].Status = instanceStatus
   320  
   321  		_, original := setOpAvsStatus(&op, "", StatusMaintenance)
   322  
   323  		// When
   324  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   325  
   326  		// Then
   327  		assert.NoError(t, err)
   328  		assert.Equal(t, original, internalEA.GetEvalStatus(op.Avs))
   329  		// restores original status from current, which is restored from Avs
   330  		assert.Equal(t, instanceStatus, internalEA.GetOriginalEvalStatus(op.Avs))
   331  	})
   332  
   333  	t.Run("reset from invalid fields (original invalid)", func(t *testing.T) {
   334  		delegator, op := newDelOpsParams(params)
   335  
   336  		_, _ = setOpAvsStatus(&op, StatusInactive, "invalid")
   337  
   338  		// When
   339  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   340  
   341  		// Then
   342  		assert.NoError(t, err)
   343  		// restores current status from default
   344  		assert.Equal(t, StatusActive, internalEA.GetEvalStatus(op.Avs))
   345  		// restores original status from current, which is restored from Avs
   346  		assert.Equal(t, StatusActive, internalEA.GetOriginalEvalStatus(op.Avs))
   347  	})
   348  
   349  	t.Run("reset from invalid fields (current invalid)", func(t *testing.T) {
   350  		delegator, op := newDelOpsParams(params)
   351  
   352  		_, original := setOpAvsStatus(&op, "invalidField", StatusMaintenance)
   353  
   354  		// When
   355  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   356  
   357  		// Then
   358  		assert.NoError(t, err)
   359  		assert.Equal(t, original, internalEA.GetEvalStatus(op.Avs))
   360  		// restores original status from current, which is restored from Avs
   361  		assert.Equal(t, params.internalMonitor.Status, internalEA.GetOriginalEvalStatus(op.Avs))
   362  	})
   363  
   364  	t.Run("reset from invalid avs fields (avs != current, original invalid)", func(t *testing.T) {
   365  		delegator, op := newDelOpsParams(params)
   366  
   367  		instanceStatus := "invalidField"
   368  		server.Evaluations.BasicEvals[params.internalMonitor.Id].Status = instanceStatus
   369  
   370  		current, _ := setOpAvsStatus(&op, StatusInactive, "invalid")
   371  
   372  		// When
   373  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   374  
   375  		// Then
   376  		assert.NoError(t, err)
   377  		// restores current status from default
   378  		assert.Equal(t, StatusActive, internalEA.GetEvalStatus(op.Avs))
   379  		// restores original status from current, which is restored from Avs
   380  		assert.Equal(t, current, internalEA.GetOriginalEvalStatus(op.Avs))
   381  	})
   382  
   383  	t.Run("reset from invalid avs fields (avs == current, original invalid)", func(t *testing.T) {
   384  		delegator, op := newDelOpsParams(params)
   385  
   386  		instanceStatus := "invalidField"
   387  		server.Evaluations.BasicEvals[params.internalMonitor.Id].Status = instanceStatus
   388  
   389  		current, _ := setOpAvsStatus(&op, StatusInactive, instanceStatus)
   390  
   391  		// When
   392  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   393  
   394  		// Then
   395  		assert.NoError(t, err)
   396  		// restores current status from default
   397  		assert.Equal(t, StatusActive, internalEA.GetEvalStatus(op.Avs))
   398  		// restores original status from current, which is restored from Avs
   399  		assert.Equal(t, current, internalEA.GetOriginalEvalStatus(op.Avs))
   400  	})
   401  
   402  	t.Run("reset from invalid avs fields (avs != current, current invalid)", func(t *testing.T) {
   403  		delegator, op := newDelOpsParams(params)
   404  
   405  		instanceStatus := "invalidField"
   406  		server.Evaluations.BasicEvals[params.internalMonitor.Id].Status = instanceStatus
   407  
   408  		_, original := setOpAvsStatus(&op, "invalid", StatusInactive)
   409  
   410  		// When
   411  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   412  
   413  		// Then
   414  		assert.NoError(t, err)
   415  		assert.Equal(t, original, internalEA.GetEvalStatus(op.Avs))
   416  		// don't push junk data into original, leave as is
   417  		assert.Equal(t, original, internalEA.GetOriginalEvalStatus(op.Avs))
   418  	})
   419  
   420  	t.Run("reset from invalid avs fields (avs == current, current invalid)", func(t *testing.T) {
   421  		delegator, op := newDelOpsParams(params)
   422  
   423  		instanceStatus := "invalidField"
   424  		server.Evaluations.BasicEvals[params.internalMonitor.Id].Status = instanceStatus
   425  
   426  		_, original := setOpAvsStatus(&op, instanceStatus, StatusInactive)
   427  
   428  		// When
   429  		err := delegator.ResetStatus(logger, &op.Avs, internalEA)
   430  
   431  		// Then
   432  		assert.NoError(t, err)
   433  		assert.Equal(t, original, internalEA.GetEvalStatus(op.Avs))
   434  		// don't push junk data into original, leave as is
   435  		assert.Equal(t, original, internalEA.GetOriginalEvalStatus(op.Avs))
   436  	})
   437  }