Skip to content

Commit

Permalink
test(e2e): use builder for regexes (#872)
Browse files Browse the repository at this point in the history
This PR introduces a new builder for regular expressions, which makes
tests easier to understand and maintain.
  • Loading branch information
phm07 authored Oct 1, 2024
1 parent 1639afd commit 6588e84
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 163 deletions.
40 changes: 22 additions & 18 deletions test/e2e/combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,28 @@ func TestCombined(t *testing.T) {
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)
assert.Regexp(t,
NewRegex().Start().
Lit("ID:").Whitespace().Int().Newline().
Lit("Type:").Whitespace().Lit("ipv4").Newline().
Lit("Name:").Whitespace().Raw(`test-floating-ip-[0-9a-f]{8}`).Newline().
Lit("Description:").Whitespace().Lit("-").Newline().
Lit("Created:").Whitespace().UnixDate().Lit(" (").HumanizeTime().Lit(")").Newline().
Lit("IP:").Whitespace().IPv4().Newline().
Lit("Blocked:").Whitespace().Lit("no").Newline().
Lit("Home Location:").Whitespace().LocationName().Newline().
Lit("Server:").Newline().
Lit(" ID:").Whitespace().Int().Newline().
Lit(" Name:").Whitespace().Raw(`test-server-[0-9a-f]{8}`).Newline().
Lit("DNS:").Newline().
Lit(" ").IPv4().Lit(": static.").IPv4().Lit(".clients.your-server.de").Newline().
Lit("Protection:").Newline().
Lit(" Delete:").Whitespace().Lit("no").Newline().
Lit("Labels:").Newline().
Lit(" No labels").Newline().
End(),
out,
)
})

t.Run("list", func(t *testing.T) {
Expand Down
45 changes: 30 additions & 15 deletions test/e2e/datacenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ func TestDatacenter(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)
assert.Regexp(t,
NewRegex().Start().
SeparatedByWhitespace("ID", "NAME", "DESCRIPTION", "LOCATION").Newline().
AnyTimes(NewRegex().Int().Whitespace().Identifier().Whitespace().AnyString().Whitespace().LocationName().Newline()).
End(),
out,
)
})

