diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go index 605f36dda36..7790db53197 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go @@ -203,10 +203,7 @@ func (o *CopyOptions) Run(args []string) error { } if len(srcSpec.PodName) != 0 && len(destSpec.PodName) != 0 { - if _, err := os.Stat(args[0]); err == nil { - return o.copyToPod(fileSpec{File: args[0]}, destSpec, &exec.ExecOptions{}) - } - return fmt.Errorf("src doesn't exist in local filesystem") + return fmt.Errorf("one of src or dest must be a local file specification") } if len(srcSpec.PodName) != 0 { @@ -245,6 +242,9 @@ func (o *CopyOptions) copyToPod(src, dest fileSpec, options *exec.ExecOptions) e if len(src.File) == 0 || len(dest.File) == 0 { return errFileCannotBeEmpty } + if _, err := os.Stat(src.File); err != nil { + return fmt.Errorf("%s doesn't exist in local filesystem", src.File) + } reader, writer := io.Pipe() // strip trailing slash (if any) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go index e7ada179b56..9af5c92e73f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go @@ -576,27 +576,36 @@ func TestCopyToPod(t *testing.T) { defer os.RemoveAll(srcFile) tests := map[string]struct { + src string dest string expectedErr bool }{ "copy to directory": { + src: srcFile, dest: "/tmp/", expectedErr: false, }, "copy to root": { + src: srcFile, dest: "/", expectedErr: false, }, "copy to empty file name": { + src: srcFile, dest: "", expectedErr: true, }, + "copy unexisting file": { + src: path.Join(srcFile, "nope"), + dest: "/tmp", + expectedErr: true, + }, } for name, test := range tests { opts := NewCopyOptions(ioStreams) src := fileSpec{ - File: srcFile, + File: test.src, } dest := fileSpec{ PodNamespace: "pod-ns",