dnscontrol/pkg/normalize/capabilities_test.go

73 lines
2.1 KiB
Go
Raw Normal View History

Tests: ensure provider capabilities are checked (#650) * Tests: ensure provider capabilities are checked Adds test: `TestCapabilitiesAreFiltered` We have a number of records and pseudo-records which in theory can only be used with a given provider if that provider indicates support. In practice, we've been missing the checks for that support and have been passing the records down anyway. The advice comment in the providers/capabilities.go file to edit `checkProviderCapabilities()` has not been reliably followed. We need an internal self-consistency test. The constants are not directly exported or enumerable based solely on the package interfaces at run-time, but with source access for a test suite, we can use the `go/ast` and related interfaces to examine the code, extract all the constants from a given package, figure out which ones we want to be handled, and then insist that they're handled. Before my recent work, we only checked: ALIAS PTR SRV CAA TLSA After this commit, we check: ALIAS AUTODNSSEC CAA NAPTR PTR R53_ALIAS SSHFP SRV TLSA I've added `AUTODNSSEC` as a new feature; `SSHFP` and `PTR` were caught in other recent commits from me; implementing this test caused me to have to add `NAPTR` and `R53_ALIAS`. I whitelist `CanUseTXTMulti` as a special-case. This should prevent regressions. We will probably want to post publicly to warn people that if they're using SSHFP/PTR/NAPTR/R53_ALIAS then they should check the feature matrix and if they don't see their provider listed, to report is as "hey that actually works" so we can update the provider flags. Bonus: our feature matrix will suddenly be more accurate. * Add comments/docs for capabilities authors * fixup! * fixup!
2020-02-25 20:22:32 +08:00
package normalize
import (
"go/ast"
"go/parser"
"go/token"
"sort"
"strings"
"testing"
)
const providersImportDir = "../../providers"
const providersPackageName = "providers"
func TestCapabilitiesAreFiltered(t *testing.T) {
// Any capabilities which we wish to whitelist because it's not directly
// something we can test against.
skipCheckCapabilities := make(map[string]struct{})
skipCheckCapabilities["CanUseTXTMulti"] = struct{}{}
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, providersImportDir, nil, 0)
if err != nil {
t.Fatalf("unable to load Go code from providers: %s", err)
}
providers, ok := pkgs[providersPackageName]
if !ok {
t.Fatalf("did not find package %q in %q", providersPackageName, providersImportDir)
}
constantNames := make([]string, 0, 50)
capabilityInts := make(map[string]int, 50)
// providers.Scope was nil in my testing
for fileName := range providers.Files {
scope := providers.Files[fileName].Scope
for itemName, obj := range scope.Objects {
if obj.Kind != ast.Con {
continue
}
// In practice, the object.Type is nil here so we can't filter for
// capabilities so easily.
if !strings.HasPrefix(itemName, "CanUse") {
continue
}
constantNames = append(constantNames, itemName)
capabilityInts[itemName] = obj.Data.(int)
}
}
sort.Strings(constantNames)
if len(providerCapabilityChecks) == 0 {
t.Fatal("missing entries in providerCapabilityChecks")
}
capIntsToNames := make(map[int]string, len(providerCapabilityChecks))
for _, pair := range providerCapabilityChecks {
capIntsToNames[int(pair.cap)] = pair.rType
}
for _, capName := range constantNames {
capInt := capabilityInts[capName]
if _, ok := skipCheckCapabilities[capName]; ok {
t.Logf("ok: providers.%s (%d) is exempt from checkProviderCapabilities", capName, capInt)
} else if rType, ok := capIntsToNames[capInt]; ok {
t.Logf("ok: providers.%s (%d) is checked for with %q", capName, capInt, rType)
} else {
t.Errorf("MISSING: providers.%s (%d) is not checked by checkProviderCapabilities", capName, capInt)
}
}
}