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

     1  package provisioning
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-project/kyma-environment-broker/internal"
     7  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
     8  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
     9  	"sigs.k8s.io/controller-runtime/pkg/client"
    10  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    11  
    12  	"github.com/kyma-project/kyma-environment-broker/internal/fixture"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/provisioner"
    14  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    15  	"github.com/sirupsen/logrus"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  const (
    21  	kubeconfigContentsFromParameters = "apiVersion: v1"
    22  	kubeconfigFromRuntime            = "kubeconfig-content"
    23  	kubeconfigFromPreviousOperation  = "kubeconfig-already-set"
    24  )
    25  
    26  func TestGetKubeconfigStep(t *testing.T) {
    27  	t.Run("should create k8s client using kubeconfig from RuntimeStatus", func(t *testing.T) {
    28  		// given
    29  		st := storage.NewMemoryStorage()
    30  		provisionerClient := provisioner.NewFakeClient()
    31  
    32  		scheme := internal.NewSchemeForTests()
    33  		err := apiextensionsv1.AddToScheme(scheme)
    34  
    35  		k8sCli := fake.NewClientBuilder().WithScheme(scheme).Build()
    36  
    37  		expectedKubeconfig := kubeconfigFromRuntime
    38  		assertedK8sClientProvider := func(kubeconfig string) (client.Client, error) {
    39  			assert.Equal(t, expectedKubeconfig, kubeconfig)
    40  			return k8sCli, nil
    41  		}
    42  		step := NewGetKubeconfigStep(st.Operations(), provisionerClient, assertedK8sClientProvider)
    43  		operation := fixture.FixProvisioningOperation("operation-id", "inst-id")
    44  		operation.Kubeconfig = ""
    45  		st.Operations().InsertOperation(operation)
    46  
    47  		input, err := operation.InputCreator.CreateProvisionRuntimeInput()
    48  		require.NoError(t, err)
    49  		provisionerClient.ProvisionRuntimeWithIDs(operation.GlobalAccountID, operation.SubAccountID, operation.RuntimeID, operation.ID, input)
    50  
    51  		// when
    52  		processedOperation, d, err := step.Run(operation, logrus.New())
    53  
    54  		// then
    55  		require.NoError(t, err)
    56  		assert.Zero(t, d)
    57  		assert.Equal(t, kubeconfigFromRuntime, processedOperation.Kubeconfig)
    58  		assert.NotEmpty(t, processedOperation.Kubeconfig)
    59  		assert.NotEmpty(t, processedOperation.K8sClient)
    60  	})
    61  	t.Run("should create k8s client for own_cluster plan using kubeconfig from provisioning parameters", func(t *testing.T) {
    62  		// given
    63  		st := storage.NewMemoryStorage()
    64  
    65  		scheme := internal.NewSchemeForTests()
    66  		err := apiextensionsv1.AddToScheme(scheme)
    67  
    68  		k8sCli := fake.NewClientBuilder().WithScheme(scheme).Build()
    69  
    70  		expectedKubeconfig := kubeconfigContentsFromParameters
    71  		assertedK8sClientProvider := func(kubeconfig string) (client.Client, error) {
    72  			assert.Equal(t, expectedKubeconfig, kubeconfig)
    73  			return k8sCli, nil
    74  		}
    75  		step := NewGetKubeconfigStep(st.Operations(), nil, assertedK8sClientProvider)
    76  		operation := fixture.FixProvisioningOperation("operation-id", "inst-id")
    77  		operation.Kubeconfig = ""
    78  		operation.ProvisioningParameters.Parameters.Kubeconfig = kubeconfigContentsFromParameters
    79  		operation.ProvisioningParameters.PlanID = broker.OwnClusterPlanID
    80  		st.Operations().InsertOperation(operation)
    81  
    82  		// when
    83  		processedOperation, d, err := step.Run(operation, logrus.New())
    84  
    85  		// then
    86  		require.NoError(t, err)
    87  		assert.Zero(t, d)
    88  		assert.Equal(t, kubeconfigContentsFromParameters, processedOperation.Kubeconfig)
    89  		assert.NotEmpty(t, processedOperation.K8sClient)
    90  	})
    91  	t.Run("should create k8s client using kubeconfig already set in operation", func(t *testing.T) {
    92  		// given
    93  		st := storage.NewMemoryStorage()
    94  
    95  		scheme := internal.NewSchemeForTests()
    96  		err := apiextensionsv1.AddToScheme(scheme)
    97  
    98  		k8sCli := fake.NewClientBuilder().WithScheme(scheme).Build()
    99  
   100  		expectedKubeconfig := kubeconfigFromPreviousOperation
   101  
   102  		assertedK8sClientProvider := func(kubeconfig string) (client.Client, error) {
   103  			assert.Equal(t, expectedKubeconfig, kubeconfig)
   104  			return k8sCli, nil
   105  		}
   106  		step := NewGetKubeconfigStep(st.Operations(), nil, assertedK8sClientProvider)
   107  		operation := fixture.FixProvisioningOperation("operation-id", "inst-id")
   108  		operation.Kubeconfig = kubeconfigFromPreviousOperation
   109  		operation.ProvisioningParameters.Parameters.Kubeconfig = ""
   110  		st.Operations().InsertOperation(operation)
   111  
   112  		// when
   113  		processedOperation, d, err := step.Run(operation, logrus.New())
   114  
   115  		// then
   116  		require.NoError(t, err)
   117  		assert.Zero(t, d)
   118  		assert.Equal(t, kubeconfigFromPreviousOperation, processedOperation.Kubeconfig)
   119  		assert.NotEmpty(t, processedOperation.K8sClient)
   120  	})
   121  	t.Run("should fail with error if there is neither kubeconfig nor runtimeID and this is not own_cluster plan", func(t *testing.T) {
   122  		// given
   123  		st := storage.NewMemoryStorage()
   124  		provisionerClient := provisioner.NewFakeClient()
   125  
   126  		scheme := internal.NewSchemeForTests()
   127  		err := apiextensionsv1.AddToScheme(scheme)
   128  
   129  		k8sCli := fake.NewClientBuilder().WithScheme(scheme).Build()
   130  
   131  		assertedK8sClientProvider := func(kubeconfig string) (client.Client, error) {
   132  			assert.Fail(t, "should not call this assertion")
   133  			return k8sCli, nil
   134  		}
   135  		step := NewGetKubeconfigStep(st.Operations(), provisionerClient, assertedK8sClientProvider)
   136  		operation := fixture.FixProvisioningOperation("operation-id", "inst-id")
   137  		operation.Kubeconfig = ""
   138  		operation.RuntimeID = ""
   139  		st.Operations().InsertOperation(operation)
   140  
   141  		// when
   142  		_, _, err = step.Run(operation, logrus.New())
   143  
   144  		// then
   145  		require.ErrorContains(t, err, "Runtime ID is empty")
   146  	})
   147  }