-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
149 lines (121 loc) · 3.02 KB
/
client_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-FileCopyrightText: 2022 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package rescached
import (
"os"
"testing"
"time"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
func TestClient_BlockdEnable(t *testing.T) {
type testCase struct {
desc string
name string
expError string
expBlockd Blockd
}
var (
cases []testCase
c testCase
gotBlockd *Blockd
err error
)
cases = []testCase{{
desc: "With invalid block.d name",
name: "xxx",
expError: "BlockdEnable: 400 hosts block.d name not found: xxx",
}, {
desc: "With valid block.d name",
name: "a.block",
expBlockd: Blockd{
Name: "a.block",
URL: "http://127.0.0.1:11180/hosts/a",
IsEnabled: true,
},
}}
for _, c = range cases {
gotBlockd, err = resc.BlockdEnable(c.name)
if err != nil {
test.Assert(t, "error", c.expError, err.Error())
continue
}
gotBlockd.LastUpdated = ""
test.Assert(t, c.desc, c.expBlockd, *gotBlockd)
}
}
func TestClient_BlockdDisable(t *testing.T) {
type testCase struct {
desc string
name string
expError string
expBlockd Blockd
}
var (
cases []testCase
c testCase
gotBlockd *Blockd
err error
)
cases = []testCase{{
desc: "With invalid block.d name",
name: "xxx",
expError: "BlockdDisable: 400 hosts block.d name not found: xxx",
}, {
desc: "With valid block.d name",
name: "a.block",
expBlockd: Blockd{
Name: "a.block",
URL: "http://127.0.0.1:11180/hosts/a",
IsEnabled: false,
},
}}
for _, c = range cases {
gotBlockd, err = resc.BlockdDisable(c.name)
if err != nil {
test.Assert(t, "error", c.expError, err.Error())
continue
}
gotBlockd.LastUpdated = ""
test.Assert(t, c.desc, c.expBlockd, *gotBlockd)
}
}
func TestClient_BlockdFetch(t *testing.T) {
var (
affectedBlockd = testEnv.HostBlockd["a.block"]
gotBlockd *Blockd
expBlockd *Blockd
expString string
gotBytes []byte
err error
)
// Revert the content of a.block.
t.Cleanup(func() {
err = os.WriteFile(affectedBlockd.fileDisabled, []byte("127.0.0.1 a.block\n"), 0600)
})
expString = `BlockdFetch: 400 httpAPIBlockdFetch: unknown hosts block.d name: xxx`
gotBlockd, err = resc.BlockdFetch("xxx")
if err != nil {
test.Assert(t, "error", expString, err.Error())
} else {
test.Assert(t, "BlockdFetch", expBlockd, gotBlockd)
}
expBlockd = &Blockd{
Name: "a.block",
URL: "http://127.0.0.1:11180/hosts/a",
IsEnabled: false,
}
// Make the block.d last updated less than 7 days ago.
testEnv.HostBlockd["a.block"].lastUpdated = time.Now().Add(-1 * 10 * 24 * time.Hour)
gotBlockd, err = resc.BlockdFetch("a.block")
if err != nil {
t.Fatal(err)
}
expBlockd.LastUpdated = gotBlockd.LastUpdated
test.Assert(t, "BlockdFetch", expBlockd, gotBlockd)
gotBytes, err = os.ReadFile(affectedBlockd.fileDisabled)
if err != nil {
t.Fatal(err)
}
expString = "127.0.0.2 a.block\n"
test.Assert(t, "BlockdFetch", expString, string(gotBytes))
}