Add module template, update run method

This commit is contained in:
Daniel 2020-02-18 15:41:24 +01:00
parent b4f014574b
commit 3f17d0e7a9
4 changed files with 170 additions and 36 deletions

42
template/module_test.go Normal file
View file

@ -0,0 +1,42 @@
package template
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/safing/portbase/dataroot"
"github.com/safing/portbase/modules"
)
func TestMain(m *testing.M) {
// tmp dir for data root (db & config)
tmpDir, err := ioutil.TempDir("", "portbase-testing-")
// initialize data dir
if err == nil {
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", err)
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", err)
}
// clean up and exit
os.RemoveAll(tmpDir)
os.Exit(exitCode)
}