Skip to content

Commit 668d33b

Browse files
committed
Bump go version to 1.6
* bump go version to 1.6 to enable darwin/arm build * update dependencies * update intergration tests
1 parent 1d5094f commit 668d33b

15 files changed

Lines changed: 541 additions & 557 deletions

File tree

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Go
1717
uses: actions/setup-go@v2
1818
with:
19-
go-version: 1.15
19+
go-version: 1.16
2020

2121
- name: Describe plugin
2222
id: plugin_describe

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77

88
strategy:
99
matrix:
10-
go-version: [1.15.x]
10+
go-version: [1.16.x]
1111
os: [ubuntu-latest, macos-latest, windows-latest]
1212

1313
runs-on: ${{ matrix.os }}

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ builds:
1212
hooks:
1313
post:
1414
- cmd: |
15-
go install github.com/hashicorp/packer/cmd/packer-plugins-check &&
15+
go install github.com/hashicorp/packer/cmd/packer-plugins-check@latest &&
1616
packer-plugins-check -load={{ .Name }}
1717
dir: "{{ dir .Path}}"
1818
flags:

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ default: build
33
test:
44
go test -v ./...
55

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

910
lint:

builder/upcloud/builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
107107
"template_prefix": b.config.TemplatePrefix,
108108
},
109109
}
110+
110111
return artifact, nil
111112
}
Lines changed: 176 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,220 @@
11
package upcloud
22

33
import (
4+
_ "embed"
5+
"errors"
46
"fmt"
7+
"io/ioutil"
58
"os"
9+
"os/exec"
10+
"regexp"
611
"strings"
712
"testing"
813

9-
builderT "github.com/hashicorp/packer-plugin-sdk/acctest"
10-
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
14+
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
15+
"github.com/hashicorp/packer-plugin-sdk/acctest"
1116
)
1217

1318
// Run tests: PACKER_ACC=1 go test -count 1 -v ./... -timeout=120m
19+
20+
// json
21+
22+
//go:embed test-fixtures/json/basic.json
23+
var testBuildBasic string
24+
25+
//go:embed test-fixtures/json/storage-uuid.json
26+
var testBuilderStorageUuid string
27+
28+
//go:embed test-fixtures/json/storage-name.json
29+
var testBuilderStorageName string
30+
31+
//go:embed test-fixtures/json/networking.json
32+
var testBuilderNetworking string
33+
1434
func TestBuilderAcc_default(t *testing.T) {
15-
builderT.Test(t, builderT.TestCase{
16-
PreCheck: func() { testAccPreCheck(t) },
17-
Builder: &Builder{},
35+
testAccPreCheck(t)
36+
37+
testCase := &acctest.PluginTestCase{
38+
Name: t.Name(),
1839
Template: testBuildBasic,
19-
Check: checkTemplateDefaultSettings(),
20-
})
40+
Check: checkTestResult(),
41+
Teardown: teardown(t.Name()),
42+
}
43+
acctest.TestPlugin(t, testCase)
2144
}
2245

2346
func TestBuilderAcc_storageUuid(t *testing.T) {
24-
builderT.Test(t, builderT.TestCase{
25-
PreCheck: func() { testAccPreCheck(t) },
26-
Builder: &Builder{},
27-
Template: testBuilderAccStorageUuid,
28-
})
47+
testAccPreCheck(t)
48+
testCase := &acctest.PluginTestCase{
49+
Name: t.Name(),
50+
Template: testBuilderStorageUuid,
51+
Check: checkTestResult(),
52+
Teardown: teardown(t.Name()),
53+
}
54+
acctest.TestPlugin(t, testCase)
2955
}
3056

3157
func TestBuilderAcc_storageName(t *testing.T) {
32-
builderT.Test(t, builderT.TestCase{
33-
PreCheck: func() { testAccPreCheck(t) },
34-
Builder: &Builder{},
35-
Template: testBuilderAccStorageName,
36-
})
58+
testAccPreCheck(t)
59+
testCase := &acctest.PluginTestCase{
60+
Name: t.Name(),
61+
Template: testBuilderStorageName,
62+
Check: checkTestResult(),
63+
Teardown: teardown(t.Name()),
64+
}
65+
acctest.TestPlugin(t, testCase)
3766
}
3867

