Skip to content

Commit 554ec50

Browse files
authored
test: add ISO e2e tests (#953)
1 parent 5b86fca commit 554ec50

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

test/e2e/iso_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//go:build e2e
2+
3+
package e2e
4+
5+
import (
6+
"context"
7+
"encoding/json"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
14+
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
15+
)
16+
17+
func TestISO(t *testing.T) {
18+
t.Parallel()
19+
20+
t.Run("list", func(t *testing.T) {
21+
t.Run("table", func(t *testing.T) {
22+
out, err := runCommand(t, "iso", "list")
23+
require.NoError(t, err)
24+
assert.Regexp(t,
25+
NewRegex().Start().
26+
SeparatedByWhitespace("ID", "NAME", "DESCRIPTION", "TYPE", "ARCHITECTURE").Newline().
27+
AnyTimes(NewRegex().
28+
Int().Whitespace().
29+
Identifier().Whitespace().
30+
AnyString().Whitespace().
31+
OneOfLit("public", "private").Whitespace().
32+
OneOf("arm", "x86").Newline()).
33+
End(),
34+
out,
35+
)
36+
})
37+
38+
t.Run("json", func(t *testing.T) {
39+
var schemas []schema.ISO
40+
isos, err := client.ISO.All(context.Background())
41+
require.NoError(t, err)
42+
for _, iso := range isos {
43+
schemas = append(schemas, hcloud.SchemaFromISO(iso))
44+
}
45+
expectedJson, err := json.Marshal(schemas)
46+
require.NoError(t, err)
47+
48+
out, err := runCommand(t, "iso", "list", "-o=json")
49+
require.NoError(t, err)
50+
assert.JSONEq(t, string(expectedJson), out)
51+
})
52+
})
53+
54+
t.Run("describe", func(t *testing.T) {
55+
t.Run("non-existing", func(t *testing.T) {
56+
out, err := runCommand(t, "iso", "describe", "non-existing-iso")
57+
require.EqualError(t, err, "iso not found: non-existing-iso")
58+
assert.Empty(t, out)
59+
})
60+
61+
t.Run("normal", func(t *testing.T) {
62+
out, err := runCommand(t, "iso", "describe", TestISOName)
63+
require.NoError(t, err)
64+
65+
assert.Regexp(t,
66+
NewRegex().Start().
67+
Lit("ID:").Whitespace().Int().Newline().
68+
Lit("Name:").Whitespace().Identifier().Newline().
69+
Lit("Description:").Whitespace().AnyString().Newline().
70+
Lit("Type:").Whitespace().OneOfLit("public", "private").Newline().
71+
Lit("Architecture:").Whitespace().OneOfLit("arm", "x86").Newline().
72+
End(),
73+
out,
74+
)
75+
})
76+
77+
t.Run("json", func(t *testing.T) {
78+
iso, _, err := client.ISO.GetByName(context.Background(), TestISOName)
79+
require.NoError(t, err)
80+
expectedJson, err := json.Marshal(hcloud.SchemaFromISO(iso))
81+
require.NoError(t, err)
82+
83+
out, err := runCommand(t, "iso", "describe", TestISOName, "-o=json")
84+
require.NoError(t, err)
85+
assert.JSONEq(t, string(expectedJson), out)
86+
})
87+
})
88+
}

test/e2e/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (r RegexBuilder) AnyString() RegexBuilder {
8686
}
8787

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

9292
func (r RegexBuilder) Age() RegexBuilder {

test/e2e/variables.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ var (
3535

3636
// TestLocationName is the default location where we execute our end-to-end tests.
3737
TestLocationName = getEnv("TEST_LOCATION", "nbg1")
38+
39+
// TestISOName is the default ISO used for testing
40+
TestISOName = getEnv("TEST_ISO_NAME", "ubuntu-24.04-live-server-amd64.iso")
3841
)
3942

4043
func getEnv(key, fallback string) string {

0 commit comments

Comments
 (0)