From e2325014dec1c5d2e00018d05b5ad6d73a3350f8 Mon Sep 17 00:00:00 2001 From: Tom Limoncelli Date: Fri, 3 Jan 2025 12:25:22 -0500 Subject: [PATCH] TESTING: parse_tests: Generate post-processed json files --- pkg/js/README-parse_tests.md | 42 ++++++++++++++++++++++++++++++++++++ pkg/js/js_test.go | 21 +++++++++++++----- pkg/prettyzone/sorting.go | 2 ++ 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 pkg/js/README-parse_tests.md diff --git a/pkg/js/README-parse_tests.md b/pkg/js/README-parse_tests.md new file mode 100644 index 0000000000..0db9c1b7a7 --- /dev/null +++ b/pkg/js/README-parse_tests.md @@ -0,0 +1,42 @@ + +# Parse Tests + +The `parse_tests` directory contains test cases for `js_test.go`. `js_test.go` +scans for files named `DDD-*.js` where `DDD` is a three-digit number. + +* `parse_tests/001-basic.js` -- The dnsconfig.js file. +* `parse_tests/001-basic.json` -- The EXPECTED output of "print-ir" for the `.js` file. +* `parse_tests/001-basic.json.ACTUAL` -- The ACTUAL output of "print-ir" for the `.js` file (not saved in git) +* `parse_tests/001-basic/foo.com.zone` -- Zonefiles from the domains mentioned in dnsconfig.js + +NOTE: The zonefiles are only tested if a matching `DDD-name/DOMAINNAME.zone` file exists. + +Any files committed to Git should be in standard format. + +# Fix formatting + +Fix the `.js` formatting: + +``` +cd parse_tests +for i in *.js ; do echo ========== $i ; dnscontrol fmt -i $i -o $i ; done +``` + +Fix the `.json` formatting: + +``` +cd parse_tests +fmtjson *.json *.json.ACTUAL +``` + +# Copy actuals to expected. + +Back-port the ACTUAL results to the expected results: + +(This is dangerous. You may be committing buggy results to the "expected" files. Carefully inspect the resulting PR.) + +``` +cd parse_tests +fmtjson *.json *.json.ACTUAL +for i in *.ACTUAL ; do f=$(basename $i .ACTUAL) ; cp $i $f ; done +``` diff --git a/pkg/js/js_test.go b/pkg/js/js_test.go index 0bd35a11be..5ff379b47c 100644 --- a/pkg/js/js_test.go +++ b/pkg/js/js_test.go @@ -9,6 +9,7 @@ import ( "testing" "unicode" + "github.com/StackExchange/dnscontrol/v4/models" "github.com/StackExchange/dnscontrol/v4/pkg/normalize" "github.com/StackExchange/dnscontrol/v4/pkg/prettyzone" "github.com/StackExchange/dnscontrol/v4/providers" @@ -49,6 +50,21 @@ func TestParsedFiles(t *testing.T) { // normalize.UpdateNameSplitHorizon(dc) // } + errs := normalize.ValidateAndNormalizeConfig(conf) + if len(errs) != 0 { + t.Fatal(errs[0]) + } + + for _, dc := range conf.Domains { + //fmt.Printf("DEBUG: PrettySort: domain=%q #rec=%d\n", dc.Name, len(dc.Records)) + //fmt.Printf("DEBUG: records = %d %v\n", len(dc.Records), dc.Records) + ps := prettyzone.PrettySort(dc.Records, dc.Name, 0, nil) + dc.Records = ps.Records + if len(dc.Records) == 0 { + dc.Records = models.Records{} + } + } + // Initialize any DNS providers mentioned. for _, dProv := range conf.DNSProviders { var pcfg = map[string]string{} @@ -91,11 +107,6 @@ func TestParsedFiles(t *testing.T) { // For each domain, if there is a zone file, test against it: - errs := normalize.ValidateAndNormalizeConfig(conf) - if len(errs) != 0 { - t.Fatal(errs[0]) - } - var dCount int for _, dc := range conf.Domains { zoneFile := filepath.Join(testDir, testName, dc.Name+".zone") diff --git a/pkg/prettyzone/sorting.go b/pkg/prettyzone/sorting.go index 6ae20f0711..ff214bd92a 100644 --- a/pkg/prettyzone/sorting.go +++ b/pkg/prettyzone/sorting.go @@ -29,6 +29,7 @@ func (z *ZoneGenData) Less(i, j int) bool { // If we are at the apex, use "@" in the sorting. compA, compB := a.NameFQDN, b.NameFQDN + //fmt.Printf("DEBUG: LabelLess(%q, %q) = %v %q %q\n", compA, compB, LabelLess(compA, compB), a.Name, b.Name) if compA != compB { if a.Name == "@" { compA = "@" @@ -122,6 +123,7 @@ func (z *ZoneGenData) Less(i, j int) bool { default: // pass through. String comparison is sufficient. } + //fmt.Printf("DEBUG: Less %q < %q == %v\n", a.String(), b.String(), a.String() < b.String()) return a.String() < b.String() }