github.com/rothwerx/packer@v0.9.0/communicator/winrm/communicator_test.go (about) 1 package winrm 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 "time" 8 9 "github.com/dylanmei/winrmtest" 10 "github.com/mitchellh/packer/packer" 11 ) 12 13 func newMockWinRMServer(t *testing.T) *winrmtest.Remote { 14 wrm := winrmtest.NewRemote() 15 16 wrm.CommandFunc( 17 winrmtest.MatchText("echo foo"), 18 func(out, err io.Writer) int { 19 out.Write([]byte("foo")) 20 return 0 21 }) 22 23 wrm.CommandFunc( 24 winrmtest.MatchPattern(`^echo c29tZXRoaW5n >> ".*"$`), 25 func(out, err io.Writer) int { 26 return 0 27 }) 28 29 wrm.CommandFunc( 30 winrmtest.MatchPattern(`^powershell.exe -EncodedCommand .*$`), 31 func(out, err io.Writer) int { 32 return 0 33 }) 34 35 wrm.CommandFunc( 36 winrmtest.MatchText("powershell"), 37 func(out, err io.Writer) int { 38 return 0 39 }) 40 41 return wrm 42 } 43 44 func TestStart(t *testing.T) { 45 wrm := newMockWinRMServer(t) 46 defer wrm.Close() 47 48 c, err := New(&Config{ 49 Host: wrm.Host, 50 Port: wrm.Port, 51 Username: "user", 52 Password: "pass", 53 Timeout: 30 * time.Second, 54 }) 55 if err != nil { 56 t.Fatalf("error creating communicator: %s", err) 57 } 58 59 var cmd packer.RemoteCmd 60 stdout := new(bytes.Buffer) 61 cmd.Command = "echo foo" 62 cmd.Stdout = stdout 63 64 err = c.Start(&cmd) 65 if err != nil { 66 t.Fatalf("error executing remote command: %s", err) 67 } 68 cmd.Wait() 69 70 if stdout.String() != "foo" { 71 t.Fatalf("bad command response: expected %q, got %q", "foo", stdout.String()) 72 } 73 } 74 75 func TestUpload(t *testing.T) { 76 wrm := newMockWinRMServer(t) 77 defer wrm.Close() 78 79 c, err := New(&Config{ 80 Host: wrm.Host, 81 Port: wrm.Port, 82 Username: "user", 83 Password: "pass", 84 Timeout: 30 * time.Second, 85 }) 86 if err != nil { 87 t.Fatalf("error creating communicator: %s", err) 88 } 89 90 err = c.Upload("C:/Temp/terraform.cmd", bytes.NewReader([]byte("something")), nil) 91 if err != nil { 92 t.Fatalf("error uploading file: %s", err) 93 } 94 }