2015-06-10 17:02:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2020-05-12 13:38:15 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2015-06-10 17:02:06 -04:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-05-12 13:38:15 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2017-04-04 16:39:01 -04:00
|
|
|
builderT "github.com/hashicorp/packer/helper/builder/testing"
|
2020-05-12 13:38:15 -04:00
|
|
|
"golang.org/x/oauth2"
|
2015-06-10 17:02:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuilderAcc_basic(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
2020-05-12 13:38:15 -04:00
|
|
|
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),
|
2015-06-10 17:02:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
if v := os.Getenv("DIGITALOCEAN_API_TOKEN"); v == "" {
|
|
|
|
t.Fatal("DIGITALOCEAN_API_TOKEN must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:38:15 -04:00
|
|
|
func makeTemplateWithImageId(t *testing.T) string {
|
2020-05-13 13:28:44 -04:00
|
|
|
if os.Getenv(builderT.TestEnvVar) != "" {
|
|
|
|
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)
|
2020-05-12 13:38:15 -04:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:28:44 -04:00
|
|
|
return ""
|
2020-05-12 13:38:15 -04:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
const testBuilderAccBasic = `
|
|
|
|
{
|
|
|
|
"builders": [{
|
2015-06-10 21:54:51 -04:00
|
|
|
"type": "test",
|
|
|
|
"region": "nyc2",
|
2020-05-12 11:37:07 -04:00
|
|
|
"size": "s-1vcpu-1gb",
|
2020-05-12 13:38:15 -04:00
|
|
|
"image": "%v",
|
2020-05-12 11:37:07 -04:00
|
|
|
"ssh_username": "root",
|
|
|
|
"user_data": "",
|
|
|
|
"user_data_file": ""
|
2015-06-10 17:02:06 -04:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|