1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-11-21 23:58:06 +00:00
kube-bench/integration/integration_test.go
Roberto Rojas 813dc6ef47
Integration Tests: Adds generateDiff Function (#561)
* Adds Diff function: Fixes #559

* changes as per PR review
2020-01-21 10:36:04 -05:00

143 lines
3.2 KiB
Go

// +build integration
package integration
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"strings"
"testing"
"time"
)
var kubebenchImg = flag.String("kubebenchImg", "aquasec/kube-bench:latest", "kube-bench image used as part of this test")
var timeout = flag.Duration("timeout", 10*time.Minute, "Test Timeout")
func TestRunWithKind(t *testing.T) {
flag.Parse()
fmt.Printf("kube-bench Container Image: %s\n", *kubebenchImg)
cases := []struct {
TestName string
KubebenchYAML string
ExpectedFile string
ExpectError bool
}{
{
TestName: "kube-bench",
KubebenchYAML: "../job.yaml",
ExpectedFile: "./testdata/job.data",
},
{
TestName: "kube-bench-node",
KubebenchYAML: "../job-node.yaml",
ExpectedFile: "./testdata/job-node.data",
},
{
TestName: "kube-bench-master",
KubebenchYAML: "../job-master.yaml",
ExpectedFile: "./testdata/job-master.data",
},
}
ctx, err := setupCluster("kube-bench", "./testdata/add-tls-kind-k8s114.yaml", *timeout)
if err != nil {
t.Fatalf("failed to setup KIND cluster error: %v", err)
}
defer func() {
ctx.Delete()
}()
if err := loadImageFromDocker(*kubebenchImg, ctx); err != nil {
t.Fatalf("failed to load kube-bench image from Docker to KIND error: %v", err)
}
clientset, err := getClientSet(ctx.KubeConfigPath())
if err != nil {
t.Fatalf("failed to connect to Kubernetes cluster error: %v", err)
}
for _, c := range cases {
t.Run(c.TestName, func(t *testing.T) {
resultData, err := runWithKind(ctx, clientset, c.TestName, c.KubebenchYAML, *kubebenchImg, *timeout)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
c, err := ioutil.ReadFile(c.ExpectedFile)
if err != nil {
t.Error(err)
}
expectedData := strings.TrimSpace(string(c))
resultData = strings.TrimSpace(resultData)
if expectedData != resultData {
t.Errorf("expected results\n\nExpected\t(<)\nResult\t(>)\n\n%s\n\n", generateDiff(expectedData, resultData))
}
})
}
}
// This is simple "diff" between 2 strings containing multiple lines.
// It's not a comprehensive diff between the 2 strings.
// It does not inditcate when lines are deleted.
func generateDiff(source, target string) string {
buf := new(bytes.Buffer)
ss := bufio.NewScanner(strings.NewReader(source))
ts := bufio.NewScanner(strings.NewReader(target))
emptySource := false
emptyTarget := false
loop:
for ln := 1; ; ln++ {
var ll, rl string
sourceScan := ss.Scan()
if sourceScan {
ll = ss.Text()
}
targetScan := ts.Scan()
if targetScan {
rl = ts.Text()
}
switch {
case !sourceScan && !targetScan:
// no more lines
break loop
case sourceScan && targetScan:
if ll != rl {
fmt.Fprintf(buf, "line: %d\n", ln)
fmt.Fprintf(buf, "< %s\n", ll)
fmt.Fprintf(buf, "> %s\n", rl)
}
case !targetScan:
if !emptyTarget {
fmt.Fprintf(buf, "line: %d\n", ln)
}
fmt.Fprintf(buf, "< %s\n", ll)
emptyTarget = true
case !sourceScan:
if !emptySource {
fmt.Fprintf(buf, "line: %d\n", ln)
}
fmt.Fprintf(buf, "> %s\n", rl)
emptySource = true
}
}
if emptySource {
fmt.Fprintf(buf, "< [[NO MORE DATA]]")
}
if emptyTarget {
fmt.Fprintf(buf, "> [[NO MORE DATA]]")
}
return buf.String()
}