badguardhome/internal/querylog/search_test.go
Eugene Burkov e113b276e7 Pull request: 2504 querylog interval
Merge in DNS/adguard-home from 2504-querylog-ivl to master

Updates #2504.

Squashed commit of the following:

commit 5d15a6f735cd195fc81c8af909b56fbc7db1fe21
Merge: 8cd5c30d 97073d0d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Jul 1 18:45:10 2021 +0300

    Merge branch 'master' into 2504-querylog-ivl

commit 8cd5c30de6f72d4b12162dbc9e3d90132795fe94
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Jul 1 18:35:50 2021 +0300

    client: fix fmt

commit e95d462c31d886bacec0735acc567fec7c962149
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Jul 1 17:58:06 2021 +0300

    home: imp code

commit 48737b249c52a997a4f34dac45fbaf699477b007
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Jul 1 17:23:18 2021 +0300

    home: imp duration

commit 44f5dc3d3ada5120d74caa24cace9a253b8f15d3
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Jul 1 16:55:31 2021 +0300

    home: imp code, docs

commit bb2826521b7e5d248ce2ab686528219c312b8ba2
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Jul 1 16:11:40 2021 +0300

    all: imp code, docs

commit d688aed1f340807a8bac8807c263956b0fc16f5b
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Jul 1 13:49:42 2021 +0300

    all: change querylog interval setting format
2021-07-01 18:50:28 +03:00

94 lines
1.8 KiB
Go

package querylog
import (
"net"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestQueryLog_Search_findClient(t *testing.T) {
const knownClientID = "client-1"
const knownClientName = "Known Client 1"
const unknownClientID = "client-2"
knownClient := &Client{
Name: knownClientName,
}
findClientCalls := 0
findClient := func(ids []string) (c *Client, _ error) {
defer func() { findClientCalls++ }()
if len(ids) == 0 {
return nil, nil
}
if ids[0] == knownClientID {
return knownClient, nil
}
return nil, nil
}
l := newQueryLog(Config{
FindClient: findClient,
BaseDir: t.TempDir(),
RotationIvl: 24 * time.Hour,
MemSize: 100,
Enabled: true,
FileEnabled: true,
AnonymizeClientIP: false,
})
t.Cleanup(l.Close)
q := &dns.Msg{
Question: []dns.Question{{
Name: "example.com",
}},
}
l.Add(AddParams{
Question: q,
ClientID: knownClientID,
ClientIP: net.IP{1, 2, 3, 4},
})
// Add the same thing again to test the cache.
l.Add(AddParams{
Question: q,
ClientID: knownClientID,
ClientIP: net.IP{1, 2, 3, 4},
})
l.Add(AddParams{
Question: q,
ClientID: unknownClientID,
ClientIP: net.IP{1, 2, 3, 5},
})
sp := &searchParams{
// Add some time to the "current" one to protect against
// low-resolution timers on some Windows machines.
//
// TODO(a.garipov): Use some kind of timeSource interface
// instead of relying on time.Now() in tests.
olderThan: time.Now().Add(10 * time.Second),
limit: 3,
}
entries, _ := l.search(sp)
assert.Equal(t, 2, findClientCalls)
require.Len(t, entries, 3)
assert.Nil(t, entries[0].client)
gotClient := entries[2].client
require.NotNil(t, gotClient)
assert.Equal(t, knownClientName, gotClient.Name)
}