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

     1  package postsql_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
     9  	"github.com/kyma-project/kyma-environment-broker/internal"
    10  
    11  	"github.com/google/uuid"
    12  	reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/events"
    14  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
    15  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    16  	"github.com/sirupsen/logrus"
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestRuntimeState(t *testing.T) {
    22  
    23  	ctx := context.Background()
    24  
    25  	t.Run("should insert and fetch RuntimeState", func(t *testing.T) {
    26  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
    27  		require.NoError(t, err)
    28  		defer containerCleanupFunc()
    29  
    30  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
    31  		require.NoError(t, err)
    32  		defer tablesCleanupFunc()
    33  
    34  		cipher := storage.NewEncrypter(cfg.SecretKey)
    35  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
    36  		require.NoError(t, err)
    37  		require.NotNil(t, brokerStorage)
    38  
    39  		fixID := "test"
    40  		givenRuntimeState := fixture.FixRuntimeState(fixID, fixID, fixID)
    41  		givenRuntimeState.KymaConfig.Version = fixID
    42  		givenRuntimeState.ClusterConfig.KubernetesVersion = fixID
    43  
    44  		svc := brokerStorage.RuntimeStates()
    45  
    46  		err = svc.Insert(givenRuntimeState)
    47  		require.NoError(t, err)
    48  
    49  		runtimeStates, err := svc.ListByRuntimeID(fixID)
    50  		require.NoError(t, err)
    51  		assert.Len(t, runtimeStates, 1)
    52  		assert.Equal(t, fixID, runtimeStates[0].KymaConfig.Version)
    53  		assert.Equal(t, fixID, runtimeStates[0].ClusterConfig.KubernetesVersion)
    54  
    55  		state, err := svc.GetByOperationID(fixID)
    56  		require.NoError(t, err)
    57  		assert.Equal(t, fixID, state.KymaConfig.Version)
    58  		assert.Equal(t, fixID, state.ClusterConfig.KubernetesVersion)
    59  	})
    60  
    61  	t.Run("should insert and fetch RuntimeState with Reconciler input", func(t *testing.T) {
    62  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
    63  		require.NoError(t, err)
    64  		defer containerCleanupFunc()
    65  
    66  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
    67  		require.NoError(t, err)
    68  		defer tablesCleanupFunc()
    69  
    70  		cipher := storage.NewEncrypter(cfg.SecretKey)
    71  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
    72  		require.NoError(t, err)
    73  		require.NotNil(t, brokerStorage)
    74  
    75  		fixRuntimeStateID := uuid.NewString()
    76  		fixRuntimeID := "runtimeID"
    77  		fixOperationID := "operationID"
    78  		givenRuntimeState := fixture.FixRuntimeState(fixRuntimeStateID, fixRuntimeID, fixOperationID)
    79  		fixClusterSetup := fixture.FixClusterSetup(fixRuntimeID)
    80  		givenRuntimeState.ClusterSetup = &fixClusterSetup
    81  
    82  		storage := brokerStorage.RuntimeStates()
    83  
    84  		err = storage.Insert(givenRuntimeState)
    85  		require.NoError(t, err)
    86  
    87  		runtimeStates, err := storage.ListByRuntimeID(fixRuntimeID)
    88  		require.NoError(t, err)
    89  		assert.Len(t, runtimeStates, 1)
    90  		assert.Equal(t, fixRuntimeStateID, runtimeStates[0].ID)
    91  		assert.Equal(t, fixRuntimeID, runtimeStates[0].ClusterSetup.RuntimeID)
    92  
    93  		const kymaVersion = "2.0.0"
    94  		assert.Equal(t, kymaVersion, runtimeStates[0].ClusterSetup.KymaConfig.Version)
    95  
    96  		state, err := storage.GetByOperationID(fixOperationID)
    97  		require.NoError(t, err)
    98  		assert.Equal(t, fixRuntimeStateID, state.ID)
    99  		assert.Equal(t, fixRuntimeID, state.ClusterSetup.RuntimeID)
   100  	})
   101  
   102  	t.Run("should distinguish between latest RuntimeStates with and without Reconciler input", func(t *testing.T) {
   103  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
   104  		require.NoError(t, err)
   105  		defer containerCleanupFunc()
   106  
   107  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
   108  		require.NoError(t, err)
   109  		defer tablesCleanupFunc()
   110  
   111  		cipher := storage.NewEncrypter(cfg.SecretKey)
   112  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
   113  		require.NoError(t, err)
   114  		require.NotNil(t, brokerStorage)
   115  
   116  		fixRuntimeID := "runtimeID"
   117  
   118  		fixRuntimeStateID1 := "runtimestate1"
   119  		fixOperationID1 := "operation1"
   120  		runtimeStateWithoutReconcilerInput1 := fixture.FixRuntimeState(fixRuntimeStateID1, fixRuntimeID, fixOperationID1)
   121  		runtimeStateWithoutReconcilerInput1.CreatedAt = runtimeStateWithoutReconcilerInput1.CreatedAt.Add(time.Hour * 2)
   122  
   123  		fixRuntimeStateID2 := "runtimestate2"
   124  		fixOperationID2 := "operation2"
   125  		runtimeStateWithReconcilerInput1 := fixture.FixRuntimeState(fixRuntimeStateID2, fixRuntimeID, fixOperationID2)
   126  		runtimeStateWithReconcilerInput1.CreatedAt = runtimeStateWithReconcilerInput1.CreatedAt.Add(time.Hour * 1)
   127  		runtimeStateWithReconcilerInput1.ClusterSetup = &reconcilerApi.Cluster{
   128  			RuntimeID: fixRuntimeID,
   129  		}
   130  
   131  		fixRuntimeStateID3 := "runtimestate3"
   132  		fixOperationID3 := "operation3"
   133  		runtimeStateWithoutReconcilerInput2 := fixture.FixRuntimeState(fixRuntimeStateID3, fixRuntimeID, fixOperationID3)
   134  
   135  		fixRuntimeStateID4 := "runtimestate4"
   136  		fixOperationID4 := "operation4"
   137  		runtimeStateWithReconcilerInput2 := fixture.FixRuntimeState(fixRuntimeStateID4, fixRuntimeID, fixOperationID4)
   138  		runtimeStateWithReconcilerInput2.ClusterSetup = &reconcilerApi.Cluster{
   139  			RuntimeID: fixRuntimeID,
   140  		}
   141  
   142  		storage := brokerStorage.RuntimeStates()
   143  
   144  		err = storage.Insert(runtimeStateWithoutReconcilerInput1)
   145  		require.NoError(t, err)
   146  		err = storage.Insert(runtimeStateWithReconcilerInput1)
   147  		require.NoError(t, err)
   148  		err = storage.Insert(runtimeStateWithoutReconcilerInput2)
   149  		require.NoError(t, err)
   150  		err = storage.Insert(runtimeStateWithReconcilerInput2)
   151  		require.NoError(t, err)
   152  
   153  		gotRuntimeStates, err := storage.ListByRuntimeID(fixRuntimeID)
   154  		require.NoError(t, err)
   155  		assert.Len(t, gotRuntimeStates, 4)
   156  
   157  		gotRuntimeState, err := storage.GetLatestByRuntimeID(fixRuntimeID)
   158  		require.NoError(t, err)
   159  		assert.Equal(t, gotRuntimeState.ID, runtimeStateWithoutReconcilerInput1.ID)
   160  		assert.Nil(t, gotRuntimeState.ClusterSetup)
   161  
   162  		gotRuntimeState, err = storage.GetLatestWithReconcilerInputByRuntimeID(fixRuntimeID)
   163  		require.NoError(t, err)
   164  		assert.Equal(t, gotRuntimeState.ID, runtimeStateWithReconcilerInput1.ID)
   165  		assert.NotNil(t, gotRuntimeState.ClusterSetup)
   166  		assert.Equal(t, gotRuntimeState.ClusterSetup.RuntimeID, runtimeStateWithReconcilerInput1.ClusterSetup.RuntimeID)
   167  	})
   168  
   169  	t.Run("should fetch latest RuntimeState with Kyma version", func(t *testing.T) {
   170  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
   171  		require.NoError(t, err)
   172  		defer containerCleanupFunc()
   173  
   174  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
   175  		require.NoError(t, err)
   176  		defer tablesCleanupFunc()
   177  
   178  		cipher := storage.NewEncrypter(cfg.SecretKey)
   179  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
   180  		require.NoError(t, err)
   181  		require.NotNil(t, brokerStorage)
   182  
   183  		fixRuntimeID := "runtimeID"
   184  		fixKymaVersion := "2.0.3"
   185  
   186  		fixRuntimeStateID1 := "runtimestate1"
   187  		fixOperationID1 := "operation1"
   188  		runtimeStateWithoutReconcilerInput := fixture.FixRuntimeState(fixRuntimeStateID1, fixRuntimeID, fixOperationID1)
   189  		runtimeStateWithoutReconcilerInput.CreatedAt = runtimeStateWithoutReconcilerInput.CreatedAt.Add(time.Hour * 2)
   190  
   191  		fixRuntimeStateID2 := "runtimestate2"
   192  		fixOperationID2 := "operation2"
   193  		runtimeStateWithReconcilerInput := fixture.FixRuntimeState(fixRuntimeStateID2, fixRuntimeID, fixOperationID2)
   194  		runtimeStateWithReconcilerInput.CreatedAt = runtimeStateWithReconcilerInput.CreatedAt.Add(time.Hour * 1)
   195  		runtimeStateWithReconcilerInput.ClusterSetup = &reconcilerApi.Cluster{
   196  			KymaConfig: reconcilerApi.KymaConfig{
   197  				Version: fixKymaVersion,
   198  			},
   199  			RuntimeID: fixRuntimeID,
   200  		}
   201  
   202  		//runtimeStateWithoutVersion := fixture.FixRuntimeState("fixRuntimeStateID3", fixRuntimeID, fixOperationID2)
   203  		runtimeStateWithoutVersion := internal.NewRuntimeState(fixRuntimeID, fixOperationID2, nil, &gqlschema.GardenerConfigInput{})
   204  		runtimeStateWithoutVersion.ID = "fixRuntimeStateID3"
   205  		runtimeStateWithoutVersion.CreatedAt = runtimeStateWithReconcilerInput.CreatedAt.Add(time.Hour * 3)
   206  
   207  		storage := brokerStorage.RuntimeStates()
   208  
   209  		err = storage.Insert(runtimeStateWithoutReconcilerInput)
   210  		require.NoError(t, err)
   211  		err = storage.Insert(runtimeStateWithReconcilerInput)
   212  		require.NoError(t, err)
   213  		err = storage.Insert(runtimeStateWithoutVersion)
   214  		require.NoError(t, err)
   215  
   216  		gotRuntimeStates, err := storage.ListByRuntimeID(fixRuntimeID)
   217  		require.NoError(t, err)
   218  		assert.Len(t, gotRuntimeStates, 3)
   219  
   220  		gotRuntimeState, err := storage.GetLatestByRuntimeID(fixRuntimeID)
   221  		require.NoError(t, err)
   222  		assert.Equal(t, runtimeStateWithoutVersion.ID, gotRuntimeState.ID)
   223  		assert.Nil(t, gotRuntimeState.ClusterSetup)
   224  
   225  		gotRuntimeState, err = storage.GetLatestWithKymaVersionByRuntimeID(fixRuntimeID)
   226  		require.NoError(t, err)
   227  		assert.Equal(t, gotRuntimeState.ID, runtimeStateWithReconcilerInput.ID)
   228  		assert.NotNil(t, gotRuntimeState.ClusterSetup)
   229  		assert.Equal(t, fixKymaVersion, gotRuntimeState.ClusterSetup.KymaConfig.Version)
   230  	})
   231  
   232  	t.Run("should fetch latest RuntimeState with Kyma version stored only in the kyma_version field", func(t *testing.T) {
   233  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_2")
   234  		require.NoError(t, err)
   235  		defer containerCleanupFunc()
   236  
   237  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
   238  		require.NoError(t, err)
   239  		defer tablesCleanupFunc()
   240  
   241  		cipher := storage.NewEncrypter(cfg.SecretKey)
   242  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
   243  		require.NoError(t, err)
   244  		require.NotNil(t, brokerStorage)
   245  
   246  		fixRuntimeID := "runtimeID"
   247  		fixKymaVersion := "2.0.3"
   248  
   249  		fixRuntimeStateID1 := "runtimestate1"
   250  		fixOperationID1 := "operation1"
   251  		runtimeStateWithoutReconcilerInput := fixture.FixRuntimeState(fixRuntimeStateID1, fixRuntimeID, fixOperationID1)
   252  		runtimeStateWithoutReconcilerInput.CreatedAt = runtimeStateWithoutReconcilerInput.CreatedAt.Add(time.Hour * 2)
   253  
   254  		fixRuntimeStateID2 := "runtimestate2"
   255  		fixOperationID2 := "operation2"
   256  		runtimeStateWithReconcilerInput := fixture.FixRuntimeState(fixRuntimeStateID2, fixRuntimeID, fixOperationID2)
   257  		runtimeStateWithReconcilerInput.CreatedAt = runtimeStateWithReconcilerInput.CreatedAt.Add(time.Hour * 1)
   258  		runtimeStateWithReconcilerInput.ClusterSetup = &reconcilerApi.Cluster{
   259  			KymaConfig: reconcilerApi.KymaConfig{
   260  				Version: fixKymaVersion,
   261  			},
   262  			RuntimeID: fixRuntimeID,
   263  		}
   264  
   265  		runtimeStatePlainVersion := internal.NewRuntimeState(fixRuntimeID, fixOperationID2, nil, &gqlschema.GardenerConfigInput{})
   266  		runtimeStatePlainVersion.ID = "fixRuntimeStateID3"
   267  		runtimeStatePlainVersion.CreatedAt = runtimeStateWithReconcilerInput.CreatedAt.Add(time.Hour * 3)
   268  		runtimeStatePlainVersion.KymaVersion = "2.1.55"
   269  
   270  		storage := brokerStorage.RuntimeStates()
   271  
   272  		err = storage.Insert(runtimeStateWithoutReconcilerInput)
   273  		require.NoError(t, err)
   274  		err = storage.Insert(runtimeStateWithReconcilerInput)
   275  		require.NoError(t, err)
   276  		err = storage.Insert(runtimeStatePlainVersion)
   277  		require.NoError(t, err)
   278  
   279  		gotRuntimeStates, err := storage.ListByRuntimeID(fixRuntimeID)
   280  		require.NoError(t, err)
   281  		assert.Len(t, gotRuntimeStates, 3)
   282  
   283  		gotRuntimeState, err := storage.GetLatestWithKymaVersionByRuntimeID(fixRuntimeID)
   284  		require.NoError(t, err)
   285  		assert.Equal(t, runtimeStatePlainVersion.ID, gotRuntimeState.ID)
   286  		assert.Equal(t, "2.1.55", gotRuntimeState.GetKymaVersion())
   287  	})
   288  
   289  	t.Run("should fetch latest RuntimeState with OIDC config", func(t *testing.T) {
   290  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
   291  		require.NoError(t, err)
   292  		defer containerCleanupFunc()
   293  
   294  		tablesCleanupFunc, err := storage.InitTestDBTables(t, cfg.ConnectionURL())
   295  		require.NoError(t, err)
   296  		defer tablesCleanupFunc()
   297  
   298  		cipher := storage.NewEncrypter(cfg.SecretKey)
   299  		brokerStorage, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
   300  		require.NoError(t, err)
   301  		require.NotNil(t, brokerStorage)
   302  
   303  		fixRuntimeID := "runtimeID"
   304  		fixKymaVersion := "2.0.4"
   305  		expectedOIDCConfig := gqlschema.OIDCConfigInput{
   306  			ClientID:       "clientID",
   307  			GroupsClaim:    "groups",
   308  			IssuerURL:      "https://issuer.url",
   309  			SigningAlgs:    []string{"RS256"},
   310  			UsernameClaim:  "sub",
   311  			UsernamePrefix: "-",
   312  		}
   313  
   314  		fixRuntimeStateID1 := "runtimestate1"
   315  		fixOperationID1 := "operation1"
   316  		runtimeStateWithOIDCConfig := fixture.FixRuntimeState(fixRuntimeStateID1, fixRuntimeID, fixOperationID1)
   317  		runtimeStateWithOIDCConfig.ClusterConfig.OidcConfig = &gqlschema.OIDCConfigInput{
   318  			ClientID:       expectedOIDCConfig.ClientID,
   319  			GroupsClaim:    expectedOIDCConfig.GroupsClaim,
   320  			IssuerURL:      expectedOIDCConfig.IssuerURL,
   321  			SigningAlgs:    expectedOIDCConfig.SigningAlgs,
   322  			UsernameClaim:  expectedOIDCConfig.UsernameClaim,
   323  			UsernamePrefix: expectedOIDCConfig.UsernamePrefix,
   324  		}
   325  		runtimeStateWithOIDCConfig.CreatedAt = runtimeStateWithOIDCConfig.CreatedAt.Add(time.Hour * 1)
   326  
   327  		fixRuntimeStateID2 := "runtimestate2"
   328  		fixOperationID2 := "operation2"
   329  		runtimeStateWithoutOIDCConfig := fixture.FixRuntimeState(fixRuntimeStateID2, fixRuntimeID, fixOperationID2)
   330  		runtimeStateWithoutOIDCConfig.CreatedAt = runtimeStateWithoutOIDCConfig.CreatedAt.Add(time.Hour * 2)
   331  		runtimeStateWithoutOIDCConfig.ClusterSetup = &reconcilerApi.Cluster{
   332  			KymaConfig: reconcilerApi.KymaConfig{
   333  				Version: fixKymaVersion,
   334  			},
   335  			RuntimeID: fixRuntimeID,
   336  		}
   337  
   338  		storage := brokerStorage.RuntimeStates()
   339  
   340  		err = storage.Insert(runtimeStateWithOIDCConfig)
   341  		require.NoError(t, err)
   342  		err = storage.Insert(runtimeStateWithoutOIDCConfig)
   343  		require.NoError(t, err)
   344  
   345  		gotRuntimeStates, err := storage.ListByRuntimeID(fixRuntimeID)
   346  		require.NoError(t, err)
   347  		assert.Len(t, gotRuntimeStates, 2)
   348  
   349  		gotRuntimeState, err := storage.GetLatestByRuntimeID(fixRuntimeID)
   350  		require.NoError(t, err)
   351  		assert.Equal(t, runtimeStateWithoutOIDCConfig.ID, gotRuntimeState.ID)
   352  		assert.NotNil(t, gotRuntimeState.ClusterSetup)
   353  
   354  		gotRuntimeState, err = storage.GetLatestWithOIDCConfigByRuntimeID(fixRuntimeID)
   355  		require.NoError(t, err)
   356  		assert.Equal(t, gotRuntimeState.ID, runtimeStateWithOIDCConfig.ID)
   357  		assert.Nil(t, gotRuntimeState.ClusterSetup)
   358  		assert.Equal(t, expectedOIDCConfig, *gotRuntimeState.ClusterConfig.OidcConfig)
   359  	})
   360  }