github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/driver/postsql/operation_test.go (about)

     1  package postsql_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/kyma-project/kyma-environment-broker/common/orchestration"
    10  	"github.com/kyma-project/kyma-environment-broker/internal"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/events"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
    14  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    15  	"github.com/kyma-project/kyma-environment-broker/internal/storage/dbmodel"
    16  	"github.com/pivotal-cf/brokerapi/v8/domain"
    17  	"github.com/sirupsen/logrus"
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestOperation(t *testing.T) {
    23  
    24  	ctx := context.Background()
    25  
    26  	t.Run("Operations - provisioning and deprovisioning", func(t *testing.T) {
    27  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
    28  		require.NoError(t, err)
    29  		defer containerCleanupFunc()
    30  
    31  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
    32  		require.NoError(t, err)
    33  		defer tablesCleanupFunc()
    34  
    35  		cipher := storage.NewEncrypter(cfg.SecretKey)
    36  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
    37  		require.NoError(t, err)
    38  		require.NotNil(t, brokerStorage)
    39  
    40  		orchestrationID := "orch-id"
    41  
    42  		givenOperation := fixture.FixOperation("operation-id", "inst-id", internal.OperationTypeProvision)
    43  		givenOperation.InputCreator = nil
    44  		givenOperation.State = domain.InProgress
    45  		givenOperation.CreatedAt = givenOperation.CreatedAt.Truncate(time.Millisecond)
    46  		givenOperation.UpdatedAt = givenOperation.UpdatedAt.Truncate(time.Millisecond)
    47  		givenOperation.Version = 1
    48  		givenOperation.OrchestrationID = orchestrationID
    49  		givenOperation.ProvisioningParameters.PlanID = broker.TrialPlanID
    50  
    51  		latestOperation := fixture.FixOperation("latest-id", "inst-id", internal.OperationTypeDeprovision)
    52  		latestOperation.InputCreator = nil
    53  		latestOperation.State = domain.InProgress
    54  		latestOperation.CreatedAt = latestOperation.CreatedAt.Truncate(time.Millisecond).Add(time.Minute)
    55  		latestOperation.UpdatedAt = latestOperation.UpdatedAt.Truncate(time.Millisecond).Add(2 * time.Minute)
    56  		latestOperation.Version = 1
    57  		latestOperation.OrchestrationID = orchestrationID
    58  		latestOperation.ProvisioningParameters.PlanID = broker.TrialPlanID
    59  
    60  		latestPendingOperation := fixture.FixOperation("latest-id-pending", "inst-id", internal.OperationTypeProvision)
    61  		latestPendingOperation.InputCreator = nil
    62  		latestPendingOperation.State = orchestration.Pending
    63  		latestPendingOperation.CreatedAt = latestPendingOperation.CreatedAt.Truncate(time.Millisecond).Add(2 * time.Minute)
    64  		latestPendingOperation.UpdatedAt = latestPendingOperation.UpdatedAt.Truncate(time.Millisecond).Add(3 * time.Minute)
    65  		latestPendingOperation.Version = 1
    66  		latestPendingOperation.OrchestrationID = orchestrationID
    67  		latestPendingOperation.ProvisioningParameters.PlanID = broker.TrialPlanID
    68  
    69  		err = brokerStorage.Orchestrations().Insert(internal.Orchestration{OrchestrationID: orchestrationID})
    70  		require.NoError(t, err)
    71  
    72  		svc := brokerStorage.Operations()
    73  
    74  		// when
    75  		err = svc.InsertOperation(givenOperation)
    76  		require.NoError(t, err)
    77  		err = svc.InsertOperation(latestOperation)
    78  		require.NoError(t, err)
    79  		err = svc.InsertOperation(latestPendingOperation)
    80  		require.NoError(t, err)
    81  
    82  		provisionOps, err := svc.GetNotFinishedOperationsByType(internal.OperationTypeProvision)
    83  		require.NoError(t, err)
    84  		assert.Len(t, provisionOps, 2)
    85  		assertOperation(t, givenOperation, provisionOps[0])
    86  
    87  		deprovisionOps, err := svc.GetNotFinishedOperationsByType(internal.OperationTypeDeprovision)
    88  		require.NoError(t, err)
    89  		assert.Len(t, deprovisionOps, 1)
    90  		assertOperation(t, latestOperation, deprovisionOps[0])
    91  
    92  		gotOperation, err := svc.GetOperationByID("operation-id")
    93  		require.NoError(t, err)
    94  		assert.Equal(t, givenOperation.ID, gotOperation.ID)
    95  
    96  		lastOp, err := svc.GetLastOperation("inst-id")
    97  		require.NoError(t, err)
    98  		assert.Equal(t, latestOperation.ID, lastOp.ID)
    99  
   100  		latestOp, err := svc.GetOperationByInstanceID("inst-id")
   101  		require.NoError(t, err)
   102  		assert.Equal(t, latestPendingOperation.ID, latestOp.ID)
   103  
   104  		// when
   105  		opList, err := svc.ListOperationsByInstanceID("inst-id")
   106  
   107  		// then
   108  		require.NoError(t, err)
   109  		assert.Equal(t, 3, len(opList))
   110  
   111  		// when
   112  		_, _, totalCount, err := svc.ListOperationsByOrchestrationID(orchestrationID, dbmodel.OperationFilter{PageSize: 10, Page: 1})
   113  
   114  		// then
   115  		require.NoError(t, err)
   116  		assert.Equal(t, 3, totalCount)
   117  		assertOperation(t, givenOperation, *gotOperation)
   118  
   119  		assertUpdateDescription(t, gotOperation, svc)
   120  
   121  		assertUpdateState(t, svc, orchestrationID, latestOp)
   122  
   123  		assertEmptyResultForNonExistingIds(t, svc)
   124  	})
   125  
   126  	t.Run("Provisioning", func(t *testing.T) {
   127  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
   128  		require.NoError(t, err)
   129  		defer containerCleanupFunc()
   130  
   131  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
   132  		require.NoError(t, err)
   133  		defer tablesCleanupFunc()
   134  
   135  		cipher := storage.NewEncrypter(cfg.SecretKey)
   136  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
   137  		require.NoError(t, err)
   138  		require.NotNil(t, brokerStorage)
   139  
   140  		orchestrationID := "orch-id"
   141  
   142  		givenOperation := fixture.FixProvisioningOperation("operation-id", "inst-id")
   143  		givenOperation.InputCreator = nil
   144  		givenOperation.State = domain.InProgress
   145  		givenOperation.CreatedAt = givenOperation.CreatedAt.Truncate(time.Millisecond)
   146  		givenOperation.UpdatedAt = givenOperation.UpdatedAt.Truncate(time.Millisecond)
   147  		givenOperation.Version = 1
   148  		givenOperation.OrchestrationID = orchestrationID
   149  		givenOperation.ProvisioningParameters.PlanID = broker.TrialPlanID
   150  
   151  		latestOperation := fixture.FixProvisioningOperation("latest-id", "inst-id")
   152  		latestOperation.InputCreator = nil
   153  		latestOperation.State = domain.InProgress
   154  		latestOperation.CreatedAt = latestOperation.CreatedAt.Truncate(time.Millisecond).Add(time.Minute)
   155  		latestOperation.UpdatedAt = latestOperation.UpdatedAt.Truncate(time.Millisecond).Add(2 * time.Minute)
   156  		latestOperation.Version = 1
   157  		latestOperation.OrchestrationID = orchestrationID
   158  		latestOperation.ProvisioningParameters.PlanID = broker.TrialPlanID
   159  
   160  		latestPendingOperation := fixture.FixProvisioningOperation("latest-id-pending", "inst-id")
   161  		latestPendingOperation.InputCreator = nil
   162  		latestPendingOperation.State = orchestration.Pending
   163  		latestPendingOperation.CreatedAt = latestPendingOperation.CreatedAt.Truncate(time.Millisecond).Add(2 * time.Minute)
   164  		latestPendingOperation.UpdatedAt = latestPendingOperation.UpdatedAt.Truncate(time.Millisecond).Add(3 * time.Minute)
   165  		latestPendingOperation.Version = 1
   166  		latestPendingOperation.OrchestrationID = orchestrationID
   167  		latestPendingOperation.ProvisioningParameters.PlanID = broker.TrialPlanID
   168  
   169  		err = brokerStorage.Orchestrations().Insert(internal.Orchestration{OrchestrationID: orchestrationID})
   170  		require.NoError(t, err)
   171  
   172  		svc := brokerStorage.Operations()
   173  
   174  		// when
   175  		err = svc.InsertOperation(givenOperation)
   176  		require.NoError(t, err)
   177  		err = svc.InsertOperation(latestOperation)
   178  		require.NoError(t, err)
   179  		err = svc.InsertOperation(latestPendingOperation)
   180  		require.NoError(t, err)
   181  
   182  		ops, err := svc.GetNotFinishedOperationsByType(internal.OperationTypeProvision)
   183  		require.NoError(t, err)
   184  		assert.Len(t, ops, 3)
   185  		assertOperation(t, givenOperation, ops[0])
   186  
   187  		gotOperation, err := svc.GetProvisioningOperationByID("operation-id")
   188  		require.NoError(t, err)
   189  
   190  		op, err := svc.GetOperationByID("operation-id")
   191  		require.NoError(t, err)
   192  		assert.Equal(t, givenOperation.ID, op.ID)
   193  
   194  		lastOp, err := svc.GetLastOperation("inst-id")
   195  		require.NoError(t, err)
   196  		assert.Equal(t, latestOperation.ID, lastOp.ID)
   197  
   198  		// then
   199  		assertOperation(t, givenOperation, gotOperation.Operation)
   200  
   201  		// when
   202  		gotOperation.Description = "new modified description"
   203  		_, err = svc.UpdateProvisioningOperation(*gotOperation)
   204  		require.NoError(t, err)
   205  
   206  		// then
   207  		gotOperation2, err := svc.GetProvisioningOperationByID("operation-id")
   208  		require.NoError(t, err)
   209  
   210  		assert.Equal(t, "new modified description", gotOperation2.Description)
   211  
   212  		// when
   213  		stats, err := svc.GetOperationStatsByPlan()
   214  		require.NoError(t, err)
   215  
   216  		assert.Equal(t, 2, stats[broker.TrialPlanID].Provisioning[domain.InProgress])
   217  
   218  		opStats, err := svc.GetOperationStatsForOrchestration(orchestrationID)
   219  		require.NoError(t, err)
   220  
   221  		assert.Equal(t, 2, opStats[orchestration.InProgress])
   222  
   223  		// when
   224  		opList, err := svc.ListProvisioningOperationsByInstanceID("inst-id")
   225  		// then
   226  		require.NoError(t, err)
   227  		assert.Equal(t, 3, len(opList))
   228  	})
   229  
   230  	t.Run("Deprovisioning", func(t *testing.T) {
   231  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
   232  		require.NoError(t, err)
   233  		defer containerCleanupFunc()
   234  
   235  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
   236  		require.NoError(t, err)
   237  		defer tablesCleanupFunc()
   238  
   239  		cipher := storage.NewEncrypter(cfg.SecretKey)
   240  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
   241  		require.NoError(t, err)
   242  		require.NotNil(t, brokerStorage)
   243  
   244  		givenOperation := fixture.FixDeprovisioningOperation("operation-id", "inst-id")
   245  		givenOperation.State = domain.InProgress
   246  		givenOperation.CreatedAt = time.Now().Truncate(time.Millisecond)
   247  		givenOperation.UpdatedAt = time.Now().Truncate(time.Millisecond).Add(time.Second)
   248  		givenOperation.ProvisionerOperationID = "target-op-id"
   249  		givenOperation.Description = "description"
   250  		givenOperation.Version = 1
   251  
   252  		svc := brokerStorage.Operations()
   253  
   254  		// when
   255  		err = svc.InsertDeprovisioningOperation(givenOperation)
   256  		require.NoError(t, err)
   257  
   258  		ops, err := svc.GetNotFinishedOperationsByType(internal.OperationTypeDeprovision)
   259  		require.NoError(t, err)
   260  		assert.Len(t, ops, 1)
   261  		assertOperation(t, givenOperation.Operation, ops[0])
   262  
   263  		gotOperation, err := svc.GetDeprovisioningOperationByID("operation-id")
   264  		require.NoError(t, err)
   265  
   266  		op, err := svc.GetOperationByID("operation-id")
   267  		require.NoError(t, err)
   268  		assert.Equal(t, givenOperation.Operation.ID, op.ID)
   269  
   270  		// then
   271  		assertDeprovisioningOperation(t, givenOperation, *gotOperation)
   272  
   273  		// when
   274  		gotOperation.Description = "new modified description"
   275  		_, err = svc.UpdateDeprovisioningOperation(*gotOperation)
   276  		require.NoError(t, err)
   277  
   278  		// then
   279  		gotOperation2, err := svc.GetDeprovisioningOperationByID("operation-id")
   280  		require.NoError(t, err)
   281  
   282  		assert.Equal(t, "new modified description", gotOperation2.Description)
   283  
   284  		// given
   285  		err = svc.InsertDeprovisioningOperation(internal.DeprovisioningOperation{
   286  			Operation: internal.Operation{
   287  				ID:         "other-op-id",
   288  				InstanceID: "inst-id",
   289  				CreatedAt:  time.Now().Add(1 * time.Hour),
   290  				UpdatedAt:  time.Now().Add(1 * time.Hour),
   291  			},
   292  		})
   293  		require.NoError(t, err)
   294  		// when
   295  		opList, err := svc.ListDeprovisioningOperationsByInstanceID("inst-id")
   296  		// then
   297  		require.NoError(t, err)
   298  		assert.Equal(t, 2, len(opList))
   299  	})
   300  
   301  	t.Run("Upgrade Kyma", func(t *testing.T) {
   302  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
   303  		require.NoError(t, err)
   304  		defer containerCleanupFunc()
   305  
   306  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
   307  		require.NoError(t, err)
   308  		defer tablesCleanupFunc()
   309  
   310  		cipher := storage.NewEncrypter(cfg.SecretKey)
   311  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
   312  		require.NoError(t, err)
   313  		require.NotNil(t, brokerStorage)
   314  
   315  		orchestrationID := "orchestration-id"
   316  
   317  		givenOperation1 := fixture.FixUpgradeKymaOperation("operation-id-1", "inst-id")
   318  		givenOperation1.State = domain.InProgress
   319  		givenOperation1.CreatedAt = time.Now().Truncate(time.Millisecond)
   320  		givenOperation1.UpdatedAt = time.Now().Truncate(time.Millisecond).Add(time.Second)
   321  		givenOperation1.ProvisionerOperationID = "target-op-id"
   322  		givenOperation1.Description = "description"
   323  		givenOperation1.OrchestrationID = orchestrationID
   324  		givenOperation1.InputCreator = nil
   325  		givenOperation1.Version = 1
   326  
   327  		givenOperation2 := fixture.FixUpgradeKymaOperation("operation-id-2", "inst-id")
   328  		givenOperation2.State = domain.InProgress
   329  		givenOperation2.CreatedAt = time.Now().Truncate(time.Millisecond).Add(time.Minute)
   330  		givenOperation2.UpdatedAt = time.Now().Truncate(time.Millisecond).Add(time.Second).Add(time.Minute)
   331  		givenOperation2.ProvisionerOperationID = "target-op-id"
   332  		givenOperation2.Description = "description"
   333  		givenOperation2.OrchestrationID = orchestrationID
   334  		givenOperation2.RuntimeOperation = fixRuntimeOperation("operation-id-2")
   335  		givenOperation2.InputCreator = nil
   336  		givenOperation2.Version = 1
   337  
   338  		givenOperation3 := fixture.FixUpgradeKymaOperation("operation-id-3", "inst-id")
   339  		givenOperation3.State = orchestration.Pending
   340  		givenOperation3.CreatedAt = time.Now().Truncate(time.Millisecond).Add(2 * time.Hour)
   341  		givenOperation3.UpdatedAt = time.Now().Truncate(time.Millisecond).Add(2 * time.Hour).Add(10 * time.Minute)
   342  		givenOperation3.ProvisionerOperationID = "target-op-id"
   343  		givenOperation3.Description = "pending-operation"
   344  		givenOperation3.OrchestrationID = orchestrationID
   345  		givenOperation3.RuntimeOperation = fixRuntimeOperation("operation-id-3")
   346  		givenOperation3.InputCreator = nil
   347  		givenOperation3.Version = 1
   348  
   349  		svc := brokerStorage.Operations()
   350  
   351  		// when
   352  		err = svc.InsertUpgradeKymaOperation(givenOperation1)
   353  		require.NoError(t, err)
   354  		err = svc.InsertUpgradeKymaOperation(givenOperation2)
   355  		require.NoError(t, err)
   356  		err = svc.InsertUpgradeKymaOperation(givenOperation3)
   357  		require.NoError(t, err)
   358  
   359  		op, err := svc.GetUpgradeKymaOperationByInstanceID("inst-id")
   360  		require.NoError(t, err)
   361  
   362  		lastOp, err := svc.GetLastOperation("inst-id")
   363  		require.NoError(t, err)
   364  		assert.Equal(t, givenOperation2.Operation.ID, lastOp.ID)
   365  
   366  		assertUpgradeKymaOperation(t, givenOperation3, *op)
   367  
   368  		ops, count, totalCount, err := svc.ListUpgradeKymaOperationsByOrchestrationID(orchestrationID, dbmodel.OperationFilter{PageSize: 10, Page: 1})
   369  		require.NoError(t, err)
   370  		assert.Len(t, ops, 3)
   371  		assert.Equal(t, count, 3)
   372  		assert.Equal(t, totalCount, 3)
   373  	})
   374  
   375  	t.Run("Upgrade Cluster", func(t *testing.T) {
   376  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
   377  		require.NoError(t, err)
   378  		defer containerCleanupFunc()
   379  
   380  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
   381  		require.NoError(t, err)
   382  		defer tablesCleanupFunc()
   383  
   384  		cipher := storage.NewEncrypter(cfg.SecretKey)
   385  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
   386  		require.NoError(t, err)
   387  		require.NotNil(t, brokerStorage)
   388  
   389  		orchestrationID := "orchestration-id"
   390  
   391  		givenOperation1 := internal.UpgradeClusterOperation{
   392  			Operation: fixture.FixOperation("operation-id-1", "inst-id", internal.OperationTypeUpgradeCluster),
   393  		}
   394  		givenOperation1.State = domain.InProgress
   395  		givenOperation1.CreatedAt = givenOperation1.CreatedAt.Truncate(time.Millisecond)
   396  		givenOperation1.UpdatedAt = givenOperation1.UpdatedAt.Truncate(time.Millisecond).Add(time.Second)
   397  		givenOperation1.ProvisionerOperationID = "target-op-id"
   398  		givenOperation1.Description = "description"
   399  		givenOperation1.Version = 1
   400  		givenOperation1.OrchestrationID = orchestrationID
   401  
   402  		givenOperation2 := internal.UpgradeClusterOperation{
   403  			Operation: fixture.FixOperation("operation-id-2", "inst-id", internal.OperationTypeUpgradeCluster),
   404  		}
   405  		givenOperation2.State = domain.InProgress
   406  		givenOperation2.CreatedAt = givenOperation2.CreatedAt.Truncate(time.Millisecond).Add(time.Minute)
   407  		givenOperation2.UpdatedAt = givenOperation2.UpdatedAt.Truncate(time.Millisecond).Add(time.Minute).Add(time.Second)
   408  		givenOperation2.ProvisionerOperationID = "target-op-id"
   409  		givenOperation2.Description = "description"
   410  		givenOperation2.Version = 1
   411  		givenOperation2.OrchestrationID = orchestrationID
   412  		givenOperation2.RuntimeOperation = fixRuntimeOperation("operation-id-2")
   413  
   414  		givenOperation3 := internal.UpgradeClusterOperation{
   415  			Operation: fixture.FixOperation("operation-id-3", "inst-id", internal.OperationTypeUpgradeCluster),
   416  		}
   417  		givenOperation3.State = orchestration.Pending
   418  		givenOperation3.CreatedAt = givenOperation3.CreatedAt.Truncate(time.Millisecond).Add(2 * time.Hour)
   419  		givenOperation3.UpdatedAt = givenOperation3.UpdatedAt.Truncate(time.Millisecond).Add(2 * time.Hour).Add(10 * time.Minute)
   420  		givenOperation3.ProvisionerOperationID = "target-op-id"
   421  		givenOperation3.Description = "pending-operation"
   422  		givenOperation3.Version = 1
   423  		givenOperation3.OrchestrationID = orchestrationID
   424  		givenOperation3.RuntimeOperation = fixRuntimeOperation("operation-id-3")
   425  
   426  		svc := brokerStorage.Operations()
   427  
   428  		// when
   429  		err = svc.InsertUpgradeClusterOperation(givenOperation1)
   430  		require.NoError(t, err)
   431  		err = svc.InsertUpgradeClusterOperation(givenOperation2)
   432  		require.NoError(t, err)
   433  		err = svc.InsertUpgradeClusterOperation(givenOperation3)
   434  		require.NoError(t, err)
   435  
   436  		// then
   437  		op, err := svc.GetUpgradeClusterOperationByID(givenOperation3.Operation.ID)
   438  		require.NoError(t, err)
   439  		assertUpgradeClusterOperation(t, givenOperation3, *op)
   440  
   441  		lastOp, err := svc.GetLastOperation("inst-id")
   442  		require.NoError(t, err)
   443  		assert.Equal(t, givenOperation2.Operation.ID, lastOp.ID)
   444  
   445  		ops, count, totalCount, err := svc.ListUpgradeClusterOperationsByOrchestrationID(orchestrationID, dbmodel.OperationFilter{PageSize: 10, Page: 1})
   446  		require.NoError(t, err)
   447  		assert.Len(t, ops, 3)
   448  		assert.Equal(t, count, 3)
   449  		assert.Equal(t, totalCount, 3)
   450  
   451  		ops, err = svc.ListUpgradeClusterOperationsByInstanceID("inst-id")
   452  		require.NoError(t, err)
   453  		assert.Len(t, ops, 3)
   454  
   455  		// when
   456  		givenOperation3.Description = "diff"
   457  		givenOperation3.ProvisionerOperationID = "modified-op-id"
   458  		op, err = svc.UpdateUpgradeClusterOperation(givenOperation3)
   459  		op.CreatedAt = op.CreatedAt.Truncate(time.Millisecond)
   460  		op.MaintenanceWindowBegin = op.MaintenanceWindowBegin.Truncate(time.Millisecond)
   461  		op.MaintenanceWindowEnd = op.MaintenanceWindowEnd.Truncate(time.Millisecond)
   462  
   463  		// then
   464  		got, err := svc.GetUpgradeClusterOperationByID(givenOperation3.Operation.ID)
   465  		require.NoError(t, err)
   466  		assertUpgradeClusterOperation(t, *op, *got)
   467  	})
   468  }
   469  
   470  func assertUpdateState(t *testing.T, svc storage.Operations, orchestrationID string, latestOp *internal.Operation) {
   471  	// when
   472  	stats, err := svc.GetOperationStatsByPlan()
   473  	require.NoError(t, err)
   474  
   475  	assert.Equal(t, 1, stats[broker.TrialPlanID].Provisioning[domain.InProgress])
   476  
   477  	opStats, err := svc.GetOperationStatsForOrchestration(orchestrationID)
   478  	require.NoError(t, err)
   479  
   480  	// then
   481  	assert.Equal(t, 2, opStats[orchestration.InProgress])
   482  
   483  	// when
   484  	latestOp.State = domain.InProgress
   485  	_, err = svc.UpdateOperation(*latestOp)
   486  	opStats, err = svc.GetOperationStatsForOrchestration(orchestrationID)
   487  	require.NoError(t, err)
   488  
   489  	// then
   490  	assert.Equal(t, 3, opStats[orchestration.InProgress])
   491  }
   492  
   493  func assertUpdateDescription(t *testing.T, gotOperation *internal.Operation, svc storage.Operations) {
   494  	// when
   495  	gotOperation.Description = "new modified description"
   496  	_, err := svc.UpdateOperation(*gotOperation)
   497  	require.NoError(t, err)
   498  
   499  	// then
   500  	gotOperation2, err := svc.GetOperationByID("operation-id")
   501  	require.NoError(t, err)
   502  
   503  	assert.Equal(t, "new modified description", gotOperation2.Description)
   504  }
   505  
   506  func assertEmptyResultForNonExistingIds(t *testing.T, svc storage.Operations) {
   507  	// when
   508  	opList, err := svc.ListOperationsByInstanceID("non-existing-inst-id")
   509  
   510  	// then
   511  	require.NoError(t, err)
   512  	assert.Equal(t, 0, len(opList))
   513  
   514  	// when
   515  	_, _, totalCount, err := svc.ListOperationsByOrchestrationID("non-existing-orchestration-id", dbmodel.OperationFilter{PageSize: 10, Page: 1})
   516  
   517  	// then
   518  	require.NoError(t, err)
   519  	assert.Equal(t, 0, totalCount)
   520  
   521  	_, err = svc.GetOperationByID("non-existing-operation-id")
   522  	require.Error(t, err, "Operation with instance_id inst-id not exist")
   523  
   524  	_, err = svc.GetLastOperation("non-existing-inst-id")
   525  	require.Error(t, err, "Operation with instance_id inst-id not exist")
   526  
   527  	_, err = svc.GetOperationByInstanceID("non-existing-inst-id")
   528  	require.Error(t, err, "operation does not exist")
   529  }
   530  
   531  func assertProvisioningOperation(t *testing.T, expected, got internal.ProvisioningOperation) {
   532  	// do not check zones and monothonic clock, see: https://golang.org/pkg/time/#Time
   533  	assert.True(t, expected.CreatedAt.Equal(got.CreatedAt), fmt.Sprintf("Expected %s got %s", expected.CreatedAt, got.CreatedAt))
   534  	assert.Equal(t, expected.ProvisioningParameters, got.ProvisioningParameters)
   535  	assert.Equal(t, expected.InstanceDetails, got.InstanceDetails)
   536  
   537  	expected.CreatedAt = got.CreatedAt
   538  	expected.UpdatedAt = got.UpdatedAt
   539  	expected.ProvisioningParameters = got.ProvisioningParameters
   540  	expected.FinishedStages = got.FinishedStages
   541  
   542  	assert.Equal(t, expected, got)
   543  }
   544  
   545  func assertDeprovisioningOperation(t *testing.T, expected, got internal.DeprovisioningOperation) {
   546  	// do not check zones and monothonic clock, see: https://golang.org/pkg/time/#Time
   547  	assert.True(t, expected.CreatedAt.Equal(got.CreatedAt), fmt.Sprintf("Expected %s got %s", expected.CreatedAt, got.CreatedAt))
   548  	assert.Equal(t, expected.InstanceDetails, got.InstanceDetails)
   549  
   550  	expected.CreatedAt = got.CreatedAt
   551  	expected.UpdatedAt = got.UpdatedAt
   552  	expected.FinishedStages = got.FinishedStages
   553  
   554  	assert.Equal(t, expected, got)
   555  }
   556  
   557  func assertUpgradeKymaOperation(t *testing.T, expected, got internal.UpgradeKymaOperation) {
   558  	// do not check zones and monothonic clock, see: https://golang.org/pkg/time/#Time
   559  	assert.True(t, expected.CreatedAt.Equal(got.CreatedAt), fmt.Sprintf("Expected %s got %s", expected.CreatedAt, got.CreatedAt))
   560  	assert.True(t, expected.MaintenanceWindowBegin.Equal(got.MaintenanceWindowBegin))
   561  	assert.True(t, expected.MaintenanceWindowEnd.Equal(got.MaintenanceWindowEnd))
   562  	assert.Equal(t, expected.InstanceDetails, got.InstanceDetails)
   563  
   564  	expected.CreatedAt = got.CreatedAt
   565  	expected.UpdatedAt = got.UpdatedAt
   566  	expected.MaintenanceWindowBegin = got.MaintenanceWindowBegin
   567  	expected.MaintenanceWindowEnd = got.MaintenanceWindowEnd
   568  	expected.FinishedStages = got.FinishedStages
   569  
   570  	assert.Equal(t, expected, got)
   571  }
   572  
   573  func assertUpgradeClusterOperation(t *testing.T, expected, got internal.UpgradeClusterOperation) {
   574  	// do not check zones and monothonic clock, see: https://golang.org/pkg/time/#Time
   575  	assert.True(t, expected.CreatedAt.Equal(got.CreatedAt), fmt.Sprintf("Expected %s got %s", expected.CreatedAt, got.CreatedAt))
   576  	assert.True(t, expected.MaintenanceWindowBegin.Equal(got.MaintenanceWindowBegin))
   577  	assert.True(t, expected.MaintenanceWindowEnd.Equal(got.MaintenanceWindowEnd))
   578  	assert.Equal(t, expected.InstanceDetails, got.InstanceDetails)
   579  
   580  	expected.CreatedAt = got.CreatedAt
   581  	expected.UpdatedAt = got.UpdatedAt
   582  	expected.MaintenanceWindowBegin = got.MaintenanceWindowBegin
   583  	expected.MaintenanceWindowEnd = got.MaintenanceWindowEnd
   584  	expected.FinishedStages = got.FinishedStages
   585  
   586  	assert.Equal(t, expected, got)
   587  }
   588  
   589  func assertOperation(t *testing.T, expected, got internal.Operation) {
   590  	// do not check zones and monothonic clock, see: https://golang.org/pkg/time/#Time
   591  	assert.True(t, expected.CreatedAt.Equal(got.CreatedAt), fmt.Sprintf("Expected %s got %s", expected.CreatedAt, got.CreatedAt))
   592  	assert.Equal(t, expected.InstanceDetails, got.InstanceDetails)
   593  
   594  	expected.CreatedAt = got.CreatedAt
   595  	expected.UpdatedAt = got.UpdatedAt
   596  	expected.FinishedStages = got.FinishedStages
   597  
   598  	assert.Equal(t, expected, got)
   599  }