Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test file needed for testing CaC content #493

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ test-benchmark: ## Run the benchmark tests -- Note that this can only be ran for
@$(GO) test -cpuprofile cpu.prof -memprofile mem.prof -bench . $(TEST_OPTIONS) $(BENCHMARK_PKG)
@echo "The pprof files generated are: cpu.prof and mem.prof"

.PHONY: test-datastreams
test-datastreams:
rhmdnd marked this conversation as resolved.
Show resolved Hide resolved
ifndef DEFAULT_CONTENT_DS_FILE_PATH
$(error DEFAULT_CONTENT_DS_FILE_PATH is not set)
endif
@echo "Testing all XCCDF files in $(DEFAULT_CONTENT_DS_FILE_PATH)"
@$(GO) test -v ./pkg/utils/ -ginkgo.v

.PHONY: e2e
e2e: e2e-set-image prep-e2e e2e-parallel e2e-test-wait e2e-serial ## Run full end-to-end tests that exercise content on an operational cluster.

Expand Down
88 changes: 88 additions & 0 deletions pkg/utils/parse_arf_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/antchfx/xmlquery"
Expand Down Expand Up @@ -882,4 +883,91 @@ Server 3.fedora.pool.ntp.org`

})
})

Describe("Testing for correct content parsing", func() {
defer GinkgoRecover()
Describe("Searching and testing all XCCDF files", func() {
Context("Validating XCCDF content", func() {
dsFilePath := os.Getenv("DEFAULT_CONTENT_DS_FILE_PATH")
if dsFilePath != "" {
fileList, err := filepath.Glob(filepath.Join(dsFilePath, "*.xml"))
Expect(err).NotTo(HaveOccurred())
Expect(fileList).NotTo(BeEmpty(), "No XCCDF files found in path, please check the path in DEFAULT_CONTENT_DS_FILE_PATH `env` variable")

for _, dsFilename := range fileList {
By(fmt.Sprintf("Testing file: %s", dsFilename), func() {
ds, err := os.Open(dsFilename)
Expect(err).NotTo(HaveOccurred())
defer ds.Close()

dsDom, err := ParseContent(ds)
Expect(err).NotTo(HaveOccurred())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: This Expect() is redundant given the following It() and Expect() clause, right?


It("Should parse the XCCDF without errors", func() {
Expect(err).NotTo(HaveOccurred())
})

// printout dsDom xml structure
fmt.Println("dsDom structure for", dsFilename)
printUniquePaths(dsDom, "", make(map[string]bool))

It("Should parse rules without errors", func() {
ruleTable := newRuleHashTable(dsDom)
Expect(ruleTable).NotTo(BeEmpty())
})

It("Should parse questionsTable without errors", func() {
questionsTable := NewOcilQuestionTable(dsDom)
Expect(questionsTable).NotTo(BeEmpty())
})

It("Should parse defTable without errors", func() {
defTable := NewDefHashTable(dsDom)
Expect(defTable).NotTo(BeEmpty())
})

It("Should parse ovalTestVarTable without errors", func() {
statesTable := newStateHashTable(dsDom)
Expect(statesTable).NotTo(BeEmpty())
objsTable := newObjHashTable(dsDom)
Expect(objsTable).NotTo(BeEmpty())
ovalTestVarTable := newValueListTable(dsDom, statesTable, objsTable)
Expect(ovalTestVarTable).NotTo(BeEmpty())
})
})
}
} else {
Skip("Skipping test for new content parsing as DEFAULT_CONTENT_DS_FILE_PATH env variable is not set")
}
})
})
})

})

// printUniquePaths prints all unique paths within an XML document, starting from a given node.
func printUniquePaths(node *xmlquery.Node, currentPath string, visitedPaths map[string]bool) {
// Construct the path for the current node.
path := currentPath
if node.Type == xmlquery.ElementNode { // Ensure it's an element node.
if path != "" {
path += "/"
}
// Append the namespace prefix if available.
if node.Prefix != "" {
path += node.Prefix + ":"
}
path += node.Data
}

// Print the path if it hasn't been visited yet.
if _, visited := visitedPaths[path]; !visited && path != "" {
fmt.Println(path)
visitedPaths[path] = true
}

// Recurse for each child node.
for child := node.FirstChild; child != nil; child = child.NextSibling {
printUniquePaths(child, path, visitedPaths)
}
}
Loading