Fix dependency graph and linter errors

This commit is contained in:
Daniel
2020-04-02 17:09:15 +02:00
parent 180f27307c
commit eec0c37101
23 changed files with 164 additions and 61 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/safing/portbase/log"
)
// Configuration Keys
var (
CfgDevModeKey = "core/devMode"
defaultDevMode bool

View File

@@ -3,9 +3,14 @@ package core
import (
"fmt"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/modules/subsystems"
"github.com/safing/portbase/modules"
// module dependencies
_ "github.com/safing/portbase/rng"
_ "github.com/safing/portmaster/status"
_ "github.com/safing/portmaster/ui"
_ "github.com/safing/portmaster/updates"
)
var (
@@ -13,7 +18,9 @@ var (
)
func init() {
module = modules.Register("core", nil, start, nil, "database", "config", "api", "random", "notifications", "subsystems", "ui", "updates", "status")
modules.Register("base", nil, registerDatabases, nil, "database", "config", "random")
module = modules.Register("core", nil, start, nil, "base", "subsystems", "status", "updates", "api", "notifications", "ui")
subsystems.Register(
"core",
"Core",
@@ -29,5 +36,5 @@ func start() error {
return fmt.Errorf("failed to start plattform-specific components: %s", err)
}
return registerDatabases()
return nil
}

View File

@@ -10,6 +10,7 @@ import (
_ "github.com/safing/portbase/database/storage/bbolt"
)
// Default Values (changeable for testing)
var (
DefaultDatabaseStorageType = "bbolt"
)

View File

@@ -12,7 +12,10 @@ import (
"github.com/safing/portbase/notifications"
)
// Default Values (changeable for testing)
var (
DefaultAPIListenAddress = "127.0.0.1:817"
dataDir string
databaseDir string
)
@@ -53,7 +56,7 @@ func globalPrep() error {
}
// set api listen address
api.SetDefaultAPIListenAddress("127.0.0.1:817")
api.SetDefaultAPIListenAddress(DefaultAPIListenAddress)
// set notification persistence
notifications.SetPersistenceBasePath("core:notifications")

View File

@@ -1,6 +1,18 @@
// package coretest provides a simple unit test setup routine.
// Package pmtesting provides a simple unit test setup routine.
//
// Just include `_ "github.com/safing/portmaster/core/pmtesting"`
// Usage:
//
// package name
//
// import (
// "testing"
//
// "github.com/safing/portmaster/core/pmtesting"
// )
//
// func TestMain(m *testing.M) {
// pmtesting.TestMain(m, module)
// }
//
package pmtesting
@@ -29,10 +41,17 @@ func init() {
flag.BoolVar(&printStackOnExit, "print-stack-on-exit", false, "prints the stack before of shutting down")
}
func TestMain(m *testing.M) {
// TestMain provides a simple unit test setup routine.
func TestMain(m *testing.M, module *modules.Module) {
// enable module for testing
module.Enable()
// switch databases to memory only
core.DefaultDatabaseStorageType = "hashmap"
// switch API to high port
core.DefaultAPIListenAddress = "127.0.0.1:10817"
// set log level
log.SetLogLevel(log.TraceLevel)
@@ -40,19 +59,22 @@ func TestMain(m *testing.M) {
tmpDir := filepath.Join(os.TempDir(), "portmaster-testing")
// initialize data dir
err := dataroot.Initialize(tmpDir, 0755)
// start modules
if err == nil {
err = modules.Start()
}
// handle setup error
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup test: %s\n", err)
printStack()
fmt.Fprintf(os.Stderr, "failed to initialize data root: %s\n", err)
os.Exit(1)
}
// run tests
exitCode := m.Run()
// start modules
var exitCode int
err = modules.Start()
if err != nil {
// starting failed
fmt.Fprintf(os.Stderr, "failed to setup test: %s\n", err)
exitCode = 1
} else {
// run tests
exitCode = m.Run()
}
// shutdown
_ = modules.Shutdown()
@@ -63,7 +85,14 @@ func TestMain(m *testing.M) {
printStack()
// clean up and exit
// keep! os.RemoveAll(tmpDir)
// Important: Do not remove tmpDir, as it is used as a cache for updates.
// remove config
_ = os.Remove(filepath.Join(tmpDir, "config.json"))
// remove databases
_ = os.Remove(filepath.Join(tmpDir, "databases.json"))
_ = os.RemoveAll(filepath.Join(tmpDir, "databases"))
os.Exit(exitCode)
}