-
Notifications
You must be signed in to change notification settings - Fork 1
/
rescached_test.go
106 lines (87 loc) · 1.74 KB
/
rescached_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// SPDX-FileCopyrightText: 2022 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package rescached
import (
"log"
"os"
"testing"
"time"
libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http"
)
const (
blockdServerAddress = "127.0.0.1:11180"
)
var (
testEnv *Environment
testServer *Server
resc *Client
)
func TestMain(m *testing.M) {
var (
err error
testStatus int
x int
)
go mockBlockdServer()
testEnv, err = LoadEnvironment("_test", "/etc/rescached/rescached.cfg")
if err != nil {
log.Fatal(err)
}
testServer, err = New(testEnv)
if err != nil {
log.Fatal(err)
}
err = testServer.Start()
if err != nil {
log.Fatal(err)
}
resc = NewClient("http://"+testEnv.WUIListen, false)
// Loop 10 times until server ready for testing.
for x = 0; x < 10; x++ {
time.Sleep(500)
_, err = resc.Env()
if err != nil {
continue
}
break
}
testStatus = m.Run()
testServer.Stop()
os.Exit(testStatus)
}
func mockBlockdServer() {
var (
serverOpts = libhttp.ServerOptions{
Address: blockdServerAddress,
}
epHostsA = libhttp.Endpoint{
Path: "/hosts/a",
Method: libhttp.RequestMethodGet,
RequestType: libhttp.RequestTypeNone,
ResponseType: libhttp.ResponseTypePlain,
Call: func(_ *libhttp.EndpointRequest) ([]byte, error) {
return []byte("127.0.0.2 a.block\n"), nil
},
}
mockServer *libhttp.Server
err error
)
mockServer, err = libhttp.NewServer(serverOpts)
if err != nil {
log.Fatal(err)
}
err = mockServer.RegisterEndpoint(epHostsA)
if err != nil {
log.Fatal(err)
}
defer func() {
err = mockServer.Stop(0)
if err != nil {
log.Fatal(err)
}
}()
err = mockServer.Start()
if err != nil {
log.Println(err)
}
}