3968
func TestBuilderAcc_networking(t *testing.T) {
40-
builderT.Test(t, builderT.TestCase{
41-
PreCheck: func() { testAccPreCheck(t) },
42-
Builder: &Builder{},
43-
Template: testBuilderAccNetworking,
44-
})
69+
testAccPreCheck(t)
70+
testCase := &acctest.PluginTestCase{
71+
Name: t.Name(),
72+
Template: testBuilderNetworking,
73+
Check: checkTestResult(),
74+
Teardown: teardown(t.Name()),
75+
}
76+
acctest.TestPlugin(t, testCase)
77+
}
78+
79+
// pkr.hcl
80+
81+
//go:embed test-fixtures/json/basic.json
82+
var testBuildBasicHcl string
83+
84+
//go:embed test-fixtures/json/storage-uuid.json
85+
var testBuilderStorageUuidHcl string
86+
87+
//go:embed test-fixtures/json/storage-name.json
88+
var testBuilderStorageNameHcl string
89+
90+
func TestBuilderAcc_default_hcl(t *testing.T) {
91+
testAccPreCheck(t)
92+
testCase := &acctest.PluginTestCase{
93+
Name: t.Name(),
94+
Template: testBuildBasicHcl,
95+
Check: checkTestResult(),
96+
Teardown: teardown(t.Name()),
97+
}
98+
acctest.TestPlugin(t, testCase)
99+
}
100+
101+
func TestBuilderAcc_storageUuid_hcl(t *testing.T) {
102+
testAccPreCheck(t)
103+
testCase := &acctest.PluginTestCase{
104+
Name: t.Name(),
105+
Template: testBuilderStorageUuidHcl,
106+
Check: checkTestResult(),
107+
Teardown: teardown(t.Name()),
108+
}
109+
acctest.TestPlugin(t, testCase)
110+
}
111+
112+
func TestBuilderAcc_storageName_hcl(t *testing.T) {
113+
testAccPreCheck(t)
114+
testCase := &acctest.PluginTestCase{
115+
Name: t.Name(),
116+
Template: testBuilderStorageNameHcl,
117+
Check: checkTestResult(),
118+
Teardown: teardown(t.Name()),
119+
}
120+
acctest.TestPlugin(t, testCase)
45121
}
46122

47123
func testAccPreCheck(t *testing.T) {
48124
if v := os.Getenv("UPCLOUD_API_USER"); v == "" {
49-
t.Fatal("UPCLOUD_API_USER must be set for acceptance tests")
125+
t.Skip("UPCLOUD_API_USER must be set for acceptance tests")
50126
}
51127
if v := os.Getenv("UPCLOUD_API_PASSWORD"); v == "" {
52-
t.Fatal("UPCLOUD_API_PASSWORD must be set for acceptance tests")
128+
t.Skip("UPCLOUD_API_PASSWORD must be set for acceptance tests")
53129
}
54130
}
55131

56-
const testBuildBasic = `
57-
{
58-
"builders": [{
59-
"type": "test",
60-
"zone": "nl-ams1",
61-
"storage_uuid": "01000000-0000-4000-8000-000050010400"
62-
}]
63-
}
64-
`
65-
66-
const testBuilderAccStorageUuid = `
67-
{
68-
"builders": [{
69-
"type": "test",
70-
"zone": "nl-ams1",
71-
"storage_uuid": "01000000-0000-4000-8000-000050010400",
72-
"ssh_username": "root",
73-
"template_prefix": "test-builder",
74-
"storage_size": "20"
75-
}]
76-
}
77-
`
78-
79-
const testBuilderAccStorageName = `
80-
{
81-
"builders": [{
82-
"type": "test",
83-
"zone": "nl-ams1",
84-
"storage_name": "ubuntu server 20.04",
85-
"ssh_username": "root",
86-
"template_prefix": "test-builder",
87-
"storage_size": "20"
88-
}]
89-
}
90-
`
91-
92-
const testBuilderAccNetworking = `
93-
{
94-
"builders": [{
95-
"type": "test",
96-
"zone": "nl-ams1",
97-
"storage_name": "ubuntu server 20.04",
98-
"ssh_username": "root",
99-
"template_prefix": "test-builder",
100-
"storage_size": "20",
101-
"network_interfaces": [
102-
{
103-
"type": "public",
104-
"ip_addresses": [
105-
{
106-
"family": "IPv4"
107-
}
108-
]
109-
},
110-
{
111-
"type": "utility",
112-
"ip_addresses": [
113-
{
114-
"family": "IPv4"
115-
}
116-
]
117-
}
118-
]
119-
}]
132+
func readLog(logfile string) (string, error) {
133+
logs, err := os.Open(logfile)
134+
if err != nil {
135+
return "", fmt.Errorf("Unable find %s", logfile)
136+
}
137+
defer logs.Close()
138+
139+
logsBytes, err := ioutil.ReadAll(logs)
140+
if err != nil {
141+
return "", fmt.Errorf("Unable to read %s", logfile)
142+
}
143+
return string(logsBytes), nil
120144
}
121-
`
122145

