packer-cn/builder/yandex/builder_acc_test.go

78 lines
1.6 KiB
Go
Raw Normal View History

package yandex
import (
2020-06-17 10:06:05 -04:00
"fmt"
"os"
"testing"
2020-06-17 10:06:05 -04:00
"github.com/go-resty/resty/v2"
builderT "github.com/hashicorp/packer/helper/builder/testing"
)
2020-06-17 10:06:05 -04:00
const InstanceMetadataAddr = "169.254.169.254"
func TestBuilderAcc_basic(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Builder: &Builder{},
Template: testBuilderAccBasic,
})
}
2020-06-17 10:06:05 -04:00
func TestBuilderAcc_instanceSA(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheckInstanceSA(t) },
Builder: &Builder{},
Template: testBuilderAccInstanceSA,
})
}
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("YC_TOKEN"); v == "" {
t.Fatal("YC_TOKEN must be set for acceptance tests")
}
if v := os.Getenv("YC_FOLDER_ID"); v == "" {
t.Fatal("YC_FOLDER_ID must be set for acceptance tests")
}
}
2020-06-17 10:06:05 -04:00
func testAccPreCheckInstanceSA(t *testing.T) {
client := resty.New()
_, err := client.R().SetHeader("Metadata-Flavor", "Google").Get(tokenUrl())
if err != nil {
2020-06-17 10:23:52 -04:00
t.Fatalf("error get Service Account token assignment: %s", err)
2020-06-17 10:06:05 -04:00
}
if v := os.Getenv("YC_FOLDER_ID"); v == "" {
t.Fatal("YC_FOLDER_ID must be set for acceptance tests")
}
}
const testBuilderAccBasic = `
{
"builders": [{
"type": "test",
"source_image_family": "ubuntu-1804-lts",
"use_ipv4_nat": "true",
"ssh_username": "ubuntu"
}]
}
`
2020-06-17 10:06:05 -04:00
const testBuilderAccInstanceSA = `
{
"builders": [{
"type": "test",
"source_image_family": "ubuntu-1804-lts",
"use_ipv4_nat": "true",
"ssh_username": "ubuntu"
}]
}
`
func tokenUrl() string {
return fmt.Sprintf("http://%s/computeMetadata/v1/instance/service-accounts/default/token", InstanceMetadataAddr)
}