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