123-
func checkTemplateDefaultSettings() builderT.TestCheckFunc {
124-
return func(artifacts []packersdk.Artifact) error {
125-
if len(artifacts) > 1 {
126-
return fmt.Errorf("more than 1 artifact")
146+
func checkTestResult() func(*exec.Cmd, string) error {
147+
return func(buildCommand *exec.Cmd, logfile string) error {
148+
149+
log, err := readLog(logfile)
150+
if err != nil {
151+
return err
152+
}
153+
154+
fmt.Print(log)
155+
156+
if buildCommand.ProcessState != nil {
157+
if buildCommand.ProcessState.ExitCode() != 0 {
158+
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
159+
}
127160
}
128161

129-
artifactRaw := artifacts[0]
130-
artifact, ok := artifactRaw.(*Artifact)
131-
if !ok {
132-
return fmt.Errorf("unknown artifact: %#v", artifactRaw)
162+
_, err = getUuidsFromLog(log)
163+
if err != nil {
164+
return err
133165
}
166+
return nil
167+
}
168+
}
169+
170+
var re = regexp.MustCompile(`"Storage template created, UUID: (.*?)"`)
171+
172+
func getUuidsFromLog(log string) ([]string, error) {
173+
var match string
174+
ms := re.FindAllStringSubmatch(log, -1)
175+
for _, m := range ms {
176+
match = m[1]
177+
}
178+
if match == "" {
179+
return nil, errors.New("Created template UUIDs not found in the log")
180+
}
181+
182+
uuid := []string{}
183+
for _, item := range strings.Split(match, ",") {
184+
item = strings.TrimSpace(item)
185+
uuid = append(uuid, item)
186+
}
187+
return uuid, nil
188+
}
189+
190+
func teardown(testName string) func() error {
191+
logfile := fmt.Sprintf("packer_log_%s.txt", testName)
134192

135-
expectedSize := 25
136-
expectedTitle := "custom-image"
193+
return func() error {
137194

138-
if artifact.Templates[0].Size != expectedSize {
139-
return fmt.Errorf("Wrong size. Expected %d, got %d", expectedSize, artifact.Templates[0].Size)
195+
log, err := readLog(logfile)
196+
if err != nil {
197+
return err
140198
}
141199

142-
if !strings.HasPrefix(artifact.Templates[0].Title, expectedTitle) {
143-
return fmt.Errorf("Wrong title prefix. Expected %q, got %q", expectedTitle, artifact.Templates[0].Title)
200+
uuids, err := getUuidsFromLog(log)
201+
if err != nil {
202+
return err
144203
}
204+
205+
driver := internal.NewDriver(&internal.DriverConfig{
206+
Username: os.Getenv("UPCLOUD_API_USER"),
207+
Password: os.Getenv("UPCLOUD_API_PASSWORD"),
208+
Timeout: DefaultTimeout,
209+
})
210+
211+
for _, u := range uuids {
212+
fmt.Printf("Cleaning up created templates: %s\n", u)
213+
if err := driver.DeleteTemplate(u); err != nil {
214+
return err
215+
}
216+
}
217+
145218
return nil
146219
}
147220
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
source "upcloud" "basic" {
3+
storage_uuid = "01000000-0000-4000-8000-000050010400"
4+
zone = "nl-ams1"
5+
}
6+
7+
build {
8+
sources = ["source.upcloud.basic"]
9+
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
source "upcloud" "storage-name" {
3+
ssh_username = "root"
4+
storage_name = "ubuntu server 20.04"
5+
storage_size = "20"
6+
template_prefix = "test-builder"
7+
zone = "nl-ams1"
8+
}
9+
10+
build {
11+
sources = ["source.upcloud.storage-name"]
12+
13+
}

0 commit comments

Comments
 (0)