dnscontrol/pkg/normalize/capabilities_test.go
Tom Limoncelli 8dea9edc34
Re-engineer TXT records for simplicity and better compliance (#1063)
TXT records are now handled different.

1. The raw input from dnsconfig.js is passed all the way to the provider. The provider can determine if it can or can't handle such records (auditrecords.go) and processes them internally as such.
2. The CanUseTXTMulti capability is no longer needed.

* DSPs now register a table of functions
* Use audits for txt record variations
* unit tests pass. integration fails.
* fix deepcopy problem
* rename to AuditRecordSupport
* Reduce use of TXTMulti
* Remove CanUseTXTMulti
* fix Test Skip
* fix DO
* fix vultr
* fix NDC
* msdns fixes
* Fix powerdns and cloudflare
* HEDNS: Fix usage of target field to resolve TXT handling (#1067)
* Fix HEXONET

Co-authored-by: Robert Blenkinsopp <robert@blenkinsopp.net>
Co-authored-by: Jakob Ackermann <das7pad@outlook.com>
2021-03-07 13:19:22 -05:00

74 lines
2.1 KiB
Go

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["CanUseBlahBlahBlah"] = 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 {
for _, cap := range pair.caps {
capIntsToNames[int(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)
}
}
}