Skip to content

Commit

Permalink
terratest roundrobin
Browse files Browse the repository at this point in the history
  • Loading branch information
abaguas committed Jun 9, 2024
1 parent 3b57b5e commit 135c491
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 52 deletions.
13 changes: 13 additions & 0 deletions terratest/examples/failover-playground-ref-gslb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: k8gb.absa.oss/v1beta1
kind: Gslb
metadata:
name: test-gslb
spec:
resourceRef:
ingress:
matchLabels:
app: test-gslb
strategy:
type: failover
dnsTtlSeconds: 5
primaryGeoTag: "eu"
19 changes: 19 additions & 0 deletions terratest/examples/failover-playground-ref-ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-gslb
labels:
app: test-gslb
spec:
ingressClassName: nginx
rules:
- host: playground-failover.cloud.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend-podinfo # Gslb should reflect Healthy status and create associated DNS records
port:
name: http
13 changes: 13 additions & 0 deletions terratest/examples/failover_ref_gslb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: k8gb.absa.oss/v1beta1
kind: Gslb
metadata:
name: test-gslb
spec:
resourceRef:
ingress:
matchLabels:
app: test-gslb
strategy:
type: failover
dnsTtlSeconds: 5
primaryGeoTag: "eu"
19 changes: 19 additions & 0 deletions terratest/examples/failover_ref_ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-gslb
labels:
app: test-gslb
spec:
ingressClassName: nginx
rules:
- host: terratest-failover.cloud.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend-podinfo # Gslb should reflect Healthy status and create associated DNS records
port:
name: http
15 changes: 15 additions & 0 deletions terratest/examples/roundrobin-weight1-ref-gslb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: k8gb.absa.oss/v1beta1
kind: Gslb
metadata:
name: test-gslb
spec:
resourceRef:
ingress:
matchLabels:
app: test-gslb
strategy:
type: roundRobin # Use a round robin load balancing strategy, when deciding which downstream clusters to route clients too
dnsTtlSeconds: 5
weight:
eu: 5
us: 5
39 changes: 39 additions & 0 deletions terratest/examples/roundrobin-weight1-ref-ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-gslb
labels:
app: test-gslb
spec:
ingressClassName: nginx
rules:
- host: terratest-notfound.cloud.example.com # This is the GSLB enabled host that clients would use
http: # This section mirrors the same structure as that of an Ingress resource and will be used verbatim when creating the corresponding Ingress resource that will match the GSLB host
paths:
- path: /
pathType: Prefix
backend:
service:
name: non-existing-app # Gslb should reflect NotFound status
port:
name: http
- host: terratest-unhealthy.cloud.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: unhealthy-app # Gslb should reflect Unhealthy status
port:
name: http
- host: terratest-roundrobin.cloud.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend-podinfo # Gslb should reflect Healthy status and create associated DNS records
port:
name: http
File renamed without changes.
12 changes: 12 additions & 0 deletions terratest/examples/roundrobin2-ref-gslb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: k8gb.absa.oss/v1beta1
kind: Gslb
metadata:
name: roundrobin-test-gslb
spec:
resourceRef:
ingress:
matchLabels:
app: roundrobin-test-gslb
strategy:
type: roundRobin # Use a round robin load balancing strategy, when deciding which downstream clusters to route clients too
dnsTtlSeconds: 5
19 changes: 19 additions & 0 deletions terratest/examples/roundrobin2-ref-ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: roundrobin-test-gslb
labels:
app: roundrobin-test-gslb
spec:
ingressClassName: nginx
rules:
- host: roundrobin-test.cloud.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend-podinfo # Gslb should reflect Healthy status and create associated DNS records
port:
name: http
27 changes: 16 additions & 11 deletions terratest/test/k8gb_abstract_full_roundrobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,35 @@ import (
"github.com/stretchr/testify/require"
)

