v2 tests fix (#1956)

* v2 tests fix
* test: added tests for sqlite
This commit is contained in:
Alexandr Stelnykovych
2025-08-07 02:04:38 +03:00
committed by GitHub
parent 5609594b2a
commit 58f4058633
15 changed files with 347 additions and 447 deletions

View File

@@ -3,67 +3,67 @@ package geoip
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/configure"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates"
)
type testInstance struct {
db *dbmodule.DBModule
api *api.API
config *config.Config
updates *updates.Updater
db *dbmodule.DBModule
api *api.API
config *config.Config
intelUpdates *updates.Updater
}
var _ instance = &testInstance{}
func (stub *testInstance) IntelUpdates() *updates.Updater {
return stub.updates
}
func (stub *testInstance) IntelUpdates() *updates.Updater { return stub.intelUpdates }
func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) Notifications() *notifications.Notifications { return nil }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) BinaryUpdates() *updates.Updater { return nil }
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
func (stub *testInstance) API() *api.API {
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
var _dataDir string
func runTest(m *testing.M) error {
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
ds, err := config.InitializeUnitTestDataroot("test-geoip")
var err error
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
return fmt.Errorf("failed to create temporary data directory: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
installDir, err := os.MkdirTemp("", "geoip_installdir")
if err != nil {
return fmt.Errorf("failed to create tmp install dir: %w", err)
}
defer func() { _ = os.RemoveAll(installDir) }()
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0")
if err != nil {
return fmt.Errorf("failed to generate mock installation: %w", err)
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the Intel update configuration
intelUpdateConfig := updates.Config{
Name: configure.DefaultIntelIndexName,
Directory: filepath.Join(_dataDir, "test_intel"),
DownloadDirectory: filepath.Join(_dataDir, "test_download_intel"),
PurgeDirectory: filepath.Join(_dataDir, "test_upgrade_obsolete_intel"),
IndexURLs: configure.DefaultIntelIndexURLs,
IndexFile: "index.json",
AutoCheck: true,
AutoDownload: true,
AutoApply: true,
}
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
// Initialize the instance with the necessary components
stub := &testInstance{}
stub.db, err = dbmodule.New(stub)
if err != nil {
@@ -77,10 +77,7 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{
Directory: installDir,
IndexFile: "index.json",
})
stub.intelUpdates, err = updates.New(stub, "Intel Updater", intelUpdateConfig)
if err != nil {
return fmt.Errorf("failed to create updates: %w", err)
}
@@ -88,7 +85,6 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("failed to initialize module: %w", err)
}
err = stub.db.Start()
if err != nil {
return fmt.Errorf("Failed to start database: %w", err)
@@ -101,7 +97,7 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("Failed to start api: %w", err)
}
err = stub.updates.Start()
err = stub.intelUpdates.Start()
if err != nil {
return fmt.Errorf("Failed to start updates: %w", err)
}

View File

@@ -21,7 +21,8 @@ func TestLocationLookup(t *testing.T) {
worker.triggerUpdate()
select {
case <-waiter:
case <-time.After(15 * time.Second):
case <-time.After(50 * time.Second):
t.Error("timeout waiting for geoip database to be initialized (updated)")
}
ip1 := net.ParseIP("81.2.69.142")

View File

@@ -3,67 +3,67 @@ package netenv
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/configure"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates"
)
type testInstance struct {
db *dbmodule.DBModule
api *api.API
config *config.Config
updates *updates.Updater
db *dbmodule.DBModule
api *api.API
config *config.Config
intelUpdates *updates.Updater
}
var _ instance = &testInstance{}
func (stub *testInstance) IntelUpdates() *updates.Updater {
return stub.updates
}
func (stub *testInstance) IntelUpdates() *updates.Updater { return stub.intelUpdates }
func (stub *testInstance) API() *api.API { return stub.api }
func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) Notifications() *notifications.Notifications { return nil }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
func (stub *testInstance) API() *api.API {
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
var _dataDir string
func runTest(m *testing.M) error {
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
ds, err := config.InitializeUnitTestDataroot("test-netenv")
var err error
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
return fmt.Errorf("failed to create temporary data directory: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
installDir, err := os.MkdirTemp("", "netenv_installdir")
if err != nil {
return fmt.Errorf("failed to create tmp install dir: %w", err)
}
defer func() { _ = os.RemoveAll(installDir) }()
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0")
if err != nil {
return fmt.Errorf("failed to generate mock installation: %w", err)
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the Intel update configuration
intelUpdateConfig := updates.Config{
Name: configure.DefaultIntelIndexName,
Directory: filepath.Join(_dataDir, "test_intel"),
DownloadDirectory: filepath.Join(_dataDir, "test_download_intel"),
PurgeDirectory: filepath.Join(_dataDir, "test_upgrade_obsolete_intel"),
IndexURLs: configure.DefaultIntelIndexURLs,
IndexFile: "index.json",
AutoCheck: true,
AutoDownload: true,
AutoApply: true,
}
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
// Initialize the instance with the necessary components
stub := &testInstance{}
stub.db, err = dbmodule.New(stub)
if err != nil {
@@ -77,10 +77,7 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{
Directory: installDir,
IndexFile: "index.json",
})
stub.intelUpdates, err = updates.New(stub, "Intel Updater", intelUpdateConfig)
if err != nil {
return fmt.Errorf("failed to create updates: %w", err)
}
@@ -96,7 +93,7 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("Failed to start api: %w", err)
}
err = stub.updates.Start()
err = stub.intelUpdates.Start()
if err != nil {
return fmt.Errorf("Failed to start updates: %w", err)
}

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"testing"
@@ -14,63 +15,62 @@ import (
"github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/configure"
"github.com/safing/portmaster/service/intel"
"github.com/safing/portmaster/service/intel/geoip"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates"
)
type testInstance struct {
db *dbmodule.DBModule
api *api.API
config *config.Config
updates *updates.Updater
geoip *geoip.GeoIP
db *dbmodule.DBModule
api *api.API
config *config.Config
intelUpdates *updates.Updater
geoip *geoip.GeoIP
}
func (stub *testInstance) IntelUpdates() *updates.Updater {
return stub.updates
}
func (stub *testInstance) IntelUpdates() *updates.Updater { return stub.intelUpdates }
func (stub *testInstance) API() *api.API { return stub.api }
func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) Notifications() *notifications.Notifications { return nil }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) BinaryUpdates() *updates.Updater { return nil }
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
func (stub *testInstance) API() *api.API {
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
var _dataDir string
func runTest(m *testing.M) error {
var err error
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to create temporary data directory: %w", err)
}
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the Intel update configuration
intelUpdateConfig := updates.Config{
Name: configure.DefaultIntelIndexName,
Directory: filepath.Join(_dataDir, "test_intel"),
DownloadDirectory: filepath.Join(_dataDir, "test_download_intel"),
PurgeDirectory: filepath.Join(_dataDir, "test_upgrade_obsolete_intel"),
IndexURLs: configure.DefaultIntelIndexURLs,
IndexFile: "index.json",
AutoCheck: true,
AutoDownload: true,
AutoApply: true,
}
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
ds, err := config.InitializeUnitTestDataroot("test-endpoints")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
installDir, err := os.MkdirTemp("", "endpoints_installdir")
if err != nil {
return fmt.Errorf("failed to create tmp install dir: %w", err)
}
defer func() { _ = os.RemoveAll(installDir) }()
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0")
if err != nil {
return fmt.Errorf("failed to generate mock installation: %w", err)
}
// Initialize the instance with the necessary components
stub := &testInstance{}
stub.db, err = dbmodule.New(stub)
if err != nil {
@@ -84,10 +84,7 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{
Directory: installDir,
IndexFile: "index.json",
})
stub.intelUpdates, err = updates.New(stub, "Intel Updater", intelUpdateConfig)
if err != nil {
return fmt.Errorf("failed to create updates: %w", err)
}
@@ -108,7 +105,7 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("Failed to start api: %w", err)
}
err = stub.updates.Start()
err = stub.intelUpdates.Start()
if err != nil {
return fmt.Errorf("Failed to start updates: %w", err)
}

View File

@@ -8,76 +8,50 @@ import (
"github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/core/base"
"github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/netenv"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates"
)
var domainFeed = make(chan string)
type testInstance struct {
db *dbmodule.DBModule
base *base.Base
api *api.API
config *config.Config
updates *updates.Updater
netenv *netenv.NetEnv
db *dbmodule.DBModule
base *base.Base
config *config.Config
netenv *netenv.NetEnv
}
func (stub *testInstance) IntelUpdates() *updates.Updater {
return stub.updates
}
func (stub *testInstance) API() *api.API {
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) NetEnv() *netenv.NetEnv {
return stub.netenv
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) GetEventSPNConnected() *mgr.EventMgr[struct{}] {
return mgr.NewEventMgr[struct{}]("spn connect", nil)
}
func (stub *testInstance) IntelUpdates() *updates.Updater { return nil }
func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) NetEnv() *netenv.NetEnv { return stub.netenv }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func runTest(m *testing.M) error {
var err error
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
fmt.Printf("failed to create temporary data directory: %s", err)
os.Exit(0)
}
defer func() { _ = os.RemoveAll(_dataDir) }()
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
ds, err := config.InitializeUnitTestDataroot("test-resolver")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
installDir, err := os.MkdirTemp("", "resolver_installdir")
if err != nil {
return fmt.Errorf("failed to create tmp install dir: %w", err)
}
defer func() { _ = os.RemoveAll(installDir) }()
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0")
if err != nil {
return fmt.Errorf("failed to generate mock installation: %w", err)
}
// Initialize the instance with the necessary components
stub := &testInstance{}
stub.db, err = dbmodule.New(stub)
if err != nil {
@@ -91,26 +65,14 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("failed to create base: %w", err)
}
stub.api, err = api.New(stub)
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.netenv, err = netenv.New(stub)
if err != nil {
return fmt.Errorf("failed to create netenv: %w", err)
}
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{
Directory: installDir,
IndexFile: "index.json",
})
if err != nil {
return fmt.Errorf("failed to create updates: %w", err)
}
module, err := New(stub)
if err != nil {
return fmt.Errorf("failed to create module: %w", err)
}
err = stub.db.Start()
if err != nil {
return fmt.Errorf("Failed to start database: %w", err)
@@ -123,14 +85,6 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("Failed to start base: %w", err)
}
err = stub.api.Start()
if err != nil {
return fmt.Errorf("Failed to start api: %w", err)
}
err = stub.updates.Start()
if err != nil {
return fmt.Errorf("Failed to start updates: %w", err)
}
err = stub.netenv.Start()
if err != nil {
return fmt.Errorf("Failed to start netenv: %w", err)

View File

@@ -10,22 +10,17 @@ import (
"github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/ui"
)
type testInstance struct{}
func (i *testInstance) Restart() {}
func (i *testInstance) Shutdown() {}
func (i *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (i *testInstance) Ready() bool {
return true
}
func (i *testInstance) SetCmdLineOperation(f func() error) {}
func (i *testInstance) Restart() {}
func (i *testInstance) Shutdown() {}
func (i *testInstance) Notifications() *notifications.Notifications { return nil }
func (i *testInstance) Ready() bool { return true }
func (i *testInstance) SetCmdLineOperation(f func() error) {}
func (i *testInstance) UI() *ui.UI { return nil }
func TestPerformUpdate(t *testing.T) {
t.Parallel()
@@ -39,11 +34,13 @@ func TestPerformUpdate(t *testing.T) {
t.Fatal(err)
}
defer func() { _ = os.RemoveAll(installedDir) }()
updateDir, err := os.MkdirTemp("", "updates_new_")
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.RemoveAll(updateDir) }()
purgeDir, err := os.MkdirTemp("", "updates_purge_")
if err != nil {
t.Fatal(err)
@@ -109,7 +106,7 @@ func TestPerformUpdate(t *testing.T) {
// GenerateMockFolder generates mock index folder for testing.
func GenerateMockFolder(dir, name, version string, published time.Time) error {
// Make sure dir exists
_ = os.MkdirAll(dir, defaultDirMode)
_ = os.MkdirAll(dir, 0o750)
// Create empty files
file, err := os.Create(filepath.Join(dir, "portmaster"))
@@ -147,7 +144,7 @@ func GenerateMockFolder(dir, name, version string, published time.Time) error {
fmt.Fprintf(os.Stderr, "failed to marshal index: %s\n", err)
}
err = os.WriteFile(filepath.Join(dir, "index.json"), indexJSON, defaultFileMode)
err = os.WriteFile(filepath.Join(dir, "index.json"), indexJSON, 0o600)
if err != nil {
return err
}