Merge pull request #90333 from bamarni/kubectl-cp-unexisting-file

[kubectl] Fail when local source file doesn't exist
This commit is contained in:
Kubernetes Prow Robot 2020-11-06 07:58:51 -08:00 committed by GitHub
commit 1485d462ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

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

View File

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