Skip to content

Commit

Permalink
terratest roundrobin
Browse files Browse the repository at this point in the history
  • Loading branch information
abaguas committed Jun 8, 2024
1 parent 3b57b5e commit abe6c5e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
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
15 changes: 8 additions & 7 deletions terratest/test/k8gb_abstract_full_roundrobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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()
Expand All @@ -35,15 +35,16 @@ func abstractTestFullRoundRobin(t *testing.T, n int) {
tags := []string{"eu", "us", "cz", "af", "ru", "ap", "uk", "ca"}
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()
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")
}
33 changes: 23 additions & 10 deletions terratest/utils/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ func (w *Workflow) WithIngress(path string) *Workflow {
if path == "" {
w.error = fmt.Errorf("empty ingress resource path")
}
w.settings.ingressResourcePath = path
var err error
w.settings.ingressResourcePath, err = filepath.Abs(path)
if err != nil {
w.error = fmt.Errorf("reading %s; %s", path, err)
}
w.settings.ingressName, err = w.getManifestName(w.settings.ingressResourcePath)
if err != nil {
w.error = err
}
return w
}

Expand All @@ -148,9 +156,6 @@ func (w *Workflow) WithGslb(path, host string) *Workflow {
w.error = err
}
w.state.gslb.host = host
if err != nil {
w.error = err
}
return w
}

Expand Down Expand Up @@ -211,12 +216,20 @@ func (w *Workflow) Start() (*Instance, error) {

// gslb
if w.settings.gslbResourcePath != "" {
w.t.Logf("Create ingress %s from %s", w.state.gslb.name, w.settings.gslbResourcePath)
k8s.KubectlApply(w.t, w.k8sOptions, w.settings.gslbResourcePath)
k8s.WaitUntilIngressAvailable(w.t, w.k8sOptions, w.state.gslb.name, 100, 1*time.Second)
ingress := k8s.GetIngress(w.t, w.k8sOptions, w.state.gslb.name)
require.Equal(w.t, ingress.Name, w.state.gslb.name)
w.settings.ingressName = w.state.gslb.name
if w.settings.ingressResourcePath == "" {
w.t.Logf("Create ingress %s from %s", w.state.gslb.name, w.settings.gslbResourcePath)
k8s.KubectlApply(w.t, w.k8sOptions, w.settings.gslbResourcePath)
k8s.WaitUntilIngressAvailable(w.t, w.k8sOptions, w.state.gslb.name, 100, 1*time.Second)
ingress := k8s.GetIngress(w.t, w.k8sOptions, w.state.gslb.name)
require.Equal(w.t, ingress.Name, w.state.gslb.name)
w.settings.ingressName = w.state.gslb.name
} else {
w.t.Logf("Create gslb %s from %s", w.state.gslb.name, w.settings.gslbResourcePath)
k8s.KubectlApply(w.t, w.k8sOptions, w.settings.gslbResourcePath)
w.t.Logf("Create ingress %s from %s", w.settings.ingressName, w.settings.ingressResourcePath)
k8s.KubectlApply(w.t, w.k8sOptions, w.settings.ingressResourcePath)
k8s.WaitUntilIngressAvailable(w.t, w.k8sOptions, w.settings.ingressName, 100, 1*time.Second)
}
}

return &Instance{
Expand Down

0 comments on commit abe6c5e

Please sign in to comment.