From e0f62c67f6400749fc903f956e379d566f28cd77 Mon Sep 17 00:00:00 2001 From: Ace Eldeib Date: Sun, 29 Dec 2019 16:07:12 -0800 Subject: [PATCH 1/2] fix: avoid passing empty variable names --- kyaml/kio/filters/container.go | 6 +++++- kyaml/kio/filters/container_test.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/kyaml/kio/filters/container.go b/kyaml/kio/filters/container.go index a45e96a89..792800de0 100644 --- a/kyaml/kio/filters/container.go +++ b/kyaml/kio/filters/container.go @@ -130,7 +130,11 @@ func (c *ContainerFilter) getArgs() []string { // export the local environment vars to the container for _, pair := range os.Environ() { - args = append(args, "-e", strings.Split(pair, "=")[0]) + tokens := strings.Split(pair, "=") + if tokens[0] == "" { + continue + } + args = append(args, "-e", tokens[0]) } return append(args, c.Image) } diff --git a/kyaml/kio/filters/container_test.go b/kyaml/kio/filters/container_test.go index b17769f6f..7ca7de951 100644 --- a/kyaml/kio/filters/container_test.go +++ b/kyaml/kio/filters/container_test.go @@ -131,7 +131,11 @@ metadata: } for _, e := range os.Environ() { // the process env - expected = append(expected, "-e", strings.Split(e, "=")[0]) + tokens := strings.Split(e, "=") + if tokens[0] == "" { + continue + } + expected = append(expected, "-e", tokens[0]) } expected = append(expected, "example.com:version") assert.Equal(t, expected, cmd.Args) From c4d3a2ff3f816564bc15e15cf1787ce6f71a4be3 Mon Sep 17 00:00:00 2001 From: Ace Eldeib Date: Sun, 29 Dec 2019 17:28:45 -0800 Subject: [PATCH 2/2] tests: make paths for x-plat friendly --- kyaml/copyutil/copyutil_test.go | 11 ++- kyaml/kio/pkgio_reader_test.go | 118 ++++++++++++++++++-------------- 2 files changed, 74 insertions(+), 55 deletions(-) diff --git a/kyaml/copyutil/copyutil_test.go b/kyaml/copyutil/copyutil_test.go index 59a25635c..7f364b1f4 100644 --- a/kyaml/copyutil/copyutil_test.go +++ b/kyaml/copyutil/copyutil_test.go @@ -4,6 +4,7 @@ package copyutil_test import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -104,7 +105,9 @@ func TestDiff_srcDestContentsDiffer(t *testing.T) { diff, err := Diff(s, d) assert.NoError(t, err) - assert.ElementsMatch(t, diff.List(), []string{"a1/f.yaml"}) + assert.ElementsMatch(t, diff.List(), []string{ + fmt.Sprintf("a1%sf.yaml", string(filepath.Separator)), + }) } // TestDiff_srcDestContentsDifferInDirs verifies if identical files @@ -130,7 +133,11 @@ func TestDiff_srcDestContentsDifferInDirs(t *testing.T) { diff, err := Diff(s, d) assert.NoError(t, err) assert.ElementsMatch(t, diff.List(), []string{ - "a1", "a1/f.yaml", "b1/f.yaml", "b1"}) + "a1", + fmt.Sprintf("a1%sf.yaml", string(filepath.Separator)), + fmt.Sprintf("b1%sf.yaml", string(filepath.Separator)), + "b1", + }) } // TestDiff_skipGitSrc verifies that .git directories in the source diff --git a/kyaml/kio/pkgio_reader_test.go b/kyaml/kio/pkgio_reader_test.go index 7ee12c862..74cef9dad 100644 --- a/kyaml/kio/pkgio_reader_test.go +++ b/kyaml/kio/pkgio_reader_test.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -269,15 +270,15 @@ func TestLocalPackageReader_Read_nestedDirs(t *testing.T) { metadata: annotations: config.kubernetes.io/index: '0' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/a_test.yaml' + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml' `, `c: d # second metadata: annotations: config.kubernetes.io/index: '1' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/a_test.yaml' + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml' `, `# second thing e: f @@ -288,8 +289,8 @@ g: metadata: annotations: config.kubernetes.io/index: '0' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/b_test.yaml' + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}b_test.yaml' `, } for i := range nodes { @@ -297,7 +298,8 @@ metadata: if !assert.NoError(t, err) { return } - if !assert.Equal(t, expected[i], val) { + want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator)) + if !assert.Equal(t, want, val) { return } } @@ -321,25 +323,29 @@ func TestLocalPackageReader_Read_matchRegex(t *testing.T) { assert.FailNow(t, "wrong number items") } - val, err := nodes[0].String() - assert.NoError(t, err) - assert.Equal(t, `a: b #first + expected := []string{ + `a: b #first metadata: annotations: config.kubernetes.io/index: '0' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/a_test.yaml' -`, val) - - val, err = nodes[1].String() - assert.NoError(t, err) - assert.Equal(t, `c: d # second + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml' +`, + `c: d # second metadata: annotations: config.kubernetes.io/index: '1' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/a_test.yaml' -`, val) + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml' +`, + } + + for i, node := range nodes { + val, err := node.String() + assert.NoError(t, err) + want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator)) + assert.Equal(t, want, val) + } } func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) { @@ -360,25 +366,29 @@ func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) { assert.FailNow(t, "wrong number items") } - val, err := nodes[0].String() - assert.NoError(t, err) - assert.Equal(t, `a: b #first + expected := []string{ + `a: b #first metadata: annotations: config.kubernetes.io/index: '0' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/a_test.yaml' -`, val) - - val, err = nodes[1].String() - assert.NoError(t, err) - assert.Equal(t, `c: d # second + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml' +`, + `c: d # second metadata: annotations: config.kubernetes.io/index: '1' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/a_test.yaml' -`, val) + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml' +`, + } + + for i, node := range nodes { + val, err := node.String() + assert.NoError(t, err) + want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator)) + assert.Equal(t, want, val) + } } func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) { @@ -398,29 +408,23 @@ func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) { if !assert.Len(t, nodes, 3) { assert.FailNow(t, "wrong number items") } - val, err := nodes[0].String() - assert.NoError(t, err) - assert.Equal(t, `a: b #first + + expected := []string{ + `a: b #first metadata: annotations: config.kubernetes.io/index: '0' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/a_test.yaml' -`, val) - - val, err = nodes[1].String() - assert.NoError(t, err) - assert.Equal(t, `c: d # second + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml' +`, + `c: d # second metadata: annotations: config.kubernetes.io/index: '1' - config.kubernetes.io/package: 'a/b' - config.kubernetes.io/path: 'a/b/a_test.yaml' -`, val) - - val, err = nodes[2].String() - assert.NoError(t, err) - assert.Equal(t, `# second thing + config.kubernetes.io/package: 'a${SEP}b' + config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml' +`, + `# second thing e: f g: h: @@ -429,9 +433,17 @@ g: metadata: annotations: config.kubernetes.io/index: '0' - config.kubernetes.io/package: 'a/c' - config.kubernetes.io/path: 'a/c/c_test.yaml' -`, val) + config.kubernetes.io/package: 'a${SEP}c' + config.kubernetes.io/path: 'a${SEP}c${SEP}c_test.yaml' +`, + } + + for i, node := range nodes { + val, err := node.String() + assert.NoError(t, err) + want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator)) + assert.Equal(t, want, val) + } } // func TestLocalPackageReaderWriter_DeleteFiles(t *testing.T) {