-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdns_test.go
89 lines (74 loc) · 1.44 KB
/
dns_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package model
import (
"fmt"
"net"
"testing"
"time"
bolt "go.etcd.io/bbolt"
)
func TestDomain(t *testing.T) {
dbpath := "test.db"
db, err := bolt.Open(
dbpath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
t.Errorf("can't open db: %v", err)
return
}
defer db.Close()
bname := []byte("dns")
err = deleteBucket(db, bname)
if err != nil {
t.Error(err)
return
}
d, err := createDomain(db, "chat.wgnetwork.")
if err != nil {
t.Error(err)
return
}
d.SetA(
ARecord{TTL: 30, A: net.IPv4(172, 16, 0, 10)},
)
d, err = storeDomain(db, d)
if err != nil {
t.Error(err)
return
}
}
func createDomain(db *bolt.DB, name string) (Domain, error) {
tx, err := db.Begin(true) // writeable tx
if err != nil {
return Domain{}, err
}
defer tx.Rollback()
d := NewDomain(name)
err = d.Store(tx)
if err != nil {
err = fmt.Errorf("can't store domain: %v", err)
return Domain{}, err
}
err = tx.Commit()
if err != nil {
err = fmt.Errorf("can't commit tx: %v", err)
return Domain{}, err
}
return d, nil
}
func storeDomain(db *bolt.DB, d Domain) (Domain, error) {
tx, err := db.Begin(true) // writeable tx
if err != nil {
return Domain{}, err
}
defer tx.Rollback()
err = d.Store(tx)
if err != nil {
err = fmt.Errorf("can't store domain: %v", err)
return Domain{}, err
}
err = tx.Commit()
if err != nil {
err = fmt.Errorf("can't commit tx: %v", err)
return Domain{}, err
}
return d, nil
}