mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #90333 from bamarni/kubectl-cp-unexisting-file
[kubectl] Fail when local source file doesn't exist
This commit is contained in:
commit
1485d462ef
@ -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)
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user