Skip to content

Commit 49e5b78

Browse files
llamerada-jpdulltz
authored andcommitted
Add test for class-name filter
Signed-off-by: Yuji Ito <llamerada.jp@gmail.com>
1 parent 9e504f9 commit 49e5b78

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed

controllers/httpproxy_controller_test.go

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package controllers
22

33
import (
44
"context"
5+
"fmt"
6+
"testing"
57
"time"
68

79
certmanagerv1alpha2 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
@@ -467,6 +469,97 @@ func testHTTPProxyReconcile() {
467469
Expect(k8sClient.List(context.Background(), crtList, client.InNamespace(ns))).ShouldNot(HaveOccurred())
468470
Expect(crtList.Items).Should(BeEmpty())
469471
})
472+
473+
It(`should not create DNSEndpoint and Certificate if the class name is not the target`, func() {
474+
ns := testNamespacePrefix + randomString(10)
475+
Expect(k8sClient.Create(context.Background(), &corev1.Namespace{
476+
ObjectMeta: ctrl.ObjectMeta{Name: ns},
477+
})).ShouldNot(HaveOccurred())
478+
defer k8sClient.Delete(context.Background(), &corev1.Namespace{
479+
ObjectMeta: ctrl.ObjectMeta{Name: ns},
480+
})
481+
482+
scm, mgr := setupManager()
483+
484+
Expect(SetupReconciler(mgr, scm, ReconcilerOptions{
485+
ServiceKey: testServiceKey,
486+
DefaultIssuerName: "test-issuer",
487+
DefaultIssuerKind: certmanagerv1alpha2.IssuerKind,
488+
CreateDNSEndpoint: true,
489+
CreateCertificate: true,
490+
IngressClassName: "class-name",
491+
})).ShouldNot(HaveOccurred())
492+
493+
stopMgr := startTestManager(mgr)
494+
defer stopMgr()
495+
496+
By("creating HTTPProxy having the annotation")
497+
hpKey := client.ObjectKey{Name: "foo", Namespace: ns}
498+
hp := newDummyHTTPProxy(hpKey)
499+
hp.Annotations[ingressClassNameAnnotation] = "wrong"
500+
Expect(k8sClient.Create(context.Background(), hp)).ShouldNot(HaveOccurred())
501+
502+
By("confirming that DNSEndpoint and Certificate do not exist")
503+
time.Sleep(time.Second)
504+
endpointList := &endpoint.DNSEndpointList{}
505+
Expect(k8sClient.List(context.Background(), endpointList, client.InNamespace(ns))).ShouldNot(HaveOccurred())
506+
Expect(endpointList.Items).Should(BeEmpty())
507+
508+
crtList := &certmanagerv1alpha2.CertificateList{}
509+
Expect(k8sClient.List(context.Background(), crtList, client.InNamespace(ns))).ShouldNot(HaveOccurred())
510+
Expect(crtList.Items).Should(BeEmpty())
511+
512+
By("creating HTTPProxy without the annotation")
513+
hpKey = client.ObjectKey{Name: "bar", Namespace: ns}
514+
hp = newDummyHTTPProxy(hpKey)
515+
Expect(k8sClient.Create(context.Background(), hp)).ShouldNot(HaveOccurred())
516+
517+
By("confirming that DNSEndpoint and Certificate do not exist")
518+
time.Sleep(time.Second)
519+
endpointList = &endpoint.DNSEndpointList{}
520+
Expect(k8sClient.List(context.Background(), endpointList, client.InNamespace(ns))).ShouldNot(HaveOccurred())
521+
Expect(endpointList.Items).Should(BeEmpty())
522+
523+
crtList = &certmanagerv1alpha2.CertificateList{}
524+
Expect(k8sClient.List(context.Background(), crtList, client.InNamespace(ns))).ShouldNot(HaveOccurred())
525+
Expect(crtList.Items).Should(BeEmpty())
526+
})
527+
528+
It(`should create Certificate if the class name equals to the target`, func() {
529+
ns := testNamespacePrefix + randomString(10)
530+
Expect(k8sClient.Create(context.Background(), &corev1.Namespace{
531+
ObjectMeta: ctrl.ObjectMeta{Name: ns},
532+
})).ShouldNot(HaveOccurred())
533+
defer k8sClient.Delete(context.Background(), &corev1.Namespace{
534+
ObjectMeta: ctrl.ObjectMeta{Name: ns},
535+
})
536+
537+
scm, mgr := setupManager()
538+
539+
Expect(SetupReconciler(mgr, scm, ReconcilerOptions{
540+
ServiceKey: testServiceKey,
541+
DefaultIssuerName: "test-issuer",
542+
DefaultIssuerKind: certmanagerv1alpha2.IssuerKind,
543+
CreateCertificate: true,
544+
IngressClassName: "class-name",
545+
})).ShouldNot(HaveOccurred())
546+
547+
stopMgr := startTestManager(mgr)
548+
defer stopMgr()
549+
550+
By("creating HTTPProxy having the annotation")
551+
hpKey := client.ObjectKey{Name: "foo", Namespace: ns}
552+
hp := newDummyHTTPProxy(hpKey)
553+
hp.Annotations[ingressClassNameAnnotation] = "class-name"
554+
Expect(k8sClient.Create(context.Background(), hp)).ShouldNot(HaveOccurred())
555+
556+
By("getting Certificate")
557+
certificate := &certmanagerv1alpha2.Certificate{}
558+
objKey := client.ObjectKey{Name: hpKey.Name, Namespace: hpKey.Namespace}
559+
Eventually(func() error {
560+
return k8sClient.Get(context.Background(), objKey, certificate)
561+
}, 5*time.Second).Should(Succeed())
562+
})
470563
}
471564

