From bad180289236da0186855c387ad5a3685b94e685 Mon Sep 17 00:00:00 2001 From: M00nF1sh Date: Mon, 24 Jun 2019 11:16:00 -0700 Subject: [PATCH] refactors to kubernetes CP command --- pkg/kubectl/cmd/cp/cp.go | 5 +--- pkg/kubectl/cmd/cp/cp_test.go | 44 ++++++++++++++++------------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/pkg/kubectl/cmd/cp/cp.go b/pkg/kubectl/cmd/cp/cp.go index ffc0f519a2c..cde9686101e 100644 --- a/pkg/kubectl/cmd/cp/cp.go +++ b/pkg/kubectl/cmd/cp/cp.go @@ -500,16 +500,13 @@ func linkJoin(base, link string) string { if filepath.IsAbs(link) { return link } - return filepath.Join(base, link) + return filepath.Join(filepath.Dir(base), link) } // isDestRelative returns true if dest is pointing outside the base directory, // false otherwise. func isDestRelative(base, dest string) bool { fullPath := dest - if !filepath.IsAbs(dest) { - fullPath = filepath.Join(base, dest) - } relative, err := filepath.Rel(base, fullPath) if err != nil { return false diff --git a/pkg/kubectl/cmd/cp/cp_test.go b/pkg/kubectl/cmd/cp/cp_test.go index d0e60bc3603..957cd214ced 100644 --- a/pkg/kubectl/cmd/cp/cp_test.go +++ b/pkg/kubectl/cmd/cp/cp_test.go @@ -200,12 +200,12 @@ func TestIsDestRelative(t *testing.T) { }{ { base: "/dir", - dest: "../link", + dest: "/dir/../link", relative: false, }, { base: "/dir", - dest: "../../link", + dest: "/dir/../../link", relative: false, }, { @@ -213,21 +213,6 @@ func TestIsDestRelative(t *testing.T) { dest: "/link", relative: false, }, - { - base: "/dir", - dest: "link", - relative: true, - }, - { - base: "/dir", - dest: "int/file/link", - relative: true, - }, - { - base: "/dir", - dest: "int/../link", - relative: true, - }, { base: "/dir", dest: "/dir/link", @@ -239,8 +224,18 @@ func TestIsDestRelative(t *testing.T) { relative: true, }, { - base: "/dir", - dest: "/dir/../../link", + base: "dir", + dest: "dir/link", + relative: true, + }, + { + base: "dir", + dest: "dir/int/../link", + relative: true, + }, + { + base: "dir", + dest: "dir/../../link", relative: false, }, } @@ -792,9 +787,10 @@ func TestUntar(t *testing.T) { } return expected + suffix } - mkBacktickExpectation := func(expected, suffix string) string { - dir, _ := filepath.Split(filepath.Clean(expected)) - if len(strings.Split(dir, string(os.PathSeparator))) <= 1 { + mkBacktickExpectation := func(path, expected, suffix string) string { + linkTarget := filepath.Join(backtick(path), "link-target") + baseDir := filepath.Join(testdir, dest) + if !isDestRelative(baseDir, linkJoin(filepath.Join(baseDir, path), linkTarget)) { return "" } return expected + suffix @@ -812,7 +808,7 @@ func TestUntar(t *testing.T) { }, file{ path: f.path + "-outerlink", linkTarget: filepath.Join(backtick(f.path), "link-target"), - expected: mkBacktickExpectation(f.expected, "-outerlink"), + expected: mkBacktickExpectation(f.path, f.expected, "-outerlink"), }, file{ path: f.path + "-outerlink-abs", linkTarget: filepath.Join(testdir, "link-target"), @@ -918,7 +914,7 @@ func TestUntar(t *testing.T) { // backtick returns a path to one directory up from the target func backtick(target string) string { - rel, _ := filepath.Rel(filepath.Dir(target), "../") + rel := filepath.Join(filepath.Dir(target), "../") return rel }