* Add a setup test for middleware/file

This fix adds a setup test for middleware/file so that there is
a basic coverage for the Corefile processing of middleware/file.

This fix is related to 308 (Will look into it).

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

* middleware/file: use helper function for test

Fixup setup_test.go and use the test.TempFile function to make things
somewhat shorter.

Use clean up the use of testing.T in TempFile - it is not used.
This commit is contained in:
Miek Gieben 2016-10-02 15:58:01 +01:00 committed by GitHub
parent e54c232c8c
commit 4096c4906d
12 changed files with 82 additions and 19 deletions

View file

@ -9,8 +9,8 @@ import (
) )
func TestCacheSet(t *testing.T) { func TestCacheSet(t *testing.T) {
fPriv, rmPriv, _ := test.TempFile(t, ".", privKey) fPriv, rmPriv, _ := test.TempFile(".", privKey)
fPub, rmPub, _ := test.TempFile(t, ".", pubKey) fPub, rmPub, _ := test.TempFile(".", pubKey)
defer rmPriv() defer rmPriv()
defer rmPub() defer rmPub()

View file

@ -32,8 +32,8 @@ func TestZoneSigningDouble(t *testing.T) {
defer rm1() defer rm1()
defer rm2() defer rm2()
fPriv1, rmPriv1, _ := test.TempFile(t, ".", privKey1) fPriv1, rmPriv1, _ := test.TempFile(".", privKey1)
fPub1, rmPub1, _ := test.TempFile(t, ".", pubKey1) fPub1, rmPub1, _ := test.TempFile(".", pubKey1)
defer rmPriv1() defer rmPriv1()
defer rmPub1() defer rmPub1()
@ -57,8 +57,8 @@ func TestZoneSigningDouble(t *testing.T) {
// TestSigningDifferentZone tests if a key for miek.nl and be used for example.org. // TestSigningDifferentZone tests if a key for miek.nl and be used for example.org.
func TestSigningDifferentZone(t *testing.T) { func TestSigningDifferentZone(t *testing.T) {
fPriv, rmPriv, _ := test.TempFile(t, ".", privKey) fPriv, rmPriv, _ := test.TempFile(".", privKey)
fPub, rmPub, _ := test.TempFile(t, ".", pubKey) fPub, rmPub, _ := test.TempFile(".", pubKey)
defer rmPriv() defer rmPriv()
defer rmPub() defer rmPub()
@ -163,8 +163,8 @@ func newDnssec(t *testing.T, zones []string) (Dnssec, func(), func()) {
} }
func newKey(t *testing.T) (*DNSKEY, func(), func()) { func newKey(t *testing.T) (*DNSKEY, func(), func()) {
fPriv, rmPriv, _ := test.TempFile(t, ".", privKey) fPriv, rmPriv, _ := test.TempFile(".", privKey)
fPub, rmPub, _ := test.TempFile(t, ".", pubKey) fPub, rmPub, _ := test.TempFile(".", pubKey)
key, err := ParseKeyFile(fPub, fPriv) key, err := ParseKeyFile(fPub, fPriv)
if err != nil { if err != nil {

View file

@ -11,7 +11,7 @@ import (
) )
func TestZoneReload(t *testing.T) { func TestZoneReload(t *testing.T) {
fileName, rm, err := test.TempFile(t, ".", reloadZoneTest) fileName, rm, err := test.TempFile(".", reloadZoneTest)
if err != nil { if err != nil {
t.Fatalf("failed to create zone: %s", err) t.Fatalf("failed to create zone: %s", err)
} }

View file

@ -0,0 +1,65 @@
package file
import (
"testing"
"github.com/miekg/coredns/middleware/test"
"github.com/mholt/caddy"
)
func TestFileParse(t *testing.T) {
zoneFileName1, rm, err := test.TempFile(".", dbMiekNL)
if err != nil {
t.Fatal(err)
}
defer rm()
zoneFileName2, rm, err := test.TempFile(".", dbDnssexNLSigned)
if err != nil {
t.Fatal(err)
}
defer rm()
tests := []struct {
inputFileRules string
shouldErr bool
expectedZones Zones
}{
{
`file`,
true,
Zones{},
},
{
`file ` + zoneFileName1 + ` miek.nl.`,
false,
Zones{Names: []string{"miek.nl."}},
},
{
`file ` + zoneFileName2 + ` dnssex.nl.`,
false,
Zones{Names: []string{"dnssex.nl."}},
},
}
for i, test := range tests {
c := caddy.NewTestController("file", test.inputFileRules)
actualZones, err := fileParse(c)
if err == nil && test.shouldErr {
t.Fatalf("Test %d expected errors, but got no error", i)
} else if err != nil && !test.shouldErr {
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
} else {
if len(actualZones.Names) != len(test.expectedZones.Names) {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedZones.Names, actualZones.Names)
}
for j, name := range test.expectedZones.Names {
if actualZones.Names[j] != name {
t.Fatalf("Test %d expected %v for %d th zone, got %v", i, name, j, actualZones.Names[j])
}
}
}
}
}

View file

@ -3,11 +3,10 @@ package test
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"testing"
) )
// TempFile will create a temporary file on disk and returns the name and a cleanup function to remove it later. // TempFile will create a temporary file on disk and returns the name and a cleanup function to remove it later.
func TempFile(t *testing.T, dir, content string) (string, func(), error) { func TempFile(dir, content string) (string, func(), error) {
f, err := ioutil.TempFile(dir, "go-test-tmpfile") f, err := ioutil.TempFile(dir, "go-test-tmpfile")
if err != nil { if err != nil {
return "", nil, err return "", nil, err

View file

@ -3,7 +3,7 @@ package test
import "testing" import "testing"
func TestTempFile(t *testing.T) { func TestTempFile(t *testing.T) {
_, f, e := TempFile(t, ".", "test") _, f, e := TempFile(".", "test")
if e != nil { if e != nil {
t.Fatalf("failed to create temp file: %s", e) t.Fatalf("failed to create temp file: %s", e)
} }

View file

@ -17,7 +17,7 @@ import (
func TestLookupCache(t *testing.T) { func TestLookupCache(t *testing.T) {
// Start auth. CoreDNS holding the auth zone. // Start auth. CoreDNS holding the auth zone.
name, rm, err := test.TempFile(t, ".", exampleOrg) name, rm, err := test.TempFile(".", exampleOrg)
if err != nil { if err != nil {
t.Fatalf("failed to created zone: %s", err) t.Fatalf("failed to created zone: %s", err)
} }

View file

@ -3,11 +3,10 @@ package test
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"testing"
) )
// TempFile will create a temporary file on disk and returns the name and a cleanup function to remove it later. // TempFile will create a temporary file on disk and returns the name and a cleanup function to remove it later.
func TempFile(t *testing.T, dir, content string) (string, func(), error) { func TempFile(dir, content string) (string, func(), error) {
f, err := ioutil.TempFile(dir, "go-test-tmpfile") f, err := ioutil.TempFile(dir, "go-test-tmpfile")
if err != nil { if err != nil {
return "", nil, err return "", nil, err

View file

@ -3,7 +3,7 @@ package test
import "testing" import "testing"
func TestTempFile(t *testing.T) { func TestTempFile(t *testing.T) {
_, f, e := TempFile(t, ".", "test") _, f, e := TempFile(".", "test")
if e != nil { if e != nil {
t.Fatalf("failed to create temp file: %s", e) t.Fatalf("failed to create temp file: %s", e)
} }

View file

@ -12,7 +12,7 @@ import (
) )
func TestLookupBalanceRewriteCacheDnssec(t *testing.T) { func TestLookupBalanceRewriteCacheDnssec(t *testing.T) {
name, rm, err := test.TempFile(t, ".", exampleOrg) name, rm, err := test.TempFile(".", exampleOrg)
if err != nil { if err != nil {
t.Fatalf("failed to created zone: %s", err) t.Fatalf("failed to created zone: %s", err)
} }

View file

@ -12,7 +12,7 @@ import (
func benchmarkLookupBalanceRewriteCache(b *testing.B) { func benchmarkLookupBalanceRewriteCache(b *testing.B) {
t := new(testing.T) t := new(testing.T)
name, rm, err := test.TempFile(t, ".", exampleOrg) name, rm, err := test.TempFile(".", exampleOrg)
if err != nil { if err != nil {
t.Fatalf("failed to created zone: %s", err) t.Fatalf("failed to created zone: %s", err)
} }

View file

@ -21,7 +21,7 @@ example.org. IN A 127.0.0.2
` `
func TestLookupProxy(t *testing.T) { func TestLookupProxy(t *testing.T) {
name, rm, err := test.TempFile(t, ".", exampleOrg) name, rm, err := test.TempFile(".", exampleOrg)
if err != nil { if err != nil {
t.Fatalf("failed to created zone: %s", err) t.Fatalf("failed to created zone: %s", err)
} }