github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/provisioning/apply_kyma_step_test.go (about)

     1  package provisioning
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/kyma-project/kyma-environment-broker/internal"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    13  	"github.com/sirupsen/logrus"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    17  	"k8s.io/apimachinery/pkg/runtime/schema"
    18  	"k8s.io/client-go/kubernetes/scheme"
    19  	"k8s.io/client-go/tools/clientcmd"
    20  	"sigs.k8s.io/controller-runtime/pkg/client"
    21  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    22  )
    23  
    24  /*
    25  Running tests with real K8S cluster instead of fake client.
    26  
    27  k3d cluster create
    28  
    29  kubectl create ns kyma-system
    30  
    31  kubectl apply -f https://raw.githubusercontent.com/kyma-project/lifecycle-manager/main/operator/config/crd/bases/operator.kyma-project.io_kymas.yaml
    32  
    33  k3d kubeconfig get --all > kubeconfig.yaml
    34  
    35  export KUBECONFIG=kubeconfig.yaml
    36  
    37  USE_KUBECONFIG=true go test -run TestCreatingKymaResource
    38  
    39  kubectl get kymas -o yaml -n kyma-system
    40  */
    41  
    42  func TestCreatingKymaResource(t *testing.T) {
    43  	// given
    44  	operation, cli := fixOperationForApplyKymaResource(t)
    45  	*operation.ProvisioningParameters.ErsContext.LicenseType = "CUSTOMER"
    46  	storage := storage.NewMemoryStorage()
    47  	storage.Operations().InsertOperation(operation)
    48  	svc := NewApplyKymaStep(storage.Operations(), cli)
    49  
    50  	// when
    51  	_, backoff, err := svc.Run(operation, logrus.New())
    52  
    53  	// then
    54  	require.NoError(t, err)
    55  	require.Zero(t, backoff)
    56  	aList := unstructured.UnstructuredList{}
    57  	aList.SetGroupVersionKind(schema.GroupVersionKind{Group: "operator.kyma-project.io", Version: "v1beta2", Kind: "KymaList"})
    58  
    59  	cli.List(context.Background(), &aList)
    60  	assert.Equal(t, 1, len(aList.Items))
    61  	assertLabelsExistsForExternalKymaResource(t, aList.Items[0])
    62  
    63  	svc.Run(operation, logrus.New())
    64  }
    65  
    66  func TestCreatingInternalKymaResource(t *testing.T) {
    67  	t.Run("With compass runtime ID", func(t *testing.T) {
    68  		// given
    69  		operation, cli := fixOperationForApplyKymaResource(t)
    70  		storage := storage.NewMemoryStorage()
    71  		storage.Operations().InsertOperation(operation)
    72  		svc := NewApplyKymaStep(storage.Operations(), cli)
    73  
    74  		// when
    75  		_, backoff, err := svc.Run(operation, logrus.New())
    76  
    77  		// then
    78  		require.NoError(t, err)
    79  		require.Zero(t, backoff)
    80  		aList := unstructured.UnstructuredList{}
    81  		aList.SetGroupVersionKind(schema.GroupVersionKind{Group: "operator.kyma-project.io", Version: "v1beta2", Kind: "KymaList"})
    82  
    83  		cli.List(context.Background(), &aList)
    84  		assert.Equal(t, 1, len(aList.Items))
    85  		assertLabelsExistsForInternalKymaResource(t, aList.Items[0])
    86  
    87  		assertCompassRuntimeIdAnnotationExists(t, aList.Items[0])
    88  		svc.Run(operation, logrus.New())
    89  	})
    90  
    91  	t.Run("Without compass runtime ID", func(t *testing.T) {
    92  		// given
    93  		operation, cli := fixOperationForApplyKymaResource(t)
    94  		operation.SetCompassRuntimeIdNotRegisteredByProvisioner()
    95  		storage := storage.NewMemoryStorage()
    96  		storage.Operations().InsertOperation(operation)
    97  		svc := NewApplyKymaStep(storage.Operations(), cli)
    98  
    99  		// when
   100  		_, backoff, err := svc.Run(operation, logrus.New())
   101  
   102  		// then
   103  		require.NoError(t, err)
   104  		require.Zero(t, backoff)
   105  		aList := unstructured.UnstructuredList{}
   106  		aList.SetGroupVersionKind(schema.GroupVersionKind{Group: "operator.kyma-project.io", Version: "v1beta2", Kind: "KymaList"})
   107  
   108  		cli.List(context.Background(), &aList)
   109  		assert.Equal(t, 1, len(aList.Items))
   110  		assertLabelsExistsForInternalKymaResource(t, aList.Items[0])
   111  
   112  		assertCompassRuntimeIdAnnotationNotExists(t, aList.Items[0])
   113  		svc.Run(operation, logrus.New())
   114  	})
   115  }
   116  
   117  func TestCreatingKymaResource_UseNamespaceFromTimeOfCreationNotTemplate(t *testing.T) {
   118  	// given
   119  	operation, cli := fixOperationForApplyKymaResource(t)
   120  	operation.KymaResourceNamespace = "namespace-in-time-of-creation"
   121  	*operation.ProvisioningParameters.ErsContext.LicenseType = "CUSTOMER"
   122  	storage := storage.NewMemoryStorage()
   123  	storage.Operations().InsertOperation(operation)
   124  	svc := NewApplyKymaStep(storage.Operations(), cli)
   125  
   126  	// when
   127  	_, backoff, err := svc.Run(operation, logrus.New())
   128  
   129  	// then
   130  	require.NoError(t, err)
   131  	require.Zero(t, backoff)
   132  	aList := unstructured.UnstructuredList{}
   133  	aList.SetGroupVersionKind(schema.GroupVersionKind{Group: "operator.kyma-project.io", Version: "v1beta2", Kind: "KymaList"})
   134  
   135  	cli.List(context.Background(), &aList)
   136  	assert.Equal(t, 1, len(aList.Items))
   137  	assertLabelsExistsForExternalKymaResource(t, aList.Items[0])
   138  
   139  	svc.Run(operation, logrus.New())
   140  	assert.Equal(t, "namespace-in-time-of-creation", operation.KymaResourceNamespace)
   141  }
   142  
   143  func TestCreatingInternalKymaResource_UseNamespaceFromTimeOfCreationNotTemplate(t *testing.T) {
   144  	// given
   145  	operation, cli := fixOperationForApplyKymaResource(t)
   146  	operation.KymaResourceNamespace = "namespace-in-time-of-creation"
   147  	storage := storage.NewMemoryStorage()
   148  	storage.Operations().InsertOperation(operation)
   149  	svc := NewApplyKymaStep(storage.Operations(), cli)
   150  
   151  	// when
   152  	_, backoff, err := svc.Run(operation, logrus.New())
   153  
   154  	// then
   155  	require.NoError(t, err)
   156  	require.Zero(t, backoff)
   157  	aList := unstructured.UnstructuredList{}
   158  	aList.SetGroupVersionKind(schema.GroupVersionKind{Group: "operator.kyma-project.io", Version: "v1beta2", Kind: "KymaList"})
   159  
   160  	cli.List(context.Background(), &aList)
   161  	assert.Equal(t, 1, len(aList.Items))
   162  	assertLabelsExistsForInternalKymaResource(t, aList.Items[0])
   163  
   164  	svc.Run(operation, logrus.New())
   165  	assert.Equal(t, "namespace-in-time-of-creation", operation.KymaResourceNamespace)
   166  }
   167  
   168  func TestUpdatinglKymaResourceIfExists(t *testing.T) {
   169  	// given
   170  	operation, cli := fixOperationForApplyKymaResource(t)
   171  	*operation.ProvisioningParameters.ErsContext.LicenseType = "CUSTOMER"
   172  	storage := storage.NewMemoryStorage()
   173  	storage.Operations().InsertOperation(operation)
   174  	svc := NewApplyKymaStep(storage.Operations(), cli)
   175  	err := cli.Create(context.Background(), &unstructured.Unstructured{Object: map[string]interface{}{
   176  		"apiVersion": "operator.kyma-project.io/v1beta2",
   177  		"kind":       "Kyma",
   178  		"metadata": map[string]interface{}{
   179  			"name":      operation.KymaResourceName,
   180  			"namespace": "kyma-system",
   181  		},
   182  		"spec": map[string]interface{}{
   183  			"channel": "stable",
   184  		},
   185  	}})
   186  	require.NoError(t, err)
   187  
   188  	// when
   189  	_, backoff, err := svc.Run(operation, logrus.New())
   190  
   191  	// then
   192  	require.NoError(t, err)
   193  	require.Zero(t, backoff)
   194  	aList := unstructured.UnstructuredList{}
   195  	aList.SetGroupVersionKind(schema.GroupVersionKind{Group: "operator.kyma-project.io", Version: "v1beta2", Kind: "KymaList"})
   196  
   197  	cli.List(context.Background(), &aList)
   198  	assert.Equal(t, 1, len(aList.Items))
   199  	assertLabelsExistsForExternalKymaResource(t, aList.Items[0])
   200  }
   201  
   202  func TestUpdatinInternalKymaResourceIfExists(t *testing.T) {
   203  	// given
   204  	operation, cli := fixOperationForApplyKymaResource(t)
   205  	storage := storage.NewMemoryStorage()
   206  	storage.Operations().InsertOperation(operation)
   207  	svc := NewApplyKymaStep(storage.Operations(), cli)
   208  	err := cli.Create(context.Background(), &unstructured.Unstructured{Object: map[string]interface{}{
   209  		"apiVersion": "operator.kyma-project.io/v1beta2",
   210  		"kind":       "Kyma",
   211  		"metadata": map[string]interface{}{
   212  			"name":      operation.KymaResourceName,
   213  			"namespace": "kyma-system",
   214  		},
   215  		"spec": map[string]interface{}{
   216  			"channel": "stable",
   217  		},
   218  	}})
   219  	require.NoError(t, err)
   220  
   221  	// when
   222  	_, backoff, err := svc.Run(operation, logrus.New())
   223  
   224  	// then
   225  	require.NoError(t, err)
   226  	require.Zero(t, backoff)
   227  	aList := unstructured.UnstructuredList{}
   228  	aList.SetGroupVersionKind(schema.GroupVersionKind{Group: "operator.kyma-project.io", Version: "v1beta2", Kind: "KymaList"})
   229  
   230  	cli.List(context.Background(), &aList)
   231  	assert.Equal(t, 1, len(aList.Items))
   232  	assertLabelsExistsForInternalKymaResource(t, aList.Items[0])
   233  }
   234  
   235  func assertLabelsExists(t *testing.T, obj unstructured.Unstructured) {
   236  	assert.Contains(t, obj.GetLabels(), "kyma-project.io/instance-id")
   237  	assert.Contains(t, obj.GetLabels(), "kyma-project.io/runtime-id")
   238  	assert.Contains(t, obj.GetLabels(), "kyma-project.io/global-account-id")
   239  }
   240  
   241  func assertLabelsExistsForInternalKymaResource(t *testing.T, obj unstructured.Unstructured) {
   242  	assert.Contains(t, obj.GetLabels(), "operator.kyma-project.io/internal")
   243  	assertLabelsExists(t, obj)
   244  }
   245  
   246  func assertCompassRuntimeIdAnnotationExists(t *testing.T, obj unstructured.Unstructured) {
   247  	t.Helper()
   248  	assert.Contains(t, obj.GetAnnotations(), "compass-runtime-id-for-migration")
   249  }
   250  
   251  func assertCompassRuntimeIdAnnotationNotExists(t *testing.T, obj unstructured.Unstructured) {
   252  	t.Helper()
   253  	assert.NotContains(t, obj.GetAnnotations(), "compass-runtime-id-for-migration")
   254  }
   255  
   256  func assertLabelsExistsForExternalKymaResource(t *testing.T, obj unstructured.Unstructured) {
   257  	assert.NotContains(t, obj.GetLabels(), "operator.kyma-project.io/internal")
   258  	assertLabelsExists(t, obj)
   259  }
   260  
   261  func fixOperationForApplyKymaResource(t *testing.T) (internal.Operation, client.Client) {
   262  	operation := fixture.FixOperation("op-id", "inst-id", internal.OperationTypeProvision)
   263  	operation.KymaTemplate = `
   264  apiVersion: operator.kyma-project.io/v1beta2
   265  kind: Kyma
   266  metadata:
   267      name: my-kyma
   268      namespace: kyma-system
   269  spec:
   270      sync:
   271          strategy: secret
   272      channel: stable
   273      modules: []
   274  `
   275  	var cli client.Client
   276  	if len(os.Getenv("KUBECONFIG")) > 0 && strings.ToLower(os.Getenv("USE_KUBECONFIG")) == "true" {
   277  		config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
   278  		if err != nil {
   279  			t.Fatal(err.Error())
   280  		}
   281  		// controller-runtime lib
   282  		scheme.Scheme.AddKnownTypeWithName(schema.GroupVersionKind{
   283  			Group:   "operator.kyma-project.io",
   284  			Version: "v1beta2",
   285  			Kind:    "kyma",
   286  		}, &unstructured.Unstructured{})
   287  
   288  		cli, err = client.New(config, client.Options{})
   289  		if err != nil {
   290  			t.Fatal(err.Error())
   291  		}
   292  		fmt.Println("using kubeconfig")
   293  	} else {
   294  		fmt.Println("using fake client")
   295  		cli = fake.NewClientBuilder().Build()
   296  	}
   297  
   298  	return operation, cli
   299  }