github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 TestScriptPath_randSeed(t *testing.T) { 159 // Pre GH-4186 fix, this value was the deterministic start the pseudorandom 160 // chain of unseeded math/rand values for Int31(). 161 staticSeedPath := "C:/Temp/terraform_1298498081.cmd" 162 c, err := New(&terraform.InstanceState{}) 163 if err != nil { 164 t.Fatalf("err: %s", err) 165 } 166 path := c.ScriptPath() 167 if path == staticSeedPath { 168 t.Fatalf("rand not seeded! got: %s", path) 169 } 170 }