2015-06-14 01:00:40 -04:00
|
|
|
package winrm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2017-04-26 12:15:30 -04:00
|
|
|
"strings"
|
2015-06-14 01:00:40 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dylanmei/winrmtest"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-14 01:00:40 -04:00
|
|
|
)
|
|
|
|
|
2017-04-26 12:15:30 -04:00
|
|
|
const PAYLOAD = "stuff"
|
|
|
|
const BASE64_ENCODED_PAYLOAD = "c3R1ZmY="
|
|
|
|
|
2015-06-14 01:00:40 -04:00
|
|
|
func newMockWinRMServer(t *testing.T) *winrmtest.Remote {
|
|
|
|
wrm := winrmtest.NewRemote()
|
|
|
|
|
|
|
|
wrm.CommandFunc(
|
|
|
|
winrmtest.MatchText("echo foo"),
|
|
|
|
func(out, err io.Writer) int {
|
|
|
|
out.Write([]byte("foo"))
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
|
|
|
wrm.CommandFunc(
|
|
|
|
winrmtest.MatchPattern(`^echo c29tZXRoaW5n >> ".*"$`),
|
|
|
|
func(out, err io.Writer) int {
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
2017-04-26 12:15:30 -04:00
|
|
|
wrm.CommandFunc(
|
|
|
|
winrmtest.MatchPattern(`^echo `+BASE64_ENCODED_PAYLOAD+` >> ".*"$`),
|
|
|
|
func(out, err io.Writer) int {
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
2015-06-14 01:00:40 -04:00
|
|
|
wrm.CommandFunc(
|
|
|
|
winrmtest.MatchPattern(`^powershell.exe -EncodedCommand .*$`),
|
|
|
|
func(out, err io.Writer) int {
|
2017-04-26 12:15:30 -04:00
|
|
|
out.Write([]byte(BASE64_ENCODED_PAYLOAD))
|
2015-06-14 01:00:40 -04:00
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
|
|
|
wrm.CommandFunc(
|
|
|
|
winrmtest.MatchText("powershell"),
|
|
|
|
func(out, err io.Writer) int {
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
|
|
|
|
return wrm
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStart(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmd packer.RemoteCmd
|
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
cmd.Command = "echo foo"
|
|
|
|
cmd.Stdout = stdout
|
|
|
|
|
|
|
|
err = c.Start(&cmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error executing remote command: %s", err)
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
if stdout.String() != "foo" {
|
|
|
|
t.Fatalf("bad command response: expected %q, got %q", "foo", stdout.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpload(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)
|
|
|
|
}
|
2017-04-26 12:15:30 -04:00
|
|
|
file := "C:/Temp/packer.cmd"
|
|
|
|
err = c.Upload(file, strings.NewReader(PAYLOAD), nil)
|
2015-06-14 01:00:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error uploading file: %s", err)
|
|
|
|
}
|
2017-04-26 12:15:30 -04:00
|
|
|
|
|
|
|
dest := new(bytes.Buffer)
|
|
|
|
err = c.Download(file, dest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error downloading file: %s", err)
|
|
|
|
}
|
|
|
|
downloadedPayload := strings.TrimRight(dest.String(), "\x00")
|
|
|
|
|
|
|
|
if downloadedPayload != PAYLOAD {
|
|
|
|
t.Fatalf("files are not equal: expected [%s] length: %v, got [%s] length %v", PAYLOAD, len(PAYLOAD), downloadedPayload, len(downloadedPayload))
|
|
|
|
}
|
|
|
|
|
2015-06-14 01:00:40 -04:00
|
|
|
}
|