Skip to content

Commit

Permalink
Bump go version to 1.6
Browse files Browse the repository at this point in the history
* bump go version to 1.6 to enable darwin/arm build
* update dependencies
* update intergration tests
  • Loading branch information
maxfrei committed May 30, 2021
1 parent 1d5094f commit 668d33b
Show file tree
Hide file tree
Showing 15 changed files with 541 additions and 557 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.16

- name: Describe plugin
id: plugin_describe
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:

strategy:
matrix:
go-version: [1.15.x]
go-version: [1.16.x]
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ builds:
hooks:
post:
- cmd: |
go install github.com/hashicorp/packer/cmd/packer-plugins-check &&
go install github.com/hashicorp/packer/cmd/packer-plugins-check@latest &&
packer-plugins-check -load={{ .Name }}
dir: "{{ dir .Path}}"
flags:
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ default: build
test:
go test -v ./...

test_integration:
test_integration: build
cp ./packer-plugin-upcloud builder/upcloud/
PACKER_ACC=1 go test -count 1 -v ./... -timeout=120m

lint:
Expand Down
1 change: 1 addition & 0 deletions builder/upcloud/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
"template_prefix": b.config.TemplatePrefix,
},
}

return artifact, nil
}
279 changes: 176 additions & 103 deletions builder/upcloud/builder_acc_test.go
Original file line number Diff line number Diff line change
@@ -1,147 +1,220 @@
package upcloud

import (
_ "embed"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
"testing"

builderT "github.com/hashicorp/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
"github.com/hashicorp/packer-plugin-sdk/acctest"
)

// Run tests: PACKER_ACC=1 go test -count 1 -v ./... -timeout=120m

// json

//go:embed test-fixtures/json/basic.json
var testBuildBasic string

//go:embed test-fixtures/json/storage-uuid.json
var testBuilderStorageUuid string

//go:embed test-fixtures/json/storage-name.json
var testBuilderStorageName string

//go:embed test-fixtures/json/networking.json
var testBuilderNetworking string

func TestBuilderAcc_default(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Builder: &Builder{},
testAccPreCheck(t)

testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuildBasic,
Check: checkTemplateDefaultSettings(),
})
Check: checkTestResult(),
Teardown: teardown(t.Name()),
}
acctest.TestPlugin(t, testCase)
}

func TestBuilderAcc_storageUuid(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Builder: &Builder{},
Template: testBuilderAccStorageUuid,
})
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderStorageUuid,
Check: checkTestResult(),
Teardown: teardown(t.Name()),
}
acctest.TestPlugin(t, testCase)
}

func TestBuilderAcc_storageName(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Builder: &Builder{},
Template: testBuilderAccStorageName,
})
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderStorageName,
Check: checkTestResult(),
Teardown: teardown(t.Name()),
}
acctest.TestPlugin(t, testCase)
}

func TestBuilderAcc_networking(t *testing.T) {
builderT.Test(t, builderT.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Builder: &Builder{},
Template: testBuilderAccNetworking,
})
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderNetworking,
Check: checkTestResult(),
Teardown: teardown(t.Name()),
}
acctest.TestPlugin(t, testCase)
}

// pkr.hcl

//go:embed test-fixtures/json/basic.json
var testBuildBasicHcl string

//go:embed test-fixtures/json/storage-uuid.json
var testBuilderStorageUuidHcl string

//go:embed test-fixtures/json/storage-name.json
var testBuilderStorageNameHcl string

func TestBuilderAcc_default_hcl(t *testing.T) {
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuildBasicHcl,
Check: checkTestResult(),
Teardown: teardown(t.Name()),
}
acctest.TestPlugin(t, testCase)
}

func TestBuilderAcc_storageUuid_hcl(t *testing.T) {
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderStorageUuidHcl,
Check: checkTestResult(),
Teardown: teardown(t.Name()),
}
acctest.TestPlugin(t, testCase)
}

func TestBuilderAcc_storageName_hcl(t *testing.T) {
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderStorageNameHcl,
Check: checkTestResult(),
Teardown: teardown(t.Name()),
}
acctest.TestPlugin(t, testCase)
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("UPCLOUD_API_USER"); v == "" {
t.Fatal("UPCLOUD_API_USER must be set for acceptance tests")
t.Skip("UPCLOUD_API_USER must be set for acceptance tests")
}
if v := os.Getenv("UPCLOUD_API_PASSWORD"); v == "" {
t.Fatal("UPCLOUD_API_PASSWORD must be set for acceptance tests")
t.Skip("UPCLOUD_API_PASSWORD must be set for acceptance tests")
}
}

