-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds all DNS related unit and integration tests from MGC repo (There aren't many :-/) Add configuration for running tests locally and via CI: * Add test-unit and test-integration make targets(Tests must be tagged correctly unit vs integration) * Use actions/setup-go@v5 (Uses node20 and avoids warnings) * Add patch to `make bundle` to avoid changing createdAt even when nothing else has updated. * Add act tool install and make targets to help verify GH actions locally. * Add integration_test_suite job to CI workflow.
- Loading branch information
Showing
8 changed files
with
422 additions
and
10 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
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
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,98 @@ | ||
//go:build integration | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/kuadrant/kuadrant-dns-operator/api/v1alpha1" | ||
) | ||
|
||
var _ = Describe("DNSHealthCheckProbe controller", func() { | ||
const ( | ||
ProbeName = "test-probe" | ||
ProbeNamespace = "default" | ||
|
||
timeout = time.Second * 10 | ||
duration = time.Second * 10 | ||
interval = time.Millisecond * 250 | ||
) | ||
|
||
Context("When creating DNSHealthCheckProbe", func() { | ||
It("Should update health status to healthy", func() { | ||
By("Performing health check") | ||
|
||
ctx := context.Background() | ||
probeObj := &v1alpha1.DNSHealthCheckProbe{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: ProbeName, | ||
Namespace: ProbeNamespace, | ||
}, | ||
Spec: v1alpha1.DNSHealthCheckProbeSpec{ | ||
Host: "localhost", | ||
Address: "0.0.0.0", | ||
Port: 3333, | ||
Interval: metav1.Duration{Duration: time.Second * 10}, | ||
Path: "/healthy", | ||
}, | ||
} | ||
|
||
Expect(k8sClient.Create(ctx, probeObj)).Should(Succeed()) | ||
|
||
Eventually(func() error { | ||
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(probeObj), probeObj) | ||
if err != nil { | ||
return err | ||
} | ||
if probeObj.Status.LastCheckedAt.Time == (time.Time{}) { | ||
return fmt.Errorf("expected probeObj.Status.LastCheckedAt to be non-zero %s, %s", probeObj.Status.LastCheckedAt.Time, (metav1.Time{}).Time) | ||
} | ||
return nil | ||
}, timeout+(time.Second*20), interval).Should(BeNil()) | ||
|
||
GinkgoWriter.Print(probeObj) | ||
|
||
Expect(*probeObj.Status.Healthy).Should(BeTrue()) | ||
Expect(probeObj.Status.LastCheckedAt).Should(Not(BeZero())) | ||
}) | ||
It("Should update health status to unhealthy", func() { | ||
By("Updating to unhealthy endpoint") | ||
|
||
ctx := context.Background() | ||
probeObj := &v1alpha1.DNSHealthCheckProbe{} | ||
|
||
err := k8sClient.Get(ctx, client.ObjectKey{ | ||
Name: ProbeName, | ||
Namespace: ProbeNamespace, | ||
}, probeObj) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
patch := client.MergeFrom(probeObj.DeepCopy()) | ||
lastUpdate := probeObj.Status.LastCheckedAt | ||
probeObj.Spec.Path = "/unhealthy" | ||
Expect(k8sClient.Patch(ctx, probeObj, patch)).To(BeNil()) | ||
|
||
Eventually(func() error { | ||
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(probeObj), probeObj) | ||
if err != nil { | ||
return err | ||
} | ||
if !probeObj.Status.LastCheckedAt.Time.After(lastUpdate.Time) { | ||
return fmt.Errorf("expected probeObj.Status.LastCheckedAt to be after lastUpdate") | ||
} | ||
return nil | ||
}, timeout+(time.Second*20), interval).Should(BeNil()) | ||
|
||
Expect(*probeObj.Status.Healthy).Should(BeFalse()) | ||
Expect(probeObj.Status.Reason).Should(Equal("Status code: 500")) | ||
}) | ||
}) | ||
}) |
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,59 @@ | ||
//go:build integration | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
TestTimeoutMedium = time.Second * 10 | ||
TestTimeoutLong = time.Second * 30 | ||
TestRetryIntervalMedium = time.Millisecond * 250 | ||
TestGatewayName = "test-placed-gateway" | ||
TestClusterNameOne = "test-placed-control" | ||
TestClusterNameTwo = "test-placed-workload-1" | ||
TestHostOne = "test.example.com" | ||
TestHostTwo = "other.example.com" | ||
TestHostWildcard = "*.example.com" | ||
TestListenerNameWildcard = "wildcard" | ||
TestListenerNameOne = "test-listener-1" | ||
TestListenerNameTwo = "test-listener-2" | ||
TestIPAddressOne = "172.0.0.1" | ||
TestIPAddressTwo = "172.0.0.2" | ||
defaultNS = "default" | ||
providerCredential = "secretname" | ||
) | ||
|
||
type testHealthServer struct { | ||
Port int | ||
} | ||
|
||
func (s *testHealthServer) Start(ctx context.Context) error { | ||
mux := http.NewServeMux() | ||
|
||
endpoint := func(expectedCode int) func(http.ResponseWriter, *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(expectedCode) | ||
} | ||
} | ||
|
||
mux.HandleFunc("/healthy", endpoint(200)) | ||
mux.HandleFunc("/unhealthy", endpoint(500)) | ||
|
||
errCh := make(chan error) | ||
|
||
go func() { | ||
errCh <- http.ListenAndServe(fmt.Sprintf(":%d", s.Port), mux) | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case err := <-errCh: | ||
return err | ||
} | ||
} |
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,104 @@ | ||
//go:build integration | ||
|
||
/* | ||
Copyright 2024. | ||
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. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/kuadrant/kuadrant-dns-operator/api/v1alpha1" | ||
//+kubebuilder:scaffold:imports | ||
) | ||
|
||
var _ = Describe("ManagedZoneReconciler", func() { | ||
Context("testing ManagedZone controller", func() { | ||
var managedZone *v1alpha1.ManagedZone | ||
var ctx context.Context | ||
|
||
BeforeEach(func() { | ||
ctx = context.Background() | ||
managedZone = &v1alpha1.ManagedZone{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example.com", | ||
Namespace: defaultNS, | ||
}, | ||
Spec: v1alpha1.ManagedZoneSpec{ | ||
ID: "example.com", | ||
DomainName: "example.com", | ||
SecretRef: v1alpha1.ProviderRef{ | ||
Name: providerCredential, | ||
}, | ||
}, | ||
} | ||
}) | ||
|
||
AfterEach(func() { | ||
// Clean up managedZones | ||
mzList := &v1alpha1.ManagedZoneList{} | ||
err := k8sClient.List(ctx, mzList, client.InNamespace(defaultNS)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
for _, mz := range mzList.Items { | ||
err = k8sClient.Delete(ctx, &mz) | ||
Expect(client.IgnoreNotFound(err)).NotTo(HaveOccurred()) | ||
} | ||
}) | ||
|
||
It("should accept a managed zone for this controller and allow deletion", func() { | ||
Expect(k8sClient.Create(ctx, managedZone)).To(BeNil()) | ||
|
||
createdMZ := &v1alpha1.ManagedZone{} | ||
|
||
Eventually(func() error { | ||
return k8sClient.Get(ctx, client.ObjectKey{Namespace: managedZone.Namespace, Name: managedZone.Name}, createdMZ) | ||
}, TestTimeoutMedium, TestRetryIntervalMedium).ShouldNot(HaveOccurred()) | ||
|
||
Expect(k8sClient.Delete(ctx, managedZone)).To(BeNil()) | ||
|
||
Eventually(func() error { | ||
err := k8sClient.Get(ctx, client.ObjectKey{Namespace: managedZone.Namespace, Name: managedZone.Name}, createdMZ) | ||
if err != nil && !errors.IsNotFound(err) { | ||
return err | ||
} | ||
return nil | ||
}, TestTimeoutMedium, TestRetryIntervalMedium).Should(BeNil()) | ||
}) | ||
|
||
It("should reject a managed zone with an invalid domain name", func() { | ||
invalidDomainNameManagedZone := &v1alpha1.ManagedZone{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "invalid_domain", | ||
Namespace: defaultNS, | ||
}, | ||
Spec: v1alpha1.ManagedZoneSpec{ | ||
ID: "invalid_domain", | ||
DomainName: "invalid_domain", | ||
}, | ||
} | ||
err := k8sClient.Create(ctx, invalidDomainNameManagedZone) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("spec.domainName in body should match")) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.