add a check and test so that the winrm communicator upload doesn't fail becuase of a nil file pointer
This commit is contained in:
parent
4bf75ac0f3
commit
7b1f7c87e5
|
@ -130,7 +130,11 @@ func (c *Communicator) Upload(path string, input io.Reader, fi *os.FileInfo) err
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(path, `\`) {
|
if strings.HasSuffix(path, `\`) {
|
||||||
// path is a directory
|
// path is a directory
|
||||||
path += filepath.Base((*fi).Name())
|
if fi != nil {
|
||||||
|
path += filepath.Base((*fi).Name())
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("Was unable to infer file basename for upload.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
log.Printf("Uploading file to '%s'", path)
|
log.Printf("Uploading file to '%s'", path)
|
||||||
return wcp.Write(path, input)
|
return wcp.Write(path, input)
|
||||||
|
|
|
@ -120,5 +120,25 @@ func TestUpload(t *testing.T) {
|
||||||
if downloadedPayload != PAYLOAD {
|
if downloadedPayload != PAYLOAD {
|
||||||
t.Fatalf("files are not equal: expected [%s] length: %v, got [%s] length %v", PAYLOAD, len(PAYLOAD), downloadedPayload, len(downloadedPayload))
|
t.Fatalf("files are not equal: expected [%s] length: %v, got [%s] length %v", PAYLOAD, len(PAYLOAD), downloadedPayload, len(downloadedPayload))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpload_nilFileInfo(t *testing.T) {
|
||||||
|
wrm := newMockWinRMServer(t)
|
||||||
|
defer wrm.Close()
|
||||||
|
|
||||||
|
c, err := New(&Config{
|
||||||
|
Host: wrm.Host,
|
||||||
|
Port: wrm.Port,
|
||||||
|
Username: "user",
|
||||||
|
Password: "pass",
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error creating communicator: %s", err)
|
||||||
|
}
|
||||||
|
file := "C:\\Temp\\"
|
||||||
|
err = c.Upload(file, strings.NewReader(PAYLOAD), nil)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Should have errored because of nil fileinfo")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue