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