Skip to content

Commit f541428

Browse files
committed
random resource names, failsafe cleanup
1 parent 2e9345c commit f541428

7 files changed

+189
-69
lines changed

e2e_test/combined_test.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,36 @@ import (
1313

1414
func TestCombined(t *testing.T) {
1515
// combined tests combine multiple resources and can thus not be run in parallel
16-
serverID := createServer(t, "test-server", TestServerType, TestImage)
16+
serverName := withSuffix("test-server")
17+
serverID, err := createServer(t, serverName, TestServerType, TestImage)
18+
if err != nil {
19+
t.Fatal(err)
20+
}
1721

18-
firewallID, err := createFirewall(t, "test-firewall")
22+
firewallName := withSuffix("test-firewall")
23+
firewallID, err := createFirewall(t, firewallName)
1924
if err != nil {
2025
t.Fatal(err)
2126
}
2227

23-
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "server", "--server", "test-server", strconv.Itoa(firewallID))
28+
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "server", "--server", serverName, strconv.Itoa(firewallID))
2429
require.NoError(t, err)
2530
assert.Equal(t, fmt.Sprintf("Firewall %d applied to resource\n", firewallID), out)
2631

2732
out, err = runCommand(t, "firewall", "delete", strconv.Itoa(firewallID))
2833
assert.Regexp(t, `^firewall with ID [0-9]+ is still in use \(resource_in_use, [0-9a-f]+\)$`, err.Error())
2934
assert.Empty(t, out)
3035

31-
out, err = runCommand(t, "firewall", "remove-from-resource", "--type", "server", "--server", "test-server", strconv.Itoa(firewallID))
36+
out, err = runCommand(t, "firewall", "remove-from-resource", "--type", "server", "--server", serverName, strconv.Itoa(firewallID))
3237
require.NoError(t, err)
3338
assert.Equal(t, fmt.Sprintf("Firewall %d removed from resource\n", firewallID), out)
3439

3540
out, err = runCommand(t, "firewall", "delete", strconv.Itoa(firewallID))
3641
require.NoError(t, err)
3742
assert.Equal(t, fmt.Sprintf("firewall %d deleted\n", firewallID), out)
3843

