Skip to content

Commit

Permalink
64 bit ids
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Sep 18, 2024
1 parent f541428 commit 801e7ab
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 89 deletions.
20 changes: 10 additions & 10 deletions e2e_test/combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@ func TestCombined(t *testing.T) {
t.Fatal(err)
}

out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "server", "--server", serverName, strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "server", "--server", serverName, strconv.FormatInt(firewallID, 10))
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))
out, err = runCommand(t, "firewall", "delete", strconv.FormatInt(firewallID, 10))
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", serverName, strconv.Itoa(firewallID))
out, err = runCommand(t, "firewall", "remove-from-resource", "--type", "server", "--server", serverName, strconv.FormatInt(firewallID, 10))
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))
out, err = runCommand(t, "firewall", "delete", strconv.FormatInt(firewallID, 10))
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("firewall %d deleted\n", firewallID), out)

floatingIPName := withSuffix("test-floating-ip")
floatingIP, err := createFloatingIP(t, floatingIPName, "ipv4", "--server", strconv.Itoa(serverID))
floatingIP, err := createFloatingIP(t, floatingIPName, "ipv4", "--server", strconv.FormatInt(serverID, 10))
if err != nil {
t.Fatal(err)
}

out, err = runCommand(t, "floating-ip", "unassign", strconv.Itoa(floatingIP))
out, err = runCommand(t, "floating-ip", "unassign", strconv.FormatInt(floatingIP, 10))
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Floating IP %d unassigned\n", floatingIP), out)

out, err = runCommand(t, "floating-ip", "assign", strconv.Itoa(floatingIP), strconv.Itoa(serverID))
out, err = runCommand(t, "floating-ip", "assign", strconv.FormatInt(floatingIP, 10), strconv.FormatInt(serverID, 10))
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Floating IP %d assigned to server %d\n", floatingIP, serverID), out)

out, err = runCommand(t, "floating-ip", "describe", strconv.Itoa(floatingIP))
out, err = runCommand(t, "floating-ip", "describe", strconv.FormatInt(floatingIP, 10))
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Type:\s+ipv4
Expand All @@ -80,11 +80,11 @@ Labels:
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("%s\n", serverName), out)

out, err = runCommand(t, "floating-ip", "delete", strconv.Itoa(floatingIP))
out, err = runCommand(t, "floating-ip", "delete", strconv.FormatInt(floatingIP, 10))
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Floating IP %d deleted\n", floatingIP), out)

out, err = runCommand(t, "server", "delete", strconv.Itoa(serverID))
out, err = runCommand(t, "server", "delete", strconv.FormatInt(serverID, 10))
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Server %d deleted\n", serverID), out)
}
68 changes: 34 additions & 34 deletions e2e_test/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ func TestFirewall(t *testing.T) {
require.EqualError(t, err, "firewall not found: non-existing-firewall")
assert.Empty(t, out)

out, err = runCommand(t, "firewall", "add-label", strconv.Itoa(firewallID), "foo=bar")
out, err = runCommand(t, "firewall", "add-label", strconv.FormatInt(firewallID, 10), "foo=bar")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Label(s) foo added to firewall %d\n", firewallID), out)

out, err = runCommand(t, "firewall", "add-label", strconv.Itoa(firewallID), "baz=qux")
out, err = runCommand(t, "firewall", "add-label", strconv.FormatInt(firewallID, 10), "baz=qux")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Label(s) baz added to firewall %d\n", firewallID), out)

firewallName = withSuffix("new-test-firewall")

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

Expand All @@ -47,7 +47,7 @@ func TestFirewall(t *testing.T) {

t.Run("add-rule", func(t *testing.T) {
t.Run("missing-args", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, `required flag(s) "direction", "protocol" not set`)
assert.Empty(t, out)
})
Expand All @@ -59,67 +59,67 @@ func TestFirewall(t *testing.T) {
})

t.Run("missing-port", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID), "--direction", "in", "--source-ips", "10.0.0.0/24", "--protocol", "tcp", "--description", "Some random description")
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10), "--direction", "in", "--source-ips", "10.0.0.0/24", "--protocol", "tcp", "--description", "Some random description")
require.EqualError(t, err, "port is required (--port)")
assert.Empty(t, out)
})

t.Run("port-not-allowed", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "icmp", "--port", "12345")
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "icmp", "--port", "12345")
require.EqualError(t, err, "port is not allowed for this protocol")
assert.Empty(t, out)
})

