github.com/pmoroney/dnscontrol@v0.2.4-0.20171024134423-fad98f73f44a/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 TestPreprocess_BoolValidation(t *testing.T) {
    20  	cf := &CloudflareApi{}
    21  	domain := newDomainConfig()
    22  	domain.Records = append(domain.Records, &models.RecordConfig{Type: "A", Target: "1.2.3.4", Metadata: map[string]string{metaProxy: "on"}})
    23  	domain.Records = append(domain.Records, &models.RecordConfig{Type: "A", Target: "1.2.3.4", Metadata: map[string]string{metaProxy: "fUll"}})
    24  	domain.Records = append(domain.Records, &models.RecordConfig{Type: "A", Target: "1.2.3.4", Metadata: map[string]string{}})
    25  	domain.Records = append(domain.Records, &models.RecordConfig{Type: "A", Target: "1.2.3.4", Metadata: map[string]string{metaProxy: "Off"}})
    26  	domain.Records = append(domain.Records, &models.RecordConfig{Type: "A", Target: "1.2.3.4", Metadata: map[string]string{metaProxy: "off"}})
    27  	err := cf.preprocessConfig(domain)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	expected := []string{"on", "full", "off", "off", "off"}
    32  	// make sure only "on" or "off", and "full" are actually set
    33  	for i, rec := range domain.Records {
    34  		if rec.Metadata[metaProxy] != expected[i] {
    35  			t.Fatalf("At index %d: expect '%s' but found '%s'", i, expected[i], rec.Metadata[metaProxy])
    36  		}
    37  	}
    38  }
    39  
    40  func TestPreprocess_BoolValidation_Fails(t *testing.T) {
    41  	cf := &CloudflareApi{}
    42  	domain := newDomainConfig()
    43  	domain.Records = append(domain.Records, &models.RecordConfig{Metadata: map[string]string{metaProxy: "true"}})
    44  	err := cf.preprocessConfig(domain)
    45  	if err == nil {
    46  		t.Fatal("Expected validation error, but got none")
    47  	}
    48  }
    49  
    50  func TestPreprocess_DefaultProxy(t *testing.T) {
    51  	cf := &CloudflareApi{}
    52  	domain := newDomainConfig()
    53  	domain.Metadata[metaProxyDefault] = "full"
    54  	domain.Records = append(domain.Records, &models.RecordConfig{Type: "A", Target: "1.2.3.4", Metadata: map[string]string{metaProxy: "on"}})
    55  	domain.Records = append(domain.Records, &models.RecordConfig{Type: "A", Target: "1.2.3.4", Metadata: map[string]string{metaProxy: "off"}})
    56  	domain.Records = append(domain.Records, &models.RecordConfig{Type: "A", Target: "1.2.3.4", Metadata: map[string]string{}})
    57  	err := cf.preprocessConfig(domain)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	expected := []string{"on", "off", "full"}
    62  	for i, rec := range domain.Records {
    63  		if rec.Metadata[metaProxy] != expected[i] {
    64  			t.Fatalf("At index %d: expect '%s' but found '%s'", i, expected[i], rec.Metadata[metaProxy])
    65  		}
    66  	}
    67  }
    68  
    69  func TestPreprocess_DefaultProxy_Validation(t *testing.T) {
    70  	cf := &CloudflareApi{}
    71  	domain := newDomainConfig()
    72  	domain.Metadata[metaProxyDefault] = "true"
    73  	err := cf.preprocessConfig(domain)
    74  	if err == nil {
    75  		t.Fatal("Expected validation error, but got none")
    76  	}
    77  }
    78  
    79  func TestIpRewriting(t *testing.T) {
    80  	var tests = []struct {
    81  		Given, Expected string
    82  		Proxy           string
    83  	}{
    84  		//outside of range
    85  		{"5.5.5.5", "5.5.5.5", "full"},
    86  		{"5.5.5.5", "5.5.5.5", "on"},
    87  		// inside range, but not proxied
    88  		{"1.2.3.4", "1.2.3.4", "on"},
    89  		//inside range and proxied
    90  		{"1.2.3.4", "255.255.255.4", "full"},
    91  	}
    92  	cf := &CloudflareApi{}
    93  	domain := newDomainConfig()
    94  	cf.ipConversions = []transform.IpConversion{{net.ParseIP("1.2.3.0"), net.ParseIP("1.2.3.40"), []net.IP{net.ParseIP("255.255.255.0")}, nil}}
    95  	for _, tst := range tests {
    96  		rec := &models.RecordConfig{Type: "A", Target: tst.Given, Metadata: map[string]string{metaProxy: tst.Proxy}}
    97  		domain.Records = append(domain.Records, rec)
    98  	}
    99  	err := cf.preprocessConfig(domain)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  	for i, tst := range tests {
   104  		rec := domain.Records[i]
   105  		if rec.Target != tst.Expected {
   106  			t.Fatalf("At index %d, expected target of %s, but found %s.", i, tst.Expected, rec.Target)
   107  		}
   108  		if tst.Proxy == "full" && tst.Given != tst.Expected && rec.Metadata[metaOriginalIP] != tst.Given {
   109  			t.Fatalf("At index %d, expected original_ip to be set", i)
   110  		}
   111  	}
   112  }