39-
floatingIP, err := createFloatingIP(t, "ipv4", "--server", strconv.Itoa(serverID))
44+
floatingIPName := withSuffix("test-floating-ip")
45+
floatingIP, err := createFloatingIP(t, floatingIPName, "ipv4", "--server", strconv.Itoa(serverID))
4046
if err != nil {
4147
t.Fatal(err)
4248
}
@@ -53,15 +59,15 @@ func TestCombined(t *testing.T) {
5359
require.NoError(t, err)
5460
assert.Regexp(t, `ID:\s+[0-9]+
5561
Type:\s+ipv4
56-
Name:\s+test-floating-ip
62+
Name:\s+test-floating-ip-[0-9a-f]{8}
5763
Description:\s+-
5864
Created:.*?
5965
IP:\s+(?:[0-9]{1,3}\.){3}[0-9]{1,3}
6066
Blocked:\s+no
6167
Home Location:\s+[a-z]{3}[0-9]*
6268
Server:
6369
\s+ID:\s+[0-9]+
64-
\s+Name:\s+test-server
70+
\s+Name:\s+test-server-[0-9a-f]{8}
6571
DNS:
6672
.*?
6773
Protection:
@@ -72,7 +78,7 @@ Labels:
7278

7379
out, err = runCommand(t, "floating-ip", "list", "-o", "columns=server", "-o", "noheader")
7480
require.NoError(t, err)
75-
assert.Equal(t, "test-server\n", out)
81+
assert.Equal(t, fmt.Sprintf("%s\n", serverName), out)
7682

7783
out, err = runCommand(t, "floating-ip", "delete", strconv.Itoa(floatingIP))
7884
require.NoError(t, err)

e2e_test/e2e_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package e2e
44

55
import (
66
"bytes"
7+
"crypto/rand"
8+
"encoding/hex"
79
"fmt"
810
"os"
911
"testing"
@@ -13,8 +15,11 @@ import (
1315
"github.com/hetznercloud/cli/internal/cli"
1416
"github.com/hetznercloud/cli/internal/state"
1517
"github.com/hetznercloud/cli/internal/state/config"
18+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
1619
)
1720

21+
var client = hcloud.NewClient(hcloud.WithToken(os.Getenv("HCLOUD_TOKEN")))
22+
1823
func TestMain(m *testing.M) {
1924
tok := os.Getenv("HCLOUD_TOKEN")
2025
if tok == "" {
@@ -26,6 +31,7 @@ func TestMain(m *testing.M) {
2631
}
2732

2833
func newRootCommand(t *testing.T) *cobra.Command {
34+
t.Helper()
2935
cfg := config.New()
3036
if err := cfg.Read("config.toml"); err != nil {
3137
t.Fatalf("unable to read config file \"%s\": %s\n", cfg.Path(), err)
@@ -40,10 +46,21 @@ func newRootCommand(t *testing.T) *cobra.Command {
4046
}
4147

4248
func runCommand(t *testing.T, args ...string) (string, error) {
49+
t.Helper()
4350
cmd := newRootCommand(t)
4451
var buf bytes.Buffer
4552
cmd.SetArgs(args)
4653
cmd.SetOut(&buf)
4754
err := cmd.Execute()
4855
return buf.String(), err
4956
}
57+
58+
func withSuffix(s string) string {
59+
b := make([]byte, 4)
60+
_, err := rand.Read(b)
61+
if err != nil {
62+
panic(err)
63+
}
64+
suffix := hex.EncodeToString(b)
65+
return fmt.Sprintf("%s-%s", s, suffix)
66+
}

e2e_test/firewall_test.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33
package e2e
44

55
import (
6+
"context"
67
"fmt"
78
"strconv"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
13+
14+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
1215
)
1316

1417
func TestFirewall(t *testing.T) {
1518
t.Parallel()
1619

17-
firewallID, err := createFirewall(t, "test-firewall", "--rules-file", "rules_file.json")
20+
firewallName := withSuffix("test-firewall")
21+
firewallID, err := createFirewall(t, firewallName, "--rules-file", "rules_file.json")
1822
if err != nil {
1923
t.Fatal(err)
2024
}
@@ -31,11 +35,13 @@ func TestFirewall(t *testing.T) {
3135
require.NoError(t, err)
3236
assert.Equal(t, fmt.Sprintf("Label(s) baz added to firewall %d\n", firewallID), out)
3337

34-
out, err = runCommand(t, "firewall", "update", strconv.Itoa(firewallID), "--name", "new-test-firewall")
38+
firewallName = withSuffix("new-test-firewall")
39+
40+
out, err = runCommand(t, "firewall", "update", strconv.Itoa(firewallID), "--name", firewallName)
3541
require.NoError(t, err)
3642
assert.Equal(t, fmt.Sprintf("Firewall %d updated\n", firewallID), out)
3743

38-
out, err = runCommand(t, "firewall", "remove-label", "new-test-firewall", "baz")
44+
out, err = runCommand(t, "firewall", "remove-label", firewallName, "baz")
3945
require.NoError(t, err)
4046
assert.Equal(t, fmt.Sprintf("Label(s) baz removed from firewall %d\n", firewallID), out)
4147

@@ -137,7 +143,7 @@ func TestFirewall(t *testing.T) {
137143
out, err = runCommand(t, "firewall", "describe", strconv.Itoa(firewallID))
138144
require.NoError(t, err)
139145
assert.Regexp(t, `ID:\s+[0-9]+
140-
Name:\s+new-test-firewall
146+
Name:\s+new-test-firewall-[0-9a-f]{8}
141147
Created:\s+.*?
142148
Labels:
143149
\s+foo: bar
@@ -264,6 +270,11 @@ Applied To:
264270
}
265271

266272
func createFirewall(t *testing.T, name string, args ...string) (int, error) {
273+
t.Helper()
274+
t.Cleanup(func() {
275+
_, _ = client.Firewall.Delete(context.Background(), &hcloud.Firewall{Name: name})
276+
})
277+
267278
out, err := runCommand(t, append([]string{"firewall", "create", "--name", name}, args...)...)
268279
if err != nil {
269280
return 0, err
@@ -277,5 +288,9 @@ func createFirewall(t *testing.T, name string, args ...string) (int, error) {
277288
if err != nil {
278289
return 0, err
279290
}
291+
292+
t.Cleanup(func() {
293+
_, _ = client.Firewall.Delete(context.Background(), &hcloud.Firewall{ID: int64(firewallID)})
294+
})
280295
return firewallID, nil
281296
}

e2e_test/floatingip_test.go

+30-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package e2e
44

55
import (
6+
"context"
67
"fmt"
78
"net"
89
"strconv"
@@ -12,21 +13,25 @@ import (
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
1415
"github.com/swaggest/assertjson"
16+
17+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
1518
)
1619

1720
func TestFloatingIP(t *testing.T) {
1821
t.Parallel()
1922

20-
_, err := createFloatingIP(t, "")
23+
floatingIPName := withSuffix("test-floating-ip")
24+
25+
_, err := createFloatingIP(t, floatingIPName, "")
2126
require.EqualError(t, err, "type is required")
2227

23-
_, err = createFloatingIP(t, "ipv4")
28+
_, err = createFloatingIP(t, floatingIPName, "ipv4")
2429
require.EqualError(t, err, "one of --home-location or --server is required")
2530

26-
_, err = createFloatingIP(t, "ipv4", "--server", "non-existing-server")
31+
_, err = createFloatingIP(t, floatingIPName, "ipv4", "--server", "non-existing-server")
2732
require.EqualError(t, err, "server not found: non-existing-server")
2833

29-
floatingIPId, err := createFloatingIP(t, "ipv4", "--home-location", TestLocationName)
34+
floatingIPId, err := createFloatingIP(t, floatingIPName, "ipv4", "--home-location", TestLocationName)
3035
if err != nil {
3136
t.Fatal(err)
3237
}
@@ -45,7 +50,9 @@ func TestFloatingIP(t *testing.T) {
4550
})
4651
})
4752

48-
out, err := runCommand(t, "floating-ip", "update", strconv.Itoa(floatingIPId), "--name", "new-test-floating-ip", "--description", "Some description")
53+
floatingIPName = withSuffix("new-test-floating-ip")
54+
55+
out, err := runCommand(t, "floating-ip", "update", strconv.Itoa(floatingIPId), "--name", floatingIPName, "--description", "Some description")
4956
require.NoError(t, err)
5057
assert.Equal(t, fmt.Sprintf("Floating IP %d updated\n", floatingIPId), out)
5158

@@ -94,7 +101,7 @@ func TestFloatingIP(t *testing.T) {
94101
require.NoError(t, err)
95102
assert.Regexp(t, `ID:\s+[0-9]+
96103
Type:\s+ipv4
97-
Name:\s+new-test-floating-ip
104+
Name:\s+new-test-floating-ip-[0-9a-f]{8}
98105
Description:\s+Some description
99106
Created:.*?
100107
IP:\s+(?:[0-9]{1,3}\.){3}[0-9]{1,3}
@@ -113,7 +120,7 @@ Labels:
113120
out, err = runCommand(t, "floating-ip", "list", "--output", "columns=id,name,type,ip,dns,server,home,blocked,protection,labels,created,age")
114121
require.NoError(t, err)
115122
assert.Regexp(t, `^ID +NAME +TYPE +IP +DNS +SERVER +HOME +BLOCKED +PROTECTION +LABELS +CREATED +AGE
116-
[0-9]+ +new-test-floating-ip +ipv4 +(?:[0-9]{1,3}\.){3}[0-9]{1,3} +s1\.example\.com +- +[a-z]{3}[0-9]* +no +delete +foo=bar.*?
123+
[0-9]+ +new-test-floating-ip-[0-9a-f]{8} +ipv4 +(?:[0-9]{1,3}\.){3}[0-9]{1,3} +s1\.example\.com +- +[a-z]{3}[0-9]* +no +delete +foo=bar.*?
117124
$`, out)
118125

119126
out, err = runCommand(t, "floating-ip", "list", "-o=json")
@@ -150,10 +157,10 @@ $`, out)
150157
"labels": {
151158
"foo": "bar"
152159
},
153-
"name": "new-test-floating-ip"
160+
"name": "%s"
154161
}
155162
]
156-
`, floatingIPId, ipStr, ipStr, TestLocationName)), []byte(out))
163+
`, floatingIPId, ipStr, ipStr, TestLocationName, floatingIPName)), []byte(out))
157164

