mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-18 09:27:02 +00:00
first simple test for json output
This commit is contained in:
parent
9b129d021a
commit
f718b2c919
2 changed files with 97 additions and 2 deletions
|
|
@ -22,11 +22,11 @@ type jsono struct {
|
||||||
nValid, nInvalid, nErrors, nSkipped int
|
nValid, nInvalid, nErrors, nSkipped int
|
||||||
}
|
}
|
||||||
|
|
||||||
func JSON(w io.Writer, withSummary bool, quiet bool) Output {
|
func JSON(w io.Writer, withSummary bool, verbose bool) Output {
|
||||||
return &jsono{
|
return &jsono{
|
||||||
w: w,
|
w: w,
|
||||||
withSummary: withSummary,
|
withSummary: withSummary,
|
||||||
verbose: quiet,
|
verbose: verbose,
|
||||||
results: []result{},
|
results: []result{},
|
||||||
nValid: 0,
|
nValid: 0,
|
||||||
nInvalid: 0,
|
nInvalid: 0,
|
||||||
|
|
|
||||||
95
pkg/output/json_test.go
Normal file
95
pkg/output/json_test.go
Normal file
|
|
@ -0,0 +1,95 @@
|
||||||
|
package output
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJSONWrite(t *testing.T) {
|
||||||
|
|
||||||
|
type result struct {
|
||||||
|
fileName, kind, version string
|
||||||
|
err error
|
||||||
|
skipped bool
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range []struct {
|
||||||
|
name string
|
||||||
|
withSummary bool
|
||||||
|
verbose bool
|
||||||
|
|
||||||
|
res []result
|
||||||
|
expect string
|
||||||
|
} {
|
||||||
|
{
|
||||||
|
"a single deployment, no summary",
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
[]result{
|
||||||
|
{
|
||||||
|
"deployment.yml",
|
||||||
|
"Deployment",
|
||||||
|
"apps/v1",
|
||||||
|
nil,
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
`{
|
||||||
|
"resources": [
|
||||||
|
{
|
||||||
|
"filename": "deployment.yml",
|
||||||
|
"kind": "Deployment",
|
||||||
|
"version": "apps/v1",
|
||||||
|
"status": "VALID",
|
||||||
|
"msg": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"a single deployment, with summary",
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
[]result{
|
||||||
|
{
|
||||||
|
"deployment.yml",
|
||||||
|
"Deployment",
|
||||||
|
"apps/v1",
|
||||||
|
nil,
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
`{
|
||||||
|
"resources": [
|
||||||
|
{
|
||||||
|
"filename": "deployment.yml",
|
||||||
|
"kind": "Deployment",
|
||||||
|
"version": "apps/v1",
|
||||||
|
"status": "VALID",
|
||||||
|
"msg": ""
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"summary": {
|
||||||
|
"valid": 1,
|
||||||
|
"invalid": 0,
|
||||||
|
"errors": 0,
|
||||||
|
"skipped": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
w := new(bytes.Buffer)
|
||||||
|
o := JSON(w, testCase.withSummary, testCase.verbose)
|
||||||
|
|
||||||
|
for _, res := range testCase.res {
|
||||||
|
o.Write(res.fileName, res.kind, res.version, res.err, res.skipped)
|
||||||
|
}
|
||||||
|
o.Flush()
|
||||||
|
|
||||||
|
if w.String() != testCase.expect {
|
||||||
|
t.Fatalf("%s - expected %s, got %s", testCase.name, testCase.expect, w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue