Skip to content

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed Feb 23, 2021
1 parent 218adac commit e2722e6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
5 changes: 4 additions & 1 deletion test/e2e/help/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ var _ = Describe("Help", func() {
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, 0, "golden/help_scan.txt", true)
sessionBytes, fileBytes := helper.GetByteData(session, "golden/help_scan.txt", true)
sessionBytes = helper.TerraformIacVersion.ReplaceAll(sessionBytes, []byte(""))
fileBytes = helper.TerraformIacVersion.ReplaceAll(fileBytes, []byte(""))
Expect(string(sessionBytes)).Should(Equal(string(fileBytes)))
})
})

Expand Down
8 changes: 4 additions & 4 deletions test/e2e/scan/scan_config_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var _ = Describe("Scan With Config Only Flag", func() {
Eventually(session, 5).Should(gexec.Exit(3))
goldenFileAbsPath, err := filepath.Abs("golden/config_only/config_only_tf.json")
Expect(err).NotTo(HaveOccurred())
helper.CompareActualWithGolden(session, goldenFileAbsPath, true)
helper.CompareActualWithGoldenConfigOnlyRegex(session, goldenFileAbsPath, true)
})
})
})
Expand All @@ -85,7 +85,7 @@ var _ = Describe("Scan With Config Only Flag", func() {
Eventually(session, 5).Should(gexec.Exit(3))
goldenFileAbsPath, err := filepath.Abs("golden/config_only/config_only_tf.yaml")
Expect(err).NotTo(HaveOccurred())
helper.CompareActualWithGolden(session, goldenFileAbsPath, true)
helper.CompareActualWithGoldenConfigOnlyRegex(session, goldenFileAbsPath, true)
})
})
})
Expand All @@ -102,7 +102,7 @@ var _ = Describe("Scan With Config Only Flag", func() {
Eventually(session, 5).Should(gexec.Exit(3))
goldenFileAbsPath, err := filepath.Abs("golden/config_only/config_only_k8s.json")
Expect(err).NotTo(HaveOccurred())
helper.CompareActualWithGolden(session, goldenFileAbsPath, true)
helper.CompareActualWithGoldenConfigOnlyRegex(session, goldenFileAbsPath, true)
})
})
})
Expand All @@ -114,7 +114,7 @@ var _ = Describe("Scan With Config Only Flag", func() {
Eventually(session, 5).Should(gexec.Exit(3))
goldenFileAbsPath, err := filepath.Abs("golden/config_only/config_only_k8s.yaml")
Expect(err).NotTo(HaveOccurred())
helper.CompareActualWithGolden(session, goldenFileAbsPath, true)
helper.CompareActualWithGoldenConfigOnlyRegex(session, goldenFileAbsPath, true)
})
})
})
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/scan/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ var _ = Describe("Scan", func() {
It("should print help for scan and exit with status code 0", func() {
session = helper.RunCommand(terrascanBinaryPath, outWriter, errWriter, scanComand, "-h")
Eventually(session).Should(gexec.Exit(0))
goldenFileAbsPath, err := filepath.Abs("golden/scan_help.txt")
Expect(err).NotTo(HaveOccurred())
helper.CompareActualWithGolden(session, goldenFileAbsPath, true)
sessionBytes, fileBytes := helper.GetByteData(session, "golden/scan_help.txt", true)
sessionBytes = helper.TerraformIacVersion.ReplaceAll(sessionBytes, []byte(""))
fileBytes = helper.TerraformIacVersion.ReplaceAll(fileBytes, []byte(""))
Expect(string(sessionBytes)).Should(Equal(string(fileBytes)))
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/scan/scan_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
// RunScanCommandAndAssertTextOutput runs the scan command with supplied paramters and compares actual and golden output
func RunScanCommandAndAssertTextOutput(terrascanBinaryPath, relGoldenFilePath string, exitCode int, isJunitXML, isStdOut bool, outWriter, errWriter io.Writer, args ...string) {
session, goldenFileAbsPath := RunScanCommand(terrascanBinaryPath, relGoldenFilePath, exitCode, outWriter, errWriter, args...)
helper.CompareActualWithGoldenRegex(session, goldenFileAbsPath, isJunitXML, isStdOut)
helper.CompareActualWithGoldenSummaryRegex(session, goldenFileAbsPath, isJunitXML, isStdOut)
}

// RunScanCommandAndAssertJSONOutput runs the scan command with supplied paramters and compares actual and golden output
Expand Down
78 changes: 41 additions & 37 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,42 @@ import (
)

var (
// ScannedAtRegex is regex for 'scanned at' attribute in violations output
ScannedAtRegex = regexp.MustCompile(`["]*[sS]canned[ _][aA]t["]*[ \t]*[:=][ \t]*["]*[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]{3,6} [+-][0-9]{4} UTC["]*[,]{0,1}`)
// FileFolderRegex is regex for 'file/folder' attribute in violations output
FileFolderRegex = regexp.MustCompile(`["]*[fF]ile\/[fF]older["]*[ \t]*[:=][ \t]*["]*(.+)\/(.+)["]*`)
// PackageRegex is regex for 'package' attribute in junit-xml output
PackageRegex = regexp.MustCompile(`package=["]*(.+)\/(.+)["]*`)
// VersionValueRegex is regex for 'value' attribute in junit-xml output (which is terrascan version)
VersionValueRegex = regexp.MustCompile(`value="v[1][\.][0-9][\.][0-9]"`)
// ScannedAt is regex for 'scanned at' attribute in violations output
ScannedAt = regexp.MustCompile(`["]*[sS]canned[ _][aA]t["]*[ \t]*[:=][ \t]*["]*[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]{1,9} [+-][0-9]{4} UTC["]*[,]{0,1}`)

// FileFolder is regex for 'file/folder' attribute in violations output
FileFolder = regexp.MustCompile(`["]*[fF]ile_[fF]older["]*[ \t]*[:=][ \t]*["]*(.+)\/(.+)["]*`)

// Package is regex for 'package' attribute in junit-xml output
Package = regexp.MustCompile(`package=["]*(.+)\/(.+)["]*`)

// VersionValue is regex for 'value' attribute in junit-xml output (which is terrascan version)
VersionValue = regexp.MustCompile(`value="v[1][\.][0-9][\.][0-9]"`)

// TerraformIacVersion is regex for terraform iac version displayed in help for scan command
TerraformIacVersion = regexp.MustCompile(`terraform: +([v][1][1-4], ){2}([v][1][1-4])`)

// SourceRegex is regex for 'file/folder' attribute in violations output
SourceRegex = regexp.MustCompile(`["]*source["]*[ \t]*[:][ \t]*["]*(.+)\/(.+)["]*`)
)

// CompareActualWithGolden compares actual string with contents of golden file path passed as parameter
func CompareActualWithGolden(session *gexec.Session, goldenFileAbsPath string, isStdOut bool) {
fileData, err := ioutil.ReadFile(goldenFileAbsPath)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

var sessionContents, fileContents string

fileContents = string(fileData)
if isStdOut {
sessionContents = string(session.Wait().Out.Contents())
} else {
sessionContents = string(session.Wait().Err.Contents())
}
sessionBytes, fileBytes := GetByteData(session, goldenFileAbsPath, isStdOut)
gomega.Expect(string(sessionBytes)).Should(gomega.Equal(string(fileBytes)))
}

fileContents = strings.TrimSpace(fileContents)
sessionContents = strings.TrimSpace(sessionContents)
gomega.Expect(sessionContents).Should(gomega.BeIdenticalTo(fileContents))
// CompareActualWithGoldenConfigOnlyRegex compares actual string with contents of golden file path passed as parameter
func CompareActualWithGoldenConfigOnlyRegex(session *gexec.Session, goldenFileAbsPath string, isStdOut bool) {
sessionBytes, fileBytes := GetByteData(session, goldenFileAbsPath, isStdOut)
sessionBytes = SourceRegex.ReplaceAll(sessionBytes, []byte(""))
fileBytes = SourceRegex.ReplaceAll(fileBytes, []byte(""))
gomega.Expect(string(sessionBytes)).Should(gomega.Equal(string(fileBytes)))
}

// CompareActualWithGoldenRegex compares actual string with contents of golden file passed as parameter
// CompareActualWithGoldenSummaryRegex compares actual string with contents of golden file passed as parameter
// ignores specified regex patterns from the actual and golden text
func CompareActualWithGoldenRegex(session *gexec.Session, goldenFileAbsPath string, isJunitXML, isStdOut bool) {
func CompareActualWithGoldenSummaryRegex(session *gexec.Session, goldenFileAbsPath string, isJunitXML, isStdOut bool) {
fileData, err := ioutil.ReadFile(goldenFileAbsPath)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
var sessionOutput, fileContents string
Expand All @@ -70,25 +74,25 @@ func CompareActualWithGoldenRegex(session *gexec.Session, goldenFileAbsPath stri
fileContents = strings.TrimSpace(fileContents)

if isJunitXML {
sessionOutput = PackageRegex.ReplaceAllString(sessionOutput, "")
fileContents = PackageRegex.ReplaceAllString(fileContents, "")
sessionOutput = Package.ReplaceAllString(sessionOutput, "")
fileContents = Package.ReplaceAllString(fileContents, "")

sessionOutput = VersionValueRegex.ReplaceAllString(sessionOutput, "")
fileContents = VersionValueRegex.ReplaceAllString(fileContents, "")
sessionOutput = VersionValue.ReplaceAllString(sessionOutput, "")
fileContents = VersionValue.ReplaceAllString(fileContents, "")
} else {
sessionOutput = ScannedAtRegex.ReplaceAllString(sessionOutput, "")
fileContents = ScannedAtRegex.ReplaceAllString(fileContents, "")
sessionOutput = ScannedAt.ReplaceAllString(sessionOutput, "")
fileContents = ScannedAt.ReplaceAllString(fileContents, "")

sessionOutput = FileFolderRegex.ReplaceAllString(sessionOutput, "")
fileContents = FileFolderRegex.ReplaceAllString(fileContents, "")
sessionOutput = FileFolder.ReplaceAllString(sessionOutput, "")
fileContents = FileFolder.ReplaceAllString(fileContents, "")
}

gomega.Expect(sessionOutput).Should(gomega.BeIdenticalTo(fileContents))
}

// CompareActualWithGoldenJSON compares actual data with contents of golden file passed as parameter
func CompareActualWithGoldenJSON(session *gexec.Session, goldenFileAbsPath string, isStdOut bool) {
sessionBytes, fileBytes := getByteData(session, goldenFileAbsPath, isStdOut)
sessionBytes, fileBytes := GetByteData(session, goldenFileAbsPath, isStdOut)

var sessionEngineOutput, fileDataEngineOutput policy.EngineOutput

Expand All @@ -102,7 +106,7 @@ func CompareActualWithGoldenJSON(session *gexec.Session, goldenFileAbsPath strin

// CompareActualWithGoldenYAML compares actual data with contents of golden file passed as parameter
func CompareActualWithGoldenYAML(session *gexec.Session, goldenFileAbsPath string, isStdOut bool) {
sessionBytes, fileBytes := getByteData(session, goldenFileAbsPath, isStdOut)
sessionBytes, fileBytes := GetByteData(session, goldenFileAbsPath, isStdOut)

var sessionEngineOutput, fileDataEngineOutput policy.EngineOutput

Expand All @@ -116,7 +120,7 @@ func CompareActualWithGoldenYAML(session *gexec.Session, goldenFileAbsPath strin

// CompareActualWithGoldenXML compares actual data with contents of golden file passed as parameter
func CompareActualWithGoldenXML(session *gexec.Session, goldenFileAbsPath string, isStdOut bool) {
sessionBytes, fileBytes := getByteData(session, goldenFileAbsPath, isStdOut)
sessionBytes, fileBytes := GetByteData(session, goldenFileAbsPath, isStdOut)

var sessionEngineOutput, fileDataEngineOutput policy.EngineOutput

Expand Down Expand Up @@ -157,8 +161,8 @@ func RunCommand(path string, outWriter, errWriter io.Writer, args ...string) *ge
return session
}

// getByteData is a helper function to get data in byte slice from session and golden file
func getByteData(session *gexec.Session, goldenFileAbsPath string, isStdOut bool) ([]byte, []byte) {
// GetByteData is a helper function to get data in byte slice from session and golden file
func GetByteData(session *gexec.Session, goldenFileAbsPath string, isStdOut bool) ([]byte, []byte) {
fileBytes, err := ioutil.ReadFile(goldenFileAbsPath)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
var sessionBytes []byte
Expand Down

0 comments on commit e2722e6

Please sign in to comment.