github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/googlecompute/config_test.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  )
     7  
     8  func testConfig(t *testing.T) map[string]interface{} {
     9  	return map[string]interface{}{
    10  		"account_file":        testAccountFile(t),
    11  		"bucket_name":         "foo",
    12  		"client_secrets_file": testClientSecretsFile(t),
    13  		"project_id":          "hashicorp",
    14  		"source_image":        "foo",
    15  		"zone":                "us-east-1a",
    16  	}
    17  }
    18  
    19  func testConfigStruct(t *testing.T) *Config {
    20  	c, warns, errs := NewConfig(testConfig(t))
    21  	if len(warns) > 0 {
    22  		t.Fatalf("bad: %#v", len(warns))
    23  	}
    24  	if errs != nil {
    25  		t.Fatalf("bad: %#v", errs)
    26  	}
    27  
    28  	return c
    29  }
    30  
    31  func testConfigErr(t *testing.T, warns []string, err error, extra string) {
    32  	if len(warns) > 0 {
    33  		t.Fatalf("bad: %#v", warns)
    34  	}
    35  	if err == nil {
    36  		t.Fatalf("should error: %s", extra)
    37  	}
    38  }
    39  
    40  func testConfigOk(t *testing.T, warns []string, err error) {
    41  	if len(warns) > 0 {
    42  		t.Fatalf("bad: %#v", warns)
    43  	}
    44  	if err != nil {
    45  		t.Fatalf("bad: %s", err)
    46  	}
    47  }
    48  
    49  func TestConfigPrepare(t *testing.T) {
    50  	cases := []struct {
    51  		Key   string
    52  		Value interface{}
    53  		Err   bool
    54  	}{
    55  		{
    56  			"unknown_key",
    57  			"bad",
    58  			true,
    59  		},
    60  
    61  		{
    62  			"bucket_name",
    63  			nil,
    64  			true,
    65  		},
    66  		{
    67  			"bucket_name",
    68  			"good",
    69  			false,
    70  		},
    71  
    72  		{
    73  			"client_secrets_file",
    74  			nil,
    75  			true,
    76  		},
    77  		{
    78  			"client_secrets_file",
    79  			testClientSecretsFile(t),
    80  			false,
    81  		},
    82  		{
    83  			"client_secrets_file",
    84  			"/tmp/i/should/not/exist",
    85  			true,
    86  		},
    87  
    88  		{
    89  			"private_key_file",
    90  			"/tmp/i/should/not/exist",
    91  			true,
    92  		},
    93  
    94  		{
    95  			"project_id",
    96  			nil,
    97  			true,
    98  		},
    99  		{
   100  			"project_id",
   101  			"foo",
   102  			false,
   103  		},
   104  
   105  		{
   106  			"source_image",
   107  			nil,
   108  			true,
   109  		},
   110  		{
   111  			"source_image",
   112  			"foo",
   113  			false,
   114  		},
   115  
   116  		{
   117  			"zone",
   118  			nil,
   119  			true,
   120  		},
   121  		{
   122  			"zone",
   123  			"foo",
   124  			false,
   125  		},
   126  
   127  		{
   128  			"ssh_timeout",
   129  			"SO BAD",
   130  			true,
   131  		},
   132  		{
   133  			"ssh_timeout",
   134  			"5s",
   135  			false,
   136  		},
   137  
   138  		{
   139  			"state_timeout",
   140  			"SO BAD",
   141  			true,
   142  		},
   143  		{
   144  			"state_timeout",
   145  			"5s",
   146  			false,
   147  		},
   148  	}
   149  
   150  	for _, tc := range cases {
   151  		raw := testConfig(t)
   152  
   153  		if tc.Value == nil {
   154  			delete(raw, tc.Key)
   155  		} else {
   156  			raw[tc.Key] = tc.Value
   157  		}
   158  
   159  		_, warns, errs := NewConfig(raw)
   160  
   161  		if tc.Err {
   162  			testConfigErr(t, warns, errs, tc.Key)
   163  		} else {
   164  			testConfigOk(t, warns, errs)
   165  		}
   166  	}
   167  }
   168  
   169  func testAccountFile(t *testing.T) string {
   170  	tf, err := ioutil.TempFile("", "packer")
   171  	if err != nil {
   172  		t.Fatalf("err: %s", err)
   173  	}
   174  	defer tf.Close()
   175  
   176  	if _, err := tf.Write([]byte(testAccountContent)); err != nil {
   177  		t.Fatalf("err: %s", err)
   178  	}
   179  
   180  	return tf.Name()
   181  }
   182  
   183  func testClientSecretsFile(t *testing.T) string {
   184  	tf, err := ioutil.TempFile("", "packer")
   185  	if err != nil {
   186  		t.Fatalf("err: %s", err)
   187  	}
   188  	defer tf.Close()
   189  
   190  	if _, err := tf.Write([]byte(testClientSecretsContent)); err != nil {
   191  		t.Fatalf("err: %s", err)
   192  	}
   193  
   194  	return tf.Name()
   195  }
   196  
   197  // This is just some dummy data that doesn't actually work (it was revoked
   198  // a long time ago).
   199  const testAccountContent = `{}`
   200  
   201  const testClientSecretsContent = `{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"774313886706-eorlsj0r4eqkh5e7nvea5fuf59ifr873@developer.gserviceaccount.com","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/774313886706-eorlsj0r4eqkh5e7nvea5fuf59ifr873@developer.gserviceaccount.com","client_id":"774313886706-eorlsj0r4eqkh5e7nvea5fuf59ifr873.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}}`