Skip to content

Commit

Permalink
random resource names, failsafe cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Sep 18, 2024
1 parent 2e9345c commit f541428
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 69 deletions.
22 changes: 14 additions & 8 deletions e2e_test/combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,36 @@ import (

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

firewallID, err := createFirewall(t, "test-firewall")
firewallName := withSuffix("test-firewall")
firewallID, err := createFirewall(t, firewallName)
if err != nil {
t.Fatal(err)
}

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

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

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

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

floatingIP, err := createFloatingIP(t, "ipv4", "--server", strconv.Itoa(serverID))
floatingIPName := withSuffix("test-floating-ip")
floatingIP, err := createFloatingIP(t, floatingIPName, "ipv4", "--server", strconv.Itoa(serverID))
if err != nil {
t.Fatal(err)
}
Expand All @@ -53,15 +59,15 @@ func TestCombined(t *testing.T) {
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Type:\s+ipv4
Name:\s+test-floating-ip
Name:\s+test-floating-ip-[0-9a-f]{8}
Description:\s+-
Created:.*?
IP:\s+(?:[0-9]{1,3}\.){3}[0-9]{1,3}
Blocked:\s+no
Home Location:\s+[a-z]{3}[0-9]*
Server:
\s+ID:\s+[0-9]+
\s+Name:\s+test-server
\s+Name:\s+test-server-[0-9a-f]{8}
DNS:
.*?
Protection:
Expand All @@ -72,7 +78,7 @@ Labels:

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

out, err = runCommand(t, "floating-ip", "delete", strconv.Itoa(floatingIP))
require.NoError(t, err)
Expand Down
17 changes: 17 additions & 0 deletions e2e_test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package e2e

import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"testing"
Expand All @@ -13,8 +15,11 @@ import (
"github.com/hetznercloud/cli/internal/cli"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var client = hcloud.NewClient(hcloud.WithToken(os.Getenv("HCLOUD_TOKEN")))

func TestMain(m *testing.M) {
tok := os.Getenv("HCLOUD_TOKEN")
if tok == "" {
Expand All @@ -26,6 +31,7 @@ func TestMain(m *testing.M) {
}

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

func runCommand(t *testing.T, args ...string) (string, error) {
t.Helper()
cmd := newRootCommand(t)
var buf bytes.Buffer
cmd.SetArgs(args)
cmd.SetOut(&buf)
err := cmd.Execute()
return buf.String(), err
}

func withSuffix(s string) string {
b := make([]byte, 4)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
suffix := hex.EncodeToString(b)
return fmt.Sprintf("%s-%s", s, suffix)
}
23 changes: 19 additions & 4 deletions e2e_test/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
package e2e

import (
"context"
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestFirewall(t *testing.T) {
t.Parallel()

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

out, err = runCommand(t, "firewall", "update", strconv.Itoa(firewallID), "--name", "new-test-firewall")
firewallName = withSuffix("new-test-firewall")

out, err = runCommand(t, "firewall", "update", strconv.Itoa(firewallID), "--name", firewallName)
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Firewall %d updated\n", firewallID), out)

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

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

func createFirewall(t *testing.T, name string, args ...string) (int, error) {
t.Helper()
t.Cleanup(func() {
_, _ = client.Firewall.Delete(context.Background(), &hcloud.Firewall{Name: name})
})

out, err := runCommand(t, append([]string{"firewall", "create", "--name", name}, args...)...)
if err != nil {
return 0, err
Expand All @@ -277,5 +288,9 @@ func createFirewall(t *testing.T, name string, args ...string) (int, error) {
if err != nil {
return 0, err
}

t.Cleanup(func() {
_, _ = client.Firewall.Delete(context.Background(), &hcloud.Firewall{ID: int64(firewallID)})
})
return firewallID, nil
}
43 changes: 30 additions & 13 deletions e2e_test/floatingip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package e2e

import (
"context"
"fmt"
"net"
"strconv"
Expand All @@ -12,21 +13,25 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/swaggest/assertjson"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestFloatingIP(t *testing.T) {
t.Parallel()

_, err := createFloatingIP(t, "")
floatingIPName := withSuffix("test-floating-ip")

_, err := createFloatingIP(t, floatingIPName, "")
require.EqualError(t, err, "type is required")

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

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

floatingIPId, err := createFloatingIP(t, "ipv4", "--home-location", TestLocationName)
floatingIPId, err := createFloatingIP(t, floatingIPName, "ipv4", "--home-location", TestLocationName)
if err != nil {
t.Fatal(err)
}
Expand All @@ -45,7 +50,9 @@ func TestFloatingIP(t *testing.T) {
})
})

out, err := runCommand(t, "floating-ip", "update", strconv.Itoa(floatingIPId), "--name", "new-test-floating-ip", "--description", "Some description")
floatingIPName = withSuffix("new-test-floating-ip")

out, err := runCommand(t, "floating-ip", "update", strconv.Itoa(floatingIPId), "--name", floatingIPName, "--description", "Some description")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Floating IP %d updated\n", floatingIPId), out)

Expand Down Expand Up @@ -94,7 +101,7 @@ func TestFloatingIP(t *testing.T) {
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Type:\s+ipv4
Name:\s+new-test-floating-ip
Name:\s+new-test-floating-ip-[0-9a-f]{8}
Description:\s+Some description
Created:.*?
IP:\s+(?:[0-9]{1,3}\.){3}[0-9]{1,3}
Expand All @@ -113,7 +120,7 @@ Labels:
out, err = runCommand(t, "floating-ip", "list", "--output", "columns=id,name,type,ip,dns,server,home,blocked,protection,labels,created,age")
require.NoError(t, err)
assert.Regexp(t, `^ID +NAME +TYPE +IP +DNS +SERVER +HOME +BLOCKED +PROTECTION +LABELS +CREATED +AGE
[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.*?
[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.*?
$`, out)

out, err = runCommand(t, "floating-ip", "list", "-o=json")
Expand Down Expand Up @@ -150,10 +157,10 @@ $`, out)
"labels": {
"foo": "bar"
},
"name": "new-test-floating-ip"
"name": "%s"
}
]
`, floatingIPId, ipStr, ipStr, TestLocationName)), []byte(out))
`, floatingIPId, ipStr, ipStr, TestLocationName, floatingIPName)), []byte(out))

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

floatingIPId, err = createFloatingIP(t, "ipv6", "--home-location", TestLocationName)
floatingIPName = withSuffix("test-floating-ipv6")
floatingIPId, err = createFloatingIP(t, floatingIPName, "ipv6", "--home-location", TestLocationName)
if err != nil {
t.Fatal(err)
}
Expand All @@ -192,7 +200,7 @@ $`, out)
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Type:\s+ipv6
Name:\s+test-floating-ip
Name:\s+test-floating-ipv6-[0-9a-f]{8}
Description:\s+-
Created:.*?
IP:\s+[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+::\/64
Expand Down Expand Up @@ -235,8 +243,13 @@ Labels:
assert.Equal(t, fmt.Sprintf("Floating IP %d deleted\n", floatingIPId), out)
}

func createFloatingIP(t *testing.T, ipType string, args ...string) (int, error) {
out, err := runCommand(t, append([]string{"floating-ip", "create", "--name", "test-floating-ip", "--type", ipType}, args...)...)
func createFloatingIP(t *testing.T, name, ipType string, args ...string) (int, error) {
t.Helper()
t.Cleanup(func() {
_, _ = client.FloatingIP.Delete(context.Background(), &hcloud.FloatingIP{Name: name})
})

out, err := runCommand(t, append([]string{"floating-ip", "create", "--name", name, "--type", ipType}, args...)...)
if err != nil {
return 0, err
}
Expand All @@ -250,5 +263,9 @@ func createFloatingIP(t *testing.T, ipType string, args ...string) (int, error)
if err != nil {
return 0, err
}

t.Cleanup(func() {
_, _ = client.FloatingIP.Delete(context.Background(), &hcloud.FloatingIP{ID: int64(id)})
})
return id, nil
}
Loading

0 comments on commit f541428

Please sign in to comment.