github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/communicator/ssh/provisioner_test.go (about) 1 package ssh 2 3 import ( 4 "testing" 5 6 "github.com/zclconf/go-cty/cty" 7 ) 8 9 func TestProvisioner_connInfo(t *testing.T) { 10 v := cty.ObjectVal(map[string]cty.Value{ 11 "type": cty.StringVal("ssh"), 12 "user": cty.StringVal("root"), 13 "password": cty.StringVal("supersecret"), 14 "private_key": cty.StringVal("someprivatekeycontents"), 15 "certificate": cty.StringVal("somecertificate"), 16 "host": cty.StringVal("127.0.0.1"), 17 "port": cty.StringVal("22"), 18 "timeout": cty.StringVal("30s"), 19 "bastion_host": cty.StringVal("127.0.1.1"), 20 "bastion_port": cty.NumberIntVal(20022), 21 }) 22 23 conf, err := parseConnectionInfo(v) 24 if err != nil { 25 t.Fatalf("err: %v", err) 26 } 27 28 if conf.User != "root" { 29 t.Fatalf("bad: %v", conf) 30 } 31 if conf.Password != "supersecret" { 32 t.Fatalf("bad: %v", conf) 33 } 34 if conf.PrivateKey != "someprivatekeycontents" { 35 t.Fatalf("bad: %v", conf) 36 } 37 if conf.Certificate != "somecertificate" { 38 t.Fatalf("bad: %v", conf) 39 } 40 if conf.Host != "127.0.0.1" { 41 t.Fatalf("bad: %v", conf) 42 } 43 if conf.Port != 22 { 44 t.Fatalf("bad: %v", conf) 45 } 46 if conf.Timeout != "30s" { 47 t.Fatalf("bad: %v", conf) 48 } 49 if conf.ScriptPath != DefaultUnixScriptPath { 50 t.Fatalf("bad: %v", conf) 51 } 52 if conf.TargetPlatform != TargetPlatformUnix { 53 t.Fatalf("bad: %v", conf) 54 } 55 if conf.BastionHost != "127.0.1.1" { 56 t.Fatalf("bad: %v", conf) 57 } 58 if conf.BastionPort != 20022 { 59 t.Fatalf("bad: %v", conf) 60 } 61 if conf.BastionUser != "root" { 62 t.Fatalf("bad: %v", conf) 63 } 64 if conf.BastionPassword != "supersecret" { 65 t.Fatalf("bad: %v", conf) 66 } 67 if conf.BastionPrivateKey != "someprivatekeycontents" { 68 t.Fatalf("bad: %v", conf) 69 } 70 } 71 72 func TestProvisioner_connInfoIpv6(t *testing.T) { 73 v := cty.ObjectVal(map[string]cty.Value{ 74 "type": cty.StringVal("ssh"), 75 "user": cty.StringVal("root"), 76 "password": cty.StringVal("supersecret"), 77 "private_key": cty.StringVal("someprivatekeycontents"), 78 "host": cty.StringVal("::1"), 79 "port": cty.StringVal("22"), 80 "timeout": cty.StringVal("30s"), 81 "bastion_host": cty.StringVal("::1"), 82 }) 83 84 conf, err := parseConnectionInfo(v) 85 if err != nil { 86 t.Fatalf("err: %v", err) 87 } 88 89 if conf.Host != "[::1]" { 90 t.Fatalf("bad: %v", conf) 91 } 92 93 if conf.BastionHost != "[::1]" { 94 t.Fatalf("bad %v", conf) 95 } 96 } 97 98 func TestProvisioner_connInfoHostname(t *testing.T) { 99 v := cty.ObjectVal(map[string]cty.Value{ 100 "type": cty.StringVal("ssh"), 101 "user": cty.StringVal("root"), 102 "password": cty.StringVal("supersecret"), 103 "private_key": cty.StringVal("someprivatekeycontents"), 104 "host": cty.StringVal("example.com"), 105 "port": cty.StringVal("22"), 106 "timeout": cty.StringVal("30s"), 107 "bastion_host": cty.StringVal("example.com"), 108 }) 109 110 conf, err := parseConnectionInfo(v) 111 if err != nil { 112 t.Fatalf("err: %v", err) 113 } 114 115 if conf.Host != "example.com" { 116 t.Fatalf("bad: %v", conf) 117 } 118 119 if conf.BastionHost != "example.com" { 120 t.Fatalf("bad %v", conf) 121 } 122 } 123 124 func TestProvisioner_connInfoEmptyHostname(t *testing.T) { 125 v := cty.ObjectVal(map[string]cty.Value{ 126 "type": cty.StringVal("ssh"), 127 "user": cty.StringVal("root"), 128 "password": cty.StringVal("supersecret"), 129 "private_key": cty.StringVal("someprivatekeycontents"), 130 "port": cty.StringVal("22"), 131 "timeout": cty.StringVal("30s"), 132 }) 133 134 _, err := parseConnectionInfo(v) 135 if err == nil { 136 t.Fatalf("bad: should not allow empty host") 137 } 138 } 139 140 func TestProvisioner_stringBastionPort(t *testing.T) { 141 v := cty.ObjectVal(map[string]cty.Value{ 142 "type": cty.StringVal("ssh"), 143 "user": cty.StringVal("root"), 144 "password": cty.StringVal("supersecret"), 145 "private_key": cty.StringVal("someprivatekeycontents"), 146 "host": cty.StringVal("example.com"), 147 "port": cty.StringVal("22"), 148 "timeout": cty.StringVal("30s"), 149 "bastion_host": cty.StringVal("example.com"), 150 "bastion_port": cty.StringVal("12345"), 151 }) 152 153 conf, err := parseConnectionInfo(v) 154 if err != nil { 155 t.Fatalf("err: %v", err) 156 } 157 158 if conf.BastionPort != 12345 { 159 t.Fatalf("bad %v", conf) 160 } 161 } 162 163 func TestProvisioner_invalidPortNumber(t *testing.T) { 164 v := cty.ObjectVal(map[string]cty.Value{ 165 "type": cty.StringVal("ssh"), 166 "user": cty.StringVal("root"), 167 "password": cty.StringVal("supersecret"), 168 "private_key": cty.StringVal("someprivatekeycontents"), 169 "host": cty.StringVal("example.com"), 170 "port": cty.NumberIntVal(123456789), 171 }) 172 173 _, err := parseConnectionInfo(v) 174 if err == nil { 175 t.Fatalf("bad: should not allow invalid port number") 176 } 177 if got, want := err.Error(), "value must be a whole number, between 0 and 65535 inclusive"; got != want { 178 t.Errorf("unexpected error\n got: %s\nwant: %s", got, want) 179 } 180 }