mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-24 04:07:02 +00:00
add tests for yaml scanning
This commit is contained in:
parent
c664451a9d
commit
c1f0e9010d
2 changed files with 132 additions and 10 deletions
|
|
@ -87,21 +87,16 @@ func findFilesInFolders(ctx context.Context, paths []string, ignoreFilePatterns
|
||||||
return files, errors
|
return files, errors
|
||||||
}
|
}
|
||||||
|
|
||||||
func findResourcesInFile(p string, resources chan<- Resource, errors chan<- error, buf []byte) {
|
func findResourcesInReader(p string, f io.Reader, resources chan<- Resource, errors chan<- error, buf []byte) {
|
||||||
f, err := os.Open(p)
|
|
||||||
defer f.Close()
|
|
||||||
if err != nil {
|
|
||||||
errors <- DiscoveryError{p, err}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(f)
|
scanner := bufio.NewScanner(f)
|
||||||
scanner.Buffer(buf, len(buf))
|
scanner.Buffer(buf, len(buf))
|
||||||
scanner.Split(SplitYAMLDocument)
|
scanner.Split(SplitYAMLDocument)
|
||||||
nRes := 0
|
nRes := 0
|
||||||
for res := scanner.Scan(); res != false; res = scanner.Scan() {
|
for res := scanner.Scan(); res != false; res = scanner.Scan() {
|
||||||
resources <- Resource{Path: p, Bytes: []byte(scanner.Text())}
|
if len(scanner.Text()) > 0 {
|
||||||
nRes++
|
resources <- Resource{Path: p, Bytes: []byte(scanner.Text())}
|
||||||
|
nRes++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
errors <- DiscoveryError{p, err}
|
errors <- DiscoveryError{p, err}
|
||||||
|
|
@ -111,6 +106,18 @@ func findResourcesInFile(p string, resources chan<- Resource, errors chan<- erro
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func findResourcesInFile(p string, resources chan<- Resource, errors chan<- error, buf []byte) {
|
||||||
|
f, err := os.Open(p)
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
errors <- DiscoveryError{p, err}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
findResourcesInReader(p, f, resources, errors, buf)
|
||||||
|
}
|
||||||
|
|
||||||
func FromFiles(ctx context.Context, paths []string, ignoreFilePatterns []string) (<-chan Resource, <-chan error) {
|
func FromFiles(ctx context.Context, paths []string, ignoreFilePatterns []string) (<-chan Resource, <-chan error) {
|
||||||
resources := make(chan Resource)
|
resources := make(chan Resource)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -87,3 +89,116 @@ func TestIsJSONFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindResourcesInReader(t *testing.T) {
|
||||||
|
maxResourceSize := 4 * 1024 * 1024 // 4MB ought to be enough for everybody
|
||||||
|
buf := make([]byte, maxResourceSize) // We reuse this to avoid multiple large memory allocations
|
||||||
|
|
||||||
|
for i, testCase := range []struct {
|
||||||
|
filePath string
|
||||||
|
yamlData string
|
||||||
|
res []Resource
|
||||||
|
errs []error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"manifest.yaml",
|
||||||
|
``,
|
||||||
|
[]Resource{
|
||||||
|
{
|
||||||
|
Path: "manifest.yaml",
|
||||||
|
Bytes: nil,
|
||||||
|
sig: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"manifest.yaml",
|
||||||
|
`---
|
||||||
|
foo: bar
|
||||||
|
`,
|
||||||
|
[]Resource{
|
||||||
|
{
|
||||||
|
Path: "manifest.yaml",
|
||||||
|
Bytes: []byte("---\nfoo: bar\n"),
|
||||||
|
sig: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"manifest.yaml",
|
||||||
|
`---
|
||||||
|
foo: bar
|
||||||
|
---
|
||||||
|
lorem: ipsum
|
||||||
|
`,
|
||||||
|
[]Resource{
|
||||||
|
{
|
||||||
|
Path: "manifest.yaml",
|
||||||
|
Bytes: []byte("---\nfoo: bar"),
|
||||||
|
sig: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Path: "manifest.yaml",
|
||||||
|
Bytes: []byte("lorem: ipsum\n"),
|
||||||
|
sig: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
res := make(chan Resource)
|
||||||
|
errs := make(chan error)
|
||||||
|
receivedResources := []Resource{}
|
||||||
|
receivedErrs := []error{}
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case receivedResource, ok := <-res:
|
||||||
|
if ok {
|
||||||
|
receivedResources = append(receivedResources, receivedResource)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
res = nil
|
||||||
|
|
||||||
|
case receivedErr, ok := <-errs:
|
||||||
|
if ok {
|
||||||
|
receivedErrs = append(receivedErrs, receivedErr)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
errs = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if res == nil && errs == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
|
r := strings.NewReader(testCase.yamlData)
|
||||||
|
findResourcesInReader(testCase.filePath, r, res, errs, buf)
|
||||||
|
close(res)
|
||||||
|
close(errs)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
if len(receivedResources) != len(testCase.res) {
|
||||||
|
t.Errorf("test %d: expected %d resources, received %d: %+v", i, len(testCase.res), len(receivedResources), receivedResources)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for j, r := range receivedResources {
|
||||||
|
if r.Path != testCase.res[j].Path {
|
||||||
|
t.Errorf("test %d, resource %d, expected path %s, received %s", i, j, testCase.res[j].Path, r.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(r.Bytes) != string(testCase.res[j].Bytes) {
|
||||||
|
t.Errorf("test %d, resource %d, expected Bytes %s, received %s", i, j, string(testCase.res[j].Bytes), string(r.Bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue