Remove duplicated TempFile testsuit (#2508)

This commit is contained in:
Xuanwo 2019-01-28 15:25:14 +08:00 committed by Miek Gieben
parent f300fa9bb2
commit ffe79b026e
8 changed files with 25 additions and 31 deletions

View file

@ -28,7 +28,7 @@ var dsTestCases = []mtest.Case{
func TestLookupDS(t *testing.T) { func TestLookupDS(t *testing.T) {
t.Parallel() t.Parallel()
name, rm, err := TempFile(".", miekNL) name, rm, err := mtest.TempFile(".", miekNL)
if err != nil { if err != nil {
t.Fatalf("Failed to create zone: %s", err) t.Fatalf("Failed to create zone: %s", err)
} }

View file

@ -1,19 +0,0 @@
package test
import (
"io/ioutil"
"os"
)
// TempFile will create a temporary file on disk and returns the name and a cleanup function to remove it later.
func TempFile(dir, content string) (string, func(), error) {
f, err := ioutil.TempFile(dir, "go-test-tmpfile")
if err != nil {
return "", nil, err
}
if err := ioutil.WriteFile(f.Name(), []byte(content), 0644); err != nil {
return "", nil, err
}
rmFunc := func() { os.Remove(f.Name()) }
return f.Name(), rmFunc, nil
}

View file

@ -3,13 +3,15 @@ package test
import ( import (
"testing" "testing"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func TestZoneExternalCNAMELookupWithoutProxy(t *testing.T) { func TestZoneExternalCNAMELookupWithoutProxy(t *testing.T) {
t.Parallel() t.Parallel()
name, rm, err := TempFile(".", exampleOrg) name, rm, err := test.TempFile(".", exampleOrg)
if err != nil { if err != nil {
t.Fatalf("Failed to create zone: %s", err) t.Fatalf("Failed to create zone: %s", err)
} }
@ -41,7 +43,7 @@ func TestZoneExternalCNAMELookupWithoutProxy(t *testing.T) {
func TestZoneExternalCNAMELookupWithProxy(t *testing.T) { func TestZoneExternalCNAMELookupWithProxy(t *testing.T) {
t.Parallel() t.Parallel()
name, rm, err := TempFile(".", exampleOrg) name, rm, err := test.TempFile(".", exampleOrg)
if err != nil { if err != nil {
t.Fatalf("Failed to create zone: %s", err) t.Fatalf("Failed to create zone: %s", err)
} }

View file

@ -5,6 +5,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/plugin/file" "github.com/coredns/coredns/plugin/file"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -13,7 +14,7 @@ import (
func TestZoneReload(t *testing.T) { func TestZoneReload(t *testing.T) {
file.TickTime = 1 * time.Second file.TickTime = 1 * time.Second
name, rm, err := TempFile(".", exampleOrg) name, rm, err := test.TempFile(".", exampleOrg)
if err != nil { if err != nil {
t.Fatalf("Failed to create zone: %s", err) t.Fatalf("Failed to create zone: %s", err)
} }

View file

@ -3,13 +3,15 @@ package test
import ( import (
"testing" "testing"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func TestZoneEDNS0Lookup(t *testing.T) { func TestZoneEDNS0Lookup(t *testing.T) {
t.Parallel() t.Parallel()
name, rm, err := TempFile(".", `$ORIGIN example.org. name, rm, err := test.TempFile(".", `$ORIGIN example.org.
@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. ( @ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. (
2017042745 ; serial 2017042745 ; serial
7200 ; refresh (2 hours) 7200 ; refresh (2 hours)
@ -56,7 +58,7 @@ www IN AAAA ::1
func TestZoneNoNS(t *testing.T) { func TestZoneNoNS(t *testing.T) {
t.Parallel() t.Parallel()
name, rm, err := TempFile(".", `$ORIGIN example.org. name, rm, err := test.TempFile(".", `$ORIGIN example.org.
@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. ( @ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. (
2017042745 ; serial 2017042745 ; serial
7200 ; refresh (2 hours) 7200 ; refresh (2 hours)

View file

@ -3,13 +3,15 @@ package test
import ( import (
"testing" "testing"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func TestZoneSRVAdditional(t *testing.T) { func TestZoneSRVAdditional(t *testing.T) {
t.Parallel() t.Parallel()
name, rm, err := TempFile(".", exampleOrg) name, rm, err := test.TempFile(".", exampleOrg)
if err != nil { if err != nil {
t.Fatalf("Failed to create zone: %s", err) t.Fatalf("Failed to create zone: %s", err)
} }

View file

@ -1,10 +1,14 @@
package test package test
import "testing" import (
"testing"
"github.com/coredns/coredns/plugin/test"
)
func TestTempFile(t *testing.T) { func TestTempFile(t *testing.T) {
t.Parallel() t.Parallel()
_, f, e := TempFile(".", "test") _, f, e := test.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

@ -3,11 +3,13 @@ package test
import ( import (
"testing" "testing"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func TestFileUpstream(t *testing.T) { func TestFileUpstream(t *testing.T) {
name, rm, err := TempFile(".", `$ORIGIN example.org. name, rm, err := test.TempFile(".", `$ORIGIN example.org.
@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. ( @ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. (
2017042745 ; serial 2017042745 ; serial
7200 ; refresh (2 hours) 7200 ; refresh (2 hours)
@ -61,7 +63,7 @@ www 3600 IN CNAME www.example.net.
// TestFileUpstreamAdditional runs two CoreDNS servers that serve example.org and foo.example.org. // TestFileUpstreamAdditional runs two CoreDNS servers that serve example.org and foo.example.org.
// example.org contains a cname to foo.example.org; this should be resolved via upstream.Self. // example.org contains a cname to foo.example.org; this should be resolved via upstream.Self.
func TestFileUpstreamAdditional(t *testing.T) { func TestFileUpstreamAdditional(t *testing.T) {
name, rm, err := TempFile(".", `$ORIGIN example.org. name, rm, err := test.TempFile(".", `$ORIGIN example.org.
@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. 2017042745 7200 3600 1209600 3600 @ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. 2017042745 7200 3600 1209600 3600
3600 IN NS b.iana-servers.net. 3600 IN NS b.iana-servers.net.
@ -73,7 +75,7 @@ www 3600 IN CNAME www.foo
} }
defer rm() defer rm()
name2, rm2, err2 := TempFile(".", `$ORIGIN foo.example.org. name2, rm2, err2 := test.TempFile(".", `$ORIGIN foo.example.org.
@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. 2017042745 7200 3600 1209600 3600 @ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. 2017042745 7200 3600 1209600 3600
3600 IN NS b.iana-servers.net. 3600 IN NS b.iana-servers.net.