AdGuardHome/internal/dhcpd/migrate_internal_test.go
Eugene Burkov 8bb1aad739 Pull request 2070: 4923 gopacket DHCP vol.4
Merge in DNS/adguard-home from 4923-gopacket-dhcp-vol.4 to master

Updates #4923.

Squashed commit of the following:

commit 4b87258c70ac98b2abb1ac95f7e916e244b3cd08
Merge: 61458864f 9b91a8740
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Nov 16 14:05:34 2023 +0300

    Merge branch 'master' into 4923-gopacket-dhcp-vol.4

commit 61458864f3df7a027e65060a5f0fb516cc7911a7
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Nov 15 18:48:40 2023 +0300

    all: imp code

commit 506a0ab81e76beebb900f86580577563b471e4e2
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 14 15:59:56 2023 +0300

    all: cleanup moving lease

commit 8d218b732662ac4308ed09d28c1bf9f65906d47c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Nov 13 18:13:39 2023 +0300

    all: rm old leases type
2023-11-16 14:14:40 +03:00

72 lines
1.6 KiB
Go

package dhcpd
import (
"encoding/json"
"net/netip"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testData = `[
{"mac":"ESIzRFVm","ip":"AQIDBA==","host":"test1","exp":1},
{"mac":"ZlVEMyIR","ip":"BAMCAQ==","host":"test2","exp":1231231231}
]`
func TestMigrateDB(t *testing.T) {
dir := t.TempDir()
oldLeasesPath := filepath.Join(dir, dbFilename)
dataDirPath := filepath.Join(dir, dataFilename)
err := os.WriteFile(oldLeasesPath, []byte(testData), 0o644)
require.NoError(t, err)
wantLeases := []*dbLease{{
Expiry: time.Unix(1, 0).Format(time.RFC3339),
Hostname: "test1",
HWAddr: "11:22:33:44:55:66",
IP: netip.MustParseAddr("1.2.3.4"),
IsStatic: true,
}, {
Expiry: time.Unix(1231231231, 0).Format(time.RFC3339),
Hostname: "test2",
HWAddr: "66:55:44:33:22:11",
IP: netip.MustParseAddr("4.3.2.1"),
IsStatic: false,
}}
conf := &ServerConfig{
WorkDir: dir,
DataDir: dir,
}
err = migrateDB(conf)
require.NoError(t, err)
_, err = os.Stat(oldLeasesPath)
require.ErrorIs(t, err, os.ErrNotExist)
var data []byte
data, err = os.ReadFile(dataDirPath)
require.NoError(t, err)
dl := &dataLeases{}
err = json.Unmarshal(data, dl)
require.NoError(t, err)
leases := dl.Leases
for i, wantLease := range wantLeases {
assert.Equal(t, wantLease.Hostname, leases[i].Hostname)
assert.Equal(t, wantLease.HWAddr, leases[i].HWAddr)
assert.Equal(t, wantLease.IP, leases[i].IP)
assert.Equal(t, wantLease.IsStatic, leases[i].IsStatic)
require.Equal(t, wantLease.Expiry, leases[i].Expiry)
}
}