Skip to content

Commit

Permalink
filepath fixes in e2e tests (#591)
Browse files Browse the repository at this point in the history
* filepath joins

* more filepath fixes

* 1. added missing copyright for few files
2. added more tests for file and remote scan
  • Loading branch information
patilpankaj212 authored Mar 8, 2021
1 parent 71a677f commit 4df6ce6
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 90 deletions.
21 changes: 12 additions & 9 deletions test/e2e/help/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package help_test

import (
"fmt"
"io"
"path/filepath"

helpUtils "github.com/accurics/terrascan/test/e2e/help"
"github.com/accurics/terrascan/test/helper"
Expand Down Expand Up @@ -53,64 +55,65 @@ var _ = Describe("Help", func() {
Describe("terrascan is run without any command", func() {
It("should print all supported commands and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, "golden/help_command.txt", true)
fmt.Println(filepath.Join("golden", "help_command.txt"))
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, filepath.Join("golden", "help_command.txt"), true)
})
})

Describe("terrascan is run -h flag", func() {
It("should print all supported commands and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, "-h")
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, "golden/help_flag.txt", true)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, filepath.Join("golden", "help_flag.txt"), true)
})
})

Describe("terrascan is run with an unkonwn command", func() {
It("should exit with status code 1 and display a error message", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, "test")
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeOne, "golden/incorrect_command.txt", false)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeOne, filepath.Join("golden", "incorrect_command.txt"), false)
})
})

Describe("help is run", func() {
Context("with no arguments", func() {
It("should print the terrascan help and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, helpCommand)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, "golden/help_command.txt", true)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, filepath.Join("golden", "help_command.txt"), true)
})
})

Context("for init command", func() {
It("should print help for init and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, helpCommand, "init")
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, "golden/help_init.txt", true)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, filepath.Join("golden", "help_init.txt"), true)
})
})

Context("for scan command", func() {
It("should print help for init and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, helpCommand, "scan")
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, "golden/help_scan.txt", true)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, filepath.Join("golden", "help_scan.txt"), true)
})
})

Context("for server command", func() {
It("should print help for init and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, helpCommand, "server")
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, "golden/help_server.txt", true)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, filepath.Join("golden", "help_server.txt"), true)
})
})

Context("for version command", func() {
It("should print help for init and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, helpCommand, "version")
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, "golden/help_version.txt", true)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, filepath.Join("golden", "help_version.txt"), true)
})
})

