Add acceptance test

This commit is contained in:
Gennady Lipenkov 2020-06-17 17:06:05 +03:00
parent bc24762e9f
commit 1157f0dcfe
1 changed files with 41 additions and 0 deletions

View File

@ -1,12 +1,17 @@
package yandex package yandex
import ( import (
"fmt"
"os" "os"
"testing" "testing"
"github.com/go-resty/resty/v2"
builderT "github.com/hashicorp/packer/helper/builder/testing" builderT "github.com/hashicorp/packer/helper/builder/testing"
) )
const InstanceMetadataAddr = "169.254.169.254"
func TestBuilderAcc_basic(t *testing.T) { func TestBuilderAcc_basic(t *testing.T) {
builderT.Test(t, builderT.TestCase{ builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -15,6 +20,14 @@ func TestBuilderAcc_basic(t *testing.T) {
}) })
} }
func TestBuilderAcc_instanceSA(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheckInstanceSA(t) },
Builder: &Builder{},
Template: testBuilderAccInstanceSA,
})
}
func testAccPreCheck(t *testing.T) { func testAccPreCheck(t *testing.T) {
if v := os.Getenv("YC_TOKEN"); v == "" { if v := os.Getenv("YC_TOKEN"); v == "" {
t.Fatal("YC_TOKEN must be set for acceptance tests") t.Fatal("YC_TOKEN must be set for acceptance tests")
@ -24,6 +37,19 @@ func testAccPreCheck(t *testing.T) {
} }
} }
func testAccPreCheckInstanceSA(t *testing.T) {
client := resty.New()
_, err := client.R().SetHeader("Metadata-Flavor", "Google").Get(tokenUrl())
if err != nil {
t.Fatalf("error get Service Account token assignment", err)
}
if v := os.Getenv("YC_FOLDER_ID"); v == "" {
t.Fatal("YC_FOLDER_ID must be set for acceptance tests")
}
}
const testBuilderAccBasic = ` const testBuilderAccBasic = `
{ {
"builders": [{ "builders": [{
@ -34,3 +60,18 @@ const testBuilderAccBasic = `
}] }]
} }
` `
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)
}