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

@@ -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)
}