github.com/ves/terraform@v0.8.0-beta2/communicator/winrm/provisioner_test.go (about) 1 package winrm 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/terraform/terraform" 7 ) 8 9 func TestProvisioner_connInfo(t *testing.T) { 10 r := &terraform.InstanceState{ 11 Ephemeral: terraform.EphemeralState{ 12 ConnInfo: map[string]string{ 13 "type": "winrm", 14 "user": "Administrator", 15 "password": "supersecret", 16 "host": "127.0.0.1", 17 "port": "5985", 18 "https": "true", 19 "timeout": "30s", 20 }, 21 }, 22 } 23 24 conf, err := parseConnectionInfo(r) 25 if err != nil { 26 t.Fatalf("err: %v", err) 27 } 28 29 if conf.User != "Administrator" { 30 t.Fatalf("expected: %v: got: %v", "Administrator", conf) 31 } 32 if conf.Password != "supersecret" { 33 t.Fatalf("expected: %v: got: %v", "supersecret", conf) 34 } 35 if conf.Host != "127.0.0.1" { 36 t.Fatalf("expected: %v: got: %v", "127.0.0.1", conf) 37 } 38 if conf.Port != 5985 { 39 t.Fatalf("expected: %v: got: %v", 5985, conf) 40 } 41 if conf.HTTPS != true { 42 t.Fatalf("expected: %v: got: %v", true, conf) 43 } 44 if conf.Timeout != "30s" { 45 t.Fatalf("expected: %v: got: %v", "30s", conf) 46 } 47 if conf.ScriptPath != DefaultScriptPath { 48 t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf) 49 } 50 } 51 52 func TestProvisioner_connInfoIpv6(t *testing.T) { 53 r := &terraform.InstanceState{ 54 Ephemeral: terraform.EphemeralState{ 55 ConnInfo: map[string]string{ 56 "type": "winrm", 57 "user": "Administrator", 58 "password": "supersecret", 59 "host": "::1", 60 "port": "5985", 61 "https": "true", 62 "timeout": "30s", 63 }, 64 }, 65 } 66 67 conf, err := parseConnectionInfo(r) 68 if err != nil { 69 t.Fatalf("err: %v", err) 70 } 71 72 if conf.User != "Administrator" { 73 t.Fatalf("expected: %v: got: %v", "Administrator", conf) 74 } 75 if conf.Password != "supersecret" { 76 t.Fatalf("expected: %v: got: %v", "supersecret", conf) 77 } 78 if conf.Host != "[::1]" { 79 t.Fatalf("expected: %v: got: %v", "[::1]", conf) 80 } 81 if conf.Port != 5985 { 82 t.Fatalf("expected: %v: got: %v", 5985, conf) 83 } 84 if conf.HTTPS != true { 85 t.Fatalf("expected: %v: got: %v", true, conf) 86 } 87 if conf.Timeout != "30s" { 88 t.Fatalf("expected: %v: got: %v", "30s", conf) 89 } 90 if conf.ScriptPath != DefaultScriptPath { 91 t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf) 92 } 93 } 94 95 func TestProvisioner_connInfoHostname(t *testing.T) { 96 r := &terraform.InstanceState{ 97 Ephemeral: terraform.EphemeralState{ 98 ConnInfo: map[string]string{ 99 "type": "winrm", 100 "user": "Administrator", 101 "password": "supersecret", 102 "host": "example.com", 103 "port": "5985", 104 "https": "true", 105 "timeout": "30s", 106 }, 107 }, 108 } 109 110 conf, err := parseConnectionInfo(r) 111 if err != nil { 112 t.Fatalf("err: %v", err) 113 } 114 115 if conf.User != "Administrator" { 116 t.Fatalf("expected: %v: got: %v", "Administrator", conf) 117 } 118 if conf.Password != "supersecret" { 119 t.Fatalf("expected: %v: got: %v", "supersecret", conf) 120 } 121 if conf.Host != "example.com" { 122 t.Fatalf("expected: %v: got: %v", "example.com", conf) 123 } 124 if conf.Port != 5985 { 125 t.Fatalf("expected: %v: got: %v", 5985, conf) 126 } 127 if conf.HTTPS != true { 128 t.Fatalf("expected: %v: got: %v", true, conf) 129 } 130 if conf.Timeout != "30s" { 131 t.Fatalf("expected: %v: got: %v", "30s", conf) 132 } 133 if conf.ScriptPath != DefaultScriptPath { 134 t.Fatalf("expected: %v: got: %v", DefaultScriptPath, conf) 135 } 136 } 137 138 func TestProvisioner_formatDuration(t *testing.T) { 139 cases := map[string]struct { 140 InstanceState *terraform.InstanceState 141 Result string 142 }{ 143 "testSeconds": { 144 InstanceState: &terraform.InstanceState{ 145 Ephemeral: terraform.EphemeralState{ 146 ConnInfo: map[string]string{ 147 "timeout": "90s", 148 }, 149 }, 150 }, 151 152 Result: "PT1M30S", 153 }, 154 "testMinutes": { 155 InstanceState: &terraform.InstanceState{ 156 Ephemeral: terraform.EphemeralState{ 157 ConnInfo: map[string]string{ 158 "timeout": "5m", 159 }, 160 }, 161 }, 162 163 Result: "PT5M", 164 }, 165 "testHours": { 166 InstanceState: &terraform.InstanceState{ 167 Ephemeral: terraform.EphemeralState{ 168 ConnInfo: map[string]string{ 169 "timeout": "1h", 170 }, 171 }, 172 }, 173 174 Result: "PT1H", 175 }, 176 } 177 178 for name, tc := range cases { 179 conf, err := parseConnectionInfo(tc.InstanceState) 180 if err != nil { 181 t.Fatalf("err: %v", err) 182 } 183 184 result := formatDuration(conf.TimeoutVal) 185 if result != tc.Result { 186 t.Fatalf("%s: expected: %s got: %s", name, tc.Result, result) 187 } 188 } 189 }