github.com/partitio/terraform@v0.11.12-beta1/communicator/winrm/communicator_test.go (about)

     1  package winrm
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"regexp"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/dylanmei/winrmtest"
    11  	"github.com/hashicorp/terraform/communicator/remote"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func newMockWinRMServer(t *testing.T) *winrmtest.Remote {
    16  	wrm := winrmtest.NewRemote()
    17  
    18  	wrm.CommandFunc(
    19  		winrmtest.MatchText("echo foo"),
    20  		func(out, err io.Writer) int {
    21  			out.Write([]byte("foo"))
    22  			return 0
    23  		})
    24  
    25  	wrm.CommandFunc(
    26  		winrmtest.MatchPattern(`^echo c29tZXRoaW5n >> ".*"$`),
    27  		func(out, err io.Writer) int {
    28  			return 0
    29  		})
    30  
    31  	wrm.CommandFunc(
    32  		winrmtest.MatchPattern(`^powershell.exe -EncodedCommand .*$`),
    33  		func(out, err io.Writer) int {
    34  			return 0
    35  		})
    36  
    37  	wrm.CommandFunc(
    38  		winrmtest.MatchText("powershell"),
    39  		func(out, err io.Writer) int {
    40  			return 0
    41  		})
    42  
    43  	return wrm
    44  }
    45  
    46  func TestStart(t *testing.T) {
    47  	wrm := newMockWinRMServer(t)
    48  	defer wrm.Close()
    49  
    50  	r := &terraform.InstanceState{
    51  		Ephemeral: terraform.EphemeralState{
    52  			ConnInfo: map[string]string{
    53  				"type":     "winrm",
    54  				"user":     "user",
    55  				"password": "pass",
    56  				"host":     wrm.Host,
    57  				"port":     strconv.Itoa(wrm.Port),
    58  				"timeout":  "30s",
    59  			},
    60  		},
    61  	}
    62  
    63  	c, err := New(r)
    64  	if err != nil {
    65  		t.Fatalf("error creating communicator: %s", err)
    66  	}
    67  
    68  	var cmd remote.Cmd
    69  	stdout := new(bytes.Buffer)
    70  	cmd.Command = "echo foo"
    71  	cmd.Stdout = stdout
    72  
    73  	err = c.Start(&cmd)
    74  	if err != nil {
    75  		t.Fatalf("error executing remote command: %s", err)
    76  	}
    77  	cmd.Wait()
    78  
    79  	if stdout.String() != "foo" {
    80  		t.Fatalf("bad command response: expected %q, got %q", "foo", stdout.String())
    81  	}
    82  }
    83  
    84  func TestUpload(t *testing.T) {
    85  	wrm := newMockWinRMServer(t)
    86  	defer wrm.Close()
    87  
    88  	r := &terraform.InstanceState{
    89  		Ephemeral: terraform.EphemeralState{
    90  			ConnInfo: map[string]string{
    91  				"type":     "winrm",
    92  				"user":     "user",
    93  				"password": "pass",
    94  				"host":     wrm.Host,
    95  				"port":     strconv.Itoa(wrm.Port),
    96  				"timeout":  "30s",
    97  			},
    98  		},
    99  	}
   100  
   101  	c, err := New(r)
   102  	if err != nil {
   103  		t.Fatalf("error creating communicator: %s", err)
   104  	}
   105  
   106  	err = c.Connect(nil)
   107  	if err != nil {
   108  		t.Fatalf("error connecting communicator: %s", err)
   109  	}
   110  	defer c.Disconnect()
   111  
   112  	err = c.Upload("C:/Temp/terraform.cmd", bytes.NewReader([]byte("something")))
   113  	if err != nil {
   114  		t.Fatalf("error uploading file: %s", err)
   115  	}
   116  }
   117  
   118  func TestScriptPath(t *testing.T) {
   119  	cases := []struct {
   120  		Input   string
   121  		Pattern string
   122  	}{
   123  		{
   124  			"/tmp/script.sh",
   125  			`^/tmp/script\.sh$`,
   126  		},
   127  		{
   128  			"/tmp/script_%RAND%.sh",
   129  			`^/tmp/script_(\d+)\.sh$`,
   130  		},
   131  	}
   132  
   133  	for _, tc := range cases {
   134  		r := &terraform.InstanceState{
   135  			Ephemeral: terraform.EphemeralState{
   136  				ConnInfo: map[string]string{
   137  					"type":        "winrm",
   138  					"script_path": tc.Input,
   139  				},
   140  			},
   141  		}
   142  		comm, err := New(r)
   143  		if err != nil {
   144  			t.Fatalf("err: %s", err)
   145  		}
   146  		output := comm.ScriptPath()
   147  
   148  		match, err := regexp.Match(tc.Pattern, []byte(output))
   149  		if err != nil {
   150  			t.Fatalf("bad: %s\n\nerr: %s", tc.Input, err)
   151  		}
   152  		if !match {
   153  			t.Fatalf("bad: %s\n\n%s", tc.Input, output)
   154  		}
   155  	}
   156  }
   157  
   158  func TestNoTransportDecorator(t *testing.T) {
   159  	wrm := newMockWinRMServer(t)
   160  	defer wrm.Close()
   161  
   162  	r := &terraform.InstanceState{
   163  		Ephemeral: terraform.EphemeralState{
   164  			ConnInfo: map[string]string{
   165  				"type":     "winrm",
   166  				"user":     "user",
   167  				"password": "pass",
   168  				"host":     wrm.Host,
   169  				"port":     strconv.Itoa(wrm.Port),
   170  				"timeout":  "30s",
   171  			},
   172  		},
   173  	}
   174  
   175  	c, err := New(r)
   176  	if err != nil {
   177  		t.Fatalf("error creating communicator: %s", err)
   178  	}
   179  
   180  	err = c.Connect(nil)
   181  	if err != nil {
   182  		t.Fatalf("error connecting communicator: %s", err)
   183  	}
   184  	defer c.Disconnect()
   185  
   186  	if c.client.TransportDecorator != nil {
   187  		t.Fatal("bad TransportDecorator: expected nil, got non-nil")
   188  	}
   189  }
   190  
   191  func TestTransportDecorator(t *testing.T) {
   192  	wrm := newMockWinRMServer(t)
   193  	defer wrm.Close()
   194  
   195  	r := &terraform.InstanceState{
   196  		Ephemeral: terraform.EphemeralState{
   197  			ConnInfo: map[string]string{
   198  				"type":     "winrm",
   199  				"user":     "user",
   200  				"password": "pass",
   201  				"host":     wrm.Host,
   202  				"port":     strconv.Itoa(wrm.Port),
   203  				"use_ntlm": "true",
   204  				"timeout":  "30s",
   205  			},
   206  		},
   207  	}
   208  
   209  	c, err := New(r)
   210  	if err != nil {
   211  		t.Fatalf("error creating communicator: %s", err)
   212  	}
   213  
   214  	err = c.Connect(nil)
   215  	if err != nil {
   216  		t.Fatalf("error connecting communicator: %s", err)
   217  	}
   218  	defer c.Disconnect()
   219  
   220  	if c.client.TransportDecorator == nil {
   221  		t.Fatal("bad TransportDecorator: expected non-nil, got nil")
   222  	}
   223  }
   224  
   225  func TestScriptPath_randSeed(t *testing.T) {
   226  	// Pre GH-4186 fix, this value was the deterministic start the pseudorandom
   227  	// chain of unseeded math/rand values for Int31().
   228  	staticSeedPath := "C:/Temp/terraform_1298498081.cmd"
   229  	c, err := New(&terraform.InstanceState{})
   230  	if err != nil {
   231  		t.Fatalf("err: %s", err)
   232  	}
   233  	path := c.ScriptPath()
   234  	if path == staticSeedPath {
   235  		t.Fatalf("rand not seeded! got: %s", path)
   236  	}
   237  }