Skip to content

Commit

Permalink
Terratest checking Local DNSEndpoint (#921)
Browse files Browse the repository at this point in the history
I had to prepare one extra terratest to verify that #914 is creating labels correctly.
The terratests that control the weight will come later. Together with the test I extended the utils
with DNSEndpoint class, which returns information about DNS endpoint.

Signed-off-by: kuritka <[email protected]>
  • Loading branch information
kuritka authored Jul 25, 2022
1 parent 7c61add commit 8820bba
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 0 deletions.
44 changes: 44 additions & 0 deletions terratest/examples/roundrobin_weight1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: k8gb.absa.oss/v1beta1
kind: Gslb
metadata:
name: test-gslb
spec:
ingress:
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
strategy:
type: roundRobin # Use a round robin load balancing strategy, when deciding which downstream clusters to route clients too
dnsTtlSeconds: 5
weight:
eu: 50%
us: 50%
88 changes: 88 additions & 0 deletions terratest/test/k8gb_weight_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package test

/*
Copyright 2022 The k8gb Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
"testing"

"k8gbterratest/utils"

"github.com/stretchr/testify/require"
)

func TestWeightsExistsInLocalDNSEndpoint(t *testing.T) {
t.Parallel()
const host = "terratest-roundrobin.cloud.example.com"
const localtargets = "localtargets-" + host
const gslbPath = "../examples/roundrobin_weight1.yaml"
instanceEU, err := utils.NewWorkflow(t, "k3d-test-gslb1", 5053).
WithGslb(gslbPath, host).
WithTestApp("eu").
Start()
require.NoError(t, err)
defer instanceEU.Kill()

instanceUS, err := utils.NewWorkflow(t, "k3d-test-gslb2", 5054).
WithGslb(gslbPath, host).
WithTestApp("us").
Start()
require.NoError(t, err)
defer instanceUS.Kill()

err = instanceEU.WaitForAppIsRunning()
require.NoError(t, err)
err = instanceUS.WaitForAppIsRunning()
require.NoError(t, err)

epLocalEU,err := instanceEU.GetExternalDNSEndpoint().GetEndpointByName(localtargets)
require.NoError(t, err, "missing EU endpoint", localtargets)
epLocalUS,err := instanceUS.GetExternalDNSEndpoint().GetEndpointByName(localtargets)
require.NoError(t, err, "missing US endpoint", localtargets)
expectedTargets := append(epLocalEU.Targets,epLocalUS.Targets...)

err = instanceEU.WaitForExpected(expectedTargets)
require.NoError(t, err)
err = instanceUS.WaitForExpected(expectedTargets)
require.NoError(t, err)

for _, instance := range []*utils.Instance{instanceEU, instanceUS} {
ep, err := instance.GetExternalDNSEndpoint().GetEndpointByName(host)
require.NoError(t, err, "missing endpoint", host)
// check all labels are correct
require.Equal(t, "roundRobin", ep.Labels["strategy"])
require.True(t, Contains(ep.Labels["weight-eu-0-50"], epLocalEU.Targets))
require.True(t, Contains(ep.Labels["weight-eu-1-50"], epLocalEU.Targets))
require.True(t, Contains(ep.Labels["weight-us-0-50"], epLocalUS.Targets))
require.True(t, Contains(ep.Labels["weight-us-1-50"], epLocalUS.Targets))
require.NotEqual(t, ep.Labels["weight-eu-0-50"], ep.Labels["weight-eu-1-50"])
require.NotEqual(t, ep.Labels["weight-us-0-50"], ep.Labels["weight-us-1-50"])
// check all targets are correct
for _, v := range epLocalEU.Targets { require.True(t, Contains(v, ep.Targets)) }
for _, v := range epLocalUS.Targets { require.True(t, Contains(v, ep.Targets)) }
}
}

func Contains(str string, values []string) bool {
for _, v := range values {
if str == v {
return true
}
}
return false
}
52 changes: 52 additions & 0 deletions terratest/utils/dnsendpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package utils

/*
Copyright 2022 The k8gb Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import "fmt"

type DNSEndpoint struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`
}

type Metadata struct {
Name string `json:"name"`
Annotations map[string]string `json:"annotations,omitempty"`
}

type Spec struct {
Endpoints []Endpoint `json:"endpoints,omitempty"`
}

type Endpoint struct {
Name string `json:"dnsName"`
RecordType string `json:"recordType"`
RecordTTL int `json:"recordTTL"`
Targets []string `json:"targets,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}

func (dep DNSEndpoint) GetEndpointByName(name string) (Endpoint, error) {
for _, ep := range dep.Spec.Endpoints {
if ep.Name == name {
return ep, nil
}
}
return Endpoint{}, fmt.Errorf("not found")
}
12 changes: 12 additions & 0 deletions terratest/utils/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ func (i *Instance) GetLocalTargets() []string {
return dig
}

func (i *Instance) GetExternalDNSEndpoint() (ep DNSEndpoint) {
const endpointName = "test-gslb"
ep = DNSEndpoint{}
j, err := k8s.RunKubectlAndGetOutputE(i.w.t, i.w.k8sOptions, "get", "dnsendpoints.externaldns.k8s.io", endpointName, "-ojson")
require.NoError(i.w.t, err)
err = json.Unmarshal([]byte(j), &ep)
if err != nil {
return ep
}
return ep
}

// HitTestApp makes HTTP GET to TestApp when installed otherwise panics.
// If the function successfully hits the TestApp, it returns the TestAppResult.
func (i *Instance) HitTestApp() (result *TestAppResult) {
Expand Down

0 comments on commit 8820bba

Please sign in to comment.