diff --git a/test/e2e/iso_test.go b/test/e2e/iso_test.go new file mode 100644 index 00000000..cd55d093 --- /dev/null +++ b/test/e2e/iso_test.go @@ -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) + }) + }) +} diff --git a/test/e2e/util.go b/test/e2e/util.go index 9be06e45..510c5973 100644 --- a/test/e2e/util.go +++ b/test/e2e/util.go @@ -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 { diff --git a/test/e2e/variables.go b/test/e2e/variables.go index a963d763..d38a180f 100644 --- a/test/e2e/variables.go +++ b/test/e2e/variables.go @@ -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 {