github.com/alouche/packer@v0.3.7/builder/digitalocean/builder_test.go (about) 1 package digitalocean 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "os" 6 "strconv" 7 "testing" 8 ) 9 10 func init() { 11 // Clear out the credential env vars 12 os.Setenv("DIGITALOCEAN_API_KEY", "") 13 os.Setenv("DIGITALOCEAN_CLIENT_ID", "") 14 } 15 16 func testConfig() map[string]interface{} { 17 return map[string]interface{}{ 18 "client_id": "foo", 19 "api_key": "bar", 20 } 21 } 22 23 func TestBuilder_ImplementsBuilder(t *testing.T) { 24 var raw interface{} 25 raw = &Builder{} 26 if _, ok := raw.(packer.Builder); !ok { 27 t.Fatalf("Builder should be a builder") 28 } 29 } 30 31 func TestBuilder_Prepare_BadType(t *testing.T) { 32 b := &Builder{} 33 c := map[string]interface{}{ 34 "api_key": []string{}, 35 } 36 37 err := b.Prepare(c) 38 if err == nil { 39 t.Fatalf("prepare should fail") 40 } 41 } 42 43 func TestBuilderPrepare_APIKey(t *testing.T) { 44 var b Builder 45 config := testConfig() 46 47 // Test good 48 config["api_key"] = "foo" 49 err := b.Prepare(config) 50 if err != nil { 51 t.Fatalf("should not have error: %s", err) 52 } 53 54 if b.config.APIKey != "foo" { 55 t.Errorf("access key invalid: %s", b.config.APIKey) 56 } 57 58 // Test bad 59 delete(config, "api_key") 60 b = Builder{} 61 err = b.Prepare(config) 62 if err == nil { 63 t.Fatal("should have error") 64 } 65 66 // Test env variable 67 delete(config, "api_key") 68 os.Setenv("DIGITALOCEAN_API_KEY", "foo") 69 defer os.Setenv("DIGITALOCEAN_API_KEY", "") 70 err = b.Prepare(config) 71 if err != nil { 72 t.Fatalf("should not have error: %s", err) 73 } 74 } 75 76 func TestBuilderPrepare_ClientID(t *testing.T) { 77 var b Builder 78 config := testConfig() 79 80 // Test good 81 config["client_id"] = "foo" 82 err := b.Prepare(config) 83 if err != nil { 84 t.Fatalf("should not have error: %s", err) 85 } 86 87 if b.config.ClientID != "foo" { 88 t.Errorf("invalid: %s", b.config.ClientID) 89 } 90 91 // Test bad 92 delete(config, "client_id") 93 b = Builder{} 94 err = b.Prepare(config) 95 if err == nil { 96 t.Fatal("should have error") 97 } 98 99 // Test env variable 100 delete(config, "client_id") 101 os.Setenv("DIGITALOCEAN_CLIENT_ID", "foo") 102 defer os.Setenv("DIGITALOCEAN_CLIENT_ID", "") 103 err = b.Prepare(config) 104 if err != nil { 105 t.Fatalf("should not have error: %s", err) 106 } 107 } 108 109 func TestBuilderPrepare_InvalidKey(t *testing.T) { 110 var b Builder 111 config := testConfig() 112 113 // Add a random key 114 config["i_should_not_be_valid"] = true 115 err := b.Prepare(config) 116 if err == nil { 117 t.Fatal("should have error") 118 } 119 } 120 121 func TestBuilderPrepare_RegionID(t *testing.T) { 122 var b Builder 123 config := testConfig() 124 125 // Test default 126 err := b.Prepare(config) 127 if err != nil { 128 t.Fatalf("should not have error: %s", err) 129 } 130 131 if b.config.RegionID != 1 { 132 t.Errorf("invalid: %d", b.config.RegionID) 133 } 134 135 // Test set 136 config["region_id"] = 2 137 b = Builder{} 138 err = b.Prepare(config) 139 if err != nil { 140 t.Fatalf("should not have error: %s", err) 141 } 142 143 if b.config.RegionID != 2 { 144 t.Errorf("invalid: %d", b.config.RegionID) 145 } 146 } 147 148 func TestBuilderPrepare_SizeID(t *testing.T) { 149 var b Builder 150 config := testConfig() 151 152 // Test default 153 err := b.Prepare(config) 154 if err != nil { 155 t.Fatalf("should not have error: %s", err) 156 } 157 158 if b.config.SizeID != 66 { 159 t.Errorf("invalid: %d", b.config.SizeID) 160 } 161 162 // Test set 163 config["size_id"] = 67 164 b = Builder{} 165 err = b.Prepare(config) 166 if err != nil { 167 t.Fatalf("should not have error: %s", err) 168 } 169 170 if b.config.SizeID != 67 { 171 t.Errorf("invalid: %d", b.config.SizeID) 172 } 173 } 174 175 func TestBuilderPrepare_ImageID(t *testing.T) { 176 var b Builder 177 config := testConfig() 178 179 // Test default 180 err := b.Prepare(config) 181 if err != nil { 182 t.Fatalf("should not have error: %s", err) 183 } 184 185 if b.config.SizeID != 66 { 186 t.Errorf("invalid: %d", b.config.SizeID) 187 } 188 189 // Test set 190 config["size_id"] = 2 191 b = Builder{} 192 err = b.Prepare(config) 193 if err != nil { 194 t.Fatalf("should not have error: %s", err) 195 } 196 197 if b.config.SizeID != 2 { 198 t.Errorf("invalid: %d", b.config.SizeID) 199 } 200 } 201 202 func TestBuilderPrepare_SSHUsername(t *testing.T) { 203 var b Builder 204 config := testConfig() 205 206 // Test default 207 err := b.Prepare(config) 208 if err != nil { 209 t.Fatalf("should not have error: %s", err) 210 } 211 212 if b.config.SSHUsername != "root" { 213 t.Errorf("invalid: %d", b.config.SSHUsername) 214 } 215 216 // Test set 217 config["ssh_username"] = "foo" 218 b = Builder{} 219 err = b.Prepare(config) 220 if err != nil { 221 t.Fatalf("should not have error: %s", err) 222 } 223 224 if b.config.SSHUsername != "foo" { 225 t.Errorf("invalid: %s", b.config.SSHUsername) 226 } 227 } 228 229 func TestBuilderPrepare_SSHTimeout(t *testing.T) { 230 var b Builder 231 config := testConfig() 232 233 // Test default 234 err := b.Prepare(config) 235 if err != nil { 236 t.Fatalf("should not have error: %s", err) 237 } 238 239 if b.config.RawSSHTimeout != "1m" { 240 t.Errorf("invalid: %d", b.config.RawSSHTimeout) 241 } 242 243 // Test set 244 config["ssh_timeout"] = "30s" 245 b = Builder{} 246 err = b.Prepare(config) 247 if err != nil { 248 t.Fatalf("should not have error: %s", err) 249 } 250 251 // Test bad 252 config["ssh_timeout"] = "tubes" 253 b = Builder{} 254 err = b.Prepare(config) 255 if err == nil { 256 t.Fatal("should have error") 257 } 258 259 } 260 261 func TestBuilderPrepare_StateTimeout(t *testing.T) { 262 var b Builder 263 config := testConfig() 264 265 // Test default 266 err := b.Prepare(config) 267 if err != nil { 268 t.Fatalf("should not have error: %s", err) 269 } 270 271 if b.config.RawStateTimeout != "6m" { 272 t.Errorf("invalid: %d", b.config.RawStateTimeout) 273 } 274 275 // Test set 276 config["state_timeout"] = "5m" 277 b = Builder{} 278 err = b.Prepare(config) 279 if err != nil { 280 t.Fatalf("should not have error: %s", err) 281 } 282 283 // Test bad 284 config["state_timeout"] = "tubes" 285 b = Builder{} 286 err = b.Prepare(config) 287 if err == nil { 288 t.Fatal("should have error") 289 } 290 291 } 292 293 func TestBuilderPrepare_SnapshotName(t *testing.T) { 294 var b Builder 295 config := testConfig() 296 297 // Test default 298 err := b.Prepare(config) 299 if err != nil { 300 t.Fatalf("should not have error: %s", err) 301 } 302 303 if b.config.SnapshotName == "" { 304 t.Errorf("invalid: %s", b.config.SnapshotName) 305 } 306 307 // Test set 308 config["snapshot_name"] = "foobarbaz" 309 b = Builder{} 310 err = b.Prepare(config) 311 if err != nil { 312 t.Fatalf("should not have error: %s", err) 313 } 314 315 // Test set with template 316 config["snapshot_name"] = "{{timestamp}}" 317 b = Builder{} 318 err = b.Prepare(config) 319 if err != nil { 320 t.Fatalf("should not have error: %s", err) 321 } 322 323 _, err = strconv.ParseInt(b.config.SnapshotName, 0, 0) 324 if err != nil { 325 t.Fatalf("failed to parse int in template: %s", err) 326 } 327 328 }