472565
func newDummyHTTPProxy(hpKey client.ObjectKey) *projectcontourv1.HTTPProxy {
@@ -487,3 +580,107 @@ func newDummyHTTPProxy(hpKey client.ObjectKey) *projectcontourv1.HTTPProxy {
487580
},
488581
}
489582
}
583+
584+
func TestIsClassNameMatched(t *testing.T) {
585+
tests := []struct {
586+
name string
587+
ingressClassName string
588+
hp *projectcontourv1.HTTPProxy
589+
want bool
590+
}{
591+
{
592+
name: "Annotation is not set",
593+
ingressClassName: "class-name",
594+
hp: &projectcontourv1.HTTPProxy{
595+
ObjectMeta: v1.ObjectMeta{
596+
Annotations: map[string]string{},
597+
},
598+
},
599+
want: false,
600+
},
601+
{
602+
name: "Both annotation are set",
603+
ingressClassName: "class-name",
604+
hp: &projectcontourv1.HTTPProxy{
605+
ObjectMeta: v1.ObjectMeta{
606+
Annotations: map[string]string{
607+
ingressClassNameAnnotation: "class-name",
608+
contourIngressClassNameAnnotation: "class-name",
609+
},
610+
},
611+
},
612+
want: true,
613+
},
614+
{
615+
name: "Both annotation are set but not matched",
616+
ingressClassName: "class-name",
617+
hp: &projectcontourv1.HTTPProxy{
618+
ObjectMeta: v1.ObjectMeta{
619+
Annotations: map[string]string{
620+
ingressClassNameAnnotation: "class-name",
621+
contourIngressClassNameAnnotation: "wrong",
622+
},
623+
},
624+
},
625+
want: false,
626+
},
627+
{
628+
name: fmt.Sprintf("Annotation %s is set", ingressClassNameAnnotation),
629+
ingressClassName: "class-name",
630+
hp: &projectcontourv1.HTTPProxy{
631+
ObjectMeta: v1.ObjectMeta{
632+
Annotations: map[string]string{
633+
ingressClassNameAnnotation: "class-name",
634+
},
635+
},
636+
},
637+
want: true,
638+
},
639+
{
640+
name: fmt.Sprintf("Annotation %s is set but not matched", ingressClassNameAnnotation),
641+
ingressClassName: "class-name",
642+
hp: &projectcontourv1.HTTPProxy{
643+
ObjectMeta: v1.ObjectMeta{
644+
Annotations: map[string]string{
645+
ingressClassNameAnnotation: "wrong",
646+
},
647+
},
648+
},
649+
want: false,
650+
},
651+
{
652+
name: fmt.Sprintf("Annotation %s is set", contourIngressClassNameAnnotation),
653+
ingressClassName: "class-name",
654+
hp: &projectcontourv1.HTTPProxy{
655+
ObjectMeta: v1.ObjectMeta{
656+
Annotations: map[string]string{
657+
contourIngressClassNameAnnotation: "class-name",
658+
},
659+
},
660+
},
661+
want: true,
662+
},
663+
{
664+
name: fmt.Sprintf("Annotation %s is set but not matched", contourIngressClassNameAnnotation),
665+
ingressClassName: "class-name",
666+
hp: &projectcontourv1.HTTPProxy{
667+
ObjectMeta: v1.ObjectMeta{
668+
Annotations: map[string]string{
669+
contourIngressClassNameAnnotation: "wrong",
670+
},
671+
},
672+
},
673+
want: false,
674+
},
675+
}
676+
for _, tt := range tests {
677+
t.Run(tt.name, func(t *testing.T) {
678+
r := &HTTPProxyReconciler{
679+
IngressClassName: tt.ingressClassName,
680+
}
681+
if got := r.isClassNameMatched(tt.hp); got != tt.want {
682+
t.Errorf("HTTPProxyReconciler.isClassNameMatched() = %v, want %v", got, tt.want)
683+
}
684+
})
685+
}
686+
}

0 commit comments

Comments
 (0)