Address kyaml windows compatibility issues

This commit is contained in:
Phillip Wittrock
2020-05-19 09:24:22 -07:00
parent 765a4888df
commit f17cec0b3f
10 changed files with 70 additions and 33 deletions

View File

@@ -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) {

View File

@@ -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()
}
}

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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:

View File

@@ -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()
}
}

View File

@@ -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}

View File

@@ -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))}
},
},

View File

@@ -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()
}
}