cidr everywhere: check all middleware (#915)

* cidr everywhere: check all middleware

Add tests for cidr in only that middleware that already tests for this.
Check the other ones manually (and put reverse in the tests cases
anyway).

Make etcd setup_test run without +build etcd tag - it is not needed
for this test - move rest of the code to lookup_test.go.

Cleanup proxy test a bit and remove TempDir as there is test.TempFile
that does the same thing.

Fixes #909

* coredns package

* Fix test compile
This commit is contained in:
Miek Gieben
2017-08-13 18:16:25 +01:00
committed by GitHub
parent 0c02708d63
commit 818d2b10ad
9 changed files with 135 additions and 127 deletions

View File

@@ -1,8 +1,6 @@
package proxy
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@@ -35,19 +33,6 @@ func TestAllowedDomain(t *testing.T) {
}
}
func writeTmpFile(t *testing.T, data string) (string, string) {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("tempDir: %v", err)
}
path := filepath.Join(tempDir, "resolv.conf")
if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
t.Fatalf("writeFile: %v", err)
}
return tempDir, path
}
func TestProxyParse(t *testing.T) {
rmFunc, cert, key, ca := getPEMFiles(t)
defer rmFunc()
@@ -65,6 +50,10 @@ func TestProxyParse(t *testing.T) {
`proxy . 8.8.8.8:53`,
false,
},
{
`proxy 10.0.0.0/24 8.8.8.8:53`,
false,
},
{
`
proxy . 8.8.8.8:53 {
@@ -103,7 +92,7 @@ proxy . 8.8.8.8:53 {
{
`
proxy . 8.8.8.8:53 {
except miek.nl example.org
except miek.nl example.org 10.0.0.0/24
}`,
false,
},
@@ -283,13 +272,18 @@ junky resolve.conf
[]string{"1.1.1.1:5000", "2.2.2.2:1234"},
},
}
for i, test := range tests {
tempDir, path := writeTmpFile(t, test.filedata)
defer os.RemoveAll(tempDir)
config := strings.Replace(test.inputUpstreams, "FILE", path, -1)
for i, tc := range tests {
path, rm, err := test.TempFile(".", tc.filedata)
if err != nil {
t.Fatalf("Test %d could not creat temp file %v", i, err)
}
defer rm()
config := strings.Replace(tc.inputUpstreams, "FILE", path, -1)
c := caddy.NewTestController("dns", config)
upstreams, err := NewStaticUpstreams(&c.Dispenser)
if (err != nil) != test.shouldErr {
if (err != nil) != tc.shouldErr {
t.Errorf("Test %d expected no error, got %v", i+1, err)
}
var hosts []string
@@ -298,18 +292,18 @@ junky resolve.conf
hosts = append(hosts, h.Name)
}
}
if !test.shouldErr {
if len(hosts) != len(test.expected) {
t.Errorf("Test %d expected %d hosts got %d", i+1, len(test.expected), len(upstreams))
if !tc.shouldErr {
if len(hosts) != len(tc.expected) {
t.Errorf("Test %d expected %d hosts got %d", i+1, len(tc.expected), len(upstreams))
} else {
ok := true
for i, v := range test.expected {
for i, v := range tc.expected {
if v != hosts[i] {
ok = false
}
}
if !ok {
t.Errorf("Test %d expected %v got %v", i+1, test.expected, upstreams)
t.Errorf("Test %d expected %v got %v", i+1, tc.expected, upstreams)
}
}
}