github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/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 113 for _, tc := range cases { 114 raw := testConfig(t) 115 116 if tc.Value == nil { 117 delete(raw, tc.Key) 118 } else { 119 raw[tc.Key] = tc.Value 120 } 121 122 _, warns, errs := NewConfig(raw) 123 124 if tc.Err { 125 testConfigErr(t, warns, errs, tc.Key) 126 } else { 127 testConfigOk(t, warns, errs) 128 } 129 } 130 } 131 132 func TestConfigDefaults(t *testing.T) { 133 cases := []struct { 134 Read func(c *Config) interface{} 135 Value interface{} 136 }{ 137 { 138 func(c *Config) interface{} { return c.Comm.Type }, 139 "ssh", 140 }, 141 142 { 143 func(c *Config) interface{} { return c.Comm.SSHPort }, 144 22, 145 }, 146 } 147 148 for _, tc := range cases { 149 raw := testConfig(t) 150 151 c, warns, errs := NewConfig(raw) 152 testConfigOk(t, warns, errs) 153 154 actual := tc.Read(c) 155 if actual != tc.Value { 156 t.Fatalf("bad: %#v", actual) 157 } 158 } 159 } 160 161 func TestImageName(t *testing.T) { 162 c, _, _ := NewConfig(testConfig(t)) 163 if strings.Contains(c.ImageName, "{{timestamp}}") { 164 t.Errorf("ImageName should be interpolated; found %s", c.ImageName) 165 } 166 } 167 168 // Helper stuff below 169 170 func testConfig(t *testing.T) map[string]interface{} { 171 return map[string]interface{}{ 172 "account_file": testAccountFile(t), 173 "project_id": "hashicorp", 174 "source_image": "foo", 175 "zone": "us-east-1a", 176 } 177 } 178 179 func testConfigStruct(t *testing.T) *Config { 180 c, warns, errs := NewConfig(testConfig(t)) 181 if len(warns) > 0 { 182 t.Fatalf("bad: %#v", len(warns)) 183 } 184 if errs != nil { 185 t.Fatalf("bad: %#v", errs) 186 } 187 188 return c 189 } 190 191 func testConfigErr(t *testing.T, warns []string, err error, extra string) { 192 if len(warns) > 0 { 193 t.Fatalf("bad: %#v", warns) 194 } 195 if err == nil { 196 t.Fatalf("should error: %s", extra) 197 } 198 } 199 200 func testConfigOk(t *testing.T, warns []string, err error) { 201 if len(warns) > 0 { 202 t.Fatalf("bad: %#v", warns) 203 } 204 if err != nil { 205 t.Fatalf("bad: %s", err) 206 } 207 } 208 209 func testAccountFile(t *testing.T) string { 210 tf, err := ioutil.TempFile("", "packer") 211 if err != nil { 212 t.Fatalf("err: %s", err) 213 } 214 defer tf.Close() 215 216 if _, err := tf.Write([]byte(testAccountContent)); err != nil { 217 t.Fatalf("err: %s", err) 218 } 219 220 return tf.Name() 221 } 222 223 // This is just some dummy data that doesn't actually work (it was revoked 224 // a long time ago). 225 const testAccountContent = `{}`