github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/communicator/ssh/provisioner_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package ssh
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/zclconf/go-cty/cty"
    10  )
    11  
    12  func TestProvisioner_connInfo(t *testing.T) {
    13  	v := cty.ObjectVal(map[string]cty.Value{
    14  		"type":         cty.StringVal("ssh"),
    15  		"user":         cty.StringVal("root"),
    16  		"password":     cty.StringVal("supersecret"),
    17  		"private_key":  cty.StringVal("someprivatekeycontents"),
    18  		"certificate":  cty.StringVal("somecertificate"),
    19  		"host":         cty.StringVal("127.0.0.1"),
    20  		"port":         cty.StringVal("22"),
    21  		"timeout":      cty.StringVal("30s"),
    22  		"bastion_host": cty.StringVal("127.0.1.1"),
    23  		"bastion_port": cty.NumberIntVal(20022),
    24  	})
    25  
    26  	conf, err := parseConnectionInfo(v)
    27  	if err != nil {
    28  		t.Fatalf("err: %v", err)
    29  	}
    30  
    31  	if conf.User != "root" {
    32  		t.Fatalf("bad: %v", conf)
    33  	}
    34  	if conf.Password != "supersecret" {
    35  		t.Fatalf("bad: %v", conf)
    36  	}
    37  	if conf.PrivateKey != "someprivatekeycontents" {
    38  		t.Fatalf("bad: %v", conf)
    39  	}
    40  	if conf.Certificate != "somecertificate" {
    41  		t.Fatalf("bad: %v", conf)
    42  	}
    43  	if conf.Host != "127.0.0.1" {
    44  		t.Fatalf("bad: %v", conf)
    45  	}
    46  	if conf.Port != 22 {
    47  		t.Fatalf("bad: %v", conf)
    48  	}
    49  	if conf.Timeout != "30s" {
    50  		t.Fatalf("bad: %v", conf)
    51  	}
    52  	if conf.ScriptPath != DefaultUnixScriptPath {
    53  		t.Fatalf("bad: %v", conf)
    54  	}
    55  	if conf.TargetPlatform != TargetPlatformUnix {
    56  		t.Fatalf("bad: %v", conf)
    57  	}
    58  	if conf.BastionHost != "127.0.1.1" {
    59  		t.Fatalf("bad: %v", conf)
    60  	}
    61  	if conf.BastionPort != 20022 {
    62  		t.Fatalf("bad: %v", conf)
    63  	}
    64  	if conf.BastionUser != "root" {
    65  		t.Fatalf("bad: %v", conf)
    66  	}
    67  	if conf.BastionPassword != "supersecret" {
    68  		t.Fatalf("bad: %v", conf)
    69  	}
    70  	if conf.BastionPrivateKey != "someprivatekeycontents" {
    71  		t.Fatalf("bad: %v", conf)
    72  	}
    73  }
    74  
    75  func TestProvisioner_connInfoIpv6(t *testing.T) {
    76  	v := cty.ObjectVal(map[string]cty.Value{
    77  		"type":         cty.StringVal("ssh"),
    78  		"user":         cty.StringVal("root"),
    79  		"password":     cty.StringVal("supersecret"),
    80  		"private_key":  cty.StringVal("someprivatekeycontents"),
    81  		"host":         cty.StringVal("::1"),
    82  		"port":         cty.StringVal("22"),
    83  		"timeout":      cty.StringVal("30s"),
    84  		"bastion_host": cty.StringVal("::1"),
    85  	})
    86  
    87  	conf, err := parseConnectionInfo(v)
    88  	if err != nil {
    89  		t.Fatalf("err: %v", err)
    90  	}
    91  
    92  	if conf.Host != "[::1]" {
    93  		t.Fatalf("bad: %v", conf)
    94  	}
    95  
    96  	if conf.BastionHost != "[::1]" {
    97  		t.Fatalf("bad %v", conf)
    98  	}
    99  }
   100  
   101  func TestProvisioner_connInfoHostname(t *testing.T) {
   102  	v := cty.ObjectVal(map[string]cty.Value{
   103  		"type":         cty.StringVal("ssh"),
   104  		"user":         cty.StringVal("root"),
   105  		"password":     cty.StringVal("supersecret"),
   106  		"private_key":  cty.StringVal("someprivatekeycontents"),
   107  		"host":         cty.StringVal("example.com"),
   108  		"port":         cty.StringVal("22"),
   109  		"timeout":      cty.StringVal("30s"),
   110  		"bastion_host": cty.StringVal("example.com"),
   111  	})
   112  
   113  	conf, err := parseConnectionInfo(v)
   114  	if err != nil {
   115  		t.Fatalf("err: %v", err)
   116  	}
   117  
   118  	if conf.Host != "example.com" {
   119  		t.Fatalf("bad: %v", conf)
   120  	}
   121  
   122  	if conf.BastionHost != "example.com" {
   123  		t.Fatalf("bad %v", conf)
   124  	}
   125  }
   126  
   127  func TestProvisioner_connInfoEmptyHostname(t *testing.T) {
   128  	v := cty.ObjectVal(map[string]cty.Value{
   129  		"type":        cty.StringVal("ssh"),
   130  		"user":        cty.StringVal("root"),
   131  		"password":    cty.StringVal("supersecret"),
   132  		"private_key": cty.StringVal("someprivatekeycontents"),
   133  		"port":        cty.StringVal("22"),
   134  		"timeout":     cty.StringVal("30s"),
   135  	})
   136  
   137  	_, err := parseConnectionInfo(v)
   138  	if err == nil {
   139  		t.Fatalf("bad: should not allow empty host")
   140  	}
   141  }
   142  
   143  func TestProvisioner_connInfoProxy(t *testing.T) {
   144  	v := cty.ObjectVal(map[string]cty.Value{
   145  		"type":                cty.StringVal("ssh"),
   146  		"user":                cty.StringVal("root"),
   147  		"password":            cty.StringVal("supersecret"),
   148  		"private_key":         cty.StringVal("someprivatekeycontents"),
   149  		"host":                cty.StringVal("example.com"),
   150  		"port":                cty.StringVal("22"),
   151  		"timeout":             cty.StringVal("30s"),
   152  		"proxy_scheme":        cty.StringVal("http"),
   153  		"proxy_host":          cty.StringVal("proxy.example.com"),
   154  		"proxy_port":          cty.StringVal("80"),
   155  		"proxy_user_name":     cty.StringVal("proxyuser"),
   156  		"proxy_user_password": cty.StringVal("proxyuser_password"),
   157  	})
   158  
   159  	conf, err := parseConnectionInfo(v)
   160  	if err != nil {
   161  		t.Fatalf("err: %v", err)
   162  	}
   163  
   164  	if conf.Host != "example.com" {
   165  		t.Fatalf("bad: %v", conf)
   166  	}
   167  
   168  	if conf.ProxyScheme != "http" {
   169  		t.Fatalf("bad: %v", conf)
   170  	}
   171  
   172  	if conf.ProxyHost != "proxy.example.com" {
   173  		t.Fatalf("bad: %v", conf)
   174  	}
   175  
   176  	if conf.ProxyPort != 80 {
   177  		t.Fatalf("bad: %v", conf)
   178  	}
   179  
   180  	if conf.ProxyUserName != "proxyuser" {
   181  		t.Fatalf("bad: %v", conf)
   182  	}
   183  
   184  	if conf.ProxyUserPassword != "proxyuser_password" {
   185  		t.Fatalf("bad: %v", conf)
   186  	}
   187  }
   188  
   189  func TestProvisioner_stringBastionPort(t *testing.T) {
   190  	v := cty.ObjectVal(map[string]cty.Value{
   191  		"type":         cty.StringVal("ssh"),
   192  		"user":         cty.StringVal("root"),
   193  		"password":     cty.StringVal("supersecret"),
   194  		"private_key":  cty.StringVal("someprivatekeycontents"),
   195  		"host":         cty.StringVal("example.com"),
   196  		"port":         cty.StringVal("22"),
   197  		"timeout":      cty.StringVal("30s"),
   198  		"bastion_host": cty.StringVal("example.com"),
   199  		"bastion_port": cty.StringVal("12345"),
   200  	})
   201  
   202  	conf, err := parseConnectionInfo(v)
   203  	if err != nil {
   204  		t.Fatalf("err: %v", err)
   205  	}
   206  
   207  	if conf.BastionPort != 12345 {
   208  		t.Fatalf("bad %v", conf)
   209  	}
   210  }
   211  
   212  func TestProvisioner_invalidPortNumber(t *testing.T) {
   213  	v := cty.ObjectVal(map[string]cty.Value{
   214  		"type":        cty.StringVal("ssh"),
   215  		"user":        cty.StringVal("root"),
   216  		"password":    cty.StringVal("supersecret"),
   217  		"private_key": cty.StringVal("someprivatekeycontents"),
   218  		"host":        cty.StringVal("example.com"),
   219  		"port":        cty.NumberIntVal(123456789),
   220  	})
   221  
   222  	_, err := parseConnectionInfo(v)
   223  	if err == nil {
   224  		t.Fatalf("bad: should not allow invalid port number")
   225  	}
   226  	if got, want := err.Error(), "value must be a whole number, between 0 and 65535 inclusive"; got != want {
   227  		t.Errorf("unexpected error\n got: %s\nwant: %s", got, want)
   228  	}
   229  }