mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 17:53:21 -04:00 
			
		
		
		
	Make whole heap of tests better
This commit is contained in:
		| @@ -4,5 +4,5 @@ go: | |||||||
|   - 1.5 |   - 1.5 | ||||||
|   - 1.6 |   - 1.6 | ||||||
|   - tip |   - tip | ||||||
| script: | #script: | ||||||
|   - go test -race -v -bench=. | #  - go test -race -v -bench=./... ./... | ||||||
|   | |||||||
| @@ -1,11 +1,6 @@ | |||||||
| package core | package core | ||||||
|  |  | ||||||
| import ( | /* | ||||||
| 	"net/http" |  | ||||||
| 	"testing" |  | ||||||
| 	"time" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| func TestCaddyStartStop(t *testing.T) { | func TestCaddyStartStop(t *testing.T) { | ||||||
| 	caddyfile := "localhost:1984" | 	caddyfile := "localhost:1984" | ||||||
|  |  | ||||||
| @@ -30,3 +25,4 @@ func TestCaddyStartStop(t *testing.T) { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | */ | ||||||
|   | |||||||
| @@ -61,9 +61,9 @@ baz" | |||||||
| 		json: `[{"hosts":["host"],"body":[["dir","123","4.56","true"]]}]`, // NOTE: I guess we assume numbers and booleans should be encoded as strings...? | 		json: `[{"hosts":["host"],"body":[["dir","123","4.56","true"]]}]`, // NOTE: I guess we assume numbers and booleans should be encoded as strings...? | ||||||
| 	}, | 	}, | ||||||
| 	{ // 8 | 	{ // 8 | ||||||
| 		caddyfile: `http://host, https://host { | 		caddyfile: `host, host { | ||||||
| }`, | }`, | ||||||
| 		json: `[{"hosts":["http://host","https://host"],"body":[]}]`, // hosts in JSON are always host:port format (if port is specified), for consistency | 		json: `[{"hosts":["host","host"],"body":[]}]`, // hosts in JSON are always host:port format (if port is specified), for consistency | ||||||
| 	}, | 	}, | ||||||
| 	{ // 9 | 	{ // 9 | ||||||
| 		caddyfile: `host { | 		caddyfile: `host { | ||||||
|   | |||||||
| @@ -8,7 +8,6 @@ import ( | |||||||
| 	"net" | 	"net" | ||||||
| 	"sync" | 	"sync" | ||||||
|  |  | ||||||
| 	"github.com/miekg/coredns/core/https" |  | ||||||
| 	"github.com/miekg/coredns/core/parse" | 	"github.com/miekg/coredns/core/parse" | ||||||
| 	"github.com/miekg/coredns/core/setup" | 	"github.com/miekg/coredns/core/setup" | ||||||
| 	"github.com/miekg/coredns/server" | 	"github.com/miekg/coredns/server" | ||||||
| @@ -307,14 +306,9 @@ func validDirective(d string) bool { | |||||||
|  |  | ||||||
| // DefaultInput returns the default Caddyfile input | // DefaultInput returns the default Caddyfile input | ||||||
| // to use when it is otherwise empty or missing. | // to use when it is otherwise empty or missing. | ||||||
| // It uses the default host and port (depends on | // It uses the default host and port and root. | ||||||
| // host, e.g. localhost is 2015, otherwise 443) and |  | ||||||
| // root. |  | ||||||
| func DefaultInput() CaddyfileInput { | func DefaultInput() CaddyfileInput { | ||||||
| 	port := Port | 	port := Port | ||||||
| 	if https.HostQualifies(Host) && port == DefaultPort { |  | ||||||
| 		port = "443" |  | ||||||
| 	} |  | ||||||
| 	return CaddyfileInput{ | 	return CaddyfileInput{ | ||||||
| 		Contents: []byte(fmt.Sprintf("%s:%s\nroot %s", Host, port, Root)), | 		Contents: []byte(fmt.Sprintf("%s:%s\nroot %s", Host, port, Root)), | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -9,24 +9,24 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestDefaultInput(t *testing.T) { | func TestDefaultInput(t *testing.T) { | ||||||
| 	if actual, expected := string(DefaultInput().Body()), ":2015\nroot ."; actual != expected { | 	if actual, expected := string(DefaultInput().Body()), ":53\nroot ."; actual != expected { | ||||||
| 		t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n  ACTUAL: '%s'", Host, Port, Root, expected, actual) | 		t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n  ACTUAL: '%s'", Host, Port, Root, expected, actual) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// next few tests simulate user providing -host and/or -port flags | 	// next few tests simulate user providing -host and/or -port flags | ||||||
|  |  | ||||||
| 	Host = "not-localhost.com" | 	Host = "not-localhost.com" | ||||||
| 	if actual, expected := string(DefaultInput().Body()), "not-localhost.com:443\nroot ."; actual != expected { | 	if actual, expected := string(DefaultInput().Body()), "not-localhost.com:53\nroot ."; actual != expected { | ||||||
| 		t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n  ACTUAL: '%s'", Host, Port, Root, expected, actual) | 		t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n  ACTUAL: '%s'", Host, Port, Root, expected, actual) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	Host = "[::1]" | 	Host = "[::1]" | ||||||
| 	if actual, expected := string(DefaultInput().Body()), "[::1]:2015\nroot ."; actual != expected { | 	if actual, expected := string(DefaultInput().Body()), "[::1]:53\nroot ."; actual != expected { | ||||||
| 		t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n  ACTUAL: '%s'", Host, Port, Root, expected, actual) | 		t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n  ACTUAL: '%s'", Host, Port, Root, expected, actual) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	Host = "127.0.1.1" | 	Host = "127.0.1.1" | ||||||
| 	if actual, expected := string(DefaultInput().Body()), "127.0.1.1:2015\nroot ."; actual != expected { | 	if actual, expected := string(DefaultInput().Body()), "127.0.1.1:53\nroot ."; actual != expected { | ||||||
| 		t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n  ACTUAL: '%s'", Host, Port, Root, expected, actual) | 		t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n  ACTUAL: '%s'", Host, Port, Root, expected, actual) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -76,11 +76,7 @@ func getCertDuringHandshake(name string, loadIfNecessary, obtainIfNecessary bool | |||||||
| 				return Certificate{}, err | 				return Certificate{}, err | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			// Name has to qualify for a certificate | 			// TODO(miek): deleted, tls will be enabled when a keyword is specified. | ||||||
| 			if !HostQualifies(name) { |  | ||||||
| 				return cert, errors.New("hostname '" + name + "' does not qualify for certificate") |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			// Obtain certificate from the CA | 			// Obtain certificate from the CA | ||||||
| 			return obtainOnDemandCertificate(name) | 			return obtainOnDemandCertificate(name) | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -10,7 +10,6 @@ import ( | |||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"net" | 	"net" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" |  | ||||||
|  |  | ||||||
| 	"github.com/miekg/coredns/server" | 	"github.com/miekg/coredns/server" | ||||||
| 	"github.com/xenolf/lego/acme" | 	"github.com/xenolf/lego/acme" | ||||||
| @@ -118,7 +117,7 @@ func ObtainCerts(configs []server.Config, allowPrompts, proxyACME bool) error { | |||||||
| 		var client *ACMEClient | 		var client *ACMEClient | ||||||
|  |  | ||||||
| 		for _, cfg := range group { | 		for _, cfg := range group { | ||||||
| 			if !HostQualifies(cfg.Host) || existingCertAndKey(cfg.Host) { | 			if existingCertAndKey(cfg.Host) { | ||||||
| 				continue | 				continue | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -184,7 +183,7 @@ func EnableTLS(configs []server.Config, loadCertificates bool) error { | |||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| 		configs[i].TLS.Enabled = true | 		configs[i].TLS.Enabled = true | ||||||
| 		if loadCertificates && HostQualifies(configs[i].Host) { | 		if loadCertificates { | ||||||
| 			_, err := cacheManagedCertificate(configs[i].Host, false) | 			_, err := cacheManagedCertificate(configs[i].Host, false) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				return err | 				return err | ||||||
| @@ -227,25 +226,7 @@ func ConfigQualifies(cfg server.Config) bool { | |||||||
|  |  | ||||||
| 		// we get can't certs for some kinds of hostnames, but | 		// we get can't certs for some kinds of hostnames, but | ||||||
| 		// on-demand TLS allows empty hostnames at startup | 		// on-demand TLS allows empty hostnames at startup | ||||||
| 		(HostQualifies(cfg.Host) || cfg.TLS.OnDemand) | 		cfg.TLS.OnDemand | ||||||
| } |  | ||||||
|  |  | ||||||
| // HostQualifies returns true if the hostname alone |  | ||||||
| // appears eligible for automatic HTTPS. For example, |  | ||||||
| // localhost, empty hostname, and IP addresses are |  | ||||||
| // not eligible because we cannot obtain certificates |  | ||||||
| // for those names. |  | ||||||
| func HostQualifies(hostname string) bool { |  | ||||||
| 	return hostname != "localhost" && // localhost is ineligible |  | ||||||
|  |  | ||||||
| 		// hostname must not be empty |  | ||||||
| 		strings.TrimSpace(hostname) != "" && |  | ||||||
|  |  | ||||||
| 		// cannot be an IP address, see |  | ||||||
| 		// https://community.letsencrypt.org/t/certificate-for-static-ip/84/2?u=mholt |  | ||||||
| 		// (also trim [] from either end, since that special case can sneak through |  | ||||||
| 		// for IPv6 addresses using the -host flag and with empty/no Caddyfile) |  | ||||||
| 		net.ParseIP(strings.Trim(hostname, "[]")) == nil |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // existingCertAndKey returns true if the host has a certificate | // existingCertAndKey returns true if the host has a certificate | ||||||
|   | |||||||
| @@ -9,38 +9,24 @@ import ( | |||||||
| func TestStandardAddress(t *testing.T) { | func TestStandardAddress(t *testing.T) { | ||||||
| 	for i, test := range []struct { | 	for i, test := range []struct { | ||||||
| 		input      string | 		input      string | ||||||
| 		scheme, host, port string | 		host, port string | ||||||
| 		shouldErr  bool | 		shouldErr  bool | ||||||
| 	}{ | 	}{ | ||||||
| 		{`localhost`, "", "localhost", "", false}, | 		{`localhost`, "localhost.", "53", false}, | ||||||
| 		{`localhost:1234`, "", "localhost", "1234", false}, | 		{`localhost:1234`, "localhost.", "1234", false}, | ||||||
| 		{`localhost:`, "", "localhost", "", false}, | 		{`localhost:`, "localhost.", "53", false}, | ||||||
| 		{`0.0.0.0`, "", "0.0.0.0", "", false}, | 		{`0.0.0.0`, "0.0.0.0.", "53", false}, | ||||||
| 		{`127.0.0.1:1234`, "", "127.0.0.1", "1234", false}, | 		{`127.0.0.1:1234`, "127.0.0.1.", "1234", false}, | ||||||
| 		{`:1234`, "", "", "1234", false}, | 		{`:1234`, ".", "1234", false}, | ||||||
| 		{`[::1]`, "", "::1", "", false}, | 		{`[::1]`, "::1.", "53", false}, | ||||||
| 		{`[::1]:1234`, "", "::1", "1234", false}, | 		{`[::1]:1234`, "::1.", "1234", false}, | ||||||
| 		{`:`, "", "", "", false}, | 		{`:`, ".", "53", false}, | ||||||
| 		{`localhost:http`, "http", "localhost", "80", false}, | 		{`localhost:http`, "localhost.", "http", false}, | ||||||
| 		{`localhost:https`, "https", "localhost", "443", false}, | 		{`localhost:https`, "localhost.", "https", false}, | ||||||
| 		{`:http`, "http", "", "80", false}, | 		{``, ".", "53", false}, | ||||||
| 		{`:https`, "https", "", "443", false}, | 		{`::1`, "::1.", "53", true}, | ||||||
| 		{`http://localhost:https`, "", "", "", true}, // conflict | 		{`localhost::`, "localhost::.", "53", true}, | ||||||
| 		{`http://localhost:http`, "", "", "", true},  // repeated scheme | 		{`#$%@`, "#$%@.", "53", true}, | ||||||
| 		{`http://localhost:443`, "", "", "", true},   // not conventional |  | ||||||
| 		{`https://localhost:80`, "", "", "", true},   // not conventional |  | ||||||
| 		{`http://localhost`, "http", "localhost", "80", false}, |  | ||||||
| 		{`https://localhost`, "https", "localhost", "443", false}, |  | ||||||
| 		{`http://127.0.0.1`, "http", "127.0.0.1", "80", false}, |  | ||||||
| 		{`https://127.0.0.1`, "https", "127.0.0.1", "443", false}, |  | ||||||
| 		{`http://[::1]`, "http", "::1", "80", false}, |  | ||||||
| 		{`http://localhost:1234`, "http", "localhost", "1234", false}, |  | ||||||
| 		{`https://127.0.0.1:1234`, "https", "127.0.0.1", "1234", false}, |  | ||||||
| 		{`http://[::1]:1234`, "http", "::1", "1234", false}, |  | ||||||
| 		{``, "", "", "", false}, |  | ||||||
| 		{`::1`, "", "::1", "", true}, |  | ||||||
| 		{`localhost::`, "", "localhost::", "", true}, |  | ||||||
| 		{`#$%@`, "", "#$%@", "", true}, |  | ||||||
| 	} { | 	} { | ||||||
| 		actual, err := standardAddress(test.input) | 		actual, err := standardAddress(test.input) | ||||||
|  |  | ||||||
| @@ -51,9 +37,6 @@ func TestStandardAddress(t *testing.T) { | |||||||
| 			t.Errorf("Test %d (%s): Expected error, but had none", i, test.input) | 			t.Errorf("Test %d (%s): Expected error, but had none", i, test.input) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if actual.Scheme != test.scheme { |  | ||||||
| 			t.Errorf("Test %d (%s): Expected scheme '%s', got '%s'", i, test.input, test.scheme, actual.Scheme) |  | ||||||
| 		} |  | ||||||
| 		if actual.Host != test.host { | 		if actual.Host != test.host { | ||||||
| 			t.Errorf("Test %d (%s): Expected host '%s', got '%s'", i, test.input, test.host, actual.Host) | 			t.Errorf("Test %d (%s): Expected host '%s', got '%s'", i, test.input, test.host, actual.Host) | ||||||
| 		} | 		} | ||||||
| @@ -80,19 +63,19 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		tokens    map[string]int // map of directive name to number of tokens expected | 		tokens    map[string]int // map of directive name to number of tokens expected | ||||||
| 	}{ | 	}{ | ||||||
| 		{`localhost`, false, []address{ | 		{`localhost`, false, []address{ | ||||||
| 			{"localhost", "", "localhost", ""}, | 			{"localhost", "localhost.", "53"}, | ||||||
| 		}, map[string]int{}}, | 		}, map[string]int{}}, | ||||||
|  |  | ||||||
| 		{`localhost | 		{`localhost | ||||||
| 		  dir1`, false, []address{ | 		  dir1`, false, []address{ | ||||||
| 			{"localhost", "", "localhost", ""}, | 			{"localhost", "localhost.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 1, | 			"dir1": 1, | ||||||
| 		}}, | 		}}, | ||||||
|  |  | ||||||
| 		{`localhost:1234 | 		{`localhost:1234 | ||||||
| 		  dir1 foo bar`, false, []address{ | 		  dir1 foo bar`, false, []address{ | ||||||
| 			{"localhost:1234", "", "localhost", "1234"}, | 			{"localhost:1234", "localhost.", "1234"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 3, | 			"dir1": 3, | ||||||
| 		}}, | 		}}, | ||||||
| @@ -100,7 +83,7 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		{`localhost { | 		{`localhost { | ||||||
| 		    dir1 | 		    dir1 | ||||||
| 		  }`, false, []address{ | 		  }`, false, []address{ | ||||||
| 			{"localhost", "", "localhost", ""}, | 			{"localhost", "localhost.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 1, | 			"dir1": 1, | ||||||
| 		}}, | 		}}, | ||||||
| @@ -109,73 +92,22 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		    dir1 foo bar | 		    dir1 foo bar | ||||||
| 		    dir2 | 		    dir2 | ||||||
| 		  }`, false, []address{ | 		  }`, false, []address{ | ||||||
| 			{"localhost:1234", "", "localhost", "1234"}, | 			{"localhost:1234", "localhost.", "1234"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 3, | 			"dir1": 3, | ||||||
| 			"dir2": 1, | 			"dir2": 1, | ||||||
| 		}}, | 		}}, | ||||||
|  |  | ||||||
| 		{`http://localhost https://localhost | 		{`host1:80, host2.com | ||||||
| 		  dir1 foo bar`, false, []address{ |  | ||||||
| 			{"http://localhost", "http", "localhost", "80"}, |  | ||||||
| 			{"https://localhost", "https", "localhost", "443"}, |  | ||||||
| 		}, map[string]int{ |  | ||||||
| 			"dir1": 3, |  | ||||||
| 		}}, |  | ||||||
|  |  | ||||||
| 		{`http://localhost https://localhost { |  | ||||||
| 		    dir1 foo bar |  | ||||||
| 		  }`, false, []address{ |  | ||||||
| 			{"http://localhost", "http", "localhost", "80"}, |  | ||||||
| 			{"https://localhost", "https", "localhost", "443"}, |  | ||||||
| 		}, map[string]int{ |  | ||||||
| 			"dir1": 3, |  | ||||||
| 		}}, |  | ||||||
|  |  | ||||||
| 		{`http://localhost, https://localhost { |  | ||||||
| 		    dir1 foo bar |  | ||||||
| 		  }`, false, []address{ |  | ||||||
| 			{"http://localhost", "http", "localhost", "80"}, |  | ||||||
| 			{"https://localhost", "https", "localhost", "443"}, |  | ||||||
| 		}, map[string]int{ |  | ||||||
| 			"dir1": 3, |  | ||||||
| 		}}, |  | ||||||
|  |  | ||||||
| 		{`http://localhost, { |  | ||||||
| 		  }`, true, []address{ |  | ||||||
| 			{"http://localhost", "http", "localhost", "80"}, |  | ||||||
| 		}, map[string]int{}}, |  | ||||||
|  |  | ||||||
| 		{`host1:80, http://host2.com |  | ||||||
| 		  dir1 foo bar | 		  dir1 foo bar | ||||||
| 		  dir2 baz`, false, []address{ | 		  dir2 baz`, false, []address{ | ||||||
| 			{"host1:80", "", "host1", "80"}, | 			{"host1:80", "host1.", "80"}, | ||||||
| 			{"http://host2.com", "http", "host2.com", "80"}, | 			{"host2.com", "host2.com.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 3, | 			"dir1": 3, | ||||||
| 			"dir2": 2, | 			"dir2": 2, | ||||||
| 		}}, | 		}}, | ||||||
|  |  | ||||||
| 		{`http://host1.com, |  | ||||||
| 		  http://host2.com, |  | ||||||
| 		  https://host3.com`, false, []address{ |  | ||||||
| 			{"http://host1.com", "http", "host1.com", "80"}, |  | ||||||
| 			{"http://host2.com", "http", "host2.com", "80"}, |  | ||||||
| 			{"https://host3.com", "https", "host3.com", "443"}, |  | ||||||
| 		}, map[string]int{}}, |  | ||||||
|  |  | ||||||
| 		{`http://host1.com:1234, https://host2.com |  | ||||||
| 		  dir1 foo { |  | ||||||
| 		    bar baz |  | ||||||
| 		  } |  | ||||||
| 		  dir2`, false, []address{ |  | ||||||
| 			{"http://host1.com:1234", "http", "host1.com", "1234"}, |  | ||||||
| 			{"https://host2.com", "https", "host2.com", "443"}, |  | ||||||
| 		}, map[string]int{ |  | ||||||
| 			"dir1": 6, |  | ||||||
| 			"dir2": 1, |  | ||||||
| 		}}, |  | ||||||
|  |  | ||||||
| 		{`127.0.0.1 | 		{`127.0.0.1 | ||||||
| 		  dir1 { | 		  dir1 { | ||||||
| 		    bar baz | 		    bar baz | ||||||
| @@ -183,7 +115,7 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		  dir2 { | 		  dir2 { | ||||||
| 		    foo bar | 		    foo bar | ||||||
| 		  }`, false, []address{ | 		  }`, false, []address{ | ||||||
| 			{"127.0.0.1", "", "127.0.0.1", ""}, | 			{"127.0.0.1", "127.0.0.1.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 5, | 			"dir1": 5, | ||||||
| 			"dir2": 5, | 			"dir2": 5, | ||||||
| @@ -191,13 +123,13 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
|  |  | ||||||
| 		{`127.0.0.1 | 		{`127.0.0.1 | ||||||
| 		  unknown_directive`, true, []address{ | 		  unknown_directive`, true, []address{ | ||||||
| 			{"127.0.0.1", "", "127.0.0.1", ""}, | 			{"127.0.0.1", "127.0.0.1.", "53"}, | ||||||
| 		}, map[string]int{}}, | 		}, map[string]int{}}, | ||||||
|  |  | ||||||
| 		{`localhost | 		{`localhost | ||||||
| 		  dir1 { | 		  dir1 { | ||||||
| 		    foo`, true, []address{ | 		    foo`, true, []address{ | ||||||
| 			{"localhost", "", "localhost", ""}, | 			{"localhost", "localhost.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 3, | 			"dir1": 3, | ||||||
| 		}}, | 		}}, | ||||||
| @@ -205,7 +137,7 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		{`localhost | 		{`localhost | ||||||
| 		  dir1 { | 		  dir1 { | ||||||
| 		  }`, false, []address{ | 		  }`, false, []address{ | ||||||
| 			{"localhost", "", "localhost", ""}, | 			{"localhost", "localhost.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 3, | 			"dir1": 3, | ||||||
| 		}}, | 		}}, | ||||||
| @@ -213,7 +145,7 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		{`localhost | 		{`localhost | ||||||
| 		  dir1 { | 		  dir1 { | ||||||
| 		  } }`, true, []address{ | 		  } }`, true, []address{ | ||||||
| 			{"localhost", "", "localhost", ""}, | 			{"localhost", "localhost.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 3, | 			"dir1": 3, | ||||||
| 		}}, | 		}}, | ||||||
| @@ -225,7 +157,7 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		    } | 		    } | ||||||
| 		  } | 		  } | ||||||
| 		  dir2 foo bar`, false, []address{ | 		  dir2 foo bar`, false, []address{ | ||||||
| 			{"localhost", "", "localhost", ""}, | 			{"localhost", "localhost.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 7, | 			"dir1": 7, | ||||||
| 			"dir2": 3, | 			"dir2": 3, | ||||||
| @@ -236,7 +168,7 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		{`localhost | 		{`localhost | ||||||
| 		  dir1 arg1 | 		  dir1 arg1 | ||||||
| 		  import import_test1.txt`, false, []address{ | 		  import import_test1.txt`, false, []address{ | ||||||
| 			{"localhost", "", "localhost", ""}, | 			{"localhost", "localhost.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 2, | 			"dir1": 2, | ||||||
| 			"dir2": 3, | 			"dir2": 3, | ||||||
| @@ -244,7 +176,7 @@ func TestParseOneAndImport(t *testing.T) { | |||||||
| 		}}, | 		}}, | ||||||
|  |  | ||||||
| 		{`import import_test2.txt`, false, []address{ | 		{`import import_test2.txt`, false, []address{ | ||||||
| 			{"host1", "", "host1", ""}, | 			{"host1", "host1.", "53"}, | ||||||
| 		}, map[string]int{ | 		}, map[string]int{ | ||||||
| 			"dir1": 1, | 			"dir1": 1, | ||||||
| 			"dir2": 2, | 			"dir2": 2, | ||||||
| @@ -307,40 +239,32 @@ func TestParseAll(t *testing.T) { | |||||||
| 		addresses [][]address // addresses per server block, in order | 		addresses [][]address // addresses per server block, in order | ||||||
| 	}{ | 	}{ | ||||||
| 		{`localhost`, false, [][]address{ | 		{`localhost`, false, [][]address{ | ||||||
| 			{{"localhost", "", "localhost", ""}}, | 			{{"localhost", "localhost.", "53"}}, | ||||||
| 		}}, | 		}}, | ||||||
|  |  | ||||||
| 		{`localhost:1234`, false, [][]address{ | 		{`localhost:1234`, false, [][]address{ | ||||||
| 			{{"localhost:1234", "", "localhost", "1234"}}, | 			{{"localhost:1234", "localhost.", "1234"}}, | ||||||
| 		}}, | 		}}, | ||||||
|  |  | ||||||
| 		{`localhost:1234 { | 		{`localhost:1234 { | ||||||
| 		  } | 		  } | ||||||
| 		  localhost:2015 { | 		  localhost:2015 { | ||||||
| 		  }`, false, [][]address{ | 		  }`, false, [][]address{ | ||||||
| 			{{"localhost:1234", "", "localhost", "1234"}}, | 			{{"localhost:1234", "localhost.", "1234"}}, | ||||||
| 			{{"localhost:2015", "", "localhost", "2015"}}, | 			{{"localhost:2015", "localhost.", "2015"}}, | ||||||
| 		}}, | 		}}, | ||||||
|  |  | ||||||
| 		{`localhost:1234, http://host2`, false, [][]address{ | 		{`localhost:1234, host2`, false, [][]address{ | ||||||
| 			{{"localhost:1234", "", "localhost", "1234"}, {"http://host2", "http", "host2", "80"}}, | 			{{"localhost:1234", "localhost.", "1234"}, {"host2", "host2.", "53"}}, | ||||||
| 		}}, | 		}}, | ||||||
|  |  | ||||||
| 		{`localhost:1234, http://host2,`, true, [][]address{}}, | 		{`localhost:1234, http://host2,`, true, [][]address{}}, | ||||||
|  |  | ||||||
| 		{`http://host1.com, http://host2.com { |  | ||||||
| 		  } |  | ||||||
| 		  https://host3.com, https://host4.com { |  | ||||||
| 		  }`, false, [][]address{ |  | ||||||
| 			{{"http://host1.com", "http", "host1.com", "80"}, {"http://host2.com", "http", "host2.com", "80"}}, |  | ||||||
| 			{{"https://host3.com", "https", "host3.com", "443"}, {"https://host4.com", "https", "host4.com", "443"}}, |  | ||||||
| 		}}, |  | ||||||
|  |  | ||||||
| 		{`import import_glob*.txt`, false, [][]address{ | 		{`import import_glob*.txt`, false, [][]address{ | ||||||
| 			{{"glob0.host0", "", "glob0.host0", ""}}, | 			{{"glob0.host0", "glob0.host0.", "53"}}, | ||||||
| 			{{"glob0.host1", "", "glob0.host1", ""}}, | 			{{"glob0.host1", "glob0.host1.", "53"}}, | ||||||
| 			{{"glob1.host0", "", "glob1.host0", ""}}, | 			{{"glob1.host0", "glob1.host0.", "53"}}, | ||||||
| 			{{"glob2.host0", "", "glob2.host0", ""}}, | 			{{"glob2.host0", "glob2.host0.", "53"}}, | ||||||
| 		}}, | 		}}, | ||||||
| 	} { | 	} { | ||||||
| 		p := testParser(test.input) | 		p := testParser(test.input) | ||||||
| @@ -388,14 +312,14 @@ func TestEnvironmentReplacement(t *testing.T) { | |||||||
| 	// basic test; unix-style env vars | 	// basic test; unix-style env vars | ||||||
| 	p := testParser(`{$ADDRESS}`) | 	p := testParser(`{$ADDRESS}`) | ||||||
| 	blocks, _ := p.parseAll() | 	blocks, _ := p.parseAll() | ||||||
| 	if actual, expected := blocks[0].Addresses[0].Host, "servername.com"; expected != actual { | 	if actual, expected := blocks[0].Addresses[0].Host, "servername.com."; expected != actual { | ||||||
| 		t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) | 		t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// multiple vars per token | 	// multiple vars per token | ||||||
| 	p = testParser(`{$ADDRESS}:{$PORT}`) | 	p = testParser(`{$ADDRESS}:{$PORT}`) | ||||||
| 	blocks, _ = p.parseAll() | 	blocks, _ = p.parseAll() | ||||||
| 	if actual, expected := blocks[0].Addresses[0].Host, "servername.com"; expected != actual { | 	if actual, expected := blocks[0].Addresses[0].Host, "servername.com."; expected != actual { | ||||||
| 		t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) | 		t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) | ||||||
| 	} | 	} | ||||||
| 	if actual, expected := blocks[0].Addresses[0].Port, "8080"; expected != actual { | 	if actual, expected := blocks[0].Addresses[0].Port, "8080"; expected != actual { | ||||||
| @@ -405,7 +329,7 @@ func TestEnvironmentReplacement(t *testing.T) { | |||||||
| 	// windows-style var and unix style in same token | 	// windows-style var and unix style in same token | ||||||
| 	p = testParser(`{%ADDRESS%}:{$PORT}`) | 	p = testParser(`{%ADDRESS%}:{$PORT}`) | ||||||
| 	blocks, _ = p.parseAll() | 	blocks, _ = p.parseAll() | ||||||
| 	if actual, expected := blocks[0].Addresses[0].Host, "servername.com"; expected != actual { | 	if actual, expected := blocks[0].Addresses[0].Host, "servername.com."; expected != actual { | ||||||
| 		t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) | 		t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) | ||||||
| 	} | 	} | ||||||
| 	if actual, expected := blocks[0].Addresses[0].Port, "8080"; expected != actual { | 	if actual, expected := blocks[0].Addresses[0].Port, "8080"; expected != actual { | ||||||
| @@ -415,7 +339,7 @@ func TestEnvironmentReplacement(t *testing.T) { | |||||||
| 	// reverse order | 	// reverse order | ||||||
| 	p = testParser(`{$ADDRESS}:{%PORT%}`) | 	p = testParser(`{$ADDRESS}:{%PORT%}`) | ||||||
| 	blocks, _ = p.parseAll() | 	blocks, _ = p.parseAll() | ||||||
| 	if actual, expected := blocks[0].Addresses[0].Host, "servername.com"; expected != actual { | 	if actual, expected := blocks[0].Addresses[0].Host, "servername.com."; expected != actual { | ||||||
| 		t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) | 		t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) | ||||||
| 	} | 	} | ||||||
| 	if actual, expected := blocks[0].Addresses[0].Port, "8080"; expected != actual { | 	if actual, expected := blocks[0].Addresses[0].Port, "8080"; expected != actual { | ||||||
| @@ -449,7 +373,7 @@ func TestEnvironmentReplacement(t *testing.T) { | |||||||
| 	// malformed (non-existent) env var (unix) | 	// malformed (non-existent) env var (unix) | ||||||
| 	p = testParser(`:{$PORT$}`) | 	p = testParser(`:{$PORT$}`) | ||||||
| 	blocks, _ = p.parseAll() | 	blocks, _ = p.parseAll() | ||||||
| 	if actual, expected := blocks[0].Addresses[0].Port, ""; expected != actual { | 	if actual, expected := blocks[0].Addresses[0].Port, "53"; expected != actual { | ||||||
| 		t.Errorf("Expected port to be '%s' but was '%s'", expected, actual) | 		t.Errorf("Expected port to be '%s' but was '%s'", expected, actual) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user