github.com/bigkraig/terraform@v0.6.4-0.20151219155159-c90d1b074e31/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 comm := &Communicator{connInfo: &connectionInfo{ScriptPath: tc.Input}} 135 output := comm.ScriptPath() 136 137 match, err := regexp.Match(tc.Pattern, []byte(output)) 138 if err != nil { 139 t.Fatalf("bad: %s\n\nerr: %s", tc.Input, err) 140 } 141 if !match { 142 t.Fatalf("bad: %s\n\n%s", tc.Input, output) 143 } 144 } 145 }