-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathupdate_test.go
136 lines (120 loc) · 4 KB
/
update_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
package coredns_omada
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func setupTestServer() *httptest.Server {
controllerId := "123bee230c77bbb45d9c8545d04d700a"
siteId := "Default"
pathLogin := fmt.Sprintf("/%s/api/v2/login", controllerId)
pathUsers := fmt.Sprintf("/%s/api/v2/users/current", controllerId)
pathClients := fmt.Sprintf("/%s/api/v2/sites/%s/clients", controllerId, siteId)
pathDevices := fmt.Sprintf("/%s/api/v2/sites/%s/devices", controllerId, siteId)
pathNetworks := fmt.Sprintf("/%s/api/v2/sites/%s/setting/lan/networks", controllerId, siteId)
pathDhcp := fmt.Sprintf("/%s/api/v2/sites/%s/setting/service/dhcp", controllerId, siteId)
responses := map[string]string{
"/api/info": "./test-data/info-response.json",
pathLogin: "./test-data/login-response.json",
pathUsers: "./test-data/users-response.json",
pathClients: "./test-data/clients-response.json",
pathDevices: "./test-data/devices-response.json",
pathNetworks: "./test-data/networks-response.json",
pathDhcp: "./test-data/dhcp-reservation-response.json",
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
responseFile, ok := responses[r.URL.Path]
if !ok {
log.Fatalf("Unexpected request path on mock server: %s", r.URL.Path)
}
response, err := os.ReadFile(responseFile)
if err != nil {
log.Fatal(err)
}
w.WriteHeader(http.StatusOK)
w.Write(response)
}))
return server
}
func TestUpdate(t *testing.T) {
testServer := setupTestServer()
defer testServer.Close()
url := testServer.URL
u := "test"
p := "test"
testOmada, err := NewOmada(context.TODO(), url, u, p)
if err != nil {
t.Fatalf("test failure on 'TestUpdate/NewOmada': %v", err)
}
testOmada.Next = testHandler()
err = testOmada.controllerInit(context.TODO())
if err != nil {
t.Fatalf("test failure on 'TestUpdate/controllerInit': %v", err)
}
testOmada.config.refresh_minutes = 1
testOmada.config.refresh_login_hours = 24
testOmada.config.resolve_clients = true
testOmada.config.resolve_devices = true
testOmada.config.resolve_dhcp_reservations = true
testOmada.config.stale_record_duration, _ = time.ParseDuration("5m")
var sites []string
for s := range testOmada.controller.Sites {
sites = append(sites, s)
}
testOmada.sites = sites
assert.Len(t, testOmada.sites, 1)
err = testOmada.updateZones()
if err != nil {
t.Fatalf("test failure on 'TestUpdate/updateZones': %v", err)
}
assert.Len(t, testOmada.zoneNames, 3)
assert.Len(t, testOmada.zones, 3)
assert.Equal(t, 13, testOmada.zones["omada.home."].Count)
tests := []testCases{
{ // foward resolve: client
qname: "client-001.omada.home.",
qtype: dns.TypeA,
wantAnswer: []string{"client-001.omada.home. 60 IN A 10.0.0.101"},
},
{ // foward resolve: DHCP reservation
qname: "client-01.omada.home.",
qtype: dns.TypeA,
wantAnswer: []string{"client-01.omada.home. 60 IN A 10.0.0.101"},
},
{ // disabled DHCP reservation
qname: "disabled-dhcp-01.omada.home.",
qtype: dns.TypeA,
wantRetCode: dns.RcodeServerFailure,
wantMsgRCode: dns.RcodeServerFailure,
},
{ // fail: non existent client
qname: "client-002.omada.home.",
qtype: dns.TypeA,
wantRetCode: dns.RcodeServerFailure,
wantMsgRCode: dns.RcodeServerFailure,
},
{ // ptr resolve: client
qname: "102.0.0.10.in-addr.arpa.",
qtype: dns.TypePTR,
wantAnswer: []string{"102.0.0.10.in-addr.arpa. 60 IN PTR win10-vm.omada.home."},
},
{ // ptr - DHCP reservation takes priority over client
qname: "101.0.0.10.in-addr.arpa.",
qtype: dns.TypePTR,
wantAnswer: []string{"101.0.0.10.in-addr.arpa. 60 IN PTR client-01.omada.home."},
},
// wildcard dhcp reservation
{
qname: "test.kubernetes.omada.home",
qtype: dns.TypeA,
wantAnswer: []string{"test.kubernetes.omada.home. 60 IN A 10.0.0.150"},
},
}
executeTestCases(t, testOmada, tests)
}