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")