Skip to content

Commit

Permalink
test(rrset): Add 'Failed' status tests (#43)
Browse files Browse the repository at this point in the history
* test(rrset): Add 'wrong type' test

Signed-off-by: Anthony TREUILLIER <[email protected]>

* test(rrset): Add 'wrong record format' test

Signed-off-by: Anthony TREUILLIER <[email protected]>

* test(rrset): Add 'unquoted TXT record' test

Signed-off-by: Anthony TREUILLIER <[email protected]>

---------

Signed-off-by: Anthony TREUILLIER <[email protected]>
  • Loading branch information
antrema authored Oct 30, 2024
1 parent b077bfa commit 74af28f
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 3 deletions.
177 changes: 177 additions & 0 deletions internal/controller/rrset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,4 +1060,181 @@ var _ = Describe("RRset Controller", func() {
Expect(createdResource.GetFinalizers()).To(ContainElement(FINALIZER_NAME), "RRset should contain the finalizer")
})
})

Context("When creating a wrong RRset", func() {
It("should reconcile the resource with Failed status", Label("wrong-rrset", "wrong-type"), func() {
ctx := context.Background()
// Specific test variables
badTypeResourceName := "wrong-type"
badTypeResourceDNSName := "wrong-type"
badTypeResourceType := "AA"
badTypeResourceRecords := []string{"1.1.1.1"}
badTypeResourceComment := "This is a wrong-type Record"

By("Creating the RRset resource")
badTypeResource := &dnsv1alpha1.RRset{
ObjectMeta: metav1.ObjectMeta{
Name: badTypeResourceName,
Namespace: resourceNamespace,
},
}
badTypeResource.SetResourceVersion("")
_, err := controllerutil.CreateOrUpdate(ctx, k8sClient, badTypeResource, func() error {
badTypeResource.Spec = dnsv1alpha1.RRsetSpec{
ZoneRef: dnsv1alpha1.ZoneRef{
Name: zoneName,
},
Type: badTypeResourceType,
Name: badTypeResourceDNSName,
TTL: resourceTTL,
Records: badTypeResourceRecords,
Comment: &badTypeResourceComment,
}
return nil
})
badTypeRRsetLookupKey := types.NamespacedName{
Name: badTypeResourceName,
Namespace: resourceNamespace,
}

Expect(err).NotTo(HaveOccurred())
Eventually(func() bool {
err := k8sClient.Get(ctx, badTypeRRsetLookupKey, badTypeResource)
return err == nil && badTypeResource.Status.SyncStatus != nil
}, timeout, interval).Should(BeTrue())

DnsFqdn := getRRsetName(badTypeResource)

By("Getting the created resource")
createdResource := &dnsv1alpha1.RRset{}
Eventually(func() bool {
err := k8sClient.Get(ctx, badTypeRRsetLookupKey, createdResource)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(getMockedRecordsForType(DnsFqdn, badTypeResourceType)).To(Equal([]string{}), "RRset should not have been created in backend")
Expect(*createdResource.Status.SyncStatus).To(Equal(FAILED_STATUS), "RRset status should be 'Failed'")
Expect(createdResource.GetOwnerReferences()).NotTo(BeEmpty(), "RRset should have setOwnerReference")
Expect(createdResource.GetOwnerReferences()[0].Name).To(Equal(zoneRef), "RRset should have setOwnerReference to Zone")
Expect(createdResource.GetFinalizers()).To(ContainElement(FINALIZER_NAME), "RRset should contain the finalizer")
})
})

Context("When creating a wrong RRset", func() {
It("should reconcile the resource with Failed status", Label("wrong-rrset", "wrong-format"), func() {
ctx := context.Background()
// Specific test variables
badFormatResourceName := "wrong-format"
badFormatResourceDNSName := "_wrong._record.format"
badFormatResourceType := "SRV"
badFormatResourceRecords := []string{"1 50 25565 test2.helloworld.com"}
badFormatResourceComment := "This is a wrong-format Record"

By("Creating the RRset resource")
badFormatResource := &dnsv1alpha1.RRset{
ObjectMeta: metav1.ObjectMeta{
Name: badFormatResourceName,
Namespace: resourceNamespace,
},
}
badFormatResource.SetResourceVersion("")
_, err := controllerutil.CreateOrUpdate(ctx, k8sClient, badFormatResource, func() error {
badFormatResource.Spec = dnsv1alpha1.RRsetSpec{
ZoneRef: dnsv1alpha1.ZoneRef{
Name: zoneName,
},
Type: badFormatResourceType,
Name: badFormatResourceDNSName,
TTL: resourceTTL,
Records: badFormatResourceRecords,
Comment: &badFormatResourceComment,
}
return nil
})
badFormatRRsetLookupKey := types.NamespacedName{
Name: badFormatResourceName,
Namespace: resourceNamespace,
}

Expect(err).NotTo(HaveOccurred())
Eventually(func() bool {
err := k8sClient.Get(ctx, badFormatRRsetLookupKey, badFormatResource)
return err == nil && badFormatResource.Status.SyncStatus != nil
}, timeout, interval).Should(BeTrue())

DnsFqdn := getRRsetName(badFormatResource)

By("Getting the created resource")
createdResource := &dnsv1alpha1.RRset{}
Eventually(func() bool {
err := k8sClient.Get(ctx, badFormatRRsetLookupKey, createdResource)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(getMockedRecordsForType(DnsFqdn, badFormatResourceType)).To(Equal([]string{}), "RRset should not have been created in backend")
Expect(*createdResource.Status.SyncStatus).To(Equal(FAILED_STATUS), "RRset status should be 'Failed'")
Expect(createdResource.GetOwnerReferences()).NotTo(BeEmpty(), "RRset should have setOwnerReference")
Expect(createdResource.GetOwnerReferences()[0].Name).To(Equal(zoneRef), "RRset should have setOwnerReference to Zone")
Expect(createdResource.GetFinalizers()).To(ContainElement(FINALIZER_NAME), "RRset should contain the finalizer")
})
})

Context("When creating a wrong RRset", func() {
It("should reconcile the resource with Failed status", Label("wrong-rrset", "unquoted-txt"), func() {
ctx := context.Background()
// Specific test variables
unquotedResourceName := "unquoted"
unquotedResourceDNSName := "unquoted-txt"
unquotedResourceType := "TXT"
unquotedResourceRecords := []string{"An unquoted record"}
unquotedResourceComment := "This is an unquoted-TXT Record"

By("Creating the RRset resource")
unquotedResource := &dnsv1alpha1.RRset{
ObjectMeta: metav1.ObjectMeta{
Name: unquotedResourceName,
Namespace: resourceNamespace,
},
}
unquotedResource.SetResourceVersion("")
_, err := controllerutil.CreateOrUpdate(ctx, k8sClient, unquotedResource, func() error {
unquotedResource.Spec = dnsv1alpha1.RRsetSpec{
ZoneRef: dnsv1alpha1.ZoneRef{
Name: zoneName,
},
Type: unquotedResourceType,
Name: unquotedResourceDNSName,
TTL: resourceTTL,
Records: unquotedResourceRecords,
Comment: &unquotedResourceComment,
}
return nil
})
unquotedRRsetLookupKey := types.NamespacedName{
Name: unquotedResourceName,
Namespace: resourceNamespace,
}

Expect(err).NotTo(HaveOccurred())
Eventually(func() bool {
err := k8sClient.Get(ctx, unquotedRRsetLookupKey, unquotedResource)
return err == nil && unquotedResource.Status.SyncStatus != nil
}, timeout, interval).Should(BeTrue())

DnsFqdn := getRRsetName(unquotedResource)

By("Getting the created resource")
createdResource := &dnsv1alpha1.RRset{}
Eventually(func() bool {
err := k8sClient.Get(ctx, unquotedRRsetLookupKey, createdResource)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(getMockedRecordsForType(DnsFqdn, unquotedResourceType)).To(Equal([]string{}), "RRset should not have been created in backend")
Expect(*createdResource.Status.SyncStatus).To(Equal(FAILED_STATUS), "RRset status should be 'Failed'")
Expect(createdResource.GetOwnerReferences()).NotTo(BeEmpty(), "RRset should have setOwnerReference")
Expect(createdResource.GetOwnerReferences()[0].Name).To(Equal(zoneRef), "RRset should have setOwnerReference to Zone")
Expect(createdResource.GetFinalizers()).To(ContainElement(FINALIZER_NAME), "RRset should contain the finalizer")
})
})
})
37 changes: 34 additions & 3 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,33 @@ func (m mockRecordsClient) Get(ctx context.Context, domain string, name string,
}

func (m mockRecordsClient) Change(ctx context.Context, domain string, name string, recordType powerdns.RRType, ttl uint32, content []string, options ...func(*powerdns.RRset)) error {
// Preliminary test - Linked to 'wrong-rrset && wrong-type' test
if string(recordType) == "AA" {
return &powerdns.Error{
StatusCode: 422,
Status: "422 Unprocessable Entity",
Message: "RRset " + name + " IN AA: unknown type given",
}
}

// Preliminary test - Linked to 'wrong-rrset && wrong-format' test
if string(recordType) == "SRV" && content[0] == strings.TrimSuffix(content[0], ".") {
return &powerdns.Error{
StatusCode: 422,
Status: "422 Unprocessable Entity",
Message: "Record " + name + "/SRV '" + strings.Join(content, ",") + "': Not in expected format (parsed as '" + strings.Join(content, ",") + ".')",
}
}

// Preliminary test - Linked to 'wrong-rrset && unquoted-txt' test
if string(recordType) == "TXT" && content[0] == strings.TrimSuffix(content[0], "\"") && content[0] == strings.TrimPrefix(content[0], "\"") {
return &powerdns.Error{
StatusCode: 422,
Status: "422 Unprocessable Entity",
Message: "Record " + name + "/TXT '" + strings.Join(content, ",") + "': Parsing record content (try 'pdnsutil check-zone'): Data field in DNS should start with quote(\") at position 0 of '" + strings.Join(content, ",") + "'",
}
}

var isRRsetIdentical, isNewRRset, ok bool
var rrset *powerdns.RRset
var comment, specifiedComment string
Expand Down Expand Up @@ -359,15 +386,19 @@ func getMockedKind(zoneName string) (result string) {
return
}

func getMockedRecordsForType(rrsetName, rrsetType string) (result []string) {
rrset, _ := readFromRecordsMap(makeCanonical(rrsetName))
func getMockedRecordsForType(rrsetName, rrsetType string) []string {
result := []string{}
rrset, ok := readFromRecordsMap(makeCanonical(rrsetName))
if !ok {
return result
}
if string(*rrset.Type) == rrsetType {
for _, r := range rrset.Records {
result = append(result, *r.Content)
}
}
slices.Sort(result)
return
return result
}

func getMockedTTL(rrsetName, rrsetType string) (result uint32) {
Expand Down

0 comments on commit 74af28f

Please sign in to comment.