builder/digitalocean: Add acceptance test for creating Droplet using image ID.

This commit is contained in:
Andrew Starr-Bochicchio 2020-05-12 13:38:15 -04:00
parent 374a29d75c
commit 05d56034dd

View File

@ -1,17 +1,29 @@
package digitalocean
import (
"context"
"fmt"
"os"
"testing"
"github.com/digitalocean/godo"
builderT "github.com/hashicorp/packer/helper/builder/testing"
"golang.org/x/oauth2"
)
func TestBuilderAcc_basic(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Builder: &Builder{},
Template: testBuilderAccBasic,
Template: fmt.Sprintf(testBuilderAccBasic, "ubuntu-20-04-x64"),
})
}
func TestBuilderAcc_imageId(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Builder: &Builder{},
Template: makeTemplateWithImageId(t),
})
}
@ -21,13 +33,26 @@ func testAccPreCheck(t *testing.T) {
}
}
func makeTemplateWithImageId(t *testing.T) string {
token := os.Getenv("DIGITALOCEAN_API_TOKEN")
client := godo.NewClient(oauth2.NewClient(context.TODO(), &apiTokenSource{
AccessToken: token,
}))
image, _, err := client.Images.GetBySlug(context.TODO(), "ubuntu-20-04-x64")
if err != nil {
t.Fatalf("failed to retrieve image ID: %s", err)
}
return fmt.Sprintf(testBuilderAccBasic, image.ID)
}
const testBuilderAccBasic = `
{
"builders": [{
"type": "test",
"region": "nyc2",
"size": "s-1vcpu-1gb",
"image": "ubuntu-20-04-x64",
"image": "%v",
"ssh_username": "root",
"user_data": "",
"user_data_file": ""