dhcpd: imp tests
This commit is contained in:
parent
8e919b0ceb
commit
27e5181200
|
@ -22,44 +22,47 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
staticIP := netip.MustParseAddr("192.168.10.10")
|
||||
staticMAC := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
||||
|
||||
conf4 := defaultV4ServerConf()
|
||||
conf4.LeaseDuration = 86400
|
||||
|
||||
serverConf := &ServerConfig{
|
||||
Enabled: true,
|
||||
Conf4: *conf4,
|
||||
WorkDir: t.TempDir(),
|
||||
DBFilePath: dbFilename,
|
||||
ConfigModified: func() {},
|
||||
}
|
||||
|
||||
wantConf := dhcpStatusResponse{
|
||||
IfaceName: "",
|
||||
V4: *conf4,
|
||||
V6: V6ServerConf{},
|
||||
Leases: []*Lease{},
|
||||
StaticLeases: []*Lease{},
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
wantLease := Lease{
|
||||
staticLease := &Lease{
|
||||
Expiry: time.Unix(leaseExpireStatic, 0),
|
||||
Hostname: staticName,
|
||||
HWAddr: staticMAC,
|
||||
IP: staticIP,
|
||||
}
|
||||
|
||||
s, err := Create(serverConf)
|
||||
s, err := Create(&ServerConfig{
|
||||
Enabled: true,
|
||||
Conf4: *defaultV4ServerConf(),
|
||||
WorkDir: t.TempDir(),
|
||||
DBFilePath: dbFilename,
|
||||
ConfigModified: func() {},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("status", func(t *testing.T) {
|
||||
// checkStatus is a helper that asserts the response of
|
||||
// [*server.handleDHCPStatus].
|
||||
checkStatus := func(t *testing.T, want *dhcpStatusResponse) {
|
||||
w := httptest.NewRecorder()
|
||||
var r *http.Request
|
||||
r, err = http.NewRequest(http.MethodGet, "", nil)
|
||||
var req *http.Request
|
||||
req, err = http.NewRequest(http.MethodGet, "", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
wantResp := dhcpStatusResponse{
|
||||
IfaceName: "",
|
||||
b := &bytes.Buffer{}
|
||||
err = json.NewEncoder(b).Encode(&want)
|
||||
require.NoError(t, err)
|
||||
|
||||
s.handleDHCPStatus(w, req)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
assert.JSONEq(t, b.String(), w.Body.String())
|
||||
}
|
||||
|
||||
// defaultResponse is a helper that returs the response with default
|
||||
// configuration.
|
||||
defaultResponse := func() *dhcpStatusResponse {
|
||||
conf4 := defaultV4ServerConf()
|
||||
conf4.LeaseDuration = 86400
|
||||
|
||||
resp := &dhcpStatusResponse{
|
||||
V4: *conf4,
|
||||
V6: V6ServerConf{},
|
||||
Leases: []*Lease{},
|
||||
|
@ -67,21 +70,20 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
Enabled: true,
|
||||
}
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
err = json.NewEncoder(b).Encode(&wantResp)
|
||||
require.NoError(t, err)
|
||||
return resp
|
||||
}
|
||||
|
||||
s.handleDHCPStatus(w, r)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
t.Run("status", func(t *testing.T) {
|
||||
resp := defaultResponse()
|
||||
|
||||
assert.JSONEq(t, b.String(), w.Body.String())
|
||||
checkStatus(t, resp)
|
||||
})
|
||||
|
||||
t.Run("add_static_lease", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
err = json.NewEncoder(b).Encode(&wantLease)
|
||||
err = json.NewEncoder(b).Encode(staticLease)
|
||||
require.NoError(t, err)
|
||||
|
||||
var r *http.Request
|
||||
|
@ -91,16 +93,10 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
s.handleDHCPAddStaticLease(w, r)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
wantConf := wantConf
|
||||
wantConf.StaticLeases = []*Lease{&wantLease}
|
||||
resp := defaultResponse()
|
||||
resp.StaticLeases = []*Lease{staticLease}
|
||||
|
||||
err = json.NewEncoder(b).Encode(&wantConf)
|
||||
require.NoError(t, err)
|
||||
|
||||
s.handleDHCPStatus(w, r)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
assert.JSONEq(t, b.String(), w.Body.String())
|
||||
checkStatus(t, resp)
|
||||
})
|
||||
|
||||
t.Run("add_invalid_lease", func(t *testing.T) {
|
||||
|
@ -108,10 +104,7 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
lease := wantLease
|
||||
lease.IP = netip.Addr{}
|
||||
|
||||
err = json.NewEncoder(b).Encode(&lease)
|
||||
err = json.NewEncoder(b).Encode(&Lease{})
|
||||
require.NoError(t, err)
|
||||
|
||||
var r *http.Request
|
||||
|
@ -120,15 +113,13 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
|
||||
s.handleDHCPAddStaticLease(w, r)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
|
||||
assert.Equal(t, "invalid IP\n", w.Body.String())
|
||||
})
|
||||
|
||||
t.Run("remove_static_lease", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
err = json.NewEncoder(b).Encode(&wantLease)
|
||||
err = json.NewEncoder(b).Encode(staticLease)
|
||||
require.NoError(t, err)
|
||||
|
||||
var r *http.Request
|
||||
|
@ -138,23 +129,19 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
s.handleDHCPRemoveStaticLease(w, r)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
err = json.NewEncoder(b).Encode(&wantConf)
|
||||
require.NoError(t, err)
|
||||
resp := defaultResponse()
|
||||
|
||||
s.handleDHCPStatus(w, r)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
assert.JSONEq(t, b.String(), w.Body.String())
|
||||
checkStatus(t, resp)
|
||||
})
|
||||
|
||||
t.Run("set_config", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
conf := wantConf
|
||||
conf.Enabled = false
|
||||
resp := defaultResponse()
|
||||
resp.Enabled = false
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
err = json.NewEncoder(b).Encode(&conf)
|
||||
err = json.NewEncoder(b).Encode(&resp)
|
||||
require.NoError(t, err)
|
||||
|
||||
var r *http.Request
|
||||
|
@ -163,5 +150,7 @@ func TestServer_handleDHCPStatus(t *testing.T) {
|
|||
|
||||
s.handleDHCPSetConfig(w, r)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
checkStatus(t, resp)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue