dnscontrol/integrationTest/helpers_test.go
Tom Limoncelli 7ab7d147fb
CHORE: Move non-provider code out of /providers (#3916)
# Issue

Fixes https://github.com/StackExchange/dnscontrol/issues/3912

# Resolution

```
#!/bin/sh

# Reset

git fetch origin main
git reset --hard origin/main
git checkout main
git branch -D tlim_moveproviders
git checkout -b tlim_moveproviders
find . -name \*.bak -delete

# Move the *.go files out of providers/

mkdir -p pkg/providers
git mv providers/*.go pkg/providers

# move the _all file out of providers/

git mv providers/_all pkg/providers/_all

# Update the imports (in go.* and the affected files)

sed -i.bak -e 's@"github.com/StackExchange/dnscontrol/v4/providers"@"github.com/StackExchange/dnscontrol/v4/pkg/providers"@g' go.* $(fgrep -lr --include '*.go' '"github.com/StackExchange/dnscontrol/v4/providers"' *)
sed -i.bak -e 's@"../../providers"@"../../pkg/providers"@g' pkg/normalize/capabilities_test.go
sed -i.bak -e 's@"github.com/StackExchange/dnscontrol/v4/providers/_all"@"github.com/StackExchange/dnscontrol/v4/pkg/providers/_all"@g' go.* $(fgrep -lr --include '*.go' '"github.com/StackExchange/dnscontrol/v4/providers/_all"' *)

# Fix the docs

sed -i.bak -e 's@StackExchange/dnscontrol/blob/main/providers/_all/all.go@StackExchange/dnscontrol/blob/main/pkg/providers/_all/all.go@g' documentation/advanced-features/writing-providers.md
sed -i.bak -e 's@StackExchange/dnscontrol/providers@StackExchange/dnscontrol/pkg/providers@g' documentation/advanced-features/writing-providers.md
sed -i.bak -e 's@StackExchange/dnscontrol/v4/providers@StackExchange/dnscontrol/v4/pkg/providers@g' documentation/advanced-features/writing-providers.md
sed -i.bak -e 's@dnscontrol/providers/providers.go@dnscontrol/pkg/providers/providers.go@g' documentation/advanced-features/writing-providers.md
sed -i.bak -e 's@providers/_all/all.go@pkg/providers/_all/all.go@g' documentation/advanced-features/writing-providers.md
#sed -i.bak -e 's@@@g' documentation/advanced-features/writing-providers.md
#sed -i.bak -e 's@@@g' documentation/advanced-features/writing-providers.md

find . -name \*.bak -delete

go fmt ./...

git status

echo git commit -a -m'CHORE: Move Non-provider files in providers to pkg/providers'



```
2025-12-15 12:53:52 -05:00

126 lines
3.4 KiB
Go

package main
// Functions for all tests in this directory.
import (
"encoding/json"
"flag"
"fmt"
"strings"
"testing"
"github.com/StackExchange/dnscontrol/v4/pkg/credsfile"
"github.com/StackExchange/dnscontrol/v4/pkg/providers"
"github.com/StackExchange/dnscontrol/v4/providers/cloudflare"
)
var (
providerFlag = flag.String("provider", "", "Provider to run (if empty, deduced from -profile)")
profileFlag = flag.String("profile", "", "Entry in profiles.json to use (if empty, copied from -provider)")
enableCFWorkers = flag.Bool("cfworkers", true, "enable CF worker tests (default true)")
enableCFRedirectMode = flag.Bool("cfredirect", false, "enable CF SingleRedirect tests (default false)")
)
func init() {
testing.Init()
flag.Parse()
}
// ---
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}
func getProvider(t *testing.T) (providers.DNSServiceProvider, string, map[string]string) {
if *providerFlag == "" && *profileFlag == "" {
t.Log("No -provider or -profile specified")
return nil, "", nil
}
// Load the profile values
jsons, err := credsfile.LoadProviderConfigs("profiles.json")
if err != nil {
t.Fatalf("Error loading provider configs: %s", err)
}
// Which profile are we using? Use the profile but default to the provider.
targetProfile := *profileFlag
if targetProfile == "" {
targetProfile = *providerFlag
}
var profileName, profileType string
var cfg map[string]string
// Find the profile we want to use.
for p, c := range jsons {
if p == targetProfile {
cfg = c
profileName = p
profileType = cfg["TYPE"]
if profileType == "" {
t.Fatalf("profiles.json profile %q does not have a TYPE field", *profileFlag)
}
break
}
}
if profileName == "" {
t.Fatalf("Profile not found: -profile=%q -provider=%q", *profileFlag, *providerFlag)
return nil, "", nil
}
// Fill in -profile if blank.
if *profileFlag == "" {
*profileFlag = profileName
}
// Fill in -provider if blank.
if *providerFlag == "" {
*providerFlag = profileType
}
// Sanity check. If the user-specifed -provider flag doesn't match what was in the file, warn them.
if *providerFlag != profileType {
fmt.Printf("WARNING: -provider=%q does not match profile TYPE=%q. Using profile TYPE.\n", *providerFlag, profileType)
*providerFlag = profileType
}
// fmt.Printf("DEBUG flag=%q Profile=%q TYPE=%q\n", *providerFlag, profileName, profileType)
fmt.Printf("Testing Profile=%q (TYPE=%q)\n", profileName, profileType)
var metadata json.RawMessage
// CLOUDFLAREAPI tests related to CLOUDFLAREAPI_SINGLE_REDIRECT/CF_REDIRECT/CF_TEMP_REDIRECT
// requires metadata to enable this feature.
// In hindsight, I have no idea why this metadata flag is required to
// use this feature. Maybe because we didn't have the capabilities
// feature at the time?
if profileType == "CLOUDFLAREAPI" {
items := []string{}
if *enableCFWorkers {
items = append(items, `"manage_workers": true`)
}
if *enableCFRedirectMode {
items = append(items, `"manage_single_redirects": true`)
}
metadata = []byte(`{ ` + strings.Join(items, `, `) + ` }`)
}
provider, err := providers.CreateDNSProvider(profileType, cfg, metadata)
if err != nil {
t.Fatal(err)
}
if profileType == "CLOUDFLAREAPI" && *enableCFWorkers {
// Cloudflare only. Will do nothing if provider != *cloudflareProvider.
if err := cloudflare.PrepareCloudflareTestWorkers(provider); err != nil {
t.Fatal(err)
}
}
return provider, cfg["domain"], cfg
}