Skip to content

Commit

Permalink
feat: Introduce mandatory module Label in module create (#116)
Browse files Browse the repository at this point in the history
* Introduce mandatory module Label in module create

* Added e2e test coverage for the mandatory label enabled

* Unnecessary check as the final generated file doesn't have labels

* correct e2e test

* changes after the PR comments

* changes after the PR comments

* changes after the PR comments
  • Loading branch information
medmes authored Dec 3, 2024
1 parent 7016a5b commit a473e00
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
4 changes: 4 additions & 0 deletions internal/service/templategenerator/templategenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
72 changes: 72 additions & 0 deletions internal/service/templategenerator/templategenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,78 @@ func TestGenerateModuleTemplateWithManagerWithoutNamespace_Success(t *testing.T)
require.Equal(t, 1, strings.Count(mockFS.writtenTemplate, "namespace"))
}

func TestGenerateModuleTemplateWithMandatoryTrue_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, "mandatory: true")
require.Contains(t, mockFS.writtenTemplate,
"\"operator.kyma-project.io/mandatory-module\": \"true\"")
}

func TestGenerateModuleTemplateWithMandatoryFalse_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.Contains(t, mockFS.writtenTemplate, "mandatory: false")
require.NotContains(t, mockFS.writtenTemplate,
"\"operator.kyma-project.io/mandatory-module\"")
}

type mockFileSystem struct {
path, writtenTemplate string
}
Expand Down
13 changes: 10 additions & 3 deletions tests/e2e/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -540,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)
Expand Down Expand Up @@ -599,8 +605,9 @@ 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"))
})
})

Expand Down

0 comments on commit a473e00

Please sign in to comment.