func abstractTestFullRoundRobin(t *testing.T, n int) {
func abstractTestFullRoundRobin(t *testing.T, n int, host string, gslbPath string, ingressPath string) {
if n < 2 || n > 8 {
t.Logf("Use value of n that represents the number of clusters from interval [2,8]")
t.FailNow()
}
t.Logf(fmt.Sprintf("Running TestFullRoundRobin for %d clusters", n))
tags := []string{"eu", "us", "cz", "af", "ru", "ap", "uk", "ca"}
testPrefix := "embedded ingress"
if ingressPath != "" {
testPrefix = "referenced ingress"
}
var instances []*utils.Instance

const host = "roundrobin-test.cloud.example.com"
const gslbPath = "../examples/roundrobin2.yaml"

// start all the test apps on all the clusters
for i := 0; i < n; i += 1 {
instance, er := utils.NewWorkflow(t, fmt.Sprintf("k3d-test-gslb%d", i+1), 5053+i).
workflow := utils.NewWorkflow(t, fmt.Sprintf("k3d-test-gslb%d", i+1), 5053+i).
WithGslb(gslbPath, host).
WithTestApp(tags[i]).
Start()
WithTestApp(tags[i])
if ingressPath != "" {
workflow = workflow.WithIngress(ingressPath)
}

instance, er := workflow.Start()
require.NoError(t, er)
instances = append(instances, instance)
defer instance.Kill()
}
var err error
t.Run(fmt.Sprintf("round-robin on %d concurrent clusters with podinfo running", n), func(t *testing.T) {
t.Run(fmt.Sprintf("%s round-robin on %d concurrent clusters with podinfo running", testPrefix, n), func(t *testing.T) {
for _, ins := range instances {
err = ins.WaitForAppIsRunning()
require.NoError(t, err)
Expand All @@ -61,13 +66,13 @@ func abstractTestFullRoundRobin(t *testing.T, n int) {
for _, ins := range instances {
workingTargets = append(workingTargets, ins.GetLocalTargets()...)
}
t.Run(fmt.Sprintf("all %d clusters should be interconnected", n), func(t *testing.T) {
t.Run(fmt.Sprintf("%s all %d clusters should be interconnected", testPrefix, n), func(t *testing.T) {
allShouldExpectTheseTargets(t, instances, workingTargets)
})

// kill the apps on clusters one by one and expect less and less targets to be available
for i, instance := range instances {
t.Run(fmt.Sprintf("kill podinfo on cluster %d (%s)", i+1, tags[i]), func(t *testing.T) {
t.Run(fmt.Sprintf("%s kill podinfo on cluster %d (%s)", testPrefix, i+1, tags[i]), func(t *testing.T) {
workingTargets = distinct(subtract(workingTargets, instance.GetLocalTargets()))
t.Logf("New expected targets: %v", workingTargets)
instance.StopTestApp()
Expand All @@ -77,7 +82,7 @@ func abstractTestFullRoundRobin(t *testing.T, n int) {

// start the test apps again on each cluster and check if the targets start appearing
for i, instance := range instances {
t.Run(fmt.Sprintf("start podinfo on cluster %d (%s)", i+1, tags[i]), func(t *testing.T) {
t.Run(fmt.Sprintf("%s start podinfo on cluster %d (%s)", testPrefix, i+1, tags[i]), func(t *testing.T) {
instance.StartTestApp()
workingTargets = distinct(append(workingTargets, instance.GetLocalTargets()...))
t.Logf("New expected targets: %v", workingTargets)
Expand Down
36 changes: 26 additions & 10 deletions terratest/test/k8gb_failover_playground_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
"fmt"
"k8gbterratest/utils"
"testing"

Expand All @@ -33,20 +34,35 @@ import (
// see: https://github.com/k8gb-io/k8gb/blob/master/docs/local.md#failover
func TestFailoverPlayground(t *testing.T) {
const host = "playground-failover.cloud.example.com"
const gslbPath = "../examples/failover-playground.yaml"
abstractTestFailoverPlayground(t, host, "../examples/failover-playground.yaml", "")
abstractTestFailoverPlayground(t, host, "../examples/failover-playground-ref-gslb.yaml", "../examples/failover-playground-ref-ingress.yaml")
}

func abstractTestFailoverPlayground(t *testing.T, host string, gslbPath string, ingressPath string) {
testPrefix := "embedded ingress"
if ingressPath != "" {
testPrefix = "referenced ingress"
}
const euGeoTag = "eu"
const usGeoTag = "us"

instanceEU, err := utils.NewWorkflow(t, "k3d-test-gslb1", 5053).
workflowEU := utils.NewWorkflow(t, "k3d-test-gslb1", 5053).
WithGslb(gslbPath, host).
WithTestApp(euGeoTag).
Start()
WithTestApp(euGeoTag)
if ingressPath != nil {
workflowEU = workflowEU.WithIngress(ingreessPath)
}
instanceEU, err := workflowEU.Start()
require.NoError(t, err)
defer instanceEU.Kill()
instanceUS, err := utils.NewWorkflow(t, "k3d-test-gslb2", 5054).

workflowUS := utils.NewWorkflow(t, "k3d-test-gslb2", 5054).
WithGslb(gslbPath, host).
WithTestApp(usGeoTag).
Start()
WithTestApp(usGeoTag)
if ingressPath != nil {
workflowUS = workflowUS.WithIngress(ingreessPath)
}
instanceUS, err := workflowUS.Start()
require.NoError(t, err)
defer instanceUS.Kill()

Expand All @@ -63,7 +79,7 @@ func TestFailoverPlayground(t *testing.T) {
assert.Equal(t, geoTag, httpResult.Message)
}

t.Run("failover on two concurrent clusters with TestApp running", func(t *testing.T) {
t.Run(fmt.Sprintf("%s failover on two concurrent clusters with TestApp running", testPrefix), func(t *testing.T) {
err = instanceEU.WaitForAppIsRunning()
require.NoError(t, err)
err = instanceUS.WaitForAppIsRunning()
Expand All @@ -73,13 +89,13 @@ func TestFailoverPlayground(t *testing.T) {
euLocalTargets := instanceEU.GetLocalTargets()
usLocalTargets := instanceUS.GetLocalTargets()

t.Run("stop podinfo on eu cluster", func(t *testing.T) {
t.Run(fmt.Sprintf("%s stop podinfo on eu cluster", testPrefix), func(t *testing.T) {
instanceEU.StopTestApp()
require.NoError(t, instanceEU.WaitForAppIsStopped())
actAndAssert(t.Name(), usGeoTag, usLocalTargets)
})

t.Run("start podinfo again on eu cluster", func(t *testing.T) {
t.Run(fmt.Sprintf("%s start podinfo again on eu cluster", testPrefix), func(t *testing.T) {
instanceEU.StartTestApp()
require.NoError(t, instanceEU.WaitForAppIsRunning())
actAndAssert(t.Name(), euGeoTag, euLocalTargets)
Expand Down
43 changes: 30 additions & 13 deletions terratest/test/k8gb_full_failover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,40 @@ import (
)

func TestFullFailover(t *testing.T) {
const host = "terratest-failover.cloud.example.com"
const gslbPath = "../examples/failover.yaml"
host := "terratest-failover.cloud.example.com"
abstractTestFullFailover(t, host,
"../examples/failover.yaml", "")
abstractTestFullFailover(t, host,
"../examples/failover-ref-gslb.yaml", "../examples/failover-ref-ingress.yaml")
}

func abstractTestFullFailover(t *testing.T, host string, gslbPath string, ingressPath string) {
testPrefix := "embedded ingress"
if ingressPath != "" {
testPrefix = "referenced ingress"
}

instanceEU, err := utils.NewWorkflow(t, "k3d-test-gslb1", 5053).
workflowEU := utils.NewWorkflow(t, "k3d-test-gslb1", 5053).
WithGslb(gslbPath, host).
WithTestApp("eu").
Start()
WithTestApp("eu")
if ingressPath != "" {
workflowEU = workflowEU.WithIngress(ingressPath)
}
instanceEU, err := workflowEU.Start()
require.NoError(t, err)
defer instanceEU.Kill()
instanceUS, err := utils.NewWorkflow(t, "k3d-test-gslb2", 5054).

workflowUS := utils.NewWorkflow(t, "k3d-test-gslb2", 5054).
WithGslb(gslbPath, host).
WithTestApp("us").
Start()
WithTestApp("us")
if ingressPath != "" {
workflowUS = workflowUS.WithIngress(ingressPath)
}
instanceUS, err := workflowUS.Start()
require.NoError(t, err)
defer instanceUS.Kill()

t.Run("failover on two concurrent clusters with podinfo running", func(t *testing.T) {
t.Run(fmt.Sprintf("%s failover on two concurrent clusters with podinfo running" testPrefix), func(t *testing.T) {
err = instanceEU.WaitForAppIsRunning()
require.NoError(t, err)
err = instanceUS.WaitForAppIsRunning()
Expand All @@ -55,31 +72,31 @@ func TestFullFailover(t *testing.T) {
euLocalTargets := instanceEU.GetLocalTargets()
usLocalTargets := instanceUS.GetLocalTargets()

t.Run("kill podinfo on the second cluster", func(t *testing.T) {
t.Run(fmt.Sprintf("%s kill podinfo on the second cluster", testPrefix), func(t *testing.T) {
instanceUS.StopTestApp()
err = instanceUS.WaitForExpected(euLocalTargets)
require.NoError(t, err)
err = instanceEU.WaitForExpected(euLocalTargets)
require.NoError(t, err)
})

t.Run("kill podinfo on the first cluster", func(t *testing.T) {
t.Run(fmt.Sprintf("%s kill podinfo on the first cluster", testPrefix), func(t *testing.T) {
instanceEU.StopTestApp()
err = instanceEU.WaitForExpected([]string{})
require.NoError(t, err)
err = instanceUS.WaitForExpected([]string{})
require.NoError(t, err)
})

t.Run("start podinfo on the second cluster", func(t *testing.T) {
t.Run(fmt.Sprintf("%s start podinfo on the second cluster", testPrefix), func(t *testing.T) {
instanceUS.StartTestApp()
err = instanceUS.WaitForExpected(usLocalTargets)
require.NoError(t, err)
err = instanceEU.WaitForExpected(usLocalTargets)
require.NoError(t, err)
})

t.Run("start podinfo on the first cluster", func(t *testing.T) {
t.Run(fmt.Sprintf("%s start podinfo on the first cluster", testPrefix), func(t *testing.T) {
instanceEU.StartTestApp()
err = instanceEU.WaitForExpected(euLocalTargets)
require.NoError(t, err)
Expand Down
6 changes: 5 additions & 1 deletion terratest/test/k8gb_full_roundrobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ import (
)

func TestFullRoundRobin(t *testing.T) {
abstractTestFullRoundRobin(t, settings.ClustersNumber)
host = "roundrobin-test.cloud.example.com"
abstractTestFullRoundRobin(t, settings.ClustersNumber, host,
"../examples/roundrobin2.yaml", "")
abstractTestFullRoundRobin(t, settings.ClustersNumber, host,
"../examples/roundrobin2-ref-gslb.yaml", "../examples/roundrobin2-ref-ingress.yaml")
}
Loading

0 comments on commit 135c491

Please sign in to comment.