github.com/opentofu/opentofu@v1.7.1/internal/communicator/winrm/provisioner_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package winrm 7 8 import ( 9 "testing" 10 11 "github.com/zclconf/go-cty/cty" 12 ) 13 14 func TestProvisioner_defaultHTTPSPort(t *testing.T) { 15 v := cty.ObjectVal(map[string]cty.Value{ 16 "type": cty.StringVal("winrm"), 17 "user": cty.StringVal("Administrator"), 18 "password": cty.StringVal("supersecret"), 19 "host": cty.StringVal("127.0.0.1"), 20 "https": cty.True, 21 }) 22 23 conf, err := parseConnectionInfo(v) 24 if err != nil { 25 t.Fatalf("err: %v", err) 26 } 27 if conf.Port != 5986 { 28 t.Fatalf("expected: %v: got: %v", 5986, conf) 29 } 30 if conf.HTTPS != true { 31 t.Fatalf("expected: %v: got: %v", true, conf) 32 } 33 } 34 35 func TestProvisioner_connInfo(t *testing.T) { 36 v := cty.ObjectVal(map[string]cty.Value{ 37 "type": cty.StringVal("winrm"), 38 "user": cty.StringVal("Administrator"), 39 "password": cty.StringVal("supersecret"), 40 "host": cty.StringVal("127.0.0.1"), 41 "port": cty.StringVal("5985"), 42 "https": cty.True, 43 "use_ntlm": cty.True, 44 "timeout": cty.StringVal("30s"), 45 }) 46 47 conf, err := parseConnectionInfo(v) 48 if err != nil { 49 t.Fatalf("err: %v", err) 50 } 51 52 if conf.User != "Administrator" { 53 t.Fatalf("expected: %v: got: %v", "Administrator", conf) 54 } 55 if conf.Password != "supersecret" { 56 t.Fatalf("expected: %v: got: %v", "supersecret", conf) 57 } 58 if conf.Host != "127.0.0.1" { 59 t.Fatalf("expected: %v: got: %v", "127.0.0.1", conf) 60 } 61 if conf.Port != 5985 { 62 t.Fatalf("expected: %v: got: %v", 5985, conf) 63 } 64 if conf.HTTPS != true { 65 t.Fatalf("expected: %v: got: %v", true, conf) 66 } 67 if conf.NTLM != true { 68 t.Fatalf("expected: %v: got: %v", true, conf) 69 } 70 if conf.Timeout != "30s" { 71 t.Fatalf("expected: %v: got: %v", "30s", conf) 72 } 73 if conf.ScriptPath != DefaultScriptPath { 74 t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf) 75 } 76 } 77 78 func TestProvisioner_connInfoCACert(t *testing.T) { 79 caCert := ` 80 -----BEGIN CERTIFICATE----- 81 MIIDBjCCAe4CCQCGWwBmOiHQdTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB 82 VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0 83 cyBQdHkgTHRkMB4XDTE2MDYyMTE2MzM0MVoXDTE3MDYyMTE2MzM0MVowRTELMAkG 84 A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0 85 IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB 86 AL+LFlsCJG5txZp4yuu+lQnuUrgBXRG+irQqcTXlV91Bp5hpmRIyhnGCtWxxDBUL 87 xrh4WN3VV/0jDzKT976oLgOy3hj56Cdqf+JlZ1qgMN5bHB3mm3aVWnrnsLbBsfwZ 88 SEbk3Kht/cE1nK2toNVW+rznS3m+eoV3Zn/DUNwGlZr42hGNs6ETn2jURY78ETqR 89 mW47xvjf86eIo7vULHJaY6xyarPqkL8DZazOmvY06hUGvGwGBny7gugfXqDG+I8n 90 cPBsGJGSAmHmVV8o0RCB9UjY+TvSMQRpEDoVlvyrGuglsD8to/4+7UcsuDGlRYN6 91 jmIOC37mOi/jwRfWL1YUa4MCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAPDxTH0oQ 92 JjKXoJgkmQxurB81RfnK/NrswJVzWbOv6ejcbhwh+/ZgJTMc15BrYcxU6vUW1V/i 93 Z7APU0qJ0icECACML+a2fRI7YdLCTiPIOmY66HY8MZHAn3dGjU5TeiUflC0n0zkP 94 mxKJe43kcYLNDItbfvUDo/GoxTXrC3EFVZyU0RhFzoVJdODlTHXMVFCzcbQEBrBJ 95 xKdShCEc8nFMneZcGFeEU488ntZoWzzms8/QpYrKa5S0Sd7umEU2Kwu4HTkvUFg/ 96 CqDUFjhydXxYRsxXBBrEiLOE5BdtJR1sH/QHxIJe23C9iHI2nS1NbLziNEApLwC4 97 GnSud83VUo9G9w== 98 -----END CERTIFICATE----- 99 ` 100 v := cty.ObjectVal(map[string]cty.Value{ 101 "type": cty.StringVal("winrm"), 102 "user": cty.StringVal("Administrator"), 103 "password": cty.StringVal("supersecret"), 104 "host": cty.StringVal("127.0.0.1"), 105 "port": cty.StringVal("5985"), 106 "https": cty.True, 107 "timeout": cty.StringVal("30s"), 108 "cacert": cty.StringVal(caCert), 109 }) 110 111 conf, err := parseConnectionInfo(v) 112 if err != nil { 113 t.Fatalf("err: %v", err) 114 } 115 116 if conf.User != "Administrator" { 117 t.Fatalf("expected: %v: got: %v", "Administrator", conf) 118 } 119 if conf.Password != "supersecret" { 120 t.Fatalf("expected: %v: got: %v", "supersecret", conf) 121 } 122 if conf.Host != "127.0.0.1" { 123 t.Fatalf("expected: %v: got: %v", "127.0.0.1", conf) 124 } 125 if conf.Port != 5985 { 126 t.Fatalf("expected: %v: got: %v", 5985, conf) 127 } 128 if conf.HTTPS != true { 129 t.Fatalf("expected: %v: got: %v", true, conf) 130 } 131 if conf.Timeout != "30s" { 132 t.Fatalf("expected: %v: got: %v", "30s", conf) 133 } 134 if conf.ScriptPath != DefaultScriptPath { 135 t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf) 136 } 137 if conf.CACert != caCert { 138 t.Fatalf("expected: %v: got: %v", caCert, conf.CACert) 139 } 140 } 141 142 func TestProvisioner_connInfoIpv6(t *testing.T) { 143 v := cty.ObjectVal(map[string]cty.Value{ 144 "type": cty.StringVal("winrm"), 145 "user": cty.StringVal("Administrator"), 146 "password": cty.StringVal("supersecret"), 147 "host": cty.StringVal("::1"), 148 "port": cty.StringVal("5985"), 149 "https": cty.True, 150 "timeout": cty.StringVal("30s"), 151 }) 152 153 conf, err := parseConnectionInfo(v) 154 if err != nil { 155 t.Fatalf("err: %v", err) 156 } 157 158 if conf.User != "Administrator" { 159 t.Fatalf("expected: %v: got: %v", "Administrator", conf) 160 } 161 if conf.Password != "supersecret" { 162 t.Fatalf("expected: %v: got: %v", "supersecret", conf) 163 } 164 if conf.Host != "[::1]" { 165 t.Fatalf("expected: %v: got: %v", "[::1]", conf) 166 } 167 if conf.Port != 5985 { 168 t.Fatalf("expected: %v: got: %v", 5985, conf) 169 } 170 if conf.HTTPS != true { 171 t.Fatalf("expected: %v: got: %v", true, conf) 172 } 173 if conf.Timeout != "30s" { 174 t.Fatalf("expected: %v: got: %v", "30s", conf) 175 } 176 if conf.ScriptPath != DefaultScriptPath { 177 t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf) 178 } 179 } 180 181 func TestProvisioner_connInfoHostname(t *testing.T) { 182 v := cty.ObjectVal(map[string]cty.Value{ 183 "type": cty.StringVal("winrm"), 184 "user": cty.StringVal("Administrator"), 185 "password": cty.StringVal("supersecret"), 186 "host": cty.StringVal("example.com"), 187 "port": cty.StringVal("5985"), 188 "https": cty.True, 189 "timeout": cty.StringVal("30s"), 190 }) 191 192 conf, err := parseConnectionInfo(v) 193 if err != nil { 194 t.Fatalf("err: %v", err) 195 } 196 197 if conf.User != "Administrator" { 198 t.Fatalf("expected: %v: got: %v", "Administrator", conf) 199 } 200 if conf.Password != "supersecret" { 201 t.Fatalf("expected: %v: got: %v", "supersecret", conf) 202 } 203 if conf.Host != "example.com" { 204 t.Fatalf("expected: %v: got: %v", "example.com", conf) 205 } 206 if conf.Port != 5985 { 207 t.Fatalf("expected: %v: got: %v", 5985, conf) 208 } 209 if conf.HTTPS != true { 210 t.Fatalf("expected: %v: got: %v", true, conf) 211 } 212 if conf.Timeout != "30s" { 213 t.Fatalf("expected: %v: got: %v", "30s", conf) 214 } 215 if conf.ScriptPath != DefaultScriptPath { 216 t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf) 217 } 218 } 219 220 func TestProvisioner_formatDuration(t *testing.T) { 221 cases := map[string]struct { 222 Config map[string]cty.Value 223 Result string 224 }{ 225 "testSeconds": { 226 Config: map[string]cty.Value{ 227 "timeout": cty.StringVal("90s"), 228 }, 229 230 Result: "PT1M30S", 231 }, 232 "testMinutes": { 233 Config: map[string]cty.Value{ 234 "timeout": cty.StringVal("5m"), 235 }, 236 237 Result: "PT5M", 238 }, 239 "testHours": { 240 Config: map[string]cty.Value{ 241 "timeout": cty.StringVal("1h"), 242 }, 243 244 Result: "PT1H", 245 }, 246 } 247 248 for name, tc := range cases { 249 // host is required in the schema 250 tc.Config["host"] = cty.StringVal("") 251 252 conf, err := parseConnectionInfo(cty.ObjectVal(tc.Config)) 253 if err != nil { 254 t.Fatalf("err: %v", err) 255 } 256 257 result := formatDuration(conf.TimeoutVal) 258 if result != tc.Result { 259 t.Fatalf("%s: expected: %s got: %s", name, tc.Result, result) 260 } 261 } 262 }