github.com/sneal/packer@v0.5.2/builder/virtualbox/common/ssh_config_test.go (about)

     1  package common
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func testSSHConfig() *SSHConfig {
    10  	return &SSHConfig{
    11  		SSHUser: "foo",
    12  	}
    13  }
    14  
    15  func TestSSHConfigPrepare(t *testing.T) {
    16  	c := testSSHConfig()
    17  	errs := c.Prepare(testConfigTemplate(t))
    18  	if len(errs) > 0 {
    19  		t.Fatalf("err: %#v", errs)
    20  	}
    21  
    22  	if c.SSHHostPortMin != 2222 {
    23  		t.Errorf("bad min ssh host port: %d", c.SSHHostPortMin)
    24  	}
    25  
    26  	if c.SSHHostPortMax != 4444 {
    27  		t.Errorf("bad max ssh host port: %d", c.SSHHostPortMax)
    28  	}
    29  
    30  	if c.SSHPort != 22 {
    31  		t.Errorf("bad ssh port: %d", c.SSHPort)
    32  	}
    33  }
    34  
    35  func TestSSHConfigPrepare_SSHHostPort(t *testing.T) {
    36  	var c *SSHConfig
    37  	var errs []error
    38  
    39  	// Bad
    40  	c = testSSHConfig()
    41  	c.SSHHostPortMin = 1000
    42  	c.SSHHostPortMax = 500
    43  	errs = c.Prepare(testConfigTemplate(t))
    44  	if len(errs) == 0 {
    45  		t.Fatalf("bad: %#v", errs)
    46  	}
    47  
    48  	// Good
    49  	c = testSSHConfig()
    50  	c.SSHHostPortMin = 50
    51  	c.SSHHostPortMax = 500
    52  	errs = c.Prepare(testConfigTemplate(t))
    53  	if len(errs) > 0 {
    54  		t.Fatalf("should not have error: %s", errs)
    55  	}
    56  }
    57  
    58  func TestSSHConfigPrepare_SSHKeyPath(t *testing.T) {
    59  	var c *SSHConfig
    60  	var errs []error
    61  
    62  	c = testSSHConfig()
    63  	c.SSHKeyPath = ""
    64  	errs = c.Prepare(testConfigTemplate(t))
    65  	if len(errs) > 0 {
    66  		t.Fatalf("should not have error: %#v", errs)
    67  	}
    68  
    69  	c = testSSHConfig()
    70  	c.SSHKeyPath = "/i/dont/exist"
    71  	errs = c.Prepare(testConfigTemplate(t))
    72  	if len(errs) == 0 {
    73  		t.Fatal("should have error")
    74  	}
    75  
    76  	// Test bad contents
    77  	tf, err := ioutil.TempFile("", "packer")
    78  	if err != nil {
    79  		t.Fatalf("err: %s", err)
    80  	}
    81  	defer os.Remove(tf.Name())
    82  	defer tf.Close()
    83  
    84  	if _, err := tf.Write([]byte("HELLO!")); err != nil {
    85  		t.Fatalf("err: %s", err)
    86  	}
    87  
    88  	c = testSSHConfig()
    89  	c.SSHKeyPath = tf.Name()
    90  	errs = c.Prepare(testConfigTemplate(t))
    91  	if len(errs) == 0 {
    92  		t.Fatal("should have error")
    93  	}
    94  
    95  	// Test good contents
    96  	tf.Seek(0, 0)
    97  	tf.Truncate(0)
    98  	tf.Write([]byte(testPem))
    99  	c = testSSHConfig()
   100  	c.SSHKeyPath = tf.Name()
   101  	errs = c.Prepare(testConfigTemplate(t))
   102  	if len(errs) > 0 {
   103  		t.Fatalf("should not have error: %#v", errs)
   104  	}
   105  }
   106  
   107  func TestSSHConfigPrepare_SSHUser(t *testing.T) {
   108  	var c *SSHConfig
   109  	var errs []error
   110  
   111  	c = testSSHConfig()
   112  	c.SSHUser = ""
   113  	errs = c.Prepare(testConfigTemplate(t))
   114  	if len(errs) == 0 {
   115  		t.Fatalf("should have error")
   116  	}
   117  
   118  	c = testSSHConfig()
   119  	c.SSHUser = "exists"
   120  	errs = c.Prepare(testConfigTemplate(t))
   121  	if len(errs) > 0 {
   122  		t.Fatalf("should not have error: %#v", errs)
   123  	}
   124  }
   125  
   126  func TestSSHConfigPrepare_SSHWaitTimeout(t *testing.T) {
   127  	var c *SSHConfig
   128  	var errs []error
   129  
   130  	// Defaults
   131  	c = testSSHConfig()
   132  	c.RawSSHWaitTimeout = ""
   133  	errs = c.Prepare(testConfigTemplate(t))
   134  	if len(errs) > 0 {
   135  		t.Fatalf("should not have error: %#v", errs)
   136  	}
   137  	if c.RawSSHWaitTimeout != "20m" {
   138  		t.Fatalf("bad value: %s", c.RawSSHWaitTimeout)
   139  	}
   140  
   141  	// Test with a bad value
   142  	c = testSSHConfig()
   143  	c.RawSSHWaitTimeout = "this is not good"
   144  	errs = c.Prepare(testConfigTemplate(t))
   145  	if len(errs) == 0 {
   146  		t.Fatal("should have error")
   147  	}
   148  
   149  	// Test with a good one
   150  	c = testSSHConfig()
   151  	c.RawSSHWaitTimeout = "5s"
   152  	errs = c.Prepare(testConfigTemplate(t))
   153  	if len(errs) > 0 {
   154  		t.Fatalf("should not have error: %#v", errs)
   155  	}
   156  }
   157  
   158  const testPem = `
   159  -----BEGIN RSA PRIVATE KEY-----
   160  MIIEpQIBAAKCAQEAxd4iamvrwRJvtNDGQSIbNvvIQN8imXTRWlRY62EvKov60vqu
   161  hh+rDzFYAIIzlmrJopvOe0clqmi3mIP9dtkjPFrYflq52a2CF5q+BdwsJXuRHbJW
   162  LmStZUwW1khSz93DhvhmK50nIaczW63u4EO/jJb3xj+wxR1Nkk9bxi3DDsYFt8SN
   163  AzYx9kjlEYQ/+sI4/ATfmdV9h78SVotjScupd9KFzzi76gWq9gwyCBLRynTUWlyD
   164  2UOfJRkOvhN6/jKzvYfVVwjPSfA9IMuooHdScmC4F6KBKJl/zf/zETM0XyzIDNmH
   165  uOPbCiljq2WoRM+rY6ET84EO0kVXbfx8uxUsqQIDAQABAoIBAQCkPj9TF0IagbM3
   166  5BSs/CKbAWS4dH/D4bPlxx4IRCNirc8GUg+MRb04Xz0tLuajdQDqeWpr6iLZ0RKV
   167  BvreLF+TOdV7DNQ4XE4gSdJyCtCaTHeort/aordL3l0WgfI7mVk0L/yfN1PEG4YG
   168  E9q1TYcyrB3/8d5JwIkjabxERLglCcP+geOEJp+QijbvFIaZR/n2irlKW4gSy6ko
   169  9B0fgUnhkHysSg49ChHQBPQ+o5BbpuLrPDFMiTPTPhdfsvGGcyCGeqfBA56oHcSF
   170  K02Fg8OM+Bd1lb48LAN9nWWY4WbwV+9bkN3Ym8hO4c3a/Dxf2N7LtAQqWZzFjvM3
   171  /AaDvAgBAoGBAPLD+Xn1IYQPMB2XXCXfOuJewRY7RzoVWvMffJPDfm16O7wOiW5+
   172  2FmvxUDayk4PZy6wQMzGeGKnhcMMZTyaq2g/QtGfrvy7q1Lw2fB1VFlVblvqhoJa
   173  nMJojjC4zgjBkXMHsRLeTmgUKyGs+fdFbfI6uejBnnf+eMVUMIdJ+6I9AoGBANCn
   174  kWO9640dttyXURxNJ3lBr2H3dJOkmD6XS+u+LWqCSKQe691Y/fZ/ZL0Oc4Mhy7I6
   175  hsy3kDQ5k2V0fkaNODQIFJvUqXw2pMewUk8hHc9403f4fe9cPrL12rQ8WlQw4yoC
   176  v2B61vNczCCUDtGxlAaw8jzSRaSI5s6ax3K7enbdAoGBAJB1WYDfA2CoAQO6y9Sl
   177  b07A/7kQ8SN5DbPaqrDrBdJziBQxukoMJQXJeGFNUFD/DXFU5Fp2R7C86vXT7HIR
   178  v6m66zH+CYzOx/YE6EsUJms6UP9VIVF0Rg/RU7teXQwM01ZV32LQ8mswhTH20o/3
   179  uqMHmxUMEhZpUMhrfq0isyApAoGAe1UxGTXfj9AqkIVYylPIq2HqGww7+jFmVEj1
   180  9Wi6S6Sq72ffnzzFEPkIQL/UA4TsdHMnzsYKFPSbbXLIWUeMGyVTmTDA5c0e5XIR
   181  lPhMOKCAzv8w4VUzMnEkTzkFY5JqFCD/ojW57KvDdNZPVB+VEcdxyAW6aKELXMAc
   182  eHLc1nkCgYEApm/motCTPN32nINZ+Vvywbv64ZD+gtpeMNP3CLrbe1X9O+H52AXa
   183  1jCoOldWR8i2bs2NVPcKZgdo6fFULqE4dBX7Te/uYEIuuZhYLNzRO1IKU/YaqsXG
   184  3bfQ8hKYcSnTfE0gPtLDnqCIxTocaGLSHeG3TH9fTw+dA8FvWpUztI4=
   185  -----END RSA PRIVATE KEY-----
   186  `