t.Run("json", func(t *testing.T) {
Expand All @@ -38,19 +43,29 @@ func TestDatacenter(t *testing.T) {
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)

assert.Regexp(t,
NewRegex().Start().
Lit("ID:").Whitespace().Int().Newline().
Lit("Name:").Whitespace().Identifier().Newline().
Lit("Description:").Whitespace().AnyString().Newline().
Lit("Location:").Newline().
Lit(" Name:").Whitespace().LocationName().Newline().
Lit(" Description:").Whitespace().AnyString().Newline().
Lit(" Country:").Whitespace().CountryCode().Newline().
Lit(" City:").Whitespace().AnyString().Newline().
Lit(" Latitude:").Whitespace().Float().Newline().
Lit(" Longitude:").Whitespace().Float().Newline().
Lit("Server Types:").Newline().
AnyTimes(
NewRegex().
Lit(" - ID: ").Int().Whitespace().
Lit("Name: ").Identifier().Whitespace().
Lit("Supported: ").OneOfLit("true", "false").Whitespace().
Lit("Available: ").OneOfLit("true", "false").Newline(),
).End(),
out,
)
})
})
}
88 changes: 46 additions & 42 deletions test/e2e/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,48 +158,52 @@ func TestFirewall(t *testing.T) {
t.Run("describe", func(t *testing.T) {
out, err := runCommand(t, "firewall", "describe", strconv.FormatInt(firewallID, 10))
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Name:\s+new-test-firewall-[0-9a-f]{8}
Created:\s+.*?
Labels:
\s+foo: bar
Rules:
\s+- Direction:\s+in
\s+Description:\s+Allow port 80
\s+Protocol:\s+tcp
\s+Port:\s+80
\s+Source IPs:
\s+28\.239\.13\.1\/32
\s+28\.239\.14\.0\/24
\s+ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b\/128
\s+- Direction:\s+in
\s+Description:\s+Allow port 443
\s+Protocol:\s+tcp
\s+Port:\s+443
\s+Source IPs:
\s+0\.0\.0\.0\/0
\s+::\/0
\s+- Direction:\s+out
\s+Protocol:\s+tcp
\s+Port:\s+80
\s+Destination IPs:
\s+28\.239\.13\.1\/32
\s+28\.239\.14\.0\/24
\s+ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b\/128
\s+- Direction:\s+in
\s+Description:\s+Some random description
\s+Protocol:\s+tcp
\s+Port:\s+9100
\s+Source IPs:
\s+10\.0\.0\.0\/24
\s+- Direction:\s+out
\s+Protocol:\s+icmp
\s+Destination IPs:
\s+192\.168\.1\.0\/24
Applied To:
\s+- Type:\s+label_selector
\s+Label Selector:\s+foo=bar
`, out)
assert.Regexp(t,
NewRegex().Start().
Lit("ID:").Whitespace().Int().Newline().
Lit("Name:").Whitespace().Raw(`new-test-firewall-[0-9a-f]{8}`).Newline().
Lit("Created:").Whitespace().UnixDate().Lit(" (").HumanizeTime().Lit(")").Newline().
Lit("Labels:").Newline().
Lit(" foo: bar").Newline().
Lit("Rules:").Newline().
Lit(" - Direction:").Whitespace().Lit("in").Newline().
Lit(" Description:").Whitespace().Lit("Allow port 80").Newline().
Lit(" Protocol:").Whitespace().Lit("tcp").Newline().
Lit(" Port:").Whitespace().Lit("80").Newline().
Lit(" Source IPs:").Newline().
Whitespace().Lit("28.239.13.1/32").Newline().
Whitespace().Lit("28.239.14.0/24").Newline().
Whitespace().Lit("ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128").Newline().
Lit(" - Direction:").Whitespace().Lit("in").Newline().
Lit(" Description:").Whitespace().Lit("Allow port 443").Newline().
Lit(" Protocol:").Whitespace().Lit("tcp").Newline().
Lit(" Port:").Whitespace().Lit("443").Newline().
Lit(" Source IPs:").Newline().
Whitespace().Lit("0.0.0.0/0").Newline().
Whitespace().Lit("::/0").Newline().
Lit(" - Direction:").Whitespace().Lit("out").Newline().
Lit(" Protocol:").Whitespace().Lit("tcp").Newline().
Lit(" Port:").Whitespace().Lit("80").Newline().
Lit(" Destination IPs:").Newline().
Whitespace().Lit("28.239.13.1/32").Newline().
Whitespace().Lit("28.239.14.0/24").Newline().
Whitespace().Lit("ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128").Newline().
Lit(" - Direction:").Whitespace().Lit("in").Newline().
Lit(" Description:").Whitespace().Lit("Some random description").Newline().
Lit(" Protocol:").Whitespace().Lit("tcp").Newline().
Lit(" Port:").Whitespace().Lit("9100").Newline().
Lit(" Source IPs:").Newline().
Whitespace().Lit("10.0.0.0/24").Newline().
Lit(" - Direction:").Whitespace().Lit("out").Newline().
Lit(" Protocol:").Whitespace().Lit("icmp").Newline().
Lit(" Destination IPs:").Newline().
Whitespace().Lit("192.168.1.0/24").Newline().
Lit("Applied To:").Newline().
Lit(" - Type:").Whitespace().Lit("label_selector").Newline().
Lit(" Label Selector:").Whitespace().Lit("foo=bar").Newline().
End(),
out,
)
})

t.Run("remove-from-resource", func(t *testing.T) {
Expand Down
109 changes: 68 additions & 41 deletions test/e2e/floatingip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,39 +109,58 @@ func TestFloatingIP(t *testing.T) {
ipStr, err = runCommand(t, "floating-ip", "describe", strconv.FormatInt(floatingIPId, 10), "--output", "format={{.IP}}")
require.NoError(t, err)
ipStr = strings.TrimSpace(ipStr)
assert.Regexp(t, `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`, ipStr)
assert.Regexp(t, NewRegex().Start().IPv4().End(), ipStr)
})

t.Run("normal", func(t *testing.T) {
out, err := runCommand(t, "floating-ip", "describe", strconv.FormatInt(floatingIPId, 10))
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Type:\s+ipv4
Name:\s+new-test-floating-ip-[0-9a-f]{8}
Description:\s+Some description
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+Not assigned
DNS:
\s+(?:[0-9]{1,3}\.){3}[0-9]{1,3}: s1\.example\.com
Protection:
\s+Delete:\s+yes
Labels:
\s+foo: bar
`, out)
assert.Regexp(t,
NewRegex().Start().
Lit("ID:").Whitespace().Int().Newline().
Lit("Type:").Whitespace().Lit("ipv4").Newline().
Lit("Name:").Whitespace().Raw(`new-test-floating-ip-[0-9a-f]{8}`).Newline().
Lit("Description:").Whitespace().Lit("Some description").Newline().
Lit("Created:").Whitespace().UnixDate().Lit(" (").HumanizeTime().Lit(")").Newline().
Lit("IP:").Whitespace().IPv4().Newline().
Lit("Blocked:").Whitespace().Lit("no").Newline().
Lit("Home Location:").Whitespace().LocationName().Newline().
Lit("Server:").Newline().
Lit(" Not assigned").Newline().
Lit("DNS:").Newline().
Lit(" ").IPv4().Lit(": s1.example.com").Newline().
Lit("Protection:").Newline().
Lit(" Delete:").Whitespace().Lit("yes").Newline().
Lit("Labels:").Newline().
Lit(" foo: bar").Newline().
End(),
out,
)
})
})

