mirror of
https://github.com/safing/portmaster
synced 2025-09-02 02:29:12 +00:00
Clean up modules, remove base module, create testing helper
This commit is contained in:
parent
544ede719c
commit
f1a2a4d3e8
7 changed files with 152 additions and 165 deletions
64
core/base.go
64
core/base.go
|
@ -1,64 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
|
|
||||||
"github.com/safing/portbase/config"
|
|
||||||
|
|
||||||
"github.com/safing/portbase/api"
|
|
||||||
"github.com/safing/portbase/database/dbmodule"
|
|
||||||
"github.com/safing/portbase/modules"
|
|
||||||
"github.com/safing/portbase/notifications"
|
|
||||||
|
|
||||||
"github.com/safing/portmaster/core/structure"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
dataDir string
|
|
||||||
databaseDir string
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
flag.StringVar(&dataDir, "data", "", "set data directory")
|
|
||||||
flag.StringVar(&databaseDir, "db", "", "alias to --data (deprecated)")
|
|
||||||
|
|
||||||
modules.Register("base", prepBase, nil, nil, "info")
|
|
||||||
}
|
|
||||||
|
|
||||||
func prepBase() error {
|
|
||||||
// backwards compatibility
|
|
||||||
if dataDir == "" {
|
|
||||||
dataDir = databaseDir
|
|
||||||
}
|
|
||||||
|
|
||||||
// check data dir
|
|
||||||
if dataDir == "" {
|
|
||||||
return errors.New("please set the data directory using --data=/path/to/data/dir")
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize structure
|
|
||||||
err := structure.Initialize(dataDir, 0755)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// set data location
|
|
||||||
dbmodule.SetDatabaseLocation("", structure.Root())
|
|
||||||
config.SetDataRoot(structure.Root())
|
|
||||||
|
|
||||||
// init config
|
|
||||||
logFlagOverrides()
|
|
||||||
err = registerConfig()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// set api listen address
|
|
||||||
api.SetDefaultAPIListenAddress("127.0.0.1:817")
|
|
||||||
|
|
||||||
// set notification persistence
|
|
||||||
notifications.SetPersistenceBasePath("core:notifications")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
modules.Register("core", nil, startCore, nil, "base", "database", "config", "api", "random")
|
modules.Register("core", nil, startCore, nil, "database", "config", "api", "random")
|
||||||
}
|
}
|
||||||
|
|
||||||
func startCore() error {
|
func startCore() error {
|
||||||
|
|
|
@ -3,15 +3,22 @@ package core
|
||||||
import (
|
import (
|
||||||
"github.com/safing/portbase/database"
|
"github.com/safing/portbase/database"
|
||||||
|
|
||||||
|
// database module
|
||||||
|
_ "github.com/safing/portbase/database/dbmodule"
|
||||||
|
|
||||||
// module dependencies
|
// module dependencies
|
||||||
_ "github.com/safing/portbase/database/storage/bbolt"
|
_ "github.com/safing/portbase/database/storage/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultDatabaseStorageType = "bbolt"
|
||||||
|
)
|
||||||
|
|
||||||
func registerDatabases() error {
|
func registerDatabases() error {
|
||||||
_, err := database.Register(&database.Database{
|
_, err := database.Register(&database.Database{
|
||||||
Name: "core",
|
Name: "core",
|
||||||
Description: "Holds core data, such as settings and profiles",
|
Description: "Holds core data, such as settings and profiles",
|
||||||
StorageType: "bbolt",
|
StorageType: DefaultDatabaseStorageType,
|
||||||
PrimaryAPI: "",
|
PrimaryAPI: "",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -21,7 +28,7 @@ func registerDatabases() error {
|
||||||
_, err = database.Register(&database.Database{
|
_, err = database.Register(&database.Database{
|
||||||
Name: "cache",
|
Name: "cache",
|
||||||
Description: "Cached data, such as Intelligence and DNS Records",
|
Description: "Cached data, such as Intelligence and DNS Records",
|
||||||
StorageType: "bbolt",
|
StorageType: DefaultDatabaseStorageType,
|
||||||
PrimaryAPI: "",
|
PrimaryAPI: "",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -31,7 +38,7 @@ func registerDatabases() error {
|
||||||
// _, err = database.Register(&database.Database{
|
// _, err = database.Register(&database.Database{
|
||||||
// Name: "history",
|
// Name: "history",
|
||||||
// Description: "Historic event data",
|
// Description: "Historic event data",
|
||||||
// StorageType: "bbolt",
|
// StorageType: DefaultDatabaseStorageType,
|
||||||
// PrimaryAPI: "",
|
// PrimaryAPI: "",
|
||||||
// })
|
// })
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
|
|
60
core/global.go
Normal file
60
core/global.go
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/api"
|
||||||
|
"github.com/safing/portbase/dataroot"
|
||||||
|
"github.com/safing/portbase/modules"
|
||||||
|
"github.com/safing/portbase/notifications"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
dataDir string
|
||||||
|
databaseDir string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&dataDir, "data", "", "set data directory")
|
||||||
|
flag.StringVar(&databaseDir, "db", "", "alias to --data (deprecated)")
|
||||||
|
|
||||||
|
modules.SetGlobalPrepFn(globalPrep)
|
||||||
|
}
|
||||||
|
|
||||||
|
func globalPrep() error {
|
||||||
|
if dataroot.Root() == nil {
|
||||||
|
// initialize data dir
|
||||||
|
|
||||||
|
// backwards compatibility
|
||||||
|
if dataDir == "" {
|
||||||
|
dataDir = databaseDir
|
||||||
|
}
|
||||||
|
|
||||||
|
// check data dir
|
||||||
|
if dataDir == "" {
|
||||||
|
return errors.New("please set the data directory using --data=/path/to/data/dir")
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize structure
|
||||||
|
err := dataroot.Initialize(dataDir, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// init config
|
||||||
|
logFlagOverrides()
|
||||||
|
err := registerConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// set api listen address
|
||||||
|
api.SetDefaultAPIListenAddress("127.0.0.1:817")
|
||||||
|
|
||||||
|
// set notification persistence
|
||||||
|
notifications.SetPersistenceBasePath("core:notifications")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
81
core/pmtesting/testing.go
Normal file
81
core/pmtesting/testing.go
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
// package coretest provides a simple unit test setup routine.
|
||||||
|
//
|
||||||
|
// Just include `_ "github.com/safing/portmaster/core/pmtesting"`
|
||||||
|
//
|
||||||
|
package pmtesting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime/pprof"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/dataroot"
|
||||||
|
"github.com/safing/portbase/log"
|
||||||
|
"github.com/safing/portbase/modules"
|
||||||
|
"github.com/safing/portmaster/core"
|
||||||
|
|
||||||
|
// module dependencies
|
||||||
|
_ "github.com/safing/portbase/database/storage/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
printStackOnExit bool
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.BoolVar(&printStackOnExit, "print-stack-on-exit", false, "prints the stack before of shutting down")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
// switch databases to memory only
|
||||||
|
core.DefaultDatabaseStorageType = "hashmap"
|
||||||
|
|
||||||
|
// set log level
|
||||||
|
log.SetLogLevel(log.TraceLevel)
|
||||||
|
|
||||||
|
// tmp dir for data root (db & config)
|
||||||
|
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()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// run tests
|
||||||
|
exitCode := m.Run()
|
||||||
|
|
||||||
|
// shutdown
|
||||||
|
_ = modules.Shutdown()
|
||||||
|
if modules.GetExitStatusCode() != 0 {
|
||||||
|
exitCode = modules.GetExitStatusCode()
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to cleanly shutdown test: %s\n", err)
|
||||||
|
}
|
||||||
|
printStack()
|
||||||
|
|
||||||
|
// clean up and exit
|
||||||
|
// keep! os.RemoveAll(tmpDir)
|
||||||
|
os.Exit(exitCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func printStack() {
|
||||||
|
if printStackOnExit {
|
||||||
|
fmt.Println("=== PRINTING TRACES ===")
|
||||||
|
fmt.Println("=== GOROUTINES ===")
|
||||||
|
_ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 2)
|
||||||
|
fmt.Println("=== BLOCKING ===")
|
||||||
|
_ = pprof.Lookup("block").WriteTo(os.Stdout, 2)
|
||||||
|
fmt.Println("=== MUTEXES ===")
|
||||||
|
_ = pprof.Lookup("mutex").WriteTo(os.Stdout, 2)
|
||||||
|
fmt.Println("=== END TRACES ===")
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,27 +0,0 @@
|
||||||
package structure
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/safing/portbase/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
root *utils.DirStructure
|
|
||||||
)
|
|
||||||
|
|
||||||
// Initialize initializes the data root directory
|
|
||||||
func Initialize(rootDir string, perm os.FileMode) error {
|
|
||||||
root = utils.NewDirStructure(rootDir, perm)
|
|
||||||
return root.Ensure()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Root returns the data root directory.
|
|
||||||
func Root() *utils.DirStructure {
|
|
||||||
return root
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRootDir calls ChildDir() on the data root directory.
|
|
||||||
func NewRootDir(dirName string, perm os.FileMode) (childDir *utils.DirStructure) {
|
|
||||||
return root.ChildDir(dirName, perm)
|
|
||||||
}
|
|
|
@ -1,70 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/safing/portbase/log"
|
|
||||||
|
|
||||||
"github.com/safing/portbase/database"
|
|
||||||
|
|
||||||
// module dependencies
|
|
||||||
_ "github.com/safing/portbase/database/storage/hashmap"
|
|
||||||
)
|
|
||||||
|
|
||||||
// InitForTesting initializes the core module directly. This is intended to be only used by unit tests that require the core (and depending) modules.
|
|
||||||
func InitForTesting() (tmpDir string, err error) {
|
|
||||||
tmpDir, err = ioutil.TempDir(os.TempDir(), "pm-testing-")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = database.Initialize(tmpDir, nil)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = database.Register(&database.Database{
|
|
||||||
Name: "core",
|
|
||||||
Description: "Holds core data, such as settings and profiles",
|
|
||||||
StorageType: "hashmap",
|
|
||||||
PrimaryAPI: "",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = database.Register(&database.Database{
|
|
||||||
Name: "cache",
|
|
||||||
Description: "Cached data, such as Intelligence and DNS Records",
|
|
||||||
StorageType: "hashmap",
|
|
||||||
PrimaryAPI: "",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// _, err = database.Register(&database.Database{
|
|
||||||
// Name: "history",
|
|
||||||
// Description: "Historic event data",
|
|
||||||
// StorageType: "hashmap",
|
|
||||||
// PrimaryAPI: "",
|
|
||||||
// })
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// start logging
|
|
||||||
err = log.Start()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
log.SetLogLevel(log.TraceLevel)
|
|
||||||
|
|
||||||
return tmpDir, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// StopTesting shuts the test environment.
|
|
||||||
func StopTesting() {
|
|
||||||
log.Shutdown()
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue