dhcpd: imp tests

This commit is contained in:
Stanislav Chzhen 2023-03-17 12:14:02 +03:00
parent 8e919b0ceb
commit 27e5181200
1 changed files with 48 additions and 59 deletions

View File

@ -22,44 +22,47 @@ func TestServer_handleDHCPStatus(t *testing.T) {
staticIP := netip.MustParseAddr("192.168.10.10") staticIP := netip.MustParseAddr("192.168.10.10")
staticMAC := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA} staticMAC := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
conf4 := defaultV4ServerConf() staticLease := &Lease{
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{
Expiry: time.Unix(leaseExpireStatic, 0), Expiry: time.Unix(leaseExpireStatic, 0),
Hostname: staticName, Hostname: staticName,
HWAddr: staticMAC, HWAddr: staticMAC,
IP: staticIP, 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) 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() w := httptest.NewRecorder()
var r *http.Request var req *http.Request
r, err = http.NewRequest(http.MethodGet, "", nil) req, err = http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err) require.NoError(t, err)
wantResp := dhcpStatusResponse{ b := &bytes.Buffer{}
IfaceName: "", 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, V4: *conf4,
V6: V6ServerConf{}, V6: V6ServerConf{},
Leases: []*Lease{}, Leases: []*Lease{},
@ -67,21 +70,20 @@ func TestServer_handleDHCPStatus(t *testing.T) {
Enabled: true, Enabled: true,
} }
b := &bytes.Buffer{} return resp
err = json.NewEncoder(b).Encode(&wantResp) }
require.NoError(t, err)
s.handleDHCPStatus(w, r) t.Run("status", func(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code) resp := defaultResponse()
assert.JSONEq(t, b.String(), w.Body.String()) checkStatus(t, resp)
}) })
t.Run("add_static_lease", func(t *testing.T) { t.Run("add_static_lease", func(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
b := &bytes.Buffer{} b := &bytes.Buffer{}
err = json.NewEncoder(b).Encode(&wantLease) err = json.NewEncoder(b).Encode(staticLease)
require.NoError(t, err) require.NoError(t, err)
var r *http.Request var r *http.Request
@ -91,16 +93,10 @@ func TestServer_handleDHCPStatus(t *testing.T) {
s.handleDHCPAddStaticLease(w, r) s.handleDHCPAddStaticLease(w, r)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
wantConf := wantConf resp := defaultResponse()
wantConf.StaticLeases = []*Lease{&wantLease} resp.StaticLeases = []*Lease{staticLease}
err = json.NewEncoder(b).Encode(&wantConf) checkStatus(t, resp)
require.NoError(t, err)
s.handleDHCPStatus(w, r)
assert.Equal(t, http.StatusOK, w.Code)
assert.JSONEq(t, b.String(), w.Body.String())
}) })
t.Run("add_invalid_lease", func(t *testing.T) { t.Run("add_invalid_lease", func(t *testing.T) {
@ -108,10 +104,7 @@ func TestServer_handleDHCPStatus(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
lease := wantLease err = json.NewEncoder(b).Encode(&Lease{})
lease.IP = netip.Addr{}
err = json.NewEncoder(b).Encode(&lease)
require.NoError(t, err) require.NoError(t, err)
var r *http.Request var r *http.Request
@ -120,15 +113,13 @@ func TestServer_handleDHCPStatus(t *testing.T) {
s.handleDHCPAddStaticLease(w, r) s.handleDHCPAddStaticLease(w, r)
assert.Equal(t, http.StatusBadRequest, w.Code) 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) { t.Run("remove_static_lease", func(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
b := &bytes.Buffer{} b := &bytes.Buffer{}
err = json.NewEncoder(b).Encode(&wantLease) err = json.NewEncoder(b).Encode(staticLease)
require.NoError(t, err) require.NoError(t, err)
var r *http.Request var r *http.Request
@ -138,23 +129,19 @@ func TestServer_handleDHCPStatus(t *testing.T) {
s.handleDHCPRemoveStaticLease(w, r) s.handleDHCPRemoveStaticLease(w, r)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
err = json.NewEncoder(b).Encode(&wantConf) resp := defaultResponse()
require.NoError(t, err)
s.handleDHCPStatus(w, r) checkStatus(t, resp)
assert.Equal(t, http.StatusOK, w.Code)
assert.JSONEq(t, b.String(), w.Body.String())
}) })
t.Run("set_config", func(t *testing.T) { t.Run("set_config", func(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
conf := wantConf resp := defaultResponse()
conf.Enabled = false resp.Enabled = false
b := &bytes.Buffer{} b := &bytes.Buffer{}
err = json.NewEncoder(b).Encode(&conf) err = json.NewEncoder(b).Encode(&resp)
require.NoError(t, err) require.NoError(t, err)
var r *http.Request var r *http.Request
@ -163,5 +150,7 @@ func TestServer_handleDHCPStatus(t *testing.T) {
s.handleDHCPSetConfig(w, r) s.handleDHCPSetConfig(w, r)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
checkStatus(t, resp)
}) })
} }