From f1a2a4d3e89ca172695eda8941d325cad0a9fe33 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 20 Mar 2020 23:03:28 +0100 Subject: [PATCH] Clean up modules, remove base module, create testing helper --- core/base.go | 64 ------------------------------- core/core.go | 2 +- core/databases.go | 13 +++++-- core/global.go | 60 +++++++++++++++++++++++++++++ core/pmtesting/testing.go | 81 +++++++++++++++++++++++++++++++++++++++ core/structure/dirs.go | 27 ------------- core/testing.go | 70 --------------------------------- 7 files changed, 152 insertions(+), 165 deletions(-) delete mode 100644 core/base.go create mode 100644 core/global.go create mode 100644 core/pmtesting/testing.go delete mode 100644 core/structure/dirs.go delete mode 100644 core/testing.go diff --git a/core/base.go b/core/base.go deleted file mode 100644 index f28ab29a..00000000 --- a/core/base.go +++ /dev/null @@ -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 -} diff --git a/core/core.go b/core/core.go index 730bfe83..ff48e05a 100644 --- a/core/core.go +++ b/core/core.go @@ -7,7 +7,7 @@ import ( ) 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 { diff --git a/core/databases.go b/core/databases.go index ddb9cf9f..65612e8c 100644 --- a/core/databases.go +++ b/core/databases.go @@ -3,15 +3,22 @@ package core import ( "github.com/safing/portbase/database" + // database module + _ "github.com/safing/portbase/database/dbmodule" + // module dependencies _ "github.com/safing/portbase/database/storage/bbolt" ) +var ( + DefaultDatabaseStorageType = "bbolt" +) + func registerDatabases() error { _, err := database.Register(&database.Database{ Name: "core", Description: "Holds core data, such as settings and profiles", - StorageType: "bbolt", + StorageType: DefaultDatabaseStorageType, PrimaryAPI: "", }) if err != nil { @@ -21,7 +28,7 @@ func registerDatabases() error { _, err = database.Register(&database.Database{ Name: "cache", Description: "Cached data, such as Intelligence and DNS Records", - StorageType: "bbolt", + StorageType: DefaultDatabaseStorageType, PrimaryAPI: "", }) if err != nil { @@ -31,7 +38,7 @@ func registerDatabases() error { // _, err = database.Register(&database.Database{ // Name: "history", // Description: "Historic event data", - // StorageType: "bbolt", + // StorageType: DefaultDatabaseStorageType, // PrimaryAPI: "", // }) // if err != nil { diff --git a/core/global.go b/core/global.go new file mode 100644 index 00000000..776305d4 --- /dev/null +++ b/core/global.go @@ -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 +} diff --git a/core/pmtesting/testing.go b/core/pmtesting/testing.go new file mode 100644 index 00000000..fc5005b2 --- /dev/null +++ b/core/pmtesting/testing.go @@ -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 ===") + } +} diff --git a/core/structure/dirs.go b/core/structure/dirs.go deleted file mode 100644 index c2df06f9..00000000 --- a/core/structure/dirs.go +++ /dev/null @@ -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) -} diff --git a/core/testing.go b/core/testing.go deleted file mode 100644 index 0b37375d..00000000 --- a/core/testing.go +++ /dev/null @@ -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() -}