const testBuildBasic = `
{
"builders": [{
"type": "test",
"zone": "nl-ams1",
"storage_uuid": "01000000-0000-4000-8000-000050010400"
}]
}
`

const testBuilderAccStorageUuid = `
{
"builders": [{
"type": "test",
"zone": "nl-ams1",
"storage_uuid": "01000000-0000-4000-8000-000050010400",
"ssh_username": "root",
"template_prefix": "test-builder",
"storage_size": "20"
}]
}
`

const testBuilderAccStorageName = `
{
"builders": [{
"type": "test",
"zone": "nl-ams1",
"storage_name": "ubuntu server 20.04",
"ssh_username": "root",
"template_prefix": "test-builder",
"storage_size": "20"
}]
}
`

const testBuilderAccNetworking = `
{
"builders": [{
"type": "test",
"zone": "nl-ams1",
"storage_name": "ubuntu server 20.04",
"ssh_username": "root",
"template_prefix": "test-builder",
"storage_size": "20",
"network_interfaces": [
{
"type": "public",
"ip_addresses": [
{
"family": "IPv4"
}
]
},
{
"type": "utility",
"ip_addresses": [
{
"family": "IPv4"
}
]
}
]
}]
func readLog(logfile string) (string, error) {
logs, err := os.Open(logfile)
if err != nil {
return "", fmt.Errorf("Unable find %s", logfile)
}
defer logs.Close()

logsBytes, err := ioutil.ReadAll(logs)
if err != nil {
return "", fmt.Errorf("Unable to read %s", logfile)
}
return string(logsBytes), nil
}
`

func checkTemplateDefaultSettings() builderT.TestCheckFunc {
return func(artifacts []packersdk.Artifact) error {
if len(artifacts) > 1 {
return fmt.Errorf("more than 1 artifact")
func checkTestResult() func(*exec.Cmd, string) error {
return func(buildCommand *exec.Cmd, logfile string) error {

log, err := readLog(logfile)
if err != nil {
return err
}

fmt.Print(log)

if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}

artifactRaw := artifacts[0]
artifact, ok := artifactRaw.(*Artifact)
if !ok {
return fmt.Errorf("unknown artifact: %#v", artifactRaw)
_, err = getUuidsFromLog(log)
if err != nil {
return err
}
return nil
}
}

var re = regexp.MustCompile(`"Storage template created, UUID: (.*?)"`)

func getUuidsFromLog(log string) ([]string, error) {
var match string
ms := re.FindAllStringSubmatch(log, -1)
for _, m := range ms {
match = m[1]
}
if match == "" {
return nil, errors.New("Created template UUIDs not found in the log")
}

uuid := []string{}
for _, item := range strings.Split(match, ",") {
item = strings.TrimSpace(item)
uuid = append(uuid, item)
}
return uuid, nil
}

func teardown(testName string) func() error {
logfile := fmt.Sprintf("packer_log_%s.txt", testName)

expectedSize := 25
expectedTitle := "custom-image"
return func() error {

if artifact.Templates[0].Size != expectedSize {
return fmt.Errorf("Wrong size. Expected %d, got %d", expectedSize, artifact.Templates[0].Size)
log, err := readLog(logfile)
if err != nil {
return err
}

if !strings.HasPrefix(artifact.Templates[0].Title, expectedTitle) {
return fmt.Errorf("Wrong title prefix. Expected %q, got %q", expectedTitle, artifact.Templates[0].Title)
uuids, err := getUuidsFromLog(log)
if err != nil {
return err
}

driver := internal.NewDriver(&internal.DriverConfig{
Username: os.Getenv("UPCLOUD_API_USER"),
Password: os.Getenv("UPCLOUD_API_PASSWORD"),
Timeout: DefaultTimeout,
})

for _, u := range uuids {
fmt.Printf("Cleaning up created templates: %s\n", u)
if err := driver.DeleteTemplate(u); err != nil {
return err
}
}

return nil
}
}
10 changes: 10 additions & 0 deletions builder/upcloud/test-fixtures/hcl2/basic.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

source "upcloud" "basic" {
storage_uuid = "01000000-0000-4000-8000-000050010400"
zone = "nl-ams1"
}

build {
sources = ["source.upcloud.basic"]

}
13 changes: 13 additions & 0 deletions builder/upcloud/test-fixtures/hcl2/storage-name.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

source "upcloud" "storage-name" {
ssh_username = "root"
storage_name = "ubuntu server 20.04"
storage_size = "20"
template_prefix = "test-builder"
zone = "nl-ams1"
}

build {
sources = ["source.upcloud.storage-name"]

}
Loading

0 comments on commit 668d33b

Please sign in to comment.