diff --git a/test/e2e/load_balancer_type_test.go b/test/e2e/load_balancer_type_test.go new file mode 100644 index 00000000..6eed4ec0 --- /dev/null +++ b/test/e2e/load_balancer_type_test.go @@ -0,0 +1,99 @@ +//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 TestLoadBalancerType(t *testing.T) { + t.Parallel() + + t.Run("list", func(t *testing.T) { + t.Run("table", func(t *testing.T) { + out, err := runCommand(t, "load-balancer-type", "list") + require.NoError(t, err) + assert.Regexp(t, + NewRegex().Start(). + SeparatedByWhitespace("ID", "NAME", "DESCRIPTION", "MAX SERVICES", "MAX CONNECTIONS", "MAX TARGETS").Newline(). + AnyTimes(NewRegex(). + Int().Whitespace(). + Identifier().Whitespace(). + AnyString().Whitespace(). + Int().Whitespace(). + Int().Whitespace(). + Int().Newline()). + End(), + out, + ) + }) + + t.Run("json", func(t *testing.T) { + var schemas []schema.LoadBalancerType + lbts, err := client.LoadBalancerType.All(context.Background()) + require.NoError(t, err) + for _, lbt := range lbts { + schemas = append(schemas, hcloud.SchemaFromLoadBalancerType(lbt)) + } + expectedJson, err := json.Marshal(schemas) + require.NoError(t, err) + + out, err := runCommand(t, "load-balancer-type", "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, "load-balancer-type", "describe", "non-existing") + require.EqualError(t, err, "Load Balancer Type not found: non-existing") + assert.Empty(t, out) + }) + + t.Run("normal", func(t *testing.T) { + out, err := runCommand(t, "load-balancer-type", "describe", TestLoadBalancerTypeName) + 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("Max Services:").Whitespace().Int().Newline(). + Lit("Max Connections:").Whitespace().Int().Newline(). + Lit("Max Targets:").Whitespace().Int().Newline(). + Lit("Max assigned Certificates:").Whitespace().Int().Newline(). + Lit("Pricings per Location:").Newline(). + AnyTimes( + NewRegex(). + Lit(" - Location:").Whitespace().LocationName().Newline(). + Lit(" Hourly:").Whitespace().Price().Newline(). + Lit(" Monthly:").Whitespace().Price().Newline(). + Lit(" Included Traffic:").Whitespace().IBytes().Newline(). + Lit(" Additional Traffic:").Whitespace().Price().Lit(" per TB").Newline(). + Newline(), + ).End(), + out, + ) + }) + + t.Run("json", func(t *testing.T) { + lbt, _, err := client.LoadBalancerType.GetByName(context.Background(), TestLoadBalancerTypeName) + require.NoError(t, err) + expectedJson, err := json.Marshal(hcloud.SchemaFromLoadBalancerType(lbt)) + require.NoError(t, err) + + out, err := runCommand(t, "load-balancer-type", "describe", TestLoadBalancerTypeName, "-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 fd5e8f58..9be06e45 100644 --- a/test/e2e/util.go +++ b/test/e2e/util.go @@ -117,6 +117,14 @@ func (r RegexBuilder) LocationName() RegexBuilder { return r.Raw(`[a-z]{3}[0-9]*`) } +func (r RegexBuilder) Price() RegexBuilder { + return r.Raw(`[€$] [0-9]+\.[0-9]+`) +} + +func (r RegexBuilder) IBytes() RegexBuilder { + return r.Raw(`[0-9]+(?:\.[0-9]+)? (?:B|[KMGTPE]iB)`) +} + func (r RegexBuilder) CountryCode() RegexBuilder { return r.Raw(`[A-Z]{2}`) } diff --git a/test/e2e/variables.go b/test/e2e/variables.go index 193cbfb2..a963d763 100644 --- a/test/e2e/variables.go +++ b/test/e2e/variables.go @@ -24,8 +24,8 @@ var ( // TestArchitecture is the default architecture used in end-to-end tests, should match the architecture of the TestServerType. TestArchitecture = getEnv("TEST_ARCHITECTURE", string(hcloud.ArchitectureX86)) - // TestLoadBalancerType is the default Load Balancer type used in end-to-end tests. - TestLoadBalancerType = "lb11" + // TestLoadBalancerTypeName is the default Load Balancer type used in end-to-end tests. + TestLoadBalancerTypeName = getEnv("TEST_LOAD_BALANCER_TYPE_NAME", "lb11") // TestDatacenterName is the default datacenter name where we execute our end-to-end tests. TestDatacenterName = getEnv("TEST_DATACENTER_NAME", "nbg1-dc3")