From 51e5f3843012cff102bd6ea6dfbcebe944a9360e Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sun, 1 Nov 2020 22:41:18 +0100 Subject: [PATCH] Add unit tests for reading resources from an io.Reader --- pkg/resource/stream_test.go | 183 ++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 pkg/resource/stream_test.go diff --git a/pkg/resource/stream_test.go b/pkg/resource/stream_test.go new file mode 100644 index 0000000..61549e7 --- /dev/null +++ b/pkg/resource/stream_test.go @@ -0,0 +1,183 @@ +package resource_test + +import ( + "bytes" + "github.com/yannh/kubeconform/pkg/resource" + "io" + "reflect" + "strings" + "sync" + "testing" +) + + + +func TestFromStream(t *testing.T) { + type have struct { + Path string + Reader io.Reader + } + + type want struct { + Resources []resource.Resource + Errors []error + } + + testCases := []struct { + Have have + Want want + } { + { + Have: have { + Path: "myfile", + Reader: strings.NewReader(` +apiVersion: v1 +kind: ReplicationController +`), + }, + Want: want{ + Resources: []resource.Resource{ + { + Path: "myfile", + Bytes: []byte(` +apiVersion: v1 +kind: ReplicationController +`), + }, + }, + Errors: []error{}, + }, + }, + { + Have: have { + Path: "myfile", + Reader: strings.NewReader(`apiVersion: v1 +--- +--- +apiVersion: v2 +`), + }, + Want: want{ + Resources: []resource.Resource{ + { + Path: "myfile", + Bytes: []byte(`apiVersion: v1 +`), + }, + { + Path: "myfile", + Bytes: []byte(``), + }, + { + Path: "myfile", + Bytes: []byte(`apiVersion: v2 +`), + }, + }, + Errors: []error{}, + }, + }, + { + Have: have { + Path: "myfile", + Reader: strings.NewReader(`apiVersion: v1 +kind: ReplicationController +--- +apiVersion: v1 +kind: Deployment +--- +apiVersion: v2 +kind: CronJob +`), + }, + Want: want{ + Resources: []resource.Resource{ + { + Path: "myfile", + Bytes: []byte(`apiVersion: v1 +kind: ReplicationController +`), + }, + { + Path: "myfile", + Bytes: []byte(`apiVersion: v1 +kind: Deployment +`), + }, + { + Path: "myfile", + Bytes: []byte(`apiVersion: v2 +kind: CronJob +`), + }, + }, + Errors: []error{}, + }, + }, + { + Have: have { + Path: "myfile", + Reader: strings.NewReader(`apiVersion: v1 +kind: ReplicationController +--- +apiVersion: v1 +kind: Deployment +`), + }, + Want: want{ + Resources: []resource.Resource{ + { + Path: "myfile", + Bytes: []byte(`apiVersion: v1 +kind: ReplicationController +`), + }, + { + Path: "myfile", + Bytes: []byte(`apiVersion: v1 +kind: Deployment +`), + }, + }, + Errors: []error{}, + }, + }, + } + + for _, testCase := range testCases { + resChan, errChan :=resource.FromStream(testCase.Have.Path, testCase.Have.Reader) + var wg sync.WaitGroup + + wg.Add(2) + go func() { + res := []resource.Resource{} + for r := range resChan { + res = append(res, r) + } + + if len(testCase.Want.Resources) != len(res) { + t.Errorf("expected %d resources, got %d", len(testCase.Want.Resources), len(res)) + } + for i, v := range res { + if bytes.Compare(v.Bytes, testCase.Want.Resources[i].Bytes) != 0 { + t.Errorf("for resource %d, got '%s', expected '%s'", i, string(res[i].Bytes), string(testCase.Want.Resources[i].Bytes)) + } + } + + wg.Done() + }() + + go func() { + errs := []error{} + for e := range errChan { + errs = append(errs, e) + } + if reflect.DeepEqual(testCase.Want.Errors, errs) == false { + t.Errorf("expected error %+s, got %+s", testCase.Want.Errors, errs) + } + wg.Done() + }() + + wg.Wait() + } +} \ No newline at end of file