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

@@ -6,6 +6,8 @@ import (
"testing"
"github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database"
_ "github.com/safing/portmaster/base/database/storage/hashmap"
"github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/spn/conf"
)
@@ -14,44 +16,75 @@ type testInstance struct {
config *config.Config
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup {
return nil
}
func (stub *testInstance) Stopping() bool {
return false
}
func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup { return nil }
func (stub *testInstance) Stopping() bool { return false }
func (stub *testInstance) IsShuttingDown() bool { return false }
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func TestMain(m *testing.M) {
instance := &testInstance{}
exitCode := 1
defer func() {
if exitCode != 0 {
os.Exit(exitCode)
}
}()
var err error
// Create a temporary directory for the data
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
fmt.Printf("failed to create temporary data directory: %s", err)
return // Exit with error
}
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the database module
err = database.Initialize(_dataDir)
if err != nil {
fmt.Printf("failed to initialize database module: %s", err)
return // Exit with error
}
_, err = database.Register(&database.Database{
Name: "core",
Description: "Holds core data, such as settings and profiles",
StorageType: "hashmap",
})
if err != nil {
fmt.Printf("failed to register core database: %s", err)
return // Exit with error
}
// Initialize the instance
instance := &testInstance{}
instance.config, err = config.New(instance)
if err != nil {
fmt.Printf("failed to create config module: %s", err)
os.Exit(0)
return // Exit with error
}
module, err = New(instance)
if err != nil {
fmt.Printf("failed to create access module: %s", err)
os.Exit(0)
return // Exit with error
}
err = instance.config.Start()
if err != nil {
fmt.Printf("failed to start config module: %s", err)
os.Exit(0)
return // Exit with error
}
err = module.Start()
if err != nil {
fmt.Printf("failed to start access module: %s", err)
os.Exit(0)
return // Exit with error
}
conf.EnableClient(true)
m.Run()
exitCode = 0 // success
}

View File

@@ -22,31 +22,24 @@ type testInstance struct {
base *base.Base
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup {
return nil
}
func (stub *testInstance) Stopping() bool {
return false
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup { return nil }
func (stub *testInstance) Stopping() bool { return false }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func runTest(m *testing.M) error {
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
// Initialize dataroot
ds, err := config.InitializeUnitTestDataroot("test-cabin")
var err error
// Create a temporary directory for the data
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
defer func() { _ = os.RemoveAll(_dataDir) }()
// Init
instance := &testInstance{}

View File

@@ -41,15 +41,24 @@ func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup {
func (stub *testInstance) Stopping() bool {
return false
}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) DataDir() string {
return _dataDir
}
var _dataDir string
func runTest(m *testing.M) error {
conf.EnablePublicHub(true) // Make hub config available.
ds, err := config.InitializeUnitTestDataroot("test-crew")
var err error
// Create a temporary directory for the data
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
defer func() { _ = os.RemoveAll(_dataDir) }()
instance := &testInstance{}
// Init

View File

@@ -29,31 +29,25 @@ type testInstance struct {
cabin *cabin.Cabin
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Metrics() *metrics.Metrics {
return stub.metrics
}
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup {
return nil
}
func (stub *testInstance) Stopping() bool {
return false
}
func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup { return nil }
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) IsShuttingDown() bool { return false }
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func runTest(m *testing.M) error {
_ = log.Start("info", true, "")
ds, err := config.InitializeUnitTestDataroot("test-docks")
var err error
// Create a temporary directory for the data
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
defer func() { _ = os.RemoveAll(_dataDir) }()
instance := &testInstance{}
runningTests = true

View File

@@ -8,91 +8,37 @@ import (
"github.com/stretchr/testify/assert"
"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/updates"
)
type testInstance struct {
db *dbmodule.DBModule
api *api.API
config *config.Config
updates *updates.Updater
base *base.Base
db *dbmodule.DBModule
base *base.Base
}
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) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Base() *base.Base {
return stub.base
}
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) DataDir() string { return _dataDir }
var _dataDir string
func runTest(m *testing.M) error {
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
ds, err := config.InitializeUnitTestDataroot("test-hub")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
var err error
installDir, err := os.MkdirTemp("", "hub_installdir")
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
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)
return fmt.Errorf("failed to create temporary data directory: %w", err)
}
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the instance with the necessary components
stub := &testInstance{}
// Init
stub.db, err = dbmodule.New(stub)
if err != nil {
return fmt.Errorf("failed to create database: %w", err)
}
stub.api, err = api.New(stub)
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.config, err = config.New(stub)
if err != nil {
return fmt.Errorf("failed to create config: %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)
}
stub.base, err = base.New(stub)
if err != nil {
return fmt.Errorf("failed to base updates: %w", err)
@@ -103,18 +49,6 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("failed to start database: %w", err)
}
err = stub.api.Start()
if err != nil {
return fmt.Errorf("failed to start api: %w", err)
}
err = stub.config.Start()
if err != nil {
return fmt.Errorf("failed to start config: %w", err)
}
err = stub.updates.Start()
if err != nil {
return fmt.Errorf("failed to start updates: %w", err)
}
err = stub.base.Start()
if err != nil {
return fmt.Errorf("failed to start base: %w", err)

View File

@@ -3,6 +3,7 @@ package navigator
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/safing/portmaster/base/api"
@@ -10,68 +11,59 @@ import (
"github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/core/base"
"github.com/safing/portmaster/service/configure"
"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
base *base.Base
geoip *geoip.GeoIP
db *dbmodule.DBModule
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) 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) Base() *base.Base {
return stub.base
}
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-navigator")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %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)
}
// Initialize the instance with the necessary components
stub := &testInstance{}
log.SetLogLevel(log.DebugLevel)
@@ -80,25 +72,14 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("failed to create db: %w", err)
}
stub.api, err = api.New(stub)
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.config, err = config.New(stub)
if err != nil {
return fmt.Errorf("failed to create config: %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)
}
stub.base, err = base.New(stub)
if err != nil {
return fmt.Errorf("failed to create base: %w", err)
}
stub.geoip, err = geoip.New(stub)
if err != nil {
return fmt.Errorf("failed to create geoip: %w", err)
@@ -112,22 +93,14 @@ func runTest(m *testing.M) error {
if err != nil {
return fmt.Errorf("failed to start db module: %w", err)
}
err = stub.api.Start()
if err != nil {
return fmt.Errorf("failed to start api: %w", err)
}
err = stub.config.Start()
if err != nil {
return fmt.Errorf("failed to start config: %w", err)
}
err = stub.updates.Start()
err = stub.intelUpdates.Start()
if err != nil {
return fmt.Errorf("failed to start updates: %w", err)
}
err = stub.base.Start()
if err != nil {
return fmt.Errorf("failed to start base module: %w", err)
}
err = stub.geoip.Start()
if err != nil {
return fmt.Errorf("failed to start geoip module: %w", err)

View File

@@ -22,6 +22,18 @@ type testInstance struct {
rng *rng.Rng
base *base.Base
cabin *cabin.Cabin
dataDir string
}
func (stub *testInstance) DataDir() string {
if len(stub.dataDir) == 0 {
var err error
stub.dataDir, err = os.MkdirTemp("", "")
if err != nil {
panic(fmt.Sprintf("failed to create temp dir: %v", err))
}
}
return stub.dataDir
}
func (stub *testInstance) Config() *config.Config {
@@ -42,11 +54,7 @@ func (stub *testInstance) Stopping() bool {
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func runTest(m *testing.M) error {
ds, err := config.InitializeUnitTestDataroot("test-terminal")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
var err error
conf.EnablePublicHub(true) // Make hub config available.