Context("for an unkonwn command", func() {
It("should display that help topic is not available for entered command and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, helpCommand, "test")
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, "golden/help_unsupported_command.txt", false)
helpUtils.ValidateExitCodeAndOutput(session, helper.ExitCodeZero, filepath.Join("golden", "help_unsupported_command.txt"), false)
})
})
})
Expand Down
14 changes: 7 additions & 7 deletions test/e2e/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var _ = Describe("Init", func() {
When("terrascan init is run with -h flag", func() {
It("should print help", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, initCommand, "-h")
goldenFileAbsPath, err := filepath.Abs("../help/golden/help_init.txt")
goldenFileAbsPath, err := filepath.Abs(filepath.Join("..", "help", "golden", "help_init.txt"))
Expect(err).NotTo(HaveOccurred())
helper.CompareActualWithGolden(session, goldenFileAbsPath, true)
})
Expand All @@ -106,7 +106,7 @@ var _ = Describe("Init", func() {
When("terrascan init command has typo. eg: inti", func() {
It("should print command suggestion", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, "inti")
goldenFileAbsPath, err := filepath.Abs("golden/init_typo_help.txt")
goldenFileAbsPath, err := filepath.Abs(filepath.Join("golden", "init_typo_help.txt"))
Expect(err).NotTo(HaveOccurred())
helper.CompareActualWithGolden(session, goldenFileAbsPath, false)
})
Expand All @@ -129,7 +129,7 @@ var _ = Describe("Init", func() {
Describe("terrascan init is run when TERRASCAN_CONFIG is set", func() {
When("the config file has invalid repo url", func() {
JustBeforeEach(func() {
os.Setenv(terrascanConfigEnvName, "config/invalid_repo.toml")
os.Setenv(terrascanConfigEnvName, filepath.Join("config", "invalid_repo.toml"))
})
JustAfterEach(func() {
os.Setenv(terrascanConfigEnvName, "")
Expand All @@ -141,7 +141,7 @@ var _ = Describe("Init", func() {
})
When("the config file has invalid branch name", func() {
JustBeforeEach(func() {
os.Setenv(terrascanConfigEnvName, "config/invalid_branch.toml")
os.Setenv(terrascanConfigEnvName, filepath.Join("config", "invalid_branch.toml"))
})
JustAfterEach(func() {
os.Setenv(terrascanConfigEnvName, "")
Expand All @@ -153,7 +153,7 @@ var _ = Describe("Init", func() {
})
When("the config file has invalid rego subdir", func() {
JustBeforeEach(func() {
os.Setenv(terrascanConfigEnvName, "config/invalid_rego_subdir.toml")
os.Setenv(terrascanConfigEnvName, filepath.Join("config", "invalid_rego_subdir.toml"))
})
JustAfterEach(func() {
os.Setenv(terrascanConfigEnvName, "")
Expand All @@ -165,7 +165,7 @@ var _ = Describe("Init", func() {
})
When("the config file has invalid path", func() {
JustBeforeEach(func() {
os.Setenv(terrascanConfigEnvName, "config/invalid_path.toml")
os.Setenv(terrascanConfigEnvName, filepath.Join("config", "invalid_path.toml"))
})
JustAfterEach(func() {
os.Setenv(terrascanConfigEnvName, "")
Expand All @@ -177,7 +177,7 @@ var _ = Describe("Init", func() {
Context("the config file has valid data", func() {
When("config file has different git repo and branch", func() {
JustBeforeEach(func() {
os.Setenv(terrascanConfigEnvName, "config/valid_config.toml")
os.Setenv(terrascanConfigEnvName, filepath.Join("config", "valid_config.toml"))
})
JustAfterEach(func() {
os.Setenv(terrascanConfigEnvName, "")
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/scan/scan_config_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _ = Describe("Scan With Config Only Flag", func() {

var iacDir string
var err error
iacDir, err = filepath.Abs("../test_data/iac/aws/aws_ami_violation")
iacDir, err = filepath.Abs(filepath.Join(awsIacRelPath, "aws_ami_violation"))

It("should not error out while getting absolute path", func() {
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -106,7 +106,7 @@ var _ = Describe("Scan With Config Only Flag", func() {

Context("for yaml files", func() {
JustBeforeEach(func() {
iacDir, err = filepath.Abs("../test_data/iac/k8s/kubernetes_ingress_violation")
iacDir, err = filepath.Abs(filepath.Join(k8sIacRelPath, "kubernetes_ingress_violation"))
})
When("output type is json", func() {
Context("it supports --config-only flag", func() {
Expand Down
19 changes: 10 additions & 9 deletions test/e2e/scan/scan_k8s_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ var _ = Describe("Scan is run for k8s files", func() {

Context("scan iac files violating k8s policies", func() {
var policyDir, iacDir string

policyDir, err1 := filepath.Abs("../test_data/policies/")
iacDir, err2 := filepath.Abs("../test_data/iac/k8s/kubernetes_ingress_violation")
policyDir, err1 := filepath.Abs(policyRootRelPath)
iacDir, err2 := filepath.Abs(filepath.Join(k8sIacRelPath, "kubernetes_ingress_violation"))

It("should not error out while getting absolute path", func() {
Expect(err1).NotTo(HaveOccurred())
Expand All @@ -59,44 +58,46 @@ var _ = Describe("Scan is run for k8s files", func() {
})
})

k8sGoldenRelPath := filepath.Join("golden", "k8s_scans", "k8s", "kubernetes_ingress_violations")

Context("iac type is specified as k8s", func() {
It("should scan and display violations in human output format", func() {
scanArgs := []string{"-i", "k8s", "-p", policyDir, "-d", iacDir}
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, "golden/k8s_scans/k8s/kubernetes_ingress_violations/kubernetes_ingress_human.txt", helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, filepath.Join(k8sGoldenRelPath, "kubernetes_ingress_human.txt"), helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
})

When("-v flag is used for verbose output", func() {
It("should display verbose output for human output format", func() {
scanArgs := []string{"-i", "k8s", "-p", policyDir, "-d", iacDir, "-v"}
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, "golden/k8s_scans/k8s/kubernetes_ingress_violations/kubernetes_ingress_human_verbose.txt", helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, filepath.Join(k8sGoldenRelPath, "kubernetes_ingress_human_verbose.txt"), helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
})
})

When("when output type is json", func() {
It("should display violations in json format", func() {
scanArgs := []string{"-i", "k8s", "-p", policyDir, "-d", iacDir, "-o", "json"}
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, "golden/k8s_scans/k8s/kubernetes_ingress_violations/kubernetes_ingress_json.txt", helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, filepath.Join(k8sGoldenRelPath, "kubernetes_ingress_json.txt"), helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
})
})

When("when output type is yaml", func() {
It("should display violations in yaml format", func() {
scanArgs := []string{"-i", "k8s", "-p", policyDir, "-d", iacDir, "-o", "yaml"}
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, "golden/k8s_scans/k8s/kubernetes_ingress_violations/kubernetes_ingress_yaml.txt", helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, filepath.Join(k8sGoldenRelPath, "kubernetes_ingress_yaml.txt"), helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
})
})

When("when output type is xml", func() {
It("should display violations in xml format", func() {
scanArgs := []string{"-i", "k8s", "-p", policyDir, "-d", iacDir, "-o", "xml"}
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, "golden/k8s_scans/k8s/kubernetes_ingress_violations/kubernetes_ingress_xml.txt", helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, filepath.Join(k8sGoldenRelPath, "kubernetes_ingress_xml.txt"), helper.ExitCodeThree, false, true, outWriter, errWriter, scanArgs...)
})
})

When("when output type is junit-xml", func() {
It("should display violations in junit-xml format", func() {
scanArgs := []string{"-i", "k8s", "-p", policyDir, "-d", iacDir, "-o", "junit-xml"}
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, "golden/k8s_scans/k8s/kubernetes_ingress_violations/kubernetes_ingress_junit_xml.txt", helper.ExitCodeThree, true, true, outWriter, errWriter, scanArgs...)
scanUtils.RunScanAndAssertGoldenOutputRegex(terrascanBinaryPath, filepath.Join(k8sGoldenRelPath, "kubernetes_ingress_junit_xml.txt"), helper.ExitCodeThree, true, true, outWriter, errWriter, scanArgs...)
})
})
})
Expand Down
Loading

0 comments on commit 4df6ce6

Please sign in to comment.