From bdf119401564eab14fc1ef521fe4c967b4e13afb Mon Sep 17 00:00:00 2001 From: medmes Date: Mon, 2 Dec 2024 16:13:13 +0100 Subject: [PATCH 1/7] Introduce mandatory module Label in module create --- .../templategenerator/templategenerator.go | 4 ++ .../templategenerator_test.go | 69 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/internal/service/templategenerator/templategenerator.go b/internal/service/templategenerator/templategenerator.go index b411287..1265f4f 100644 --- a/internal/service/templategenerator/templategenerator.go +++ b/internal/service/templategenerator/templategenerator.go @@ -215,6 +215,10 @@ func generateLabels(config *contentprovider.ModuleConfig) map[string]string { labels[shared.InternalLabel] = shared.EnableLabelValue } + if config.Mandatory { + labels[shared.IsMandatoryModule] = shared.EnableLabelValue + } + return labels } diff --git a/internal/service/templategenerator/templategenerator_test.go b/internal/service/templategenerator/templategenerator_test.go index 4b3a20b..c06a175 100644 --- a/internal/service/templategenerator/templategenerator_test.go +++ b/internal/service/templategenerator/templategenerator_test.go @@ -201,6 +201,75 @@ func TestGenerateModuleTemplateWithManagerWithoutNamespace_Success(t *testing.T) require.Equal(t, 1, strings.Count(mockFS.writtenTemplate, "namespace")) } +func TestGenerateModuleTemplateWithMandatoryIsTrueItShouldAddModuleLabelToTrue_Success(t *testing.T) { + mockFS := &mockFileSystem{} + svc, _ := templategenerator.NewService(mockFS) + + moduleConfig := &contentprovider.ModuleConfig{ + Namespace: "default", + Version: "1.0.0", + Labels: map[string]string{"key": "value"}, + Annotations: map[string]string{"annotation": "value"}, + Mandatory: true, + Manifest: "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml", + Resources: contentprovider.Resources{"someResource": "https://some.other/location/template-operator.yaml"}, + } + descriptor := testutils.CreateComponentDescriptor("example.com/component", "1.0.0") + data := []byte("test-data") + + err := svc.GenerateModuleTemplate(moduleConfig, descriptor, data, true, "output.yaml") + + require.NoError(t, err) + require.Equal(t, "output.yaml", mockFS.path) + require.Contains(t, mockFS.writtenTemplate, "version: 1.0.0") + require.Contains(t, mockFS.writtenTemplate, "moduleName: component") + require.Contains(t, mockFS.writtenTemplate, "component-1.0.0") + require.Contains(t, mockFS.writtenTemplate, "default") + require.Contains(t, mockFS.writtenTemplate, "test-data") + require.Contains(t, mockFS.writtenTemplate, "example.com/component") + require.Contains(t, mockFS.writtenTemplate, "someResource") + require.Contains(t, mockFS.writtenTemplate, "https://some.other/location/template-operator.yaml") + require.Contains(t, mockFS.writtenTemplate, "rawManifest") + require.Contains(t, mockFS.writtenTemplate, + "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml") + require.Contains(t, mockFS.writtenTemplate, + "\"operator.kyma-project.io/mandatory-module\": \"true\"") +} + +func TestGenerateModuleTemplateWithMandatoryIsFalseItShouldNotAddModuleLabelToTrue_Success(t *testing.T) { + mockFS := &mockFileSystem{} + svc, _ := templategenerator.NewService(mockFS) + + moduleConfig := &contentprovider.ModuleConfig{ + Namespace: "default", + Version: "1.0.0", + Labels: map[string]string{"key": "value"}, + Annotations: map[string]string{"annotation": "value"}, + Mandatory: false, + Manifest: "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml", + Resources: contentprovider.Resources{"someResource": "https://some.other/location/template-operator.yaml"}, + } + descriptor := testutils.CreateComponentDescriptor("example.com/component", "1.0.0") + data := []byte("test-data") + + err := svc.GenerateModuleTemplate(moduleConfig, descriptor, data, true, "output.yaml") + + require.NoError(t, err) + require.Equal(t, "output.yaml", mockFS.path) + require.Contains(t, mockFS.writtenTemplate, "version: 1.0.0") + require.Contains(t, mockFS.writtenTemplate, "moduleName: component") + require.Contains(t, mockFS.writtenTemplate, "component-1.0.0") + require.Contains(t, mockFS.writtenTemplate, "default") + require.Contains(t, mockFS.writtenTemplate, "test-data") + require.Contains(t, mockFS.writtenTemplate, "example.com/component") + require.Contains(t, mockFS.writtenTemplate, "someResource") + require.Contains(t, mockFS.writtenTemplate, "https://some.other/location/template-operator.yaml") + require.Contains(t, mockFS.writtenTemplate, "rawManifest") + require.Contains(t, mockFS.writtenTemplate, + "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml") + require.NotContains(t, mockFS.writtenTemplate, "mandatory-module") +} + type mockFileSystem struct { path, writtenTemplate string } From 636c49eeda60880f9dc420c9ad5f3b6c68560175 Mon Sep 17 00:00:00 2001 From: medmes Date: Mon, 2 Dec 2024 16:35:17 +0100 Subject: [PATCH 2/7] Added e2e test coverage for the mandatory label enabled --- tests/e2e/create/create_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/e2e/create/create_test.go b/tests/e2e/create/create_test.go index fb86a75..0de1d82 100644 --- a/tests/e2e/create/create_test.go +++ b/tests/e2e/create/create_test.go @@ -352,6 +352,7 @@ var _ = Describe("Test 'create' command", Ordered, func() { By("And spec.mandatory should be false") Expect(template.Spec.Mandatory).To(BeFalse()) + Expect(template.Spec.Labels).ToNot(HaveKey(shared.MandatoryLabel)) By("And spec.associatedResources should be empty") Expect(template.Spec.AssociatedResources).To(BeEmpty()) @@ -601,6 +602,7 @@ var _ = Describe("Test 'create' command", Ordered, func() { By("And spec.mandatory should be true") Expect(template.Spec.Mandatory).To(BeTrue()) + Expect(template.Labels[shared.MandatoryLabel]).To(Equal("true")) }) }) From 5c6a313f70fdd70c4be12f1852264080717cea6f Mon Sep 17 00:00:00 2001 From: medmes Date: Mon, 2 Dec 2024 16:55:40 +0100 Subject: [PATCH 3/7] Unnecessary check as the final generated file doesn't have labels --- tests/e2e/create/create_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/e2e/create/create_test.go b/tests/e2e/create/create_test.go index 0de1d82..3a0d68e 100644 --- a/tests/e2e/create/create_test.go +++ b/tests/e2e/create/create_test.go @@ -352,7 +352,6 @@ var _ = Describe("Test 'create' command", Ordered, func() { By("And spec.mandatory should be false") Expect(template.Spec.Mandatory).To(BeFalse()) - Expect(template.Spec.Labels).ToNot(HaveKey(shared.MandatoryLabel)) By("And spec.associatedResources should be empty") Expect(template.Spec.AssociatedResources).To(BeEmpty()) From eff3b4b77aae4930da7a4ef384a4915a350b4bff Mon Sep 17 00:00:00 2001 From: medmes Date: Mon, 2 Dec 2024 17:15:51 +0100 Subject: [PATCH 4/7] correct e2e test --- tests/e2e/create/create_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/create/create_test.go b/tests/e2e/create/create_test.go index 3a0d68e..cfc2ec9 100644 --- a/tests/e2e/create/create_test.go +++ b/tests/e2e/create/create_test.go @@ -601,7 +601,7 @@ var _ = Describe("Test 'create' command", Ordered, func() { By("And spec.mandatory should be true") Expect(template.Spec.Mandatory).To(BeTrue()) - Expect(template.Labels[shared.MandatoryLabel]).To(Equal("true")) + Expect(template.Labels[shared.IsMandatoryModule]).To(Equal("true")) }) }) From 049e035acfaafde2cfe66c470e641332e58726c6 Mon Sep 17 00:00:00 2001 From: medmes Date: Tue, 3 Dec 2024 09:42:56 +0100 Subject: [PATCH 5/7] changes after the PR comments --- .../service/templategenerator/templategenerator_test.go | 5 +++-- tests/e2e/create/create_test.go | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/service/templategenerator/templategenerator_test.go b/internal/service/templategenerator/templategenerator_test.go index c06a175..ea80823 100644 --- a/internal/service/templategenerator/templategenerator_test.go +++ b/internal/service/templategenerator/templategenerator_test.go @@ -201,7 +201,7 @@ func TestGenerateModuleTemplateWithManagerWithoutNamespace_Success(t *testing.T) require.Equal(t, 1, strings.Count(mockFS.writtenTemplate, "namespace")) } -func TestGenerateModuleTemplateWithMandatoryIsTrueItShouldAddModuleLabelToTrue_Success(t *testing.T) { +func TestGenerateModuleTemplateWithMandatoryTrue_Success(t *testing.T) { mockFS := &mockFileSystem{} svc, _ := templategenerator.NewService(mockFS) @@ -232,11 +232,12 @@ func TestGenerateModuleTemplateWithMandatoryIsTrueItShouldAddModuleLabelToTrue_S require.Contains(t, mockFS.writtenTemplate, "rawManifest") require.Contains(t, mockFS.writtenTemplate, "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml") + require.Contains(t, mockFS.writtenTemplate, "mandatory: true") require.Contains(t, mockFS.writtenTemplate, "\"operator.kyma-project.io/mandatory-module\": \"true\"") } -func TestGenerateModuleTemplateWithMandatoryIsFalseItShouldNotAddModuleLabelToTrue_Success(t *testing.T) { +func TestGenerateModuleTemplateWithMandatoryFalse_Success(t *testing.T) { mockFS := &mockFileSystem{} svc, _ := templategenerator.NewService(mockFS) diff --git a/tests/e2e/create/create_test.go b/tests/e2e/create/create_test.go index cfc2ec9..553d297 100644 --- a/tests/e2e/create/create_test.go +++ b/tests/e2e/create/create_test.go @@ -350,8 +350,11 @@ var _ = Describe("Test 'create' command", Ordered, func() { Expect(github.Type).To(Equal(githubAccessSpec.Type)) Expect(githubAccessSpec.RepoURL).To(Equal("https://github.com/kyma-project/template-operator")) - By("And spec.mandatory should be false") + By("And module template should not marked as mandatory") Expect(template.Spec.Mandatory).To(BeFalse()) + val, ok := template.Labels[shared.IsMandatoryModule] + Expect(val).To(BeEmpty()) + Expect(ok).To(BeFalse()) By("And spec.associatedResources should be empty") Expect(template.Spec.AssociatedResources).To(BeEmpty()) @@ -599,7 +602,7 @@ var _ = Describe("Test 'create' command", Ordered, func() { Expect(template.Spec.ModuleName).To(Equal("template-operator")) Expect(template.Spec.Version).To(Equal("1.0.4")) - By("And spec.mandatory should be true") + By("And module template should be marked as mandatory") Expect(template.Spec.Mandatory).To(BeTrue()) Expect(template.Labels[shared.IsMandatoryModule]).To(Equal("true")) }) From b144f9b94ecf83bb5d5f225f90bf1294130431ab Mon Sep 17 00:00:00 2001 From: medmes Date: Tue, 3 Dec 2024 10:26:21 +0100 Subject: [PATCH 6/7] changes after the PR comments --- internal/service/templategenerator/templategenerator_test.go | 3 +++ tests/e2e/create/create_test.go | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/service/templategenerator/templategenerator_test.go b/internal/service/templategenerator/templategenerator_test.go index ea80823..0d6f68e 100644 --- a/internal/service/templategenerator/templategenerator_test.go +++ b/internal/service/templategenerator/templategenerator_test.go @@ -269,6 +269,9 @@ func TestGenerateModuleTemplateWithMandatoryFalse_Success(t *testing.T) { require.Contains(t, mockFS.writtenTemplate, "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml") require.NotContains(t, mockFS.writtenTemplate, "mandatory-module") + require.Contains(t, mockFS.writtenTemplate, "mandatory: false") + require.NotContains(t, mockFS.writtenTemplate, + "\"operator.kyma-project.io/mandatory-module\"") } type mockFileSystem struct { diff --git a/tests/e2e/create/create_test.go b/tests/e2e/create/create_test.go index 553d297..b7f861c 100644 --- a/tests/e2e/create/create_test.go +++ b/tests/e2e/create/create_test.go @@ -543,8 +543,11 @@ var _ = Describe("Test 'create' command", Ordered, func() { Expect(github.Type).To(Equal(githubAccessSpec.Type)) Expect(githubAccessSpec.RepoURL).To(Equal("https://github.com/kyma-project/template-operator")) - By("And spec.mandatory should be false") + By("And module template should not marked as mandatory") Expect(template.Spec.Mandatory).To(BeFalse()) + val, ok := template.Labels[shared.IsMandatoryModule] + Expect(val).To(BeEmpty()) + Expect(ok).To(BeFalse()) By("And security scan labels should be correct") secScanLabels := flatten(descriptor.Sources[0].Labels) From ac5c9a722f4c5a93aff176f90eb6778bbb122a17 Mon Sep 17 00:00:00 2001 From: medmes Date: Tue, 3 Dec 2024 10:28:12 +0100 Subject: [PATCH 7/7] changes after the PR comments --- internal/service/templategenerator/templategenerator_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/service/templategenerator/templategenerator_test.go b/internal/service/templategenerator/templategenerator_test.go index 0d6f68e..bb5a84a 100644 --- a/internal/service/templategenerator/templategenerator_test.go +++ b/internal/service/templategenerator/templategenerator_test.go @@ -268,7 +268,6 @@ func TestGenerateModuleTemplateWithMandatoryFalse_Success(t *testing.T) { require.Contains(t, mockFS.writtenTemplate, "rawManifest") require.Contains(t, mockFS.writtenTemplate, "https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml") - require.NotContains(t, mockFS.writtenTemplate, "mandatory-module") require.Contains(t, mockFS.writtenTemplate, "mandatory: false") require.NotContains(t, mockFS.writtenTemplate, "\"operator.kyma-project.io/mandatory-module\"")