2015-04-10 20:34:46 +02:00
|
|
|
package winrm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-11-25 19:19:27 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2015-04-10 20:34:46 +02:00
|
|
|
)
|
|
|
|
|
2018-12-13 00:19:02 +01:00
|
|
|
func TestProvisioner_defaultHTTPSPort(t *testing.T) {
|
2020-11-25 19:19:27 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"type": cty.StringVal("winrm"),
|
|
|
|
"user": cty.StringVal("Administrator"),
|
|
|
|
"password": cty.StringVal("supersecret"),
|
|
|
|
"host": cty.StringVal("127.0.0.1"),
|
|
|
|
"https": cty.True,
|
|
|
|
})
|
2018-12-13 00:19:02 +01:00
|
|
|
|
2020-11-25 19:19:27 +01:00
|
|
|
conf, err := parseConnectionInfo(v)
|
2018-12-13 00:19:02 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if conf.Port != 5986 {
|
|
|
|
t.Fatalf("expected: %v: got: %v", 5986, conf)
|
|
|
|
}
|
|
|
|
if conf.HTTPS != true {
|
|
|
|
t.Fatalf("expected: %v: got: %v", true, conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 20:34:46 +02:00
|
|
|
func TestProvisioner_connInfo(t *testing.T) {
|
2020-11-25 19:19:27 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"type": cty.StringVal("winrm"),
|
|
|
|
"user": cty.StringVal("Administrator"),
|
|
|
|
"password": cty.StringVal("supersecret"),
|
|
|
|
"host": cty.StringVal("127.0.0.1"),
|
|
|
|
"port": cty.StringVal("5985"),
|
|
|
|
"https": cty.True,
|
|
|
|
"use_ntlm": cty.True,
|
|
|
|
"timeout": cty.StringVal("30s"),
|
|
|
|
})
|
2015-04-10 20:34:46 +02:00
|
|
|
|
2020-11-25 19:19:27 +01:00
|
|
|
conf, err := parseConnectionInfo(v)
|
2015-04-10 20:34:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.User != "Administrator" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "Administrator", conf)
|
|
|
|
}
|
|
|
|
if conf.Password != "supersecret" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "supersecret", conf)
|
|
|
|
}
|
|
|
|
if conf.Host != "127.0.0.1" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "127.0.0.1", conf)
|
|
|
|
}
|
|
|
|
if conf.Port != 5985 {
|
|
|
|
t.Fatalf("expected: %v: got: %v", 5985, conf)
|
|
|
|
}
|
|
|
|
if conf.HTTPS != true {
|
|
|
|
t.Fatalf("expected: %v: got: %v", true, conf)
|
|
|
|
}
|
2018-03-31 04:01:49 +02:00
|
|
|
if conf.NTLM != true {
|
|
|
|
t.Fatalf("expected: %v: got: %v", true, conf)
|
|
|
|
}
|
2015-04-10 20:34:46 +02:00
|
|
|
if conf.Timeout != "30s" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "30s", conf)
|
|
|
|
}
|
|
|
|
if conf.ScriptPath != DefaultScriptPath {
|
|
|
|
t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 22:28:41 +02:00
|
|
|
func TestProvisioner_connInfoCACert(t *testing.T) {
|
|
|
|
caCert := `
|
|
|
|
-----BEGIN CERTIFICATE-----
|
|
|
|
MIIDBjCCAe4CCQCGWwBmOiHQdTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB
|
|
|
|
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
|
|
|
|
cyBQdHkgTHRkMB4XDTE2MDYyMTE2MzM0MVoXDTE3MDYyMTE2MzM0MVowRTELMAkG
|
|
|
|
A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0
|
|
|
|
IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
|
|
|
|
AL+LFlsCJG5txZp4yuu+lQnuUrgBXRG+irQqcTXlV91Bp5hpmRIyhnGCtWxxDBUL
|
|
|
|
xrh4WN3VV/0jDzKT976oLgOy3hj56Cdqf+JlZ1qgMN5bHB3mm3aVWnrnsLbBsfwZ
|
|
|
|
SEbk3Kht/cE1nK2toNVW+rznS3m+eoV3Zn/DUNwGlZr42hGNs6ETn2jURY78ETqR
|
|
|
|
mW47xvjf86eIo7vULHJaY6xyarPqkL8DZazOmvY06hUGvGwGBny7gugfXqDG+I8n
|
|
|
|
cPBsGJGSAmHmVV8o0RCB9UjY+TvSMQRpEDoVlvyrGuglsD8to/4+7UcsuDGlRYN6
|
|
|
|
jmIOC37mOi/jwRfWL1YUa4MCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAPDxTH0oQ
|
|
|
|
JjKXoJgkmQxurB81RfnK/NrswJVzWbOv6ejcbhwh+/ZgJTMc15BrYcxU6vUW1V/i
|
|
|
|
Z7APU0qJ0icECACML+a2fRI7YdLCTiPIOmY66HY8MZHAn3dGjU5TeiUflC0n0zkP
|
|
|
|
mxKJe43kcYLNDItbfvUDo/GoxTXrC3EFVZyU0RhFzoVJdODlTHXMVFCzcbQEBrBJ
|
|
|
|
xKdShCEc8nFMneZcGFeEU488ntZoWzzms8/QpYrKa5S0Sd7umEU2Kwu4HTkvUFg/
|
|
|
|
CqDUFjhydXxYRsxXBBrEiLOE5BdtJR1sH/QHxIJe23C9iHI2nS1NbLziNEApLwC4
|
|
|
|
GnSud83VUo9G9w==
|
|
|
|
-----END CERTIFICATE-----
|
|
|
|
`
|
2020-11-25 19:19:27 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"type": cty.StringVal("winrm"),
|
|
|
|
"user": cty.StringVal("Administrator"),
|
|
|
|
"password": cty.StringVal("supersecret"),
|
|
|
|
"host": cty.StringVal("127.0.0.1"),
|
|
|
|
"port": cty.StringVal("5985"),
|
|
|
|
"https": cty.True,
|
|
|
|
"timeout": cty.StringVal("30s"),
|
|
|
|
"cacert": cty.StringVal(caCert),
|
|
|
|
})
|
2017-10-23 22:28:41 +02:00
|
|
|
|
2020-11-25 19:19:27 +01:00
|
|
|
conf, err := parseConnectionInfo(v)
|
2017-10-23 22:28:41 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.User != "Administrator" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "Administrator", conf)
|
|
|
|
}
|
|
|
|
if conf.Password != "supersecret" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "supersecret", conf)
|
|
|
|
}
|
|
|
|
if conf.Host != "127.0.0.1" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "127.0.0.1", conf)
|
|
|
|
}
|
|
|
|
if conf.Port != 5985 {
|
|
|
|
t.Fatalf("expected: %v: got: %v", 5985, conf)
|
|
|
|
}
|
|
|
|
if conf.HTTPS != true {
|
|
|
|
t.Fatalf("expected: %v: got: %v", true, conf)
|
|
|
|
}
|
|
|
|
if conf.Timeout != "30s" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "30s", conf)
|
|
|
|
}
|
|
|
|
if conf.ScriptPath != DefaultScriptPath {
|
|
|
|
t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf)
|
|
|
|
}
|
|
|
|
if conf.CACert != caCert {
|
|
|
|
t.Fatalf("expected: %v: got: %v", caCert, conf.CACert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 00:45:24 +02:00
|
|
|
func TestProvisioner_connInfoIpv6(t *testing.T) {
|
2020-11-25 19:19:27 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"type": cty.StringVal("winrm"),
|
|
|
|
"user": cty.StringVal("Administrator"),
|
|
|
|
"password": cty.StringVal("supersecret"),
|
|
|
|
"host": cty.StringVal("::1"),
|
|
|
|
"port": cty.StringVal("5985"),
|
|
|
|
"https": cty.True,
|
|
|
|
"timeout": cty.StringVal("30s"),
|
|
|
|
})
|
2016-09-04 00:45:24 +02:00
|
|
|
|
2020-11-25 19:19:27 +01:00
|
|
|
conf, err := parseConnectionInfo(v)
|
2016-09-04 00:45:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.User != "Administrator" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "Administrator", conf)
|
|
|
|
}
|
|
|
|
if conf.Password != "supersecret" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "supersecret", conf)
|
|
|
|
}
|
|
|
|
if conf.Host != "[::1]" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "[::1]", conf)
|
|
|
|
}
|
|
|
|
if conf.Port != 5985 {
|
|
|
|
t.Fatalf("expected: %v: got: %v", 5985, conf)
|
|
|
|
}
|
|
|
|
if conf.HTTPS != true {
|
|
|
|
t.Fatalf("expected: %v: got: %v", true, conf)
|
|
|
|
}
|
|
|
|
if conf.Timeout != "30s" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "30s", conf)
|
|
|
|
}
|
|
|
|
if conf.ScriptPath != DefaultScriptPath {
|
|
|
|
t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_connInfoHostname(t *testing.T) {
|
2020-11-25 19:19:27 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"type": cty.StringVal("winrm"),
|
|
|
|
"user": cty.StringVal("Administrator"),
|
|
|
|
"password": cty.StringVal("supersecret"),
|
|
|
|
"host": cty.StringVal("example.com"),
|
|
|
|
"port": cty.StringVal("5985"),
|
|
|
|
"https": cty.True,
|
|
|
|
"timeout": cty.StringVal("30s"),
|
|
|
|
})
|
2016-09-04 00:45:24 +02:00
|
|
|
|
2020-11-25 19:19:27 +01:00
|
|
|
conf, err := parseConnectionInfo(v)
|
2016-09-04 00:45:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.User != "Administrator" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "Administrator", conf)
|
|
|
|
}
|
|
|
|
if conf.Password != "supersecret" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "supersecret", conf)
|
|
|
|
}
|
|
|
|
if conf.Host != "example.com" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "example.com", conf)
|
|
|
|
}
|
|
|
|
if conf.Port != 5985 {
|
|
|
|
t.Fatalf("expected: %v: got: %v", 5985, conf)
|
|
|
|
}
|
|
|
|
if conf.HTTPS != true {
|
|
|
|
t.Fatalf("expected: %v: got: %v", true, conf)
|
|
|
|
}
|
|
|
|
if conf.Timeout != "30s" {
|
|
|
|
t.Fatalf("expected: %v: got: %v", "30s", conf)
|
|
|
|
}
|
|
|
|
if conf.ScriptPath != DefaultScriptPath {
|
|
|
|
t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 20:34:46 +02:00
|
|
|
func TestProvisioner_formatDuration(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
2020-11-25 19:19:27 +01:00
|
|
|
Config map[string]cty.Value
|
|
|
|
Result string
|
2015-04-10 20:34:46 +02:00
|
|
|
}{
|
|
|
|
"testSeconds": {
|
2020-11-25 19:19:27 +01:00
|
|
|
Config: map[string]cty.Value{
|
|
|
|
"timeout": cty.StringVal("90s"),
|
2015-04-10 20:34:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Result: "PT1M30S",
|
|
|
|
},
|
|
|
|
"testMinutes": {
|
2020-11-25 19:19:27 +01:00
|
|
|
Config: map[string]cty.Value{
|
|
|
|
"timeout": cty.StringVal("5m"),
|
2015-04-10 20:34:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Result: "PT5M",
|
|
|
|
},
|
|
|
|
"testHours": {
|
2020-11-25 19:19:27 +01:00
|
|
|
Config: map[string]cty.Value{
|
|
|
|
"timeout": cty.StringVal("1h"),
|
2015-04-10 20:34:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Result: "PT1H",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
2020-11-25 19:19:27 +01:00
|
|
|
// host is required in the schema
|
|
|
|
tc.Config["host"] = cty.StringVal("")
|
|
|
|
|
|
|
|
conf, err := parseConnectionInfo(cty.ObjectVal(tc.Config))
|
2015-04-10 20:34:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := formatDuration(conf.TimeoutVal)
|
|
|
|
if result != tc.Result {
|
|
|
|
t.Fatalf("%s: expected: %s got: %s", name, tc.Result, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|