-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Terratest checking Local DNSEndpoint (#921)
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
Showing
4 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters