github.com/kurthockenbury/dnscontrol@v0.2.8/providers/cloudflare/preprocess_test.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  
     7  	"github.com/StackExchange/dnscontrol/models"
     8  	"github.com/StackExchange/dnscontrol/pkg/transform"
     9  )
    10  
    11  func newDomainConfig() *models.DomainConfig {
    12  	return &models.DomainConfig{
    13  		Name:     "test.com",
    14  		Records:  []*models.RecordConfig{},
    15  		Metadata: map[string]string{},
    16  	}
    17  }
    18  
    19  func makeRCmeta(meta map[string]string) *models.RecordConfig {
    20  	rc := models.RecordConfig{
    21  		Type:     "A",
    22  		Metadata: meta,
    23  	}
    24  	rc.SetLabel("foo", "example.tld")
    25  	rc.SetTarget("1.2.3.4")
    26  	return &rc
    27  }
    28  
    29  func TestPreprocess_BoolValidation(t *testing.T) {
    30  	cf := &CloudflareApi{}
    31  
    32  	domain := newDomainConfig()
    33  	domain.Records = append(domain.Records, makeRCmeta(map[string]string{metaProxy: "on"}))
    34  	domain.Records = append(domain.Records, makeRCmeta(map[string]string{metaProxy: "fUll"}))
    35  	domain.Records = append(domain.Records, makeRCmeta(map[string]string{}))
    36  	domain.Records = append(domain.Records, makeRCmeta(map[string]string{metaProxy: "Off"}))
    37  	domain.Records = append(domain.Records, makeRCmeta(map[string]string{metaProxy: "off"}))
    38  	err := cf.preprocessConfig(domain)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	expected := []string{"on", "full", "off", "off", "off"}
    43  	// make sure only "on" or "off", and "full" are actually set
    44  	for i, rec := range domain.Records {
    45  		if rec.Metadata[metaProxy] != expected[i] {
    46  			t.Fatalf("At index %d: expect '%s' but found '%s'", i, expected[i], rec.Metadata[metaProxy])
    47  		}
    48  	}
    49  }
    50  
    51  func TestPreprocess_BoolValidation_Fails(t *testing.T) {
    52  	cf := &CloudflareApi{}
    53  	domain := newDomainConfig()
    54  	domain.Records = append(domain.Records, &models.RecordConfig{Metadata: map[string]string{metaProxy: "true"}})
    55  	err := cf.preprocessConfig(domain)
    56  	if err == nil {
    57  		t.Fatal("Expected validation error, but got none")
    58  	}
    59  }
    60  
    61  func TestPreprocess_DefaultProxy(t *testing.T) {
    62  	cf := &CloudflareApi{}
    63  	domain := newDomainConfig()
    64  	domain.Metadata[metaProxyDefault] = "full"
    65  	domain.Records = append(domain.Records, makeRCmeta(map[string]string{metaProxy: "on"}))
    66  	domain.Records = append(domain.Records, makeRCmeta(map[string]string{metaProxy: "off"}))
    67  	domain.Records = append(domain.Records, makeRCmeta(map[string]string{}))
    68  	err := cf.preprocessConfig(domain)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	expected := []string{"on", "off", "full"}
    73  	for i, rec := range domain.Records {
    74  		if rec.Metadata[metaProxy] != expected[i] {
    75  			t.Fatalf("At index %d: expect '%s' but found '%s'", i, expected[i], rec.Metadata[metaProxy])
    76  		}
    77  	}
    78  }
    79  
    80  func TestPreprocess_DefaultProxy_Validation(t *testing.T) {
    81  	cf := &CloudflareApi{}
    82  	domain := newDomainConfig()
    83  	domain.Metadata[metaProxyDefault] = "true"
    84  	err := cf.preprocessConfig(domain)
    85  	if err == nil {
    86  		t.Fatal("Expected validation error, but got none")
    87  	}
    88  }
    89  
    90  func TestIpRewriting(t *testing.T) {
    91  	var tests = []struct {
    92  		Given, Expected string
    93  		Proxy           string
    94  	}{
    95  		// outside of range
    96  		{"5.5.5.5", "5.5.5.5", "full"},
    97  		{"5.5.5.5", "5.5.5.5", "on"},
    98  		// inside range, but not proxied
    99  		{"1.2.3.4", "1.2.3.4", "on"},
   100  		// inside range and proxied
   101  		{"1.2.3.4", "255.255.255.4", "full"},
   102  	}
   103  	cf := &CloudflareApi{}
   104  	domain := newDomainConfig()
   105  	cf.ipConversions = []transform.IpConversion{{
   106  		Low:      net.ParseIP("1.2.3.0"),
   107  		High:     net.ParseIP("1.2.3.40"),
   108  		NewBases: []net.IP{net.ParseIP("255.255.255.0")},
   109  		NewIPs:   nil}}
   110  	for _, tst := range tests {
   111  		rec := &models.RecordConfig{Type: "A", Metadata: map[string]string{metaProxy: tst.Proxy}}
   112  		rec.SetTarget(tst.Given)
   113  		domain.Records = append(domain.Records, rec)
   114  	}
   115  	err := cf.preprocessConfig(domain)
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  	for i, tst := range tests {
   120  		rec := domain.Records[i]
   121  		if rec.GetTargetField() != tst.Expected {
   122  			t.Fatalf("At index %d, expected target of %s, but found %s.", i, tst.Expected, rec.GetTargetField())
   123  		}
   124  		if tst.Proxy == "full" && tst.Given != tst.Expected && rec.Metadata[metaOriginalIP] != tst.Given {
   125  			t.Fatalf("At index %d, expected original_ip to be set", i)
   126  		}
   127  	}
   128  }