Skip to content

Commit

Permalink
test: add ISO e2e tests (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 authored Jan 16, 2025
1 parent 5b86fca commit 554ec50
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
88 changes: 88 additions & 0 deletions test/e2e/iso_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//go:build e2e

package e2e

import (
"context"
"encoding/json"
"testing"

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

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

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

t.Run("list", func(t *testing.T) {
t.Run("table", func(t *testing.T) {
out, err := runCommand(t, "iso", "list")
require.NoError(t, err)
assert.Regexp(t,
NewRegex().Start().
SeparatedByWhitespace("ID", "NAME", "DESCRIPTION", "TYPE", "ARCHITECTURE").Newline().
AnyTimes(NewRegex().
Int().Whitespace().
Identifier().Whitespace().
AnyString().Whitespace().
OneOfLit("public", "private").Whitespace().
OneOf("arm", "x86").Newline()).
End(),
out,
)
})

t.Run("json", func(t *testing.T) {
var schemas []schema.ISO
isos, err := client.ISO.All(context.Background())
require.NoError(t, err)
for _, iso := range isos {
schemas = append(schemas, hcloud.SchemaFromISO(iso))
}
expectedJson, err := json.Marshal(schemas)
require.NoError(t, err)

out, err := runCommand(t, "iso", "list", "-o=json")
require.NoError(t, err)
assert.JSONEq(t, string(expectedJson), out)
})
})

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

t.Run("normal", func(t *testing.T) {
out, err := runCommand(t, "iso", "describe", TestISOName)
require.NoError(t, err)

assert.Regexp(t,
NewRegex().Start().
Lit("ID:").Whitespace().Int().Newline().
Lit("Name:").Whitespace().Identifier().Newline().
Lit("Description:").Whitespace().AnyString().Newline().
Lit("Type:").Whitespace().OneOfLit("public", "private").Newline().
Lit("Architecture:").Whitespace().OneOfLit("arm", "x86").Newline().
End(),
out,
)
})

t.Run("json", func(t *testing.T) {
iso, _, err := client.ISO.GetByName(context.Background(), TestISOName)
require.NoError(t, err)
expectedJson, err := json.Marshal(hcloud.SchemaFromISO(iso))
require.NoError(t, err)

out, err := runCommand(t, "iso", "describe", TestISOName, "-o=json")
require.NoError(t, err)
assert.JSONEq(t, string(expectedJson), out)
})
})
}
2 changes: 1 addition & 1 deletion test/e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (r RegexBuilder) AnyString() RegexBuilder {
}

func (r RegexBuilder) Identifier() RegexBuilder {
return r.Raw(`[a-zA-Z0-9](?:[a-zA-Z0-9\-.]*[a-zA-Z0-9])?`)
return r.Raw(`[a-zA-Z0-9](?:[a-zA-Z0-9\-_.]*[a-zA-Z0-9])?`)
}

func (r RegexBuilder) Age() RegexBuilder {
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var (

// TestLocationName is the default location where we execute our end-to-end tests.
TestLocationName = getEnv("TEST_LOCATION", "nbg1")

// TestISOName is the default ISO used for testing
TestISOName = getEnv("TEST_ISO_NAME", "ubuntu-24.04-live-server-amd64.iso")
)

func getEnv(key, fallback string) string {
Expand Down

0 comments on commit 554ec50

Please sign in to comment.