1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2025-07-07 07:12:37 +00:00
kube-bench/cmd/run_test.go
Liz Rice f2caa1f0ec
Add run subcommand (#529)
* test: fix TestGetConfigFilePath

This test wasn't correctly creating the test file due to the wrong directory permissions on the temp file. This wasn't detected due to a lack of error checking.

Also, the code was only checking for file not exist rather than lack of permission to read file (or any other error).

The combination of these two things means the test wasn't checking what it thought it was checking, and passed more by luck than judgment.

* add getYamlFilesFromDir

* add getTestYamlFiles and test

* docs: Update master / node help text

* return path + filename from getYamlFilesFromDir

* subcommand run to run specific section files
2019-12-02 15:40:44 +00:00

85 lines
1.8 KiB
Go

package cmd
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestGetTestYamlFiles(t *testing.T) {
cases := []struct {
name string
sections []string
benchmark string
succeed bool
expCount int
}{
{
name: "Specify two sections",
sections: []string{"one", "two"},
benchmark: "benchmark",
succeed: true,
expCount: 2,
},
{
name: "Specify a section that doesn't exist",
sections: []string{"one", "missing"},
benchmark: "benchmark",
succeed: false,
},
{
name: "No sections specified - should return everything except config.yaml",
sections: []string{},
benchmark: "benchmark",
succeed: true,
expCount: 3,
},
{
name: "Specify benchmark that doesn't exist",
sections: []string{"one"},
benchmark: "missing",
succeed: false,
},
}
// Set up temp config directory
var err error
cfgDir, err = ioutil.TempDir("", "kube-bench-test")
if err != nil {
t.Fatalf("Failed to create temp directory")
}
defer os.RemoveAll(cfgDir)
d := filepath.Join(cfgDir, "benchmark")
err = os.Mkdir(d, 0766)
if err != nil {
t.Fatalf("Failed to create temp dir")
}
// We never expect config.yaml to be returned
for _, filename := range []string{"one.yaml", "two.yaml", "three.yaml", "config.yaml"} {
err = ioutil.WriteFile(filepath.Join(d, filename), []byte("hello world"), 0666)
if err != nil {
t.Fatalf("error writing temp file %s: %v", filename, err)
}
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
yamlFiles, err := getTestYamlFiles(c.sections, c.benchmark)
if err != nil && c.succeed {
t.Fatalf("Error %v", err)
}
if err == nil && !c.succeed {
t.Fatalf("Expected failure")
}
if len(yamlFiles) != c.expCount {
t.Fatalf("Expected %d, got %d", c.expCount, len(yamlFiles))
}
})
}
}