github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/cis/e2e/account_cleanup_test.go (about)

     1  //go:build cis
     2  // +build cis
     3  
     4  package e2e
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"github.com/kyma-project/kyma-environment-broker/internal/cis"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/logger"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/storage/driver/postsql/events"
    14  
    15  	"github.com/sirupsen/logrus"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  // go test --tags="cis" -v
    20  func TestSubAccountCleanup(t *testing.T) {
    21  	ctx := context.Background()
    22  
    23  	t.Log("ensure docker network")
    24  	cleanupNetwork, err := storage.EnsureTestNetworkForDB(t, ctx)
    25  	require.NoError(t, err)
    26  	defer cleanupNetwork()
    27  
    28  	t.Run("CIS 1.0", func(t *testing.T) {
    29  		// Given
    30  		instances := fixInstances()
    31  
    32  		t.Log("create image with postgres database")
    33  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t, ctx, "test_DB_1")
    34  		require.NoError(t, err)
    35  		defer containerCleanupFunc()
    36  
    37  		t.Log("initialize database by creating instances table")
    38  		err = initTestDBInstancesTables(t, cfg.ConnectionURL())
    39  		require.NoError(t, err)
    40  
    41  		t.Log("create storage manager")
    42  		cipher := storage.NewEncrypter(cfg.SecretKey)
    43  		storageManager, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
    44  		require.NoError(t, err)
    45  
    46  		t.Log("fill instances table")
    47  		for _, instance := range instances {
    48  			err := storageManager.Instances().Insert(instance)
    49  			require.NoError(t, err)
    50  		}
    51  
    52  		t.Log("create CIS fake server")
    53  		testServer := fixHTTPServer(t)
    54  		defer testServer.Close()
    55  
    56  		t.Log("create CIS client")
    57  		client := cis.NewClientVer1(context.TODO(), cis.Config{
    58  			EventServiceURL: testServer.URL,
    59  			PageSize:        "10",
    60  		}, logger.NewLogDummy())
    61  		client.SetHttpClient(testServer.Client())
    62  
    63  		t.Log("create broker client mock and assert execution deprovisioning for first 30 instances")
    64  		brokerClient := NewFakeBrokerClient(storageManager.Instances())
    65  
    66  		t.Log("create subaccount cleanup service")
    67  		sacs := cis.NewSubAccountCleanupService(client, brokerClient, storageManager.Instances(), logger.NewLogDummy())
    68  
    69  		// When
    70  		err = sacs.Run()
    71  
    72  		// Then
    73  		require.NoError(t, err)
    74  
    75  		amount, err := storageManager.Instances().GetNumberOfInstancesForGlobalAccountID(globalAccountID)
    76  		require.NoError(t, err)
    77  		require.Equal(t, 10, amount)
    78  	})
    79  
    80  	t.Run("CIS 2.0", func(t *testing.T) {
    81  		// Given
    82  		instances := fixInstances()
    83  
    84  		t.Log("create image with postgres database")
    85  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t, ctx, "test_DB_2")
    86  		require.NoError(t, err)
    87  		defer containerCleanupFunc()
    88  
    89  		t.Log("initialize database by creating instances table")
    90  		err = initTestDBInstancesTables(t, cfg.ConnectionURL())
    91  		require.NoError(t, err)
    92  
    93  		t.Log("create storage manager")
    94  		cipher := storage.NewEncrypter(cfg.SecretKey)
    95  		storageManager, _, err := storage.NewFromConfig(cfg, events.Config{}, cipher, logrus.StandardLogger())
    96  		require.NoError(t, err)
    97  
    98  		t.Log("fill instances table")
    99  		for _, instance := range instances {
   100  			err := storageManager.Instances().Insert(instance)
   101  			require.NoError(t, err)
   102  		}
   103  
   104  		t.Log("create CIS fake server")
   105  		testServer := fixHTTPServer(t)
   106  		defer testServer.Close()
   107  
   108  		t.Log("create CIS client")
   109  		client := cis.NewClient(context.TODO(), cis.Config{
   110  			EventServiceURL: testServer.URL,
   111  			PageSize:        "10",
   112  		}, logger.NewLogDummy())
   113  		client.SetHttpClient(testServer.Client())
   114  
   115  		t.Log("create broker client mock and assert execution deprovisioning for first 30 instances")
   116  		brokerClient := NewFakeBrokerClient(storageManager.Instances())
   117  
   118  		t.Log("create subaccount cleanup service")
   119  		sacs := cis.NewSubAccountCleanupService(client, brokerClient, storageManager.Instances(), logger.NewLogDummy())
   120  
   121  		// When
   122  		err = sacs.Run()
   123  
   124  		// Then
   125  		require.NoError(t, err)
   126  
   127  		amount, err := storageManager.Instances().GetNumberOfInstancesForGlobalAccountID(globalAccountID)
   128  		require.NoError(t, err)
   129  		require.Equal(t, 10, amount)
   130  	})
   131  }