github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/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_connInfoProxy(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  		"proxy_scheme":        cty.StringVal("http"),
   150  		"proxy_host":          cty.StringVal("proxy.example.com"),
   151  		"proxy_port":          cty.StringVal("80"),
   152  		"proxy_user_name":     cty.StringVal("proxyuser"),
   153  		"proxy_user_password": cty.StringVal("proxyuser_password"),
   154  	})
   155  
   156  	conf, err := parseConnectionInfo(v)
   157  	if err != nil {
   158  		t.Fatalf("err: %v", err)
   159  	}
   160  
   161  	if conf.Host != "example.com" {
   162  		t.Fatalf("bad: %v", conf)
   163  	}
   164  
   165  	if conf.ProxyScheme != "http" {
   166  		t.Fatalf("bad: %v", conf)
   167  	}
   168  
   169  	if conf.ProxyHost != "proxy.example.com" {
   170  		t.Fatalf("bad: %v", conf)
   171  	}
   172  
   173  	if conf.ProxyPort != 80 {
   174  		t.Fatalf("bad: %v", conf)
   175  	}
   176  
   177  	if conf.ProxyUserName != "proxyuser" {
   178  		t.Fatalf("bad: %v", conf)
   179  	}
   180  
   181  	if conf.ProxyUserPassword != "proxyuser_password" {
   182  		t.Fatalf("bad: %v", conf)
   183  	}
   184  }
   185  
   186  func TestProvisioner_stringBastionPort(t *testing.T) {
   187  	v := cty.ObjectVal(map[string]cty.Value{
   188  		"type":         cty.StringVal("ssh"),
   189  		"user":         cty.StringVal("root"),
   190  		"password":     cty.StringVal("supersecret"),
   191  		"private_key":  cty.StringVal("someprivatekeycontents"),
   192  		"host":         cty.StringVal("example.com"),
   193  		"port":         cty.StringVal("22"),
   194  		"timeout":      cty.StringVal("30s"),
   195  		"bastion_host": cty.StringVal("example.com"),
   196  		"bastion_port": cty.StringVal("12345"),
   197  	})
   198  
   199  	conf, err := parseConnectionInfo(v)
   200  	if err != nil {
   201  		t.Fatalf("err: %v", err)
   202  	}
   203  
   204  	if conf.BastionPort != 12345 {
   205  		t.Fatalf("bad %v", conf)
   206  	}
   207  }
   208  
   209  func TestProvisioner_invalidPortNumber(t *testing.T) {
   210  	v := cty.ObjectVal(map[string]cty.Value{
   211  		"type":        cty.StringVal("ssh"),
   212  		"user":        cty.StringVal("root"),
   213  		"password":    cty.StringVal("supersecret"),
   214  		"private_key": cty.StringVal("someprivatekeycontents"),
   215  		"host":        cty.StringVal("example.com"),
   216  		"port":        cty.NumberIntVal(123456789),
   217  	})
   218  
   219  	_, err := parseConnectionInfo(v)
   220  	if err == nil {
   221  		t.Fatalf("bad: should not allow invalid port number")
   222  	}
   223  	if got, want := err.Error(), "value must be a whole number, between 0 and 65535 inclusive"; got != want {
   224  		t.Errorf("unexpected error\n got: %s\nwant: %s", got, want)
   225  	}
   226  }