158165
out, err = runCommand(t, "floating-ip", "delete", strconv.Itoa(floatingIPId))
159166
assert.Regexp(t, `^Floating IP deletion is protected \(protected, [0-9a-f]+\)$`, err.Error())
@@ -183,7 +190,8 @@ $`, out)
183190
require.NoError(t, err)
184191
assert.Equal(t, fmt.Sprintf("Floating IP %d deleted\n", floatingIPId), out)
185192

186-
floatingIPId, err = createFloatingIP(t, "ipv6", "--home-location", TestLocationName)
193+
floatingIPName = withSuffix("test-floating-ipv6")
194+
floatingIPId, err = createFloatingIP(t, floatingIPName, "ipv6", "--home-location", TestLocationName)
187195
if err != nil {
188196
t.Fatal(err)
189197
}
@@ -192,7 +200,7 @@ $`, out)
192200
require.NoError(t, err)
193201
assert.Regexp(t, `ID:\s+[0-9]+
194202
Type:\s+ipv6
195-
Name:\s+test-floating-ip
203+
Name:\s+test-floating-ipv6-[0-9a-f]{8}
196204
Description:\s+-
197205
Created:.*?
198206
IP:\s+[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+::\/64
@@ -235,8 +243,13 @@ Labels:
235243
assert.Equal(t, fmt.Sprintf("Floating IP %d deleted\n", floatingIPId), out)
236244
}
237245

238-
func createFloatingIP(t *testing.T, ipType string, args ...string) (int, error) {
239-
out, err := runCommand(t, append([]string{"floating-ip", "create", "--name", "test-floating-ip", "--type", ipType}, args...)...)
246+
func createFloatingIP(t *testing.T, name, ipType string, args ...string) (int, error) {
247+
t.Helper()
248+
t.Cleanup(func() {
249+
_, _ = client.FloatingIP.Delete(context.Background(), &hcloud.FloatingIP{Name: name})
250+
})
251+
252+
out, err := runCommand(t, append([]string{"floating-ip", "create", "--name", name, "--type", ipType}, args...)...)
240253
if err != nil {
241254
return 0, err
242255
}
@@ -250,5 +263,9 @@ func createFloatingIP(t *testing.T, ipType string, args ...string) (int, error)
250263
if err != nil {
251264
return 0, err
252265
}
266+
267+
t.Cleanup(func() {
268+
_, _ = client.FloatingIP.Delete(context.Background(), &hcloud.FloatingIP{ID: int64(id)})
269+
})
253270
return id, nil
254271
}

0 commit comments

Comments
 (0)