Skip to content

Commit

Permalink
TESTING: parse_tests: Generate post-processed json files
Browse files Browse the repository at this point in the history
  • Loading branch information
tlimoncelli committed Jan 3, 2025
1 parent e664dae commit e232501
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
42 changes: 42 additions & 0 deletions pkg/js/README-parse_tests.md
Original file line number Diff line number Diff line change
@@ -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
```
21 changes: 16 additions & 5 deletions pkg/js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions pkg/prettyzone/sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "@"
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit e232501

Please sign in to comment.