Move dns test helpers to separate package.

This commit is contained in:
Joachim Bauch 2026-01-12 14:34:44 +01:00
commit 69d63ffd11
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
10 changed files with 235 additions and 52 deletions

View file

@ -19,17 +19,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package dns
package internal
import (
"net"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
logtest "github.com/strukturag/nextcloud-spreed-signaling/log/test"
)
type MockLookup struct {
@ -39,8 +33,7 @@ type MockLookup struct {
ips map[string][]net.IP
}
func NewMockLookupForTest(t *testing.T) *MockLookup {
t.Helper()
func NewMockLookup() *MockLookup {
mock := &MockLookup{
ips: make(map[string][]net.IP),
}
@ -61,7 +54,7 @@ func (m *MockLookup) Get(host string) []net.IP {
return m.ips[host]
}
func (m *MockLookup) lookup(host string) ([]net.IP, error) {
func (m *MockLookup) Lookup(host string) ([]net.IP, error) {
m.RLock()
defer m.RUnlock()
@ -76,23 +69,3 @@ func (m *MockLookup) lookup(host string) ([]net.IP, error) {
return append([]net.IP{}, ips...), nil
}
func NewMonitorForTest(t *testing.T, interval time.Duration, lookup *MockLookup) *Monitor {
t.Helper()
require := require.New(t)
logger := logtest.NewLoggerForTest(t)
var lookupFunc MonitorLookupFunc
if lookup != nil {
lookupFunc = lookup.lookup
}
monitor, err := NewMonitor(logger, interval, lookupFunc)
require.NoError(err)
t.Cleanup(func() {
monitor.Stop()
})
require.NoError(monitor.Start())
return monitor
}

View file

@ -0,0 +1,58 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2026 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package internal
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMockLookup(t *testing.T) {
t.Parallel()
assert := assert.New(t)
host1 := "domain1.invalid"
host2 := "domain2.invalid"
lookup := NewMockLookup()
assert.Empty(lookup.Get(host1))
assert.Empty(lookup.Get(host2))
ips := []net.IP{
net.ParseIP("1.2.3.4"),
}
lookup.Set(host1, ips)
assert.Equal(ips, lookup.Get(host1))
assert.Empty(lookup.Get(host2))
if resolved, err := lookup.Lookup(host1); assert.NoError(err) {
assert.Equal(ips, resolved)
}
var de *net.DNSError
if resolved, err := lookup.Lookup(host2); assert.ErrorAs(err, &de, "expected error, got %+v", resolved) {
assert.True(de.IsNotFound)
assert.Equal(host2, de.Name)
}
}

View file

@ -33,8 +33,30 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/dns/internal"
logtest "github.com/strukturag/nextcloud-spreed-signaling/log/test"
)
func NewMonitorForTest(t *testing.T, interval time.Duration, lookup *internal.MockLookup) *Monitor {
t.Helper()
require := require.New(t)
logger := logtest.NewLoggerForTest(t)
var lookupFunc MonitorLookupFunc
if lookup != nil {
lookupFunc = lookup.Lookup
}
monitor, err := NewMonitor(logger, interval, lookupFunc)
require.NoError(err)
t.Cleanup(func() {
monitor.Stop()
})
require.NoError(monitor.Start())
return monitor
}
type monitorReceiverRecord struct {
all []net.IP
add []net.IP
@ -158,7 +180,7 @@ func (r *monitorReceiver) ExpectNone() {
func TestMonitor(t *testing.T) {
t.Parallel()
lookup := NewMockLookupForTest(t)
lookup := internal.NewMockLookup()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
@ -339,7 +361,7 @@ func (r *deadlockMonitorReceiver) Close() {
func TestMonitorDeadlock(t *testing.T) {
t.Parallel()
lookup := NewMockLookupForTest(t)
lookup := internal.NewMockLookup()
ip1 := net.ParseIP("192.168.0.1")
ip2 := net.ParseIP("192.168.0.2")
lookup.Set("foo", []net.IP{ip1})

59
dns/test/dns.go Normal file
View file

@ -0,0 +1,59 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2025 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/dns"
"github.com/strukturag/nextcloud-spreed-signaling/dns/internal"
logtest "github.com/strukturag/nextcloud-spreed-signaling/log/test"
)
type MockLookup = internal.MockLookup
func NewMockLookup() *MockLookup {
return internal.NewMockLookup()
}
func NewMonitorForTest(t *testing.T, interval time.Duration, lookup *MockLookup) *dns.Monitor {
t.Helper()
require := require.New(t)
logger := logtest.NewLoggerForTest(t)
var lookupFunc dns.MonitorLookupFunc
if lookup != nil {
lookupFunc = lookup.Lookup
}
monitor, err := dns.NewMonitor(logger, interval, lookupFunc)
require.NoError(err)
t.Cleanup(func() {
monitor.Stop()
})
require.NoError(monitor.Start())
return monitor
}

68
dns/test/dns_test.go Normal file
View file

@ -0,0 +1,68 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2026 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package test
import (
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/dns"
)
func TestDnsMonitor(t *testing.T) {
t.Parallel()
require := require.New(t)
assert := assert.New(t)
lookup := NewMockLookup()
ips := []net.IP{
net.ParseIP("1.2.3.4"),
}
lookup.Set("domain.invalid", ips)
monitor := NewMonitorForTest(t, time.Second, lookup)
called := make(chan struct{})
callback1 := func(entry *dns.MonitorEntry, all []net.IP, add []net.IP, keep []net.IP, remove []net.IP) {
defer func() {
called <- struct{}{}
}()
assert.Equal(ips, all)
assert.Equal(ips, add)
assert.Empty(keep)
assert.Empty(remove)
}
entry1, err := monitor.Add("domain.invalid", callback1)
require.NoError(err)
t.Cleanup(func() {
monitor.Remove(entry1)
})
<-called
}

View file

@ -33,6 +33,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/dns"
dnstest "github.com/strukturag/nextcloud-spreed-signaling/dns/test"
"github.com/strukturag/nextcloud-spreed-signaling/etcd"
"github.com/strukturag/nextcloud-spreed-signaling/etcd/etcdtest"
"github.com/strukturag/nextcloud-spreed-signaling/log"
@ -40,8 +41,8 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/test"
)
func NewClientsForTestWithConfig(t *testing.T, config *goconf.ConfigFile, etcdClient etcd.Client, lookup *dns.MockLookup) (*Clients, *dns.Monitor) {
dnsMonitor := dns.NewMonitorForTest(t, time.Hour, lookup) // will be updated manually
func NewClientsForTestWithConfig(t *testing.T, config *goconf.ConfigFile, etcdClient etcd.Client, lookup *dnstest.MockLookup) (*Clients, *dns.Monitor) {
dnsMonitor := dnstest.NewMonitorForTest(t, time.Hour, lookup) // will be updated manually
logger := logtest.NewLoggerForTest(t)
ctx := log.NewLoggerContext(t.Context(), logger)
client, err := NewClients(ctx, config, etcdClient, dnsMonitor, "0.0.0")
@ -53,7 +54,7 @@ func NewClientsForTestWithConfig(t *testing.T, config *goconf.ConfigFile, etcdCl
return client, dnsMonitor
}
func NewClientsForTest(t *testing.T, addr string, lookup *dns.MockLookup) (*Clients, *dns.Monitor) {
func NewClientsForTest(t *testing.T, addr string, lookup *dnstest.MockLookup) (*Clients, *dns.Monitor) {
config := goconf.NewConfigFile()
config.AddOption("grpc", "targets", addr)
config.AddOption("grpc", "dnsdiscovery", "true")
@ -61,7 +62,7 @@ func NewClientsForTest(t *testing.T, addr string, lookup *dns.MockLookup) (*Clie
return NewClientsForTestWithConfig(t, config, nil, lookup)
}
func NewClientsWithEtcdForTest(t *testing.T, embedEtcd *etcdtest.Server, lookup *dns.MockLookup) (*Clients, *dns.Monitor) {
func NewClientsWithEtcdForTest(t *testing.T, embedEtcd *etcdtest.Server, lookup *dnstest.MockLookup) (*Clients, *dns.Monitor) {
config := goconf.NewConfigFile()
config.AddOption("etcd", "endpoints", embedEtcd.URL().String())
@ -95,7 +96,7 @@ func Test_GrpcClients_DnsDiscovery(t *testing.T) { // nolint:paralleltest
test.EnsureNoGoroutinesLeak(t, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
lookup := dns.NewMockLookupForTest(t)
lookup := dnstest.NewMockLookup()
target := "testgrpc:12345"
ip1 := net.ParseIP("192.168.0.1")
ip2 := net.ParseIP("192.168.0.2")
@ -147,7 +148,7 @@ func Test_GrpcClients_DnsDiscovery(t *testing.T) { // nolint:paralleltest
func Test_GrpcClients_DnsDiscoveryInitialFailed(t *testing.T) {
t.Parallel()
assert := assert.New(t)
lookup := dns.NewMockLookupForTest(t)
lookup := dnstest.NewMockLookup()
target := "testgrpc:12345"
ip1 := net.ParseIP("192.168.0.1")
targetWithIp1 := fmt.Sprintf("%s (%s)", target, ip1)

View file

@ -30,6 +30,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/dns"
dnstest "github.com/strukturag/nextcloud-spreed-signaling/dns/test"
"github.com/strukturag/nextcloud-spreed-signaling/etcd"
"github.com/strukturag/nextcloud-spreed-signaling/etcd/etcdtest"
"github.com/strukturag/nextcloud-spreed-signaling/grpc"
@ -37,8 +38,8 @@ import (
logtest "github.com/strukturag/nextcloud-spreed-signaling/log/test"
)
func NewClientsForTestWithConfig(t *testing.T, config *goconf.ConfigFile, etcdClient etcd.Client, lookup *dns.MockLookup) (*grpc.Clients, *dns.Monitor) {
dnsMonitor := dns.NewMonitorForTest(t, time.Hour, lookup) // will be updated manually
func NewClientsForTestWithConfig(t *testing.T, config *goconf.ConfigFile, etcdClient etcd.Client, lookup *dnstest.MockLookup) (*grpc.Clients, *dns.Monitor) {
dnsMonitor := dnstest.NewMonitorForTest(t, time.Hour, lookup) // will be updated manually
logger := logtest.NewLoggerForTest(t)
ctx := log.NewLoggerContext(t.Context(), logger)
client, err := grpc.NewClients(ctx, config, etcdClient, dnsMonitor, "0.0.0")
@ -50,7 +51,7 @@ func NewClientsForTestWithConfig(t *testing.T, config *goconf.ConfigFile, etcdCl
return client, dnsMonitor
}
func NewClientsForTest(t *testing.T, addr string, lookup *dns.MockLookup) (*grpc.Clients, *dns.Monitor) {
func NewClientsForTest(t *testing.T, addr string, lookup *dnstest.MockLookup) (*grpc.Clients, *dns.Monitor) {
config := goconf.NewConfigFile()
config.AddOption("grpc", "targets", addr)
config.AddOption("grpc", "dnsdiscovery", "true")
@ -58,7 +59,7 @@ func NewClientsForTest(t *testing.T, addr string, lookup *dns.MockLookup) (*grpc
return NewClientsForTestWithConfig(t, config, nil, lookup)
}
func NewClientsWithEtcdForTest(t *testing.T, embedEtcd *etcdtest.Server, lookup *dns.MockLookup) (*grpc.Clients, *dns.Monitor) {
func NewClientsWithEtcdForTest(t *testing.T, embedEtcd *etcdtest.Server, lookup *dnstest.MockLookup) (*grpc.Clients, *dns.Monitor) {
config := goconf.NewConfigFile()
config.AddOption("etcd", "endpoints", embedEtcd.URL().String())

View file

@ -31,16 +31,17 @@ import (
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/dns"
dnstest "github.com/strukturag/nextcloud-spreed-signaling/dns/test"
logtest "github.com/strukturag/nextcloud-spreed-signaling/log/test"
)
func newProxyConfigStatic(t *testing.T, proxy McuProxy, dnsDiscovery bool, lookup *dns.MockLookup, urls ...string) (Config, *dns.Monitor) {
func newProxyConfigStatic(t *testing.T, proxy McuProxy, dnsDiscovery bool, lookup *dnstest.MockLookup, urls ...string) (Config, *dns.Monitor) {
cfg := goconf.NewConfigFile()
cfg.AddOption("mcu", "url", strings.Join(urls, " "))
if dnsDiscovery {
cfg.AddOption("mcu", "dnsdiscovery", "true")
}
dnsMonitor := dns.NewMonitorForTest(t, time.Hour, lookup) // will be updated manually
dnsMonitor := dnstest.NewMonitorForTest(t, time.Hour, lookup) // will be updated manually
logger := logtest.NewLoggerForTest(t)
p, err := NewConfigStatic(logger, cfg, proxy, dnsMonitor)
require.NoError(t, err)
@ -78,7 +79,7 @@ func TestProxyConfigStaticSimple(t *testing.T) {
func TestProxyConfigStaticDNS(t *testing.T) {
t.Parallel()
lookup := dns.NewMockLookupForTest(t)
lookup := dnstest.NewMockLookup()
proxy := newMcuProxyForConfig(t)
config, dnsMonitor := newProxyConfigStatic(t, proxy, true, lookup, "https://foo/")
require.NoError(t, config.Start())

View file

@ -40,7 +40,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/dns"
dnstest "github.com/strukturag/nextcloud-spreed-signaling/dns/test"
"github.com/strukturag/nextcloud-spreed-signaling/etcd"
"github.com/strukturag/nextcloud-spreed-signaling/etcd/etcdtest"
"github.com/strukturag/nextcloud-spreed-signaling/geoip"
@ -198,7 +198,7 @@ func Test_sortConnectionsForCountryWithOverride(t *testing.T) {
}
}
func newMcuProxyForTestWithOptions(t *testing.T, options testserver.ProxyTestOptions, idx int, lookup *dns.MockLookup) (*proxySFU, *goconf.ConfigFile) {
func newMcuProxyForTestWithOptions(t *testing.T, options testserver.ProxyTestOptions, idx int, lookup *dnstest.MockLookup) (*proxySFU, *goconf.ConfigFile) {
t.Helper()
require := require.New(t)
if options.Etcd == nil {
@ -286,7 +286,7 @@ func newMcuProxyForTestWithOptions(t *testing.T, options testserver.ProxyTestOpt
return proxy, cfg
}
func newMcuProxyForTestWithServers(t *testing.T, servers []testserver.ProxyTestServer, idx int, lookup *dns.MockLookup) *proxySFU {
func newMcuProxyForTestWithServers(t *testing.T, servers []testserver.ProxyTestServer, idx int, lookup *dnstest.MockLookup) *proxySFU {
t.Helper()
proxy, _ := newMcuProxyForTestWithOptions(t, testserver.ProxyTestOptions{
@ -295,7 +295,7 @@ func newMcuProxyForTestWithServers(t *testing.T, servers []testserver.ProxyTestS
return proxy
}
func newMcuProxyForTest(t *testing.T, idx int, lookup *dns.MockLookup) *proxySFU {
func newMcuProxyForTest(t *testing.T, idx int, lookup *dnstest.MockLookup) *proxySFU {
t.Helper()
server := testserver.NewProxyServerForTest(t, "DE")
@ -381,7 +381,7 @@ func Test_ProxyAddRemoveConnectionsDnsDiscovery(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
lookup := dns.NewMockLookupForTest(t)
lookup := dnstest.NewMockLookup()
server1 := testserver.NewProxyServerForTest(t, "DE")
server1.Start()

View file

@ -36,7 +36,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/dns"
dnstest "github.com/strukturag/nextcloud-spreed-signaling/dns/test"
"github.com/strukturag/nextcloud-spreed-signaling/etcd"
"github.com/strukturag/nextcloud-spreed-signaling/etcd/etcdtest"
grpctest "github.com/strukturag/nextcloud-spreed-signaling/grpc/test"
@ -57,7 +57,7 @@ type ConnectionWaiter interface {
WaitForConnectionsEstablished(ctx context.Context, waitMap map[string]bool) error
}
func NewMcuProxyForTestWithOptions(t *testing.T, options testserver.ProxyTestOptions, idx int, lookup *dns.MockLookup) (sfu.SFU, *goconf.ConfigFile) {
func NewMcuProxyForTestWithOptions(t *testing.T, options testserver.ProxyTestOptions, idx int, lookup *dnstest.MockLookup) (sfu.SFU, *goconf.ConfigFile) {
t.Helper()
require := require.New(t)
require.NotEmpty(options.Servers)