From f17cec0b3f8ba5e31e00d9b970e4591f201ff46e Mon Sep 17 00:00:00 2001 From: Phillip Wittrock Date: Tue, 19 May 2020 09:24:22 -0700 Subject: [PATCH] Address kyaml windows compatibility issues --- kyaml/copyutil/copyutil_test.go | 8 +++--- kyaml/fn/runtime/container/container_test.go | 5 +++- .../runtime/runtimeutil/runtimeutil_test.go | 7 +++-- kyaml/fn/runtime/starlark/starlark_test.go | 1 - kyaml/kio/pkgio_reader.go | 3 +++ kyaml/kio/pkgio_writer_test.go | 17 ++++++------ kyaml/kio/tree_test.go | 8 +++--- kyaml/runfn/runfn.go | 25 ++++++++--------- kyaml/runfn/runfn_test.go | 2 +- kyaml/testutil/testutil.go | 27 ++++++++++++++++++- 10 files changed, 70 insertions(+), 33 deletions(-) diff --git a/kyaml/copyutil/copyutil_test.go b/kyaml/copyutil/copyutil_test.go index 02ac7a57b..70e73cd1e 100644 --- a/kyaml/copyutil/copyutil_test.go +++ b/kyaml/copyutil/copyutil_test.go @@ -8,7 +8,6 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "testing" "github.com/stretchr/testify/assert" @@ -216,6 +215,7 @@ func TestSyncFile(t *testing.T) { err = ioutil.WriteFile(f1Name, []byte("abc"), 0600) assert.NoError(t, err) err = ioutil.WriteFile(f2Name, []byte("def"), 0644) + expectedFileInfo, _ := os.Stat(f2Name) assert.NoError(t, err) err = SyncFile(f1Name, f2Name) assert.NoError(t, err) @@ -223,7 +223,7 @@ func TestSyncFile(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "abc", string(actual)) dstFileInfo, _ := os.Stat(f2Name) - assert.Equal(t, "-rw-r--r--", dstFileInfo.Mode().String()) + assert.Equal(t, expectedFileInfo.Mode().String(), dstFileInfo.Mode().String()) } // TestSyncFileNoDestFile tests if new file is created at destination with source file content @@ -242,7 +242,8 @@ func TestSyncFileNoDestFile(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "abc", string(actual)) dstFileInfo, _ := os.Stat(f2Name) - assert.Equal(t, "-rw-r--r--", dstFileInfo.Mode().String()) + srcFileInfo, _ := os.Stat(f1Name) + assert.Equal(t, srcFileInfo.Mode().String(), dstFileInfo.Mode().String()) } // TestSyncFileNoSrcFile tests if destination file is deleted if source file doesn't exist @@ -259,7 +260,6 @@ func TestSyncFileNoSrcFile(t *testing.T) { assert.NoError(t, err) _, err = ioutil.ReadFile(f2Name) assert.Error(t, err) - assert.True(t, strings.Contains(err.Error(), "no such file or directory")) } func TestPrettyFileDiff(t *testing.T) { diff --git a/kyaml/fn/runtime/container/container_test.go b/kyaml/fn/runtime/container/container_test.go index 1c84eda6c..c7fd5dcdd 100644 --- a/kyaml/fn/runtime/container/container_test.go +++ b/kyaml/fn/runtime/container/container_test.go @@ -197,7 +197,10 @@ func TestFilter_ExitCode(t *testing.T) { t.FailNow() } - if !assert.EqualError(t, instance.GetExit(), "fork/exec /not/real/command: no such file or directory") { + if !assert.Error(t, instance.GetExit()) { + t.FailNow() + } + if !assert.Contains(t, instance.GetExit().Error(), "/not/real/command") { t.FailNow() } } diff --git a/kyaml/fn/runtime/runtimeutil/runtimeutil_test.go b/kyaml/fn/runtime/runtimeutil/runtimeutil_test.go index cc474b4d7..60e026fcb 100644 --- a/kyaml/fn/runtime/runtimeutil/runtimeutil_test.go +++ b/kyaml/fn/runtime/runtimeutil/runtimeutil_test.go @@ -426,7 +426,7 @@ metadata: { name: "write_results_bad_results_file", - expectedError: "open /not/real/file: no such file or directory", + expectedError: "open /not/real/file:", noMakeResultsFile: true, run: testRun{ output: ` @@ -1048,7 +1048,10 @@ metadata: } output, err := tt.instance.Filter(inputs) if tt.expectedError != "" { - if !assert.EqualError(t, err, tt.expectedError) { + if !assert.Error(t, err) { + t.FailNow() + } + if !assert.Contains(t, err.Error(), tt.expectedError) { t.FailNow() } return diff --git a/kyaml/fn/runtime/starlark/starlark_test.go b/kyaml/fn/runtime/starlark/starlark_test.go index 8e2ec4eae..d0fc57eb4 100644 --- a/kyaml/fn/runtime/starlark/starlark_test.go +++ b/kyaml/fn/runtime/starlark/starlark_test.go @@ -481,7 +481,6 @@ spec: for i := range tests { test := tests[i] t.Run(test.name, func(t *testing.T) { - os.Clearenv() for k, v := range test.env { os.Setenv(k, v) } diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index a4eeeeac1..854c8d392 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -173,6 +173,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) { if r.PackagePath == "" { return nil, fmt.Errorf("must specify package path") } + + // use slash for path + r.PackagePath = filepath.ToSlash(r.PackagePath) if len(r.MatchFilesGlob) == 0 { r.MatchFilesGlob = DefaultMatch } diff --git a/kyaml/kio/pkgio_writer_test.go b/kyaml/kio/pkgio_writer_test.go index 510573be1..497b5d7f9 100644 --- a/kyaml/kio/pkgio_writer_test.go +++ b/kyaml/kio/pkgio_writer_test.go @@ -10,6 +10,8 @@ import ( "path/filepath" "testing" + "sigs.k8s.io/kustomize/kyaml/testutil" + "github.com/stretchr/testify/assert" . "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/yaml" @@ -186,7 +188,9 @@ func TestLocalPackageWriter_Write_absPath(t *testing.T) { d, node1, node2, node3 := getWriterInputs(t) defer os.RemoveAll(d) - node4, err := yaml.Parse(fmt.Sprintf(`e: f + d = filepath.ToSlash(d) + + n4 := fmt.Sprintf(`e: f g: h: - i # has a list @@ -195,16 +199,13 @@ metadata: annotations: config.kubernetes.io/index: a config.kubernetes.io/path: "%s/a/b/b_test.yaml" # use a different path, should still collide -`, d)) - if !assert.NoError(t, err) { - assert.FailNow(t, err.Error()) - } +`, d) + node4, err := yaml.Parse(n4) + testutil.AssertNoError(t, err, n4) w := LocalPackageWriter{PackagePath: d} err = w.Write([]*yaml.RNode{node2, node1, node3, node4}) - if assert.Error(t, err) { - assert.Contains(t, err.Error(), "package paths may not be absolute paths") - } + testutil.AssertErrorContains(t, err, "package paths may not be absolute paths") } // TestLocalPackageWriter_Write_missingPath tests: diff --git a/kyaml/kio/tree_test.go b/kyaml/kio/tree_test.go index c167d66ca..10e60ed52 100644 --- a/kyaml/kio/tree_test.go +++ b/kyaml/kio/tree_test.go @@ -5,6 +5,8 @@ package kio_test import ( "bytes" + "fmt" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -68,15 +70,15 @@ spec: t.FailNow() } - if !assert.Equal(t, ` + if !assert.Equal(t, fmt.Sprintf(` ├── bar-package │   └── [f2.yaml] Deployment bar └── foo-package ├── [f1.yaml] Deployment default/foo ├── [f1.yaml] Service default/foo - └── foo-package/3 + └── foo-package%s3 └── [f3.yaml] Deployment default/foo -`, out.String()) { +`, string(filepath.Separator)), out.String()) { t.FailNow() } } diff --git a/kyaml/runfn/runfn.go b/kyaml/runfn/runfn.go index 21d85b690..8564f343d 100644 --- a/kyaml/runfn/runfn.go +++ b/kyaml/runfn/runfn.go @@ -271,21 +271,21 @@ func sortFns(buff *kio.PackageBuffer) { // functions deeper in the file system tree should be run first sort.Slice(buff.Nodes, func(i, j int) bool { mi, _ := buff.Nodes[i].GetMeta() - pi := mi.Annotations[kioutil.PathAnnotation] - if path.Base(path.Dir(pi)) == "functions" { + pi := filepath.ToSlash(mi.Annotations[kioutil.PathAnnotation]) + if filepath.Base(path.Dir(pi)) == "functions" { // don't count the functions dir, the functions are scoped 1 level above - pi = path.Dir(path.Dir(pi)) + pi = filepath.Dir(path.Dir(pi)) } else { - pi = path.Dir(pi) + pi = filepath.Dir(pi) } mj, _ := buff.Nodes[j].GetMeta() - pj := mj.Annotations[kioutil.PathAnnotation] - if path.Base(path.Dir(pj)) == "functions" { + pj := filepath.ToSlash(mj.Annotations[kioutil.PathAnnotation]) + if filepath.Base(path.Dir(pj)) == "functions" { // don't count the functions dir, the functions are scoped 1 level above - pj = path.Dir(path.Dir(pj)) + pj = filepath.Dir(path.Dir(pj)) } else { - pj = path.Dir(pj) + pj = filepath.Dir(pj) } // i is "less" than j (comes earlier) if its depth is greater -- e.g. run @@ -365,9 +365,9 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode) (kio.Filter var p string if spec.Starlark.Path != "" { - p = m.Annotations[kioutil.PathAnnotation] - spec.Starlark.Path = path.Clean(spec.Starlark.Path) - if path.IsAbs(spec.Starlark.Path) { + p = filepath.ToSlash(path.Clean(m.Annotations[kioutil.PathAnnotation])) + spec.Starlark.Path = filepath.ToSlash(path.Clean(spec.Starlark.Path)) + if filepath.IsAbs(spec.Starlark.Path) || path.IsAbs(spec.Starlark.Path) { return nil, errors.Errorf( "absolute function path %s not allowed", spec.Starlark.Path) } @@ -375,8 +375,9 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode) (kio.Filter return nil, errors.Errorf( "function path %s not allowed to start with ../", spec.Starlark.Path) } - p = path.Join(r.Path, path.Dir(p), spec.Starlark.Path) + p = filepath.ToSlash(filepath.Join(r.Path, filepath.Dir(p), spec.Starlark.Path)) } + fmt.Println(p) sf := &starlark.Filter{Name: spec.Starlark.Name, Path: p, URL: spec.Starlark.URL} diff --git a/kyaml/runfn/runfn_test.go b/kyaml/runfn/runfn_test.go index 787641333..860bae9a2 100644 --- a/kyaml/runfn/runfn_test.go +++ b/kyaml/runfn/runfn_test.go @@ -525,7 +525,7 @@ metadata: enableStarlark: true, outFn: func(path string) []string { return []string{ - fmt.Sprintf("name: path: %s/foo/a/b/c url: program:", path)} + fmt.Sprintf("name: path: %s/foo/a/b/c url: program:", filepath.ToSlash(path))} }, }, diff --git a/kyaml/testutil/testutil.go b/kyaml/testutil/testutil.go index aa4a3e866..100ad9684 100644 --- a/kyaml/testutil/testutil.go +++ b/kyaml/testutil/testutil.go @@ -1,13 +1,19 @@ // Copyright 2019 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -package testutil_test +package testutil import ( "bytes" "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/yaml" + + "testing" + + goerrors "github.com/go-errors/errors" + + "github.com/stretchr/testify/assert" ) func UpdateYamlString(doc string, functions ...yaml.Filter) (string, error) { @@ -34,3 +40,22 @@ func UpdateYamlBytes(b []byte, function ...yaml.Filter) ([]byte, error) { }.Execute() return out.Bytes(), err } + +func AssertErrorContains(t *testing.T, err error, value string, msg ...string) { + if !assert.Error(t, err, msg) { + t.FailNow() + } + if !assert.Contains(t, err.Error(), value, msg) { + t.FailNow() + } +} + +func AssertNoError(t *testing.T, err error, msg ...string) { + if !assert.NoError(t, err, msg) { + gerr, ok := err.(*goerrors.Error) + if ok { + t.Fatal(string(gerr.Stack())) + } + t.FailNow() + } +}