mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Merge pull request #2501 from pwittrock/windows
Address kyaml windows compatibility issues
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -216,6 +215,7 @@ func TestSyncFile(t *testing.T) {
|
|||||||
err = ioutil.WriteFile(f1Name, []byte("abc"), 0600)
|
err = ioutil.WriteFile(f1Name, []byte("abc"), 0600)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
err = ioutil.WriteFile(f2Name, []byte("def"), 0644)
|
err = ioutil.WriteFile(f2Name, []byte("def"), 0644)
|
||||||
|
expectedFileInfo, _ := os.Stat(f2Name)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
err = SyncFile(f1Name, f2Name)
|
err = SyncFile(f1Name, f2Name)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@@ -223,7 +223,7 @@ func TestSyncFile(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "abc", string(actual))
|
assert.Equal(t, "abc", string(actual))
|
||||||
dstFileInfo, _ := os.Stat(f2Name)
|
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
|
// 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.NoError(t, err)
|
||||||
assert.Equal(t, "abc", string(actual))
|
assert.Equal(t, "abc", string(actual))
|
||||||
dstFileInfo, _ := os.Stat(f2Name)
|
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
|
// 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)
|
assert.NoError(t, err)
|
||||||
_, err = ioutil.ReadFile(f2Name)
|
_, err = ioutil.ReadFile(f2Name)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.True(t, strings.Contains(err.Error(), "no such file or directory"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrettyFileDiff(t *testing.T) {
|
func TestPrettyFileDiff(t *testing.T) {
|
||||||
|
|||||||
@@ -197,7 +197,10 @@ func TestFilter_ExitCode(t *testing.T) {
|
|||||||
t.FailNow()
|
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()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -426,7 +426,7 @@ metadata:
|
|||||||
|
|
||||||
{
|
{
|
||||||
name: "write_results_bad_results_file",
|
name: "write_results_bad_results_file",
|
||||||
expectedError: "open /not/real/file: no such file or directory",
|
expectedError: "open /not/real/file:",
|
||||||
noMakeResultsFile: true,
|
noMakeResultsFile: true,
|
||||||
run: testRun{
|
run: testRun{
|
||||||
output: `
|
output: `
|
||||||
@@ -1048,7 +1048,10 @@ metadata:
|
|||||||
}
|
}
|
||||||
output, err := tt.instance.Filter(inputs)
|
output, err := tt.instance.Filter(inputs)
|
||||||
if tt.expectedError != "" {
|
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()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -481,7 +481,6 @@ spec:
|
|||||||
for i := range tests {
|
for i := range tests {
|
||||||
test := tests[i]
|
test := tests[i]
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
os.Clearenv()
|
|
||||||
for k, v := range test.env {
|
for k, v := range test.env {
|
||||||
os.Setenv(k, v)
|
os.Setenv(k, v)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,6 +173,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
|
|||||||
if r.PackagePath == "" {
|
if r.PackagePath == "" {
|
||||||
return nil, fmt.Errorf("must specify package path")
|
return nil, fmt.Errorf("must specify package path")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use slash for path
|
||||||
|
r.PackagePath = filepath.ToSlash(r.PackagePath)
|
||||||
if len(r.MatchFilesGlob) == 0 {
|
if len(r.MatchFilesGlob) == 0 {
|
||||||
r.MatchFilesGlob = DefaultMatch
|
r.MatchFilesGlob = DefaultMatch
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/testutil"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
. "sigs.k8s.io/kustomize/kyaml/kio"
|
. "sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
@@ -186,7 +188,9 @@ func TestLocalPackageWriter_Write_absPath(t *testing.T) {
|
|||||||
d, node1, node2, node3 := getWriterInputs(t)
|
d, node1, node2, node3 := getWriterInputs(t)
|
||||||
defer os.RemoveAll(d)
|
defer os.RemoveAll(d)
|
||||||
|
|
||||||
node4, err := yaml.Parse(fmt.Sprintf(`e: f
|
d = filepath.ToSlash(d)
|
||||||
|
|
||||||
|
n4 := fmt.Sprintf(`e: f
|
||||||
g:
|
g:
|
||||||
h:
|
h:
|
||||||
- i # has a list
|
- i # has a list
|
||||||
@@ -195,16 +199,13 @@ metadata:
|
|||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: a
|
config.kubernetes.io/index: a
|
||||||
config.kubernetes.io/path: "%s/a/b/b_test.yaml" # use a different path, should still collide
|
config.kubernetes.io/path: "%s/a/b/b_test.yaml" # use a different path, should still collide
|
||||||
`, d))
|
`, d)
|
||||||
if !assert.NoError(t, err) {
|
node4, err := yaml.Parse(n4)
|
||||||
assert.FailNow(t, err.Error())
|
testutil.AssertNoError(t, err, n4)
|
||||||
}
|
|
||||||
|
|
||||||
w := LocalPackageWriter{PackagePath: d}
|
w := LocalPackageWriter{PackagePath: d}
|
||||||
err = w.Write([]*yaml.RNode{node2, node1, node3, node4})
|
err = w.Write([]*yaml.RNode{node2, node1, node3, node4})
|
||||||
if assert.Error(t, err) {
|
testutil.AssertErrorContains(t, err, "package paths may not be absolute paths")
|
||||||
assert.Contains(t, err.Error(), "package paths may not be absolute paths")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestLocalPackageWriter_Write_missingPath tests:
|
// TestLocalPackageWriter_Write_missingPath tests:
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ package kio_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -68,15 +70,15 @@ spec:
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !assert.Equal(t, `
|
if !assert.Equal(t, fmt.Sprintf(`
|
||||||
├── bar-package
|
├── bar-package
|
||||||
│ └── [f2.yaml] Deployment bar
|
│ └── [f2.yaml] Deployment bar
|
||||||
└── foo-package
|
└── foo-package
|
||||||
├── [f1.yaml] Deployment default/foo
|
├── [f1.yaml] Deployment default/foo
|
||||||
├── [f1.yaml] Service default/foo
|
├── [f1.yaml] Service default/foo
|
||||||
└── foo-package/3
|
└── foo-package%s3
|
||||||
└── [f3.yaml] Deployment default/foo
|
└── [f3.yaml] Deployment default/foo
|
||||||
`, out.String()) {
|
`, string(filepath.Separator)), out.String()) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -271,21 +271,21 @@ func sortFns(buff *kio.PackageBuffer) {
|
|||||||
// functions deeper in the file system tree should be run first
|
// functions deeper in the file system tree should be run first
|
||||||
sort.Slice(buff.Nodes, func(i, j int) bool {
|
sort.Slice(buff.Nodes, func(i, j int) bool {
|
||||||
mi, _ := buff.Nodes[i].GetMeta()
|
mi, _ := buff.Nodes[i].GetMeta()
|
||||||
pi := mi.Annotations[kioutil.PathAnnotation]
|
pi := filepath.ToSlash(mi.Annotations[kioutil.PathAnnotation])
|
||||||
if path.Base(path.Dir(pi)) == "functions" {
|
if filepath.Base(path.Dir(pi)) == "functions" {
|
||||||
// don't count the functions dir, the functions are scoped 1 level above
|
// 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 {
|
} else {
|
||||||
pi = path.Dir(pi)
|
pi = filepath.Dir(pi)
|
||||||
}
|
}
|
||||||
|
|
||||||
mj, _ := buff.Nodes[j].GetMeta()
|
mj, _ := buff.Nodes[j].GetMeta()
|
||||||
pj := mj.Annotations[kioutil.PathAnnotation]
|
pj := filepath.ToSlash(mj.Annotations[kioutil.PathAnnotation])
|
||||||
if path.Base(path.Dir(pj)) == "functions" {
|
if filepath.Base(path.Dir(pj)) == "functions" {
|
||||||
// don't count the functions dir, the functions are scoped 1 level above
|
// 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 {
|
} else {
|
||||||
pj = path.Dir(pj)
|
pj = filepath.Dir(pj)
|
||||||
}
|
}
|
||||||
|
|
||||||
// i is "less" than j (comes earlier) if its depth is greater -- e.g. run
|
// 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
|
var p string
|
||||||
if spec.Starlark.Path != "" {
|
if spec.Starlark.Path != "" {
|
||||||
p = m.Annotations[kioutil.PathAnnotation]
|
p = filepath.ToSlash(path.Clean(m.Annotations[kioutil.PathAnnotation]))
|
||||||
spec.Starlark.Path = path.Clean(spec.Starlark.Path)
|
spec.Starlark.Path = filepath.ToSlash(path.Clean(spec.Starlark.Path))
|
||||||
if path.IsAbs(spec.Starlark.Path) {
|
if filepath.IsAbs(spec.Starlark.Path) || path.IsAbs(spec.Starlark.Path) {
|
||||||
return nil, errors.Errorf(
|
return nil, errors.Errorf(
|
||||||
"absolute function path %s not allowed", spec.Starlark.Path)
|
"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(
|
return nil, errors.Errorf(
|
||||||
"function path %s not allowed to start with ../", spec.Starlark.Path)
|
"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}
|
sf := &starlark.Filter{Name: spec.Starlark.Name, Path: p, URL: spec.Starlark.URL}
|
||||||
|
|
||||||
|
|||||||
@@ -525,7 +525,7 @@ metadata:
|
|||||||
enableStarlark: true,
|
enableStarlark: true,
|
||||||
outFn: func(path string) []string {
|
outFn: func(path string) []string {
|
||||||
return []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))}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package testutil_test
|
package testutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"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) {
|
func UpdateYamlString(doc string, functions ...yaml.Filter) (string, error) {
|
||||||
@@ -34,3 +40,22 @@ func UpdateYamlBytes(b []byte, function ...yaml.Filter) ([]byte, error) {
|
|||||||
}.Execute()
|
}.Execute()
|
||||||
return out.Bytes(), err
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user