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