t.Run("list", func(t *testing.T) {
t.Run("table", func(t *testing.T) {
out, err := runCommand(t, "floating-ip", "list", "--output", "columns=id,name,type,ip,dns,server,home,blocked,protection,labels,created,age")
require.NoError(t, err)
assert.Regexp(t, `^ID +NAME +TYPE +IP +DNS +SERVER +HOME +BLOCKED +PROTECTION +LABELS +CREATED +AGE
[0-9]+ +new-test-floating-ip-[0-9a-f]{8} +ipv4 +(?:[0-9]{1,3}\.){3}[0-9]{1,3} +s1\.example\.com +- +[a-z]{3}[0-9]* +no +delete +foo=bar.*?
$`, out)
assert.Regexp(t,
NewRegex().Start().
SeparatedByWhitespace("ID", "NAME", "TYPE", "IP", "DNS", "SERVER", "HOME", "BLOCKED", "PROTECTION", "LABELS", "CREATED", "AGE").Newline().
Int().Whitespace().
Raw(`new-test-floating-ip-[0-9a-f]{8}`).Whitespace().
Lit("ipv4").Whitespace().
IPv4().Whitespace().
Lit("s1.example.com").Whitespace().
Lit("-").Whitespace().
LocationName().Whitespace().
Lit("no").Whitespace().
Lit("delete").Whitespace().
Lit("foo=bar").Whitespace().
UnixDate().Whitespace().
Age().Newline().
End(),
out,
)
})

t.Run("json", func(t *testing.T) {
Expand Down Expand Up @@ -238,23 +257,27 @@ $`, out)
t.Run("normal", func(t *testing.T) {
out, err := runCommand(t, "floating-ip", "describe", strconv.FormatInt(floatingIPId, 10))
require.NoError(t, err)
assert.Regexp(t, `ID:\s+[0-9]+
Type:\s+ipv6
Name:\s+test-floating-ipv6-[0-9a-f]{8}
Description:\s+-
Created:.*?
IP:\s+[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+::\/64
Blocked:\s+no
Home Location:\s+[a-z]{3}[0-9]*
Server:
\s+Not assigned
DNS:
\s+No reverse DNS entries
Protection:
\s+Delete:\s+no
Labels:
\s+No labels
`, out)
assert.Regexp(t,
NewRegex().Start().
Lit("ID:").Whitespace().Int().Newline().
Lit("Type:").Whitespace().Lit("ipv6").Newline().
Lit("Name:").Whitespace().Raw(`test-floating-ipv6-[0-9a-f]{8}`).Newline().
Lit("Description:").Whitespace().Lit("-").Newline().
Lit("Created:").Whitespace().UnixDate().Lit(" (").HumanizeTime().Lit(")").Newline().
Lit("IP:").Whitespace().IPv6().Lit("/64").Newline().
Lit("Blocked:").Whitespace().Lit("no").Newline().
Lit("Home Location:").Whitespace().LocationName().Newline().
Lit("Server:").Newline().
Lit(" Not assigned").Newline().
Lit("DNS:").Newline().
Lit(" No reverse DNS entries").Newline().
Lit("Protection:").Newline().
Lit(" Delete:").Whitespace().Lit("no").Newline().
Lit("Labels:").Newline().
Lit(" No labels").Newline().
End(),
out,
)
})
})

Expand All @@ -275,9 +298,13 @@ Labels:
t.Run("list", func(t *testing.T) {
out, err := runCommand(t, "floating-ip", "list", "-o", "columns=ip,dns")
require.NoError(t, err)
assert.Regexp(t, fmt.Sprintf(`^IP +DNS
%s\/64 +2 entries
`, ipStr), out)
assert.Regexp(t,
NewRegex().Start().
SeparatedByWhitespace("IP", "DNS").Newline().
Lit(ipStr).Lit("/64").Whitespace().Lit("2 entries").Newline().
End(),
out,
)
})

t.Run("delete", func(t *testing.T) {
Expand Down
Loading

0 comments on commit 6588e84

Please sign in to comment.