github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtimeoverrides/runtimeoverrides_test.go (about)

     1  package runtimeoverrides
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/ptr"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/runtimeoverrides/automock"
    10  	"github.com/stretchr/testify/require"
    11  	coreV1 "k8s.io/api/core/v1"
    12  	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"k8s.io/apimachinery/pkg/runtime"
    14  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    15  )
    16  
    17  func TestRuntimeOverrides_Append(t *testing.T) {
    18  	t.Run("Success when there is ConfigMap with overrides for given planID and Kyma version", func(t *testing.T) {
    19  		// GIVEN
    20  		cm := &coreV1.ConfigMap{
    21  			ObjectMeta: metaV1.ObjectMeta{
    22  				Name:      "overrides",
    23  				Namespace: namespace,
    24  				Labels: map[string]string{
    25  					"overrides-version-1.15.1": "true",
    26  					"overrides-plan-foo":       "true",
    27  				},
    28  			},
    29  			Data: map[string]string{"test1": "test1abc"},
    30  		}
    31  		sch := runtime.NewScheme()
    32  		require.NoError(t, coreV1.AddToScheme(sch))
    33  		client := fake.NewFakeClientWithScheme(sch, cm)
    34  
    35  		inputAppenderMock := &automock.InputAppender{}
    36  		defer inputAppenderMock.AssertExpectations(t)
    37  		inputAppenderMock.On("AppendGlobalOverrides", []*gqlschema.ConfigEntryInput{
    38  			{
    39  				Key:   "test1",
    40  				Value: "test1abc",
    41  			},
    42  		}).Return(nil).Once()
    43  
    44  		runtimeOverrides := NewRuntimeOverrides(context.TODO(), client)
    45  
    46  		// WHEN
    47  		err := runtimeOverrides.Append(inputAppenderMock, "foo", "1.15.1")
    48  
    49  		// THEN
    50  		require.NoError(t, err)
    51  	})
    52  
    53  	t.Run("Success when there are ConfigMap and Secret with overrides for given planID and Kyma version", func(t *testing.T) {
    54  		// GIVEN
    55  		cm := &coreV1.ConfigMap{
    56  			ObjectMeta: metaV1.ObjectMeta{
    57  				Name:      "overrides",
    58  				Namespace: namespace,
    59  				Labels: map[string]string{
    60  					"overrides-version-1.15.1": "true",
    61  					"overrides-plan-foo":       "true",
    62  				},
    63  			},
    64  			Data: map[string]string{"test1": "test1abc"},
    65  		}
    66  		secret := &coreV1.Secret{
    67  			ObjectMeta: metaV1.ObjectMeta{
    68  				Name:      "overrides-secret",
    69  				Namespace: namespace,
    70  				Labels: map[string]string{
    71  					"runtime-override": "true",
    72  				},
    73  			},
    74  			Data: map[string][]byte{"test2": []byte("test2abc")},
    75  		}
    76  		sch := runtime.NewScheme()
    77  		require.NoError(t, coreV1.AddToScheme(sch))
    78  		client := fake.NewFakeClientWithScheme(sch, cm, secret)
    79  
    80  		inputAppenderMock := &automock.InputAppender{}
    81  		defer inputAppenderMock.AssertExpectations(t)
    82  		inputAppenderMock.On("AppendGlobalOverrides", []*gqlschema.ConfigEntryInput{
    83  			{
    84  				Key:   "test1",
    85  				Value: "test1abc",
    86  			},
    87  		}).Return(nil).Once()
    88  		inputAppenderMock.On("AppendGlobalOverrides", []*gqlschema.ConfigEntryInput{
    89  			{
    90  				Key:    "test2",
    91  				Value:  "test2abc",
    92  				Secret: ptr.Bool(true),
    93  			},
    94  		}).Return(nil).Once()
    95  
    96  		runtimeOverrides := NewRuntimeOverrides(context.TODO(), client)
    97  
    98  		// WHEN
    99  		err := runtimeOverrides.Append(inputAppenderMock, "foo", "1.15.1")
   100  
   101  		// THEN
   102  		require.NoError(t, err)
   103  	})
   104  
   105  	t.Run("Success when there is multiple ConfigMaps and Secrets, some with overrides - global and component scoped", func(t *testing.T) {
   106  		// GIVEN
   107  		sch := runtime.NewScheme()
   108  		require.NoError(t, coreV1.AddToScheme(sch))
   109  		client := fake.NewFakeClientWithScheme(sch, fixResources()...)
   110  
   111  		inputAppenderMock := &automock.InputAppender{}
   112  		defer inputAppenderMock.AssertExpectations(t)
   113  		inputAppenderMock.On("AppendOverrides", "core", []*gqlschema.ConfigEntryInput{
   114  			{
   115  				Key:    "test1",
   116  				Value:  "test1abc",
   117  				Secret: ptr.Bool(true),
   118  			},
   119  		}).Return(nil).Once()
   120  		inputAppenderMock.On("AppendOverrides", "helm", []*gqlschema.ConfigEntryInput{
   121  			{
   122  				Key:    "test3",
   123  				Value:  "test3abc",
   124  				Secret: ptr.Bool(true),
   125  			},
   126  		}).Return(nil).Once()
   127  		inputAppenderMock.On("AppendGlobalOverrides", []*gqlschema.ConfigEntryInput{
   128  			{
   129  				Key:    "test4",
   130  				Value:  "test4abc",
   131  				Secret: ptr.Bool(true),
   132  			},
   133  		}).Return(nil).Once()
   134  		inputAppenderMock.On("AppendOverrides", "core", []*gqlschema.ConfigEntryInput{
   135  			{
   136  				Key:   "test5",
   137  				Value: "test5abc",
   138  			},
   139  		}).Return(nil).Once()
   140  		inputAppenderMock.On("AppendGlobalOverrides", []*gqlschema.ConfigEntryInput{
   141  			{
   142  				Key:   "test7",
   143  				Value: "test7abc",
   144  			},
   145  		}).Return(nil).Once()
   146  
   147  		runtimeOverrides := NewRuntimeOverrides(context.TODO(), client)
   148  
   149  		// WHEN
   150  		err := runtimeOverrides.Append(inputAppenderMock, "foo", "1.15.1")
   151  
   152  		// THEN
   153  		require.NoError(t, err)
   154  	})
   155  
   156  	t.Run("Error when there is no ConfigMap with overrides present", func(t *testing.T) {
   157  		// GIVEN
   158  		sch := runtime.NewScheme()
   159  		require.NoError(t, coreV1.AddToScheme(sch))
   160  		client := fake.NewFakeClientWithScheme(sch)
   161  
   162  		inputAppenderMock := &automock.InputAppender{}
   163  		defer inputAppenderMock.AssertExpectations(t)
   164  
   165  		runtimeOverrides := NewRuntimeOverrides(context.TODO(), client)
   166  
   167  		// WHEN
   168  		err := runtimeOverrides.Append(inputAppenderMock, "foo", "1.15.1")
   169  
   170  		// THEN
   171  		require.Error(t, err, "no global overrides for plan 'foo' and Kyma version '1.15.1'")
   172  	})
   173  }
   174  
   175  func fixResources() []runtime.Object {
   176  	var resources []runtime.Object
   177  
   178  	resources = append(resources, &coreV1.Secret{
   179  		ObjectMeta: metaV1.ObjectMeta{
   180  			Name:      "secret#1",
   181  			Namespace: namespace,
   182  			Labels: map[string]string{
   183  				"runtime-override": "true",
   184  				"component":        "core",
   185  			},
   186  		},
   187  		Data: map[string][]byte{"test1": []byte("test1abc")},
   188  	})
   189  	resources = append(resources, &coreV1.Secret{
   190  		ObjectMeta: metaV1.ObjectMeta{
   191  			Name:      "secret#2",
   192  			Namespace: namespace,
   193  			Labels: map[string]string{
   194  				"component": "core",
   195  			},
   196  		},
   197  		Data: map[string][]byte{"test2": []byte("test2abc")},
   198  	})
   199  	resources = append(resources, &coreV1.Secret{
   200  		ObjectMeta: metaV1.ObjectMeta{
   201  			Name:      "secret#3",
   202  			Namespace: namespace,
   203  			Labels: map[string]string{
   204  				"runtime-override": "true",
   205  				"component":        "helm",
   206  			},
   207  		},
   208  		Data: map[string][]byte{"test3": []byte("test3abc")},
   209  	})
   210  	resources = append(resources, &coreV1.Secret{
   211  		ObjectMeta: metaV1.ObjectMeta{
   212  			Name:      "secret#4",
   213  			Namespace: namespace,
   214  			Labels: map[string]string{
   215  				"runtime-override": "true",
   216  			},
   217  		},
   218  		Data: map[string][]byte{"test4": []byte("test4abc")},
   219  	})
   220  	resources = append(resources, &coreV1.ConfigMap{
   221  		ObjectMeta: metaV1.ObjectMeta{
   222  			Name:      "configmap#1",
   223  			Namespace: namespace,
   224  			Labels: map[string]string{
   225  				"overrides-version-1.15.1": "true",
   226  				"overrides-plan-foo":       "true",
   227  				"component":                "core",
   228  			},
   229  		},
   230  		Data: map[string]string{"test5": "test5abc"},
   231  	})
   232  	resources = append(resources, &coreV1.ConfigMap{
   233  		ObjectMeta: metaV1.ObjectMeta{
   234  			Name:      "configmap#2",
   235  			Namespace: "default",
   236  			Labels: map[string]string{
   237  				"overrides-version-1.15.1": "true",
   238  				"overrides-plan-foo":       "true",
   239  				"overrides-plan-lite":      "true",
   240  				"component":                "helm",
   241  			},
   242  		},
   243  		Data: map[string]string{"test6": "test6abc"},
   244  	})
   245  	resources = append(resources, &coreV1.ConfigMap{
   246  		ObjectMeta: metaV1.ObjectMeta{
   247  			Name:      "configmap#3",
   248  			Namespace: namespace,
   249  			Labels: map[string]string{
   250  				"overrides-version-1.15.1": "true",
   251  				"overrides-plan-foo":       "true",
   252  			},
   253  		},
   254  		Data: map[string]string{"test7": "test7abc"},
   255  	})
   256  	resources = append(resources, &coreV1.ConfigMap{
   257  		ObjectMeta: metaV1.ObjectMeta{
   258  			Name:      "configmap#4",
   259  			Namespace: namespace,
   260  			Labels: map[string]string{
   261  				"overrides-version-1.15.0": "true",
   262  				"overrides-plan-foo":       "true",
   263  				"overrides-plan-lite":      "true",
   264  			},
   265  		},
   266  		Data: map[string]string{"test8": "test8abc"},
   267  	})
   268  
   269  	return resources
   270  }