|
1 | 1 | package upcloud |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + _ "embed" |
| 5 | + "errors" |
4 | 6 | "fmt" |
| 7 | + "io/ioutil" |
5 | 8 | "os" |
| 9 | + "os/exec" |
| 10 | + "regexp" |
6 | 11 | "strings" |
7 | 12 | "testing" |
8 | 13 |
|
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" |
11 | 16 | ) |
12 | 17 |
|
13 | 18 | // 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 | + |
14 | 34 | 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(), |
18 | 39 | Template: testBuildBasic, |
19 | | - Check: checkTemplateDefaultSettings(), |
20 | | - }) |
| 40 | + Check: checkTestResult(), |
| 41 | + Teardown: teardown(t.Name()), |
| 42 | + } |
| 43 | + acctest.TestPlugin(t, testCase) |
21 | 44 | } |
22 | 45 |
|
23 | 46 | 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) |
29 | 55 | } |
30 | 56 |
|
31 | 57 | 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) |
37 | 66 | } |
38 | 67 |
|
39 | 68 | 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) |
45 | 121 | } |
46 | 122 |
|
47 | 123 | func testAccPreCheck(t *testing.T) { |
48 | 124 | 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") |
50 | 126 | } |
51 | 127 | 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") |
53 | 129 | } |
54 | 130 | } |
55 | 131 |
|
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 |
120 | 144 | } |
121 | | -` |
122 | 145 |
|
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 | + } |
127 | 160 | } |
128 | 161 |
|
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 |
133 | 165 | } |
| 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) |
134 | 192 |
|
135 | | - expectedSize := 25 |
136 | | - expectedTitle := "custom-image" |
| 193 | + return func() error { |
137 | 194 |
|
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 |
140 | 198 | } |
141 | 199 |
|
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 |
144 | 203 | } |
| 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 | + |
145 | 218 | return nil |
146 | 219 | } |
147 | 220 | } |
0 commit comments