t.Run("invalid-direction", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID), "--direction", "foo", "--destination-ips", "192.168.1.0/24", "--protocol", "tcp", "--port", "12345")
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10), "--direction", "foo", "--destination-ips", "192.168.1.0/24", "--protocol", "tcp", "--port", "12345")
require.EqualError(t, err, "invalid direction: foo")
assert.Empty(t, out)
})

t.Run("invalid-protocol", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "abc", "--port", "12345")
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "abc", "--port", "12345")
require.EqualError(t, err, "invalid protocol: abc")
assert.Empty(t, out)
})

t.Run("tcp-in", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID), "--direction", "in", "--source-ips", "10.0.0.0/24", "--protocol", "tcp", "--port", "9100", "--description", "Some random description")
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10), "--direction", "in", "--source-ips", "10.0.0.0/24", "--protocol", "tcp", "--port", "9100", "--description", "Some random description")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Firewall Rules for Firewall %d updated\n", firewallID), out)
})

t.Run("icmp-out", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "icmp")
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "icmp")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Firewall Rules for Firewall %d updated\n", firewallID), out)
})

t.Run("invalid-ip-out", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID), "--direction", "out", "--destination-ips", "invalid-ip", "--protocol", "tcp", "--port", "9100")
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10), "--direction", "out", "--destination-ips", "invalid-ip", "--protocol", "tcp", "--port", "9100")
require.EqualError(t, err, "destination error on index 0: invalid CIDR address: invalid-ip")
assert.Empty(t, out)
})

t.Run("invalid-ip-range-out", func(t *testing.T) {
out, err := runCommand(t, "firewall", "add-rule", strconv.Itoa(firewallID), "--direction", "in", "--source-ips", "10.1.2.3/8", "--protocol", "tcp", "--port", "9100")
out, err := runCommand(t, "firewall", "add-rule", strconv.FormatInt(firewallID, 10), "--direction", "in", "--source-ips", "10.1.2.3/8", "--protocol", "tcp", "--port", "9100")
require.EqualError(t, err, "source ips error on index 0: 10.1.2.3/8 is not the start of the cidr block 10.0.0.0/8")
assert.Empty(t, out)
})
})

t.Run("apply-to-resource", func(t *testing.T) {
t.Run("unknown-type", func(t *testing.T) {
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "non-existing-type", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "non-existing-type", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, "unknown type non-existing-type")
assert.Empty(t, out)
})
t.Run("missing-server", func(t *testing.T) {
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "server", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "server", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, "type server need a --server specific")
assert.Empty(t, out)
})
t.Run("missing-label-selector", func(t *testing.T) {
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "label_selector", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "label_selector", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, "type label_selector need a --label-selector specific")
assert.Empty(t, out)
})
Expand All @@ -129,18 +129,18 @@ func TestFirewall(t *testing.T) {
assert.Empty(t, out)
})
t.Run("unknown-server", func(t *testing.T) {
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "server", "--server", "non-existing-server", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "server", "--server", "non-existing-server", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, "Server not found: non-existing-server")
assert.Empty(t, out)
})
t.Run("label-selector", func(t *testing.T) {
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "label_selector", "--label-selector", "foo=bar", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "apply-to-resource", "--type", "label_selector", "--label-selector", "foo=bar", strconv.FormatInt(firewallID, 10))
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Firewall %d applied to resource\n", firewallID), out)
})
})

out, err = runCommand(t, "firewall", "describe", strconv.Itoa(firewallID))
out, err = runCommand(t, "firewall", "describe", strconv.FormatInt(firewallID, 10))
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Name:\s+new-test-firewall-[0-9a-f]{8}
Expand Down Expand Up @@ -187,17 +187,17 @@ Applied To:

t.Run("remove-from-resource", func(t *testing.T) {
t.Run("unknown-type", func(t *testing.T) {
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "non-existing-type", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "non-existing-type", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, "unknown type non-existing-type")
assert.Empty(t, out)
})
t.Run("missing-server", func(t *testing.T) {
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "server", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "server", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, "type server need a --server specific")
assert.Empty(t, out)
})
t.Run("missing-label-selector", func(t *testing.T) {
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "label_selector", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "label_selector", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, "type label_selector need a --label-selector specific")
assert.Empty(t, out)
})
Expand All @@ -207,12 +207,12 @@ Applied To:
assert.Empty(t, out)
})
t.Run("unknown-server", func(t *testing.T) {
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "server", "--server", "non-existing-server", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "server", "--server", "non-existing-server", strconv.FormatInt(firewallID, 10))
require.EqualError(t, err, "Server not found: non-existing-server")
assert.Empty(t, out)
})
t.Run("label-selector", func(t *testing.T) {
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "label_selector", "--label-selector", "foo=bar", strconv.Itoa(firewallID))
out, err := runCommand(t, "firewall", "remove-from-resource", "--type", "label_selector", "--label-selector", "foo=bar", strconv.FormatInt(firewallID, 10))
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Firewall %d removed from resource\n", firewallID), out)
})
Expand All @@ -226,31 +226,31 @@ Applied To:
})

t.Run("missing-port", func(t *testing.T) {
out, err := runCommand(t, "firewall", "delete-rule", strconv.Itoa(firewallID), "--direction", "in", "--source-ips", "10.0.0.0/24", "--protocol", "tcp")
out, err := runCommand(t, "firewall", "delete-rule", strconv.FormatInt(firewallID, 10), "--direction", "in", "--source-ips", "10.0.0.0/24", "--protocol", "tcp")
require.EqualError(t, err, "port is required (--port)")
assert.Empty(t, out)
})

t.Run("port-not-allowed", func(t *testing.T) {
out, err := runCommand(t, "firewall", "delete-rule", strconv.Itoa(firewallID), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "icmp", "--port", "12345")
out, err := runCommand(t, "firewall", "delete-rule", strconv.FormatInt(firewallID, 10), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "icmp", "--port", "12345")
require.EqualError(t, err, "port is not allowed for this protocol")
assert.Empty(t, out)
})

t.Run("tcp-in", func(t *testing.T) {
out, err := runCommand(t, "firewall", "delete-rule", strconv.Itoa(firewallID), "--direction", "in", "--source-ips", "10.0.0.0/24", "--protocol", "tcp", "--port", "9100", "--description", "Some random description")
out, err := runCommand(t, "firewall", "delete-rule", strconv.FormatInt(firewallID, 10), "--direction", "in", "--source-ips", "10.0.0.0/24", "--protocol", "tcp", "--port", "9100", "--description", "Some random description")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Firewall Rules for Firewall %d updated\n", firewallID), out)
})

t.Run("icmp-out", func(t *testing.T) {
out, err := runCommand(t, "firewall", "delete-rule", strconv.Itoa(firewallID), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "icmp")
out, err := runCommand(t, "firewall", "delete-rule", strconv.FormatInt(firewallID, 10), "--direction", "out", "--destination-ips", "192.168.1.0/24", "--protocol", "icmp")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Firewall Rules for Firewall %d updated\n", firewallID), out)
})

t.Run("non-existing-rule", func(t *testing.T) {
out, err := runCommand(t, "firewall", "delete-rule", strconv.Itoa(firewallID), "--direction", "in", "--source-ips", "123.123.123.123/32", "--port", "1234", "--protocol", "tcp")
out, err := runCommand(t, "firewall", "delete-rule", strconv.FormatInt(firewallID, 10), "--direction", "in", "--source-ips", "123.123.123.123/32", "--port", "1234", "--protocol", "tcp")
require.EqualError(t, err, fmt.Sprintf("the specified rule was not found in the ruleset of Firewall %d", firewallID))
assert.Empty(t, out)
})
Expand All @@ -260,16 +260,16 @@ Applied To:
require.EqualError(t, err, "Firewall not found: non-existing-firewall")
assert.Empty(t, out)

out, err = runCommand(t, "firewall", "replace-rules", strconv.Itoa(firewallID), "--rules-file", "rules_file.json")
out, err = runCommand(t, "firewall", "replace-rules", strconv.FormatInt(firewallID, 10), "--rules-file", "rules_file.json")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Firewall Rules for Firewall %d updated\n", firewallID), out)

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

func createFirewall(t *testing.T, name string, args ...string) (int, error) {
func createFirewall(t *testing.T, name string, args ...string) (int64, error) {
t.Helper()
t.Cleanup(func() {
_, _ = client.Firewall.Delete(context.Background(), &hcloud.Firewall{Name: name})
Expand All @@ -284,13 +284,13 @@ func createFirewall(t *testing.T, name string, args ...string) (int, error) {
return 0, fmt.Errorf("invalid response: %s", out)
}

firewallID, err := strconv.Atoi(out[9 : len(out)-9])
id, err := strconv.ParseInt(out[9:len(out)-9], 10, 64)
if err != nil {
return 0, err
}

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

0 comments on commit 801e7ab

Please sign in to comment.