github.com/HashDataInc/packer@v1.3.2/helper/communicator/config_test.go (about) 1 package communicator 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/hashicorp/packer/template/interpolate" 8 "github.com/masterzen/winrm" 9 ) 10 11 func testConfig() *Config { 12 return &Config{ 13 SSHUsername: "root", 14 } 15 } 16 17 func TestConfigType(t *testing.T) { 18 c := testConfig() 19 if err := c.Prepare(testContext(t)); len(err) > 0 { 20 t.Fatalf("bad: %#v", err) 21 } 22 23 if c.Type != "ssh" { 24 t.Fatalf("bad: %#v", c) 25 } 26 } 27 28 func TestConfig_none(t *testing.T) { 29 c := &Config{Type: "none"} 30 if err := c.Prepare(testContext(t)); len(err) > 0 { 31 t.Fatalf("bad: %#v", err) 32 } 33 } 34 35 func TestConfig_badtype(t *testing.T) { 36 c := &Config{Type: "foo"} 37 if err := c.Prepare(testContext(t)); len(err) != 1 { 38 t.Fatalf("bad: %#v", err) 39 } 40 } 41 42 func TestConfig_winrm_noport(t *testing.T) { 43 c := &Config{ 44 Type: "winrm", 45 WinRMUser: "admin", 46 } 47 if err := c.Prepare(testContext(t)); len(err) > 0 { 48 t.Fatalf("bad: %#v", err) 49 } 50 51 if c.WinRMPort != 5985 { 52 t.Fatalf("WinRMPort doesn't match default port 5985 when SSL is not enabled and no port is specified.") 53 } 54 55 } 56 57 func TestConfig_winrm_noport_ssl(t *testing.T) { 58 c := &Config{ 59 Type: "winrm", 60 WinRMUser: "admin", 61 WinRMUseSSL: true, 62 } 63 if err := c.Prepare(testContext(t)); len(err) > 0 { 64 t.Fatalf("bad: %#v", err) 65 } 66 67 if c.WinRMPort != 5986 { 68 t.Fatalf("WinRMPort doesn't match default port 5986 when SSL is enabled and no port is specified.") 69 } 70 71 } 72 73 func TestConfig_winrm_port(t *testing.T) { 74 c := &Config{ 75 Type: "winrm", 76 WinRMUser: "admin", 77 WinRMPort: 5509, 78 } 79 if err := c.Prepare(testContext(t)); len(err) > 0 { 80 t.Fatalf("bad: %#v", err) 81 } 82 83 if c.WinRMPort != 5509 { 84 t.Fatalf("WinRMPort doesn't match custom port 5509 when SSL is not enabled.") 85 } 86 87 } 88 89 func TestConfig_winrm_port_ssl(t *testing.T) { 90 c := &Config{ 91 Type: "winrm", 92 WinRMUser: "admin", 93 WinRMPort: 5510, 94 WinRMUseSSL: true, 95 } 96 if err := c.Prepare(testContext(t)); len(err) > 0 { 97 t.Fatalf("bad: %#v", err) 98 } 99 100 if c.WinRMPort != 5510 { 101 t.Fatalf("WinRMPort doesn't match custom port 5510 when SSL is enabled.") 102 } 103 104 } 105 106 func TestConfig_winrm_use_ntlm(t *testing.T) { 107 c := &Config{ 108 Type: "winrm", 109 WinRMUser: "admin", 110 WinRMUseNTLM: true, 111 } 112 if err := c.Prepare(testContext(t)); len(err) > 0 { 113 t.Fatalf("bad: %#v", err) 114 } 115 116 if c.WinRMTransportDecorator == nil { 117 t.Fatalf("WinRMTransportDecorator not set.") 118 } 119 120 expected := &winrm.ClientNTLM{} 121 actual := c.WinRMTransportDecorator() 122 123 if !reflect.DeepEqual(expected, actual) { 124 t.Fatalf("WinRMTransportDecorator isn't ClientNTLM.") 125 } 126 127 } 128 129 func TestConfig_winrm(t *testing.T) { 130 c := &Config{ 131 Type: "winrm", 132 WinRMUser: "admin", 133 } 134 if err := c.Prepare(testContext(t)); len(err) > 0 { 135 t.Fatalf("bad: %#v", err) 136 } 137 } 138 139 func testContext(t *testing.T) *interpolate.Context { 140 return nil 141 }