github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/googlecompute/config_test.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestConfigPrepare(t *testing.T) {
    10  	cases := []struct {
    11  		Key   string
    12  		Value interface{}
    13  		Err   bool
    14  	}{
    15  		{
    16  			"unknown_key",
    17  			"bad",
    18  			true,
    19  		},
    20  
    21  		{
    22  			"private_key_file",
    23  			"/tmp/i/should/not/exist",
    24  			true,
    25  		},
    26  
    27  		{
    28  			"project_id",
    29  			nil,
    30  			true,
    31  		},
    32  		{
    33  			"project_id",
    34  			"foo",
    35  			false,
    36  		},
    37  
    38  		{
    39  			"source_image",
    40  			nil,
    41  			true,
    42  		},
    43  		{
    44  			"source_image",
    45  			"foo",
    46  			false,
    47  		},
    48  
    49  		{
    50  			"zone",
    51  			nil,
    52  			true,
    53  		},
    54  		{
    55  			"zone",
    56  			"foo",
    57  			false,
    58  		},
    59  
    60  		{
    61  			"ssh_timeout",
    62  			"SO BAD",
    63  			true,
    64  		},
    65  		{
    66  			"ssh_timeout",
    67  			"5s",
    68  			false,
    69  		},
    70  
    71  		{
    72  			"state_timeout",
    73  			"SO BAD",
    74  			true,
    75  		},
    76  		{
    77  			"state_timeout",
    78  			"5s",
    79  			false,
    80  		},
    81  		{
    82  			"use_internal_ip",
    83  			nil,
    84  			false,
    85  		},
    86  		{
    87  			"use_internal_ip",
    88  			false,
    89  			false,
    90  		},
    91  		{
    92  			"use_internal_ip",
    93  			"SO VERY BAD",
    94  			true,
    95  		},
    96  		{
    97  			"preemptible",
    98  			nil,
    99  			false,
   100  		},
   101  		{
   102  			"preemptible",
   103  			false,
   104  			false,
   105  		},
   106  		{
   107  			"preemptible",
   108  			"SO VERY BAD",
   109  			true,
   110  		},
   111  		{
   112  			"image_family",
   113  			nil,
   114  			false,
   115  		},
   116  		{
   117  			"image_family",
   118  			"",
   119  			false,
   120  		},
   121  		{
   122  			"image_family",
   123  			"foo-bar",
   124  			false,
   125  		},
   126  		{
   127  			"image_family",
   128  			"foo bar",
   129  			true,
   130  		},
   131  		{
   132  			"scopes",
   133  			[]string{},
   134  			false,
   135  		},
   136  		{
   137  			"scopes",
   138  			[]string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control", "https://www.googleapis.com/auth/sqlservice.admin"},
   139  			false,
   140  		},
   141  		{
   142  			"scopes",
   143  			[]string{"https://www.googleapis.com/auth/cloud-platform"},
   144  			false,
   145  		},
   146  	}
   147  
   148  	for _, tc := range cases {
   149  		raw := testConfig(t)
   150  
   151  		if tc.Value == nil {
   152  			delete(raw, tc.Key)
   153  		} else {
   154  			raw[tc.Key] = tc.Value
   155  		}
   156  
   157  		_, warns, errs := NewConfig(raw)
   158  
   159  		if tc.Err {
   160  			testConfigErr(t, warns, errs, tc.Key)
   161  		} else {
   162  			testConfigOk(t, warns, errs)
   163  		}
   164  	}
   165  }
   166  
   167  func TestConfigDefaults(t *testing.T) {
   168  	cases := []struct {
   169  		Read  func(c *Config) interface{}
   170  		Value interface{}
   171  	}{
   172  		{
   173  			func(c *Config) interface{} { return c.Comm.Type },
   174  			"ssh",
   175  		},
   176  
   177  		{
   178  			func(c *Config) interface{} { return c.Comm.SSHPort },
   179  			22,
   180  		},
   181  	}
   182  
   183  	for _, tc := range cases {
   184  		raw := testConfig(t)
   185  
   186  		c, warns, errs := NewConfig(raw)
   187  		testConfigOk(t, warns, errs)
   188  
   189  		actual := tc.Read(c)
   190  		if actual != tc.Value {
   191  			t.Fatalf("bad: %#v", actual)
   192  		}
   193  	}
   194  }
   195  
   196  func TestImageName(t *testing.T) {
   197  	c, _, _ := NewConfig(testConfig(t))
   198  	if !strings.HasPrefix(c.ImageName, "packer-") {
   199  		t.Fatalf("ImageName should have 'packer-' prefix, found %s", c.ImageName)
   200  	}
   201  	if strings.Contains(c.ImageName, "{{timestamp}}") {
   202  		t.Errorf("ImageName should be interpolated; found %s", c.ImageName)
   203  	}
   204  }
   205  
   206  func TestRegion(t *testing.T) {
   207  	c, _, _ := NewConfig(testConfig(t))
   208  	if c.Region != "us-east1" {
   209  		t.Fatalf("Region should be 'us-east1' given Zone of 'us-east1-a', but is %s", c.Region)
   210  	}
   211  }
   212  
   213  // Helper stuff below
   214  
   215  func testConfig(t *testing.T) map[string]interface{} {
   216  	return map[string]interface{}{
   217  		"account_file": testAccountFile(t),
   218  		"project_id":   "hashicorp",
   219  		"source_image": "foo",
   220  		"ssh_username": "root",
   221  		"image_family": "bar",
   222  		"zone":         "us-east1-a",
   223  	}
   224  }
   225  
   226  func testConfigStruct(t *testing.T) *Config {
   227  	c, warns, errs := NewConfig(testConfig(t))
   228  	if len(warns) > 0 {
   229  		t.Fatalf("bad: %#v", len(warns))
   230  	}
   231  	if errs != nil {
   232  		t.Fatalf("bad: %#v", errs)
   233  	}
   234  
   235  	return c
   236  }
   237  
   238  func testConfigErr(t *testing.T, warns []string, err error, extra string) {
   239  	if len(warns) > 0 {
   240  		t.Fatalf("bad: %#v", warns)
   241  	}
   242  	if err == nil {
   243  		t.Fatalf("should error: %s", extra)
   244  	}
   245  }
   246  
   247  func testConfigOk(t *testing.T, warns []string, err error) {
   248  	if len(warns) > 0 {
   249  		t.Fatalf("bad: %#v", warns)
   250  	}
   251  	if err != nil {
   252  		t.Fatalf("bad: %s", err)
   253  	}
   254  }
   255  
   256  func testAccountFile(t *testing.T) string {
   257  	tf, err := ioutil.TempFile("", "packer")
   258  	if err != nil {
   259  		t.Fatalf("err: %s", err)
   260  	}
   261  	defer tf.Close()
   262  
   263  	if _, err := tf.Write([]byte(testAccountContent)); err != nil {
   264  		t.Fatalf("err: %s", err)
   265  	}
   266  
   267  	return tf.Name()
   268  }
   269  
   270  // This is just some dummy data that doesn't actually work (it was revoked
   271  // a long time ago).
   272  const testAccountContent = `{}`