-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
89 lines (73 loc) · 1.85 KB
/
main_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 main
import (
"context"
"fmt"
"math/rand"
"net"
"strings"
"testing"
"time"
_ "github.com/coredns/coredns/core/plugin"
"github.com/coredns/coredns/coremain"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/suite"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/server/v3/embed"
)
type EmbednsTestSuite struct {
suite.Suite
}
func (suite *EmbednsTestSuite) SetupTest() {
go func() {
cfg := embed.NewConfig()
cfg.LogLevel = "debug"
cfg.LogOutputs = []string{"./etcd.log"}
cfg.Dir = "default.etcd"
_, err := embed.StartEtcd(cfg)
suite.Nil(err)
coremain.Run()
}()
}
func (suite *EmbednsTestSuite) TestARecord() {
ctx := context.Background()
etcdAddresses := "localhost:2379"
cli, err := clientv3.New(
clientv3.Config{
Endpoints: strings.Split(etcdAddresses, ","),
DialTimeout: 3 * time.Second,
Context: ctx,
},
)
suite.Nil(err)
defer cli.Close()
hostname := namesgenerator.GetRandomName(12)
suite.T().Logf("hostname is: %s", hostname)
key := fmt.Sprintf("/coredns/%s/x1", hostname)
hostip := fmt.Sprintf("10.233.%d.%d", rand.Intn(128), rand.Intn(100))
val := fmt.Sprintf(`{"host": "%s","ttl":60}`, hostip)
ctx, cancelFunc := context.WithTimeout(ctx, 3*time.Second)
defer func() {
cancelFunc()
}()
_, err = cli.Put(ctx, key, val)
suite.Nil(err)
// nolint
defer cli.Delete(ctx, key)
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Millisecond * time.Duration(10000),
}
return d.DialContext(ctx, network, "localhost:53")
},
}
ip, err := r.LookupHost(context.Background(), hostname)
suite.Nil(err)
suite.Equal(ip[0], hostip)
}
func (suite *EmbednsTestSuite) TearDownSuite() {
}
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(EmbednsTestSuite))
}