Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(e2e): add end to end testing #785

Merged
merged 15 commits into from
Sep 23, 2024
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:

test:
runs-on: ubuntu-latest
permissions:
id-token: write
apricote marked this conversation as resolved.
Show resolved Hide resolved
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/e2e_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: E2E Tests
apricote marked this conversation as resolved.
Show resolved Hide resolved

on:
push:
branches: [main]
pull_request:

jobs:
test:
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23"

- uses: hetznercloud/tps-action@main

- name: Run tests
run: go test -tags e2e -coverpkg=./... -coverprofile=coverage.txt -v -race ./e2e_test

- name: Upload coverage reports to Codecov
if: >
!startsWith(github.head_ref, 'renovate/') &&
!startsWith(github.head_ref, 'release-please--')
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
apricote marked this conversation as resolved.
Show resolved Hide resolved
109 changes: 109 additions & 0 deletions e2e_test/combined_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//go:build e2e

package e2e

import (
"fmt"
"strconv"
"testing"

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

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

firewallName := withSuffix("test-firewall")
firewallID, err := createFirewall(t, firewallName)
require.NoError(t, err)

t.Run("firewall", func(t *testing.T) {
t.Run("apply-to-server", func(t *testing.T) {
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)
})

t.Run("delete-in-use", func(t *testing.T) {
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)
})

t.Run("remove-from-server", func(t *testing.T) {
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)
})

t.Run("delete", func(t *testing.T) {
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.FormatInt(serverID, 10))
require.NoError(t, err)

t.Run("floating-ip", func(t *testing.T) {
t.Run("unassign", func(t *testing.T) {
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)
})

t.Run("assign", func(t *testing.T) {
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)
})

t.Run("describe", func(t *testing.T) {
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
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-[0-9a-f]{8}
DNS:
.*?
Protection:
\s+Delete:\s+no
Labels:
\s+No labels
`, out)
})

t.Run("list", func(t *testing.T) {
out, err := runCommand(t, "floating-ip", "list", "-o", "columns=server", "-o", "noheader")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("%s\n", serverName), out)
})

t.Run("delete", func(t *testing.T) {
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)
})
})

t.Run("delete-server", func(t *testing.T) {
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)
})
}
1 change: 1 addition & 0 deletions e2e_test/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# config for tests goes here
56 changes: 56 additions & 0 deletions e2e_test/datacenter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//go:build e2e

package e2e

import (
"encoding/json"
"testing"

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

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

t.Run("list", func(t *testing.T) {
t.Run("table", func(t *testing.T) {
out, err := runCommand(t, "datacenter", "list")
require.NoError(t, err)
assert.Regexp(t, `ID +NAME +DESCRIPTION +LOCATION
([0-9]+ +[a-z0-9\-]+ +[a-zA-Z0-9\- ]+ +[a-z0-9\-]+\n)+`, out)
})

t.Run("json", func(t *testing.T) {
out, err := runCommand(t, "datacenter", "list", "-o=json")
require.NoError(t, err)
require.NoError(t, json.Unmarshal([]byte(out), new([]any)))
})
})

t.Run("describe", func(t *testing.T) {
t.Run("non-existing", func(t *testing.T) {
out, err := runCommand(t, "datacenter", "describe", "123456")
require.EqualError(t, err, "datacenter not found: 123456")
assert.Empty(t, out)
})

t.Run("normal", func(t *testing.T) {
out, err := runCommand(t, "datacenter", "describe", TestDatacenterID)
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Name:\s+[a-z0-9\-]+
Description:\s+[a-zA-Z0-9\- ]+
Location:
+Name:\s+[a-z0-9]+
+Description:\s+[a-zA-Z0-9\- ]+
+Country:\s+[A-Z]{2}
+City:\s+[A-Za-z]+
+Latitude:\s+[0-9\.]+
+Longitude:\s+[0-9\.]+
Server Types:
(\s+- ID: [0-9]+\s+Name: [a-z0-9]+\s+Supported: (true|false)\s+Available: (true|false))+
`, out)
})
})
}
66 changes: 66 additions & 0 deletions e2e_test/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//go:build e2e

package e2e

import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"testing"

"github.com/spf13/cobra"

"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 == "" {
fmt.Println("HCLOUD_TOKEN is not set")
os.Exit(1)
return
}
os.Exit(m.Run())
}

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)
}

s, err := state.New(cfg)
if err != nil {
t.Fatal(err)
}

return cli.NewRootCommand(s)
}

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)
}
Loading
Loading