-
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.
Merge pull request #4 from mikenairn/fix_make_bundle
Add Tests and CI config
- Loading branch information
Showing
8 changed files
with
411 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,48 @@ | ||
//go:build integration | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
TestTimeoutMedium = time.Second * 10 | ||
TestTimeoutLong = time.Second * 30 | ||
TestRetryIntervalMedium = time.Millisecond * 250 | ||
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.