use random ports and test db paths
continuous-integration/drone/push Build was killed Details
continuous-integration/drone/pr Build was killed Details

This commit is contained in:
Elizabeth 2024-04-02 16:51:13 -06:00
parent 52d061e7cc
commit 35a5e9a263
Signed by: simponic
GPG Key ID: 2909B9A7FF6213EE
2 changed files with 36 additions and 28 deletions

View File

@ -2,3 +2,4 @@
hatecomputers.club
Dockerfile
*.db
.drone.yml

View File

@ -2,6 +2,8 @@ package hcdns
import (
"database/sql"
"fmt"
"math/rand"
"os"
"sync"
"testing"
@ -9,17 +11,24 @@ import (
"git.hatecomputers.club/hatecomputers/hatecomputers.club/args"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/database"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/hcdns"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/utils"
"github.com/miekg/dns"
)
const (
testDBPath = "test.db"
address = "127.0.0.1:8353"
dnsPort = 8353
)
func destroy(conn *sql.DB, path string) {
conn.Close()
os.Remove(path)
}
func setup(dbPath string) (*sql.DB, *dns.Server, *sync.WaitGroup) {
testDb := database.MakeConn(&dbPath)
func randomPort() int {
return rand.Intn(3000) + 10000
}
func setup() (*sql.DB, *dns.Server, int, *string, func()) {
randomDb := utils.RandomId()
dnsPort := randomPort()
testDb := database.MakeConn(&randomDb)
database.Migrate(testDb)
testUser := &database.User{
ID: "test",
@ -37,20 +46,21 @@ func setup(dbPath string) (*sql.DB, *dns.Server, *sync.WaitGroup) {
waitGroup.Done()
}()
return testDb, server, &waitGroup
}
address := fmt.Sprintf("127.0.0.1:%d", dnsPort)
return testDb, server, dnsPort, &address, func() {
testDb.Close()
os.Remove(randomDb)
func destroy(conn *sql.DB, path string) {
conn.Close()
os.Remove(path)
server.Shutdown()
waitGroup.Wait()
}
}
func TestWhenCNAMEIsResolved(t *testing.T) {
t.Log("TestWhenCNAMEIsResolved")
testDb, server, _ := setup(testDBPath)
defer destroy(testDb, testDBPath)
defer server.Shutdown()
testDb, _, _, addr, cleanup := setup()
defer cleanup()
cname := &database.DNSRecord{
ID: "1",
@ -79,7 +89,7 @@ func TestWhenCNAMEIsResolved(t *testing.T) {
message := new(dns.Msg)
message.SetQuestion(domain, qtype)
in, _, err := client.Exchange(message, address)
in, _, err := client.Exchange(message, *addr)
if err != nil {
t.Fatal(err)
@ -125,9 +135,8 @@ func TestWhenCNAMEIsResolved(t *testing.T) {
func TestWhenNoRecordNxDomain(t *testing.T) {
t.Log("TestWhenNoRecordNxDomain")
testDb, server, _ := setup(testDBPath)
defer destroy(testDb, testDBPath)
defer server.Shutdown()
_, _, _, addr, cleanup := setup()
defer cleanup()
qtype := dns.TypeA
domain := dns.Fqdn("nonexistant.example.com.")
@ -135,7 +144,7 @@ func TestWhenNoRecordNxDomain(t *testing.T) {
message := new(dns.Msg)
message.SetQuestion(domain, qtype)
in, _, err := client.Exchange(message, address)
in, _, err := client.Exchange(message, *addr)
if err != nil {
t.Fatal(err)
@ -153,9 +162,8 @@ func TestWhenNoRecordNxDomain(t *testing.T) {
func TestWhenUnresolvingCNAME(t *testing.T) {
t.Log("TestWhenUnresolvingCNAME")
testDb, server, _ := setup(testDBPath)
defer destroy(testDb, testDBPath)
defer server.Shutdown()
testDb, _, _, addr, cleanup := setup()
defer cleanup()
cname := &database.DNSRecord{
ID: "1",
@ -174,7 +182,7 @@ func TestWhenUnresolvingCNAME(t *testing.T) {
message := new(dns.Msg)
message.SetQuestion(domain, qtype)
in, _, err := client.Exchange(message, address)
in, _, err := client.Exchange(message, *addr)
if err != nil {
t.Fatal(err)
@ -208,9 +216,8 @@ func TestWhenUnresolvingCNAME(t *testing.T) {
func TestWhenUnresolvingCNAMEWithMaxDepth(t *testing.T) {
t.Log("TestWhenUnresolvingCNAMEWithMaxDepth")
testDb, server, _ := setup(testDBPath)
defer destroy(testDb, testDBPath)
defer server.Shutdown()
testDb, _, _, addr, cleanup := setup()
defer cleanup()
cname := &database.DNSRecord{
ID: "1",
@ -229,7 +236,7 @@ func TestWhenUnresolvingCNAMEWithMaxDepth(t *testing.T) {
message := new(dns.Msg)
message.SetQuestion(domain, qtype)
in, _, err := client.Exchange(message, address)
in, _, err := client.Exchange(message, *addr)
if err != nil {
t.Fatal(err)