github.com/HashDataInc/packer@v1.3.2/communicator/winrm/communicator_test.go (about)

     1  package winrm
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/dylanmei/winrmtest"
    11  	"github.com/hashicorp/packer/packer"
    12  )
    13  
    14  const PAYLOAD = "stuff"
    15  const BASE64_ENCODED_PAYLOAD = "c3R1ZmY="
    16  
    17  func newMockWinRMServer(t *testing.T) *winrmtest.Remote {
    18  	wrm := winrmtest.NewRemote()
    19  
    20  	wrm.CommandFunc(
    21  		winrmtest.MatchText("echo foo"),
    22  		func(out, err io.Writer) int {
    23  			out.Write([]byte("foo"))
    24  			return 0
    25  		})
    26  
    27  	wrm.CommandFunc(
    28  		winrmtest.MatchPattern(`^echo c29tZXRoaW5n >> ".*"$`),
    29  		func(out, err io.Writer) int {
    30  			return 0
    31  		})
    32  
    33  	wrm.CommandFunc(
    34  		winrmtest.MatchPattern(`^echo `+BASE64_ENCODED_PAYLOAD+` >> ".*"$`),
    35  		func(out, err io.Writer) int {
    36  			return 0
    37  		})
    38  
    39  	wrm.CommandFunc(
    40  		winrmtest.MatchPattern(`^powershell.exe -EncodedCommand .*$`),
    41  		func(out, err io.Writer) int {
    42  			out.Write([]byte(BASE64_ENCODED_PAYLOAD))
    43  			return 0
    44  		})
    45  
    46  	wrm.CommandFunc(
    47  		winrmtest.MatchText("powershell"),
    48  		func(out, err io.Writer) int {
    49  			return 0
    50  		})
    51  	wrm.CommandFunc(
    52  		winrmtest.MatchText(`powershell -Command "(Get-Item C:/Temp/packer.cmd) -is [System.IO.DirectoryInfo]"`),
    53  		func(out, err io.Writer) int {
    54  			out.Write([]byte("False"))
    55  			return 0
    56  		})
    57  
    58  	return wrm
    59  }
    60  
    61  func TestStart(t *testing.T) {
    62  	wrm := newMockWinRMServer(t)
    63  	defer wrm.Close()
    64  
    65  	c, err := New(&Config{
    66  		Host:     wrm.Host,
    67  		Port:     wrm.Port,
    68  		Username: "user",
    69  		Password: "pass",
    70  		Timeout:  30 * time.Second,
    71  	})
    72  	if err != nil {
    73  		t.Fatalf("error creating communicator: %s", err)
    74  	}
    75  
    76  	var cmd packer.RemoteCmd
    77  	stdout := new(bytes.Buffer)
    78  	cmd.Command = "echo foo"
    79  	cmd.Stdout = stdout
    80  
    81  	err = c.Start(&cmd)
    82  	if err != nil {
    83  		t.Fatalf("error executing remote command: %s", err)
    84  	}
    85  	cmd.Wait()
    86  
    87  	if stdout.String() != "foo" {
    88  		t.Fatalf("bad command response: expected %q, got %q", "foo", stdout.String())
    89  	}
    90  }
    91  
    92  func TestUpload(t *testing.T) {
    93  	wrm := newMockWinRMServer(t)
    94  	defer wrm.Close()
    95  
    96  	c, err := New(&Config{
    97  		Host:     wrm.Host,
    98  		Port:     wrm.Port,
    99  		Username: "user",
   100  		Password: "pass",
   101  		Timeout:  30 * time.Second,
   102  	})
   103  	if err != nil {
   104  		t.Fatalf("error creating communicator: %s", err)
   105  	}
   106  	file := "C:/Temp/packer.cmd"
   107  	err = c.Upload(file, strings.NewReader(PAYLOAD), nil)
   108  	if err != nil {
   109  		t.Fatalf("error uploading file: %s", err)
   110  	}
   111  
   112  	dest := new(bytes.Buffer)
   113  	err = c.Download(file, dest)
   114  	if err != nil {
   115  		t.Fatalf("error downloading file: %s", err)
   116  	}
   117  	downloadedPayload := dest.String()
   118  
   119  	if downloadedPayload != PAYLOAD {
   120  		t.Fatalf("files are not equal: expected [%s] length: %v, got [%s] length %v", PAYLOAD, len(PAYLOAD), downloadedPayload, len(downloadedPayload))
   121  	}
   122  
   123  }