mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2025-05-05 08:29:19 +00:00
* issue #335: Adds json/yaml unmarshal Unit Tests. * issue #335: Adds jsonpath Unit Tests. * issue #335: Removes log package.
This commit is contained in:
parent
3926ba3977
commit
d43cdfdf01
@ -56,8 +56,8 @@ type compare struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type testOutput struct {
|
type testOutput struct {
|
||||||
testResult bool
|
testResult bool
|
||||||
actualResult string
|
actualResult string
|
||||||
ExpectedResult string
|
ExpectedResult string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,36 +76,22 @@ func (t *testItem) execute(s string) *testOutput {
|
|||||||
} else {
|
} else {
|
||||||
// Path != "" - we don't know whether it's YAML or JSON but
|
// Path != "" - we don't know whether it's YAML or JSON but
|
||||||
// we can just try one then the other
|
// we can just try one then the other
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
var jsonInterface interface{}
|
var jsonInterface interface{}
|
||||||
|
|
||||||
if t.Path != "" {
|
if t.Path != "" {
|
||||||
err := json.Unmarshal([]byte(s), &jsonInterface)
|
err := unmarshal(s, &jsonInterface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := yaml.Unmarshal([]byte(s), &jsonInterface)
|
fmt.Fprintf(os.Stderr, "failed to load YAML or JSON from provided input \"%s\": %v\n", s, err)
|
||||||
if err != nil {
|
return failTestItem("failed to load YAML or JSON")
|
||||||
fmt.Fprintf(os.Stderr, "failed to load YAML or JSON from provided input \"%s\": %v\n", s, err)
|
|
||||||
return failTestItem("failed to load YAML or JSON")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the jsonpath/yamlpath expression...
|
jsonpathResult, err := executeJSONPath(t.Path, &jsonInterface)
|
||||||
j := jsonpath.New("jsonpath")
|
|
||||||
j.AllowMissingKeys(true)
|
|
||||||
err := j.Parse(t.Path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "unable to parse path expression \"%s\": %v\n", t.Path, err)
|
fmt.Fprintf(os.Stderr, "unable to parse path expression \"%s\": %v\n", t.Path, err)
|
||||||
return failTestItem("unable to parse path expression")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = j.Execute(buf, jsonInterface)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "error executing path expression \"%s\": %v\n", t.Path, err)
|
|
||||||
return failTestItem("error executing path expression")
|
return failTestItem("error executing path expression")
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonpathResult := fmt.Sprintf("%s", buf)
|
|
||||||
match = (jsonpathResult != "")
|
match = (jsonpathResult != "")
|
||||||
flagVal = jsonpathResult
|
flagVal = jsonpathResult
|
||||||
}
|
}
|
||||||
@ -205,6 +191,35 @@ func (t *testItem) execute(s string) *testOutput {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unmarshal(s string, jsonInterface *interface{}) error {
|
||||||
|
data := []byte(s)
|
||||||
|
err := json.Unmarshal(data, jsonInterface)
|
||||||
|
if err != nil {
|
||||||
|
err := yaml.Unmarshal(data, jsonInterface)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeJSONPath(path string, jsonInterface interface{}) (string, error) {
|
||||||
|
j := jsonpath.New("jsonpath")
|
||||||
|
j.AllowMissingKeys(true)
|
||||||
|
err := j.Parse(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err = j.Execute(buf, jsonInterface)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
jsonpathResult := fmt.Sprintf("%s", buf)
|
||||||
|
return jsonpathResult, nil
|
||||||
|
}
|
||||||
|
|
||||||
type tests struct {
|
type tests struct {
|
||||||
TestItems []*testItem `yaml:"test_items"`
|
TestItems []*testItem `yaml:"test_items"`
|
||||||
BinOp binOp `yaml:"bin_op"`
|
BinOp binOp `yaml:"bin_op"`
|
||||||
|
@ -201,3 +201,124 @@ func TestTestExecuteExceptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTestUnmarshal(t *testing.T) {
|
||||||
|
type kubeletConfig struct {
|
||||||
|
Kind string
|
||||||
|
ApiVersion string
|
||||||
|
Address string
|
||||||
|
}
|
||||||
|
cases := []struct {
|
||||||
|
content string
|
||||||
|
jsonInterface interface{}
|
||||||
|
expectedToFail bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
`{
|
||||||
|
"kind": "KubeletConfiguration",
|
||||||
|
"apiVersion": "kubelet.config.k8s.io/v1beta1",
|
||||||
|
"address": "0.0.0.0"
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
kubeletConfig{},
|
||||||
|
false,
|
||||||
|
}, {
|
||||||
|
`
|
||||||
|
kind: KubeletConfiguration
|
||||||
|
address: 0.0.0.0
|
||||||
|
apiVersion: kubelet.config.k8s.io/v1beta1
|
||||||
|
authentication:
|
||||||
|
anonymous:
|
||||||
|
enabled: false
|
||||||
|
webhook:
|
||||||
|
cacheTTL: 2m0s
|
||||||
|
enabled: true
|
||||||
|
x509:
|
||||||
|
clientCAFile: /etc/kubernetes/pki/ca.crt
|
||||||
|
tlsCipherSuites:
|
||||||
|
- TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
|
||||||
|
- TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
|
||||||
|
`,
|
||||||
|
kubeletConfig{},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`
|
||||||
|
kind: ddress: 0.0.0.0
|
||||||
|
apiVersion: kubelet.config.k8s.io/v1beta
|
||||||
|
`,
|
||||||
|
kubeletConfig{},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
err := unmarshal(c.content, &c.jsonInterface)
|
||||||
|
if err != nil {
|
||||||
|
if !c.expectedToFail {
|
||||||
|
t.Errorf("%s, expectedToFail:%v, got:%v\n", c.content, c.expectedToFail, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if c.expectedToFail {
|
||||||
|
t.Errorf("%s, expectedToFail:%v, got:Did not fail\n", c.content, c.expectedToFail)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExecuteJSONPath(t *testing.T) {
|
||||||
|
type kubeletConfig struct {
|
||||||
|
Kind string
|
||||||
|
ApiVersion string
|
||||||
|
Address string
|
||||||
|
}
|
||||||
|
cases := []struct {
|
||||||
|
jsonPath string
|
||||||
|
jsonInterface kubeletConfig
|
||||||
|
expectedResult string
|
||||||
|
expectedToFail bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// JSONPath parse works, results don't match
|
||||||
|
"{.Kind}",
|
||||||
|
kubeletConfig{
|
||||||
|
Kind: "KubeletConfiguration",
|
||||||
|
ApiVersion: "kubelet.config.k8s.io/v1beta1",
|
||||||
|
Address: "127.0.0.0",
|
||||||
|
},
|
||||||
|
"blah",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// JSONPath parse works, results match
|
||||||
|
"{.Kind}",
|
||||||
|
kubeletConfig{
|
||||||
|
Kind: "KubeletConfiguration",
|
||||||
|
ApiVersion: "kubelet.config.k8s.io/v1beta1",
|
||||||
|
Address: "127.0.0.0",
|
||||||
|
},
|
||||||
|
"KubeletConfiguration",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// JSONPath parse fails
|
||||||
|
"{.ApiVersion",
|
||||||
|
kubeletConfig{
|
||||||
|
Kind: "KubeletConfiguration",
|
||||||
|
ApiVersion: "kubelet.config.k8s.io/v1beta1",
|
||||||
|
Address: "127.0.0.0",
|
||||||
|
},
|
||||||
|
"",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
result, err := executeJSONPath(c.jsonPath, c.jsonInterface)
|
||||||
|
if err != nil && !c.expectedToFail {
|
||||||
|
t.Fatalf("jsonPath:%q, expectedResult:%q got:%v\n", c.jsonPath, c.expectedResult, err)
|
||||||
|
}
|
||||||
|
if c.expectedResult != result && !c.expectedToFail {
|
||||||
|
t.Errorf("jsonPath:%q, expectedResult:%q got:%q\n", c.jsonPath, c.expectedResult, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user