Skip to content

Commit

Permalink
adapt testcases to new modulectl api
Browse files Browse the repository at this point in the history
  • Loading branch information
lindnerby committed Sep 24, 2024
1 parent f26ab1e commit 34f5f08
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 102 deletions.
21 changes: 4 additions & 17 deletions .github/workflows/test-e2e-create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,14 @@ jobs:
ls -la ./bin
mv ./bin/modulectl-linux /usr/local/bin/modulectl
timeout-minutes: 5
- name: Checkout template-operator
uses: actions/checkout@v4
with:
repository: kyma-project/template-operator
path: ./template-operator/
- name: Copy template-operator to e2e testdata folder
run: |
mkdir -p ./tests/e2e/create/testdata/template-operator/
cp -r ./../template-operator/ ./tests/e2e/create/testdata/template-operator/
- name: Set template-operator github URL
run: |
cd ./template-operator
echo "TEST_REPOSITORY_URL=$(git remote get-url origin)" >> "$GITHUB_ENV"
# - name: Set template-operator github URL
# run: |
# cd ./template-operator
# echo "TEST_REPOSITORY_URL=$(git remote get-url origin)" >> "$GITHUB_ENV"
- name: Install k3d and create registry
run: |
wget -qO - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | TAG=$K3D_VERSION bash
k3d registry create oci.localhost --port 5001
- name: Make manifests template-operator
run: |
cd ./template-operator
make build-manifests
- name: Run tests
run: |
make -C tests/e2e test-create-cmd
Expand Down
34 changes: 7 additions & 27 deletions tests/e2e/create/create_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ func Test_Create(t *testing.T) {
// Command wrapper for `modulectl create`

type createCmd struct {
name string
registry string
path string
output string
version string
moduleConfigFile string
secScanConfig string
moduleArchiveVersionOverwrite bool
insecure bool
registry string
output string
moduleConfigFile string
gitRemote string
insecure bool
}

func (cmd *createCmd) execute() error {
Expand All @@ -40,32 +36,16 @@ func (cmd *createCmd) execute() error {
args = append(args, "--module-config-file="+cmd.moduleConfigFile)
}

if cmd.path != "" {
args = append(args, "--path="+cmd.path)
}

if cmd.name != "" {
args = append(args, "--name="+cmd.name)
}

if cmd.registry != "" {
args = append(args, "--registry="+cmd.registry)
}

if cmd.secScanConfig != "" {
args = append(args, "--sec-scanners-config="+cmd.secScanConfig)
}

if cmd.output != "" {
args = append(args, "--output="+cmd.output)
}

if cmd.version != "" {
args = append(args, "--version="+cmd.version)
}

if cmd.moduleArchiveVersionOverwrite {
args = append(args, "--module-archive-version-overwrite")
if cmd.gitRemote != "" {
args = append(args, "--git-remote="+cmd.gitRemote)
}

if cmd.insecure {
Expand Down
126 changes: 68 additions & 58 deletions tests/e2e/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ var _ = Describe("Test 'create' command", Ordered, func() {
// moduleTemplateVersion := os.Getenv("MODULE_TEMPLATE_VERSION")

ociRegistry := "http://k3d-oci.localhost:5001"
moduleRepoPath := "./testdata/template-operator/"
moduleTemplateVersion := "1.0.0"
moduleConfigFilePath := moduleRepoPath + "module-config.yaml"
templateOutputPath := moduleRepoPath + "template.yaml"
securityScanConfigFile := moduleRepoPath + "sec-scanners-config.yaml"
changedSecScanConfigFile := moduleRepoPath + "sec-scanners-config-changed.yaml"

// TODO build module-config file in test
validModuleConfigFile := "./testdata/module-config-valid.yaml"
invalidModuleConfigFile := "./testdata/module-config-missing-required.yaml"

templateOutputPath := "/tmp/template.yaml"

// TODO build module-config file in test
//securityScanConfigFile := moduleRepoPath + "sec-scanners-config.yaml"
//changedSecScanConfigFile := moduleRepoPath + "sec-scanners-config-changed.yaml"

Context("Given 'modulectl create' command", func() {
var cmd createCmd
Expand All @@ -56,18 +61,14 @@ var _ = Describe("Test 'create' command", Ordered, func() {
err := cmd.execute()
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("Error: \"--module-config-file\" flag is required"))

By("And no module template.yaml is generated")
Expect(filesIn(moduleRepoPath)).Should(Not(ContainElement("template.yaml")))
})
})

Context("Given 'modulectl create' command", func() {
var cmd createCmd
It("When invoked with '--module-config-file' and '--path'", func() {
It("When invoked with '--module-config-file' using valid file", func() {
cmd = createCmd{
moduleConfigFile: moduleConfigFilePath,
path: moduleRepoPath,
moduleConfigFile: validModuleConfigFile,
}
})
It("Then the command should succeed", func() {
Expand All @@ -78,12 +79,40 @@ var _ = Describe("Test 'create' command", Ordered, func() {
})
})

Context("Given 'modulectl create' command", func() {
var cmd createCmd
It("When invoked with '--module-config-file' using file with missing required fields", func() {
cmd = createCmd{
moduleConfigFile: invalidModuleConfigFile,
}
})
It("Then the command should fail", func() {
err := cmd.execute()
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("Error: field is required"))
})
})

Context("Given 'modulectl create' command", func() {
var cmd createCmd
It("When invoked with existing '--registry' and missing '--insecure' flag", func() {
cmd = createCmd{
moduleConfigFile: validModuleConfigFile,
registry: ociRegistry,
}
})
It("Then the command should fail", func() {
err := cmd.execute()
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("Error: could not push"))
})
})

Context("Given 'modulectl create' command", func() {
var cmd createCmd
It("When invoked with existing '--registry' and '--insecure' flag", func() {
cmd = createCmd{
moduleConfigFile: moduleConfigFilePath,
path: moduleRepoPath,
moduleConfigFile: validModuleConfigFile,
registry: ociRegistry,
insecure: true,
output: templateOutputPath,
Expand All @@ -93,7 +122,7 @@ var _ = Describe("Test 'create' command", Ordered, func() {
Expect(cmd.execute()).To(Succeed())

By("And module template file should be generated")
Expect(filesIn("./testdata/template-operator/")).Should(ContainElement("template.yaml"))
Expect(filesIn("/tmp/")).Should(ContainElement("template.yaml"))
})
It("Then module template should contain the expected content", func() {
template, err := readModuleTemplate(templateOutputPath)
Expand All @@ -119,13 +148,19 @@ var _ = Describe("Test 'create' command", Ordered, func() {
resource := descriptor.Resources[0]
Expect(resource.Name).To(Equal("template-operator"))
Expect(resource.Relation).To(Equal(ocmmetav1.ExternalRelation))
Expect(resource.Type).To(Equal("ociImage"))
Expect(resource.Type).To(Equal("ociArtifact"))
Expect(resource.Version).To(Equal(moduleTemplateVersion))

resource = descriptor.Resources[1]
Expect(resource.Name).To(Equal("raw-manifest"))
Expect(resource.Relation).To(Equal(ocmmetav1.LocalRelation))
Expect(resource.Type).To(Equal("yaml"))
Expect(resource.Type).To(Equal("directory"))
Expect(resource.Version).To(Equal(moduleTemplateVersion))

resource = descriptor.Resources[2]
Expect(resource.Name).To(Equal("default-cr"))
Expect(resource.Relation).To(Equal(ocmmetav1.LocalRelation))
Expect(resource.Type).To(Equal("directory"))
Expect(resource.Version).To(Equal(moduleTemplateVersion))

By("And descriptor.component.resources[0].access should be correct")
Expand All @@ -143,6 +178,16 @@ var _ = Describe("Test 'create' command", Ordered, func() {
Expect(ok).To(BeTrue())
Expect(localBlobAccessSpec.GetType()).To(Equal(localblob.Type))
Expect(localBlobAccessSpec.LocalReference).To(ContainSubstring("sha256:"))
Expect(localBlobAccessSpec.MediaType).To(Equal("application/x-tar"))

By("And descriptor.component.resources[2].access should be correct")
defaultCRResourceAccessSpec, err := ocm.DefaultContext().AccessSpecForSpec(descriptor.Resources[2].Access)
Expect(err).ToNot(HaveOccurred())
defaultCRAccessSpec, ok := defaultCRResourceAccessSpec.(*localblob.AccessSpec)
Expect(ok).To(BeTrue())
Expect(defaultCRAccessSpec.GetType()).To(Equal(localblob.Type))
Expect(defaultCRAccessSpec.LocalReference).To(ContainSubstring("sha256:"))
Expect(defaultCRAccessSpec.MediaType).To(Equal("application/x-tar"))

By("And descriptor.component.sources should be correct")
Expect(len(descriptor.Sources)).To(Equal(1))
Expand All @@ -152,14 +197,14 @@ var _ = Describe("Test 'create' command", Ordered, func() {
githubAccessSpec, ok := sourceAccessSpec.(*github.AccessSpec)
Expect(ok).To(BeTrue())
Expect(github.Type).To(Equal(githubAccessSpec.Type))
Expect(githubAccessSpec.RepoURL).To(ContainSubstring("template-operator.git"))
Expect(githubAccessSpec.RepoURL).To(Equal("https://github.com/kyma-project/template-operator"))

By("And spec.mandatory should be false")
Expect(template.Spec.Mandatory).To(BeFalse())

By("And security scan labels should be correct")
secScanLabels := flatten(descriptor.Sources[0].Labels)
Expect(secScanLabels).To(HaveKeyWithValue("git.kyma-project.io/ref", "refs/heads/main"))
Expect(secScanLabels).To(HaveKeyWithValue("git.kyma-project.io/ref", "HEAD"))
Expect(secScanLabels).To(HaveKeyWithValue("scan.security.kyma-project.io/rc-tag", "1.0.0"))
Expect(secScanLabels).To(HaveKeyWithValue("scan.security.kyma-project.io/language", "golang-mod"))
Expect(secScanLabels).To(HaveKeyWithValue("scan.security.kyma-project.io/dev-branch", "main"))
Expand All @@ -170,51 +215,16 @@ var _ = Describe("Test 'create' command", Ordered, func() {

Context("Given 'modulectl create' command", func() {
var cmd createCmd
It("When invoked with '--module-archive-version-overwrite' flag", func() {
It("When invoked with same version that already exists in the registry", func() {
cmd = createCmd{
path: moduleRepoPath,
registry: ociRegistry,
moduleConfigFile: moduleConfigFilePath,
version: moduleTemplateVersion,
moduleArchiveVersionOverwrite: true,
secScanConfig: securityScanConfigFile,
}
})
It("Then the command should succeed", func() {
Expect(cmd.execute()).To(Succeed())
})
})

Context("Given 'modulectl create' command", func() {
var cmd createCmd
It("When invoked with same version module and same content without '--module-archive-version-overwrite' flag", func() {
cmd = createCmd{
path: moduleRepoPath,
moduleConfigFile: validModuleConfigFile,
registry: ociRegistry,
moduleConfigFile: moduleConfigFilePath,
version: moduleTemplateVersion,
secScanConfig: securityScanConfigFile,
}
})
It("Then the command should succeed, because content has not change and overwrite flag not needed", func() {
Expect(cmd.execute()).To(Succeed())
})
})

Context("Given 'modulectl create' command", func() {
var cmd createCmd
It("When invoked with same version module, but different content without '--module-archive-version-overwrite' flag", func() {
cmd = createCmd{
path: moduleRepoPath,
registry: ociRegistry,
moduleConfigFile: moduleConfigFilePath,
version: moduleTemplateVersion,
secScanConfig: changedSecScanConfigFile,
insecure: true,
}
})
It("Then the command should fail with same version exists message, because overwrite flag is required", func() {
It("Then the command should fail with same version exists message", func() {
err := cmd.execute()
Expect(err).To(Equal(errCreateModuleFailedWithSameVersion))
Expect(err.Error()).Should(ContainSubstring("could not push component version: component version already exists"))
})
})
})
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/create/testdata/module-config-missing-required.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
channel: regular
version: 1.0.0
manifest: https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml
security: sec-scanners-config.yaml
defaultCR: https://github.com/kyma-project/template-operator/releases/download/1.0.1/default-sample-cr.yaml
annotations:
operator.kyma-project.io/doc-url: https://kyma-project.io
8 changes: 8 additions & 0 deletions tests/e2e/create/testdata/module-config-valid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: kyma-project.io/module/template-operator
channel: regular
version: 1.0.0
manifest: https://github.com/kyma-project/template-operator/releases/download/1.0.1/template-operator.yaml
security: sec-scanners-config.yaml
defaultCR: https://github.com/kyma-project/template-operator/releases/download/1.0.1/default-sample-cr.yaml
annotations:
operator.kyma-project.io/doc-url: https://kyma-project.io

0 comments on commit 34f5f08

Please sign in to comment.