2022-11-30 13:57:55 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
scn "blackforestbytes.com/simplecloudnotifier"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/google"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/jobs"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/logic"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/push"
|
2024-07-16 17:19:55 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
2022-11-30 13:57:55 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Void = struct{}
|
|
|
|
|
2022-12-14 17:02:18 +01:00
|
|
|
func StartSimpleWebserver(t *testing.T) (*logic.Application, string, func()) {
|
2022-11-30 22:59:33 +01:00
|
|
|
InitTests()
|
2022-11-30 13:57:55 +01:00
|
|
|
|
2023-01-06 00:39:21 +01:00
|
|
|
uuid1, _ := langext.NewHexUUID()
|
2022-11-30 13:57:55 +01:00
|
|
|
uuid2, _ := langext.NewHexUUID()
|
2023-01-06 00:39:21 +01:00
|
|
|
uuid3, _ := langext.NewHexUUID()
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
dbdir := t.TempDir()
|
2023-01-06 00:39:21 +01:00
|
|
|
dbfile1 := filepath.Join(dbdir, uuid1+".sqlite3")
|
|
|
|
dbfile2 := filepath.Join(dbdir, uuid2+".sqlite3")
|
|
|
|
dbfile3 := filepath.Join(dbdir, uuid3+".sqlite3")
|
2022-11-30 13:57:55 +01:00
|
|
|
|
|
|
|
err := os.MkdirAll(dbdir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:39:21 +01:00
|
|
|
f1, err := os.Create(dbfile1)
|
2022-11-30 13:57:55 +01:00
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
2023-01-06 00:39:21 +01:00
|
|
|
err = f1.Close()
|
2022-11-30 13:57:55 +01:00
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
2023-01-06 00:39:21 +01:00
|
|
|
err = os.Chmod(dbfile1, 0777)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
f2, err := os.Create(dbfile2)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = f2.Close()
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(dbfile2, 0777)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
f3, err := os.Create(dbfile3)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = f3.Close()
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(dbfile3, 0777)
|
2022-11-30 13:57:55 +01:00
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:39:21 +01:00
|
|
|
TPrintln("DatabaseFile<main>: " + dbfile1)
|
|
|
|
TPrintln("DatabaseFile<requests>: " + dbfile2)
|
|
|
|
TPrintln("DatabaseFile<logs>: " + dbfile3)
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
scn.Conf = CreateTestConfig(t, dbfile1, dbfile2, dbfile3)
|
2022-11-30 13:57:55 +01:00
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
sqlite, err := logic.NewDBPool(scn.Conf)
|
2022-11-30 13:57:55 +01:00
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app := logic.NewApp(sqlite)
|
|
|
|
|
|
|
|
if err := app.Migrate(); err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
ginengine := ginext.NewEngine(ginext.Options{
|
|
|
|
AllowCors: &scn.Conf.Cors,
|
|
|
|
GinDebug: &scn.Conf.GinDebug,
|
|
|
|
BufferBody: langext.PTrue,
|
|
|
|
Timeout: langext.Ptr(time.Duration(int64(scn.Conf.RequestTimeout) * int64(scn.Conf.RequestMaxRetry))),
|
|
|
|
BuildRequestBindError: logic.BuildGinRequestError,
|
|
|
|
})
|
2022-11-30 13:57:55 +01:00
|
|
|
|
|
|
|
router := api.NewRouter(app)
|
|
|
|
|
|
|
|
nc := push.NewTestSink()
|
|
|
|
|
|
|
|
apc := google.NewDummy()
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
app.Init(scn.Conf, ginengine, nc, apc, []logic.Job{
|
2023-01-13 17:17:17 +01:00
|
|
|
jobs.NewDeliveryRetryJob(app),
|
|
|
|
jobs.NewRequestLogCollectorJob(app),
|
|
|
|
})
|
2022-11-30 13:57:55 +01:00
|
|
|
|
2023-01-14 00:48:51 +01:00
|
|
|
err = router.Init(ginengine)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-30 13:57:55 +01:00
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
stop := func() {
|
2023-05-28 12:31:14 +02:00
|
|
|
t.Logf("Stopping App")
|
2022-12-07 22:11:44 +01:00
|
|
|
app.Stop()
|
2023-01-14 00:48:51 +01:00
|
|
|
_ = app.IsRunning.WaitWithTimeout(5*time.Second, false)
|
2023-05-28 12:31:14 +02:00
|
|
|
t.Logf("Stopped App")
|
2023-01-06 00:39:21 +01:00
|
|
|
_ = os.Remove(dbfile1)
|
|
|
|
_ = os.Remove(dbfile2)
|
|
|
|
_ = os.Remove(dbfile3)
|
2022-12-07 22:11:44 +01:00
|
|
|
}
|
2022-11-30 23:46:28 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
go func() { app.Run() }()
|
2022-11-30 21:39:14 +01:00
|
|
|
|
2022-11-30 23:46:28 +01:00
|
|
|
err = app.IsRunning.WaitWithTimeout(100*time.Millisecond, true)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
2022-11-30 21:39:14 +01:00
|
|
|
|
2022-12-14 17:02:18 +01:00
|
|
|
return app, "http://127.0.0.1:" + app.Port, stop
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
2023-07-27 17:44:06 +02:00
|
|
|
|
|
|
|
func StartSimpleTestspace(t *testing.T) (string, string, string, scn.Config, func()) {
|
|
|
|
InitTests()
|
|
|
|
|
|
|
|
uuid1, _ := langext.NewHexUUID()
|
|
|
|
uuid2, _ := langext.NewHexUUID()
|
|
|
|
uuid3, _ := langext.NewHexUUID()
|
|
|
|
|
|
|
|
dbdir := t.TempDir()
|
|
|
|
dbfile1 := filepath.Join(dbdir, uuid1+".sqlite3")
|
|
|
|
dbfile2 := filepath.Join(dbdir, uuid2+".sqlite3")
|
|
|
|
dbfile3 := filepath.Join(dbdir, uuid3+".sqlite3")
|
|
|
|
|
|
|
|
err := os.MkdirAll(dbdir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f1, err := os.Create(dbfile1)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = f1.Close()
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(dbfile1, 0777)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
f2, err := os.Create(dbfile2)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = f2.Close()
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(dbfile2, 0777)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
f3, err := os.Create(dbfile3)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = f3.Close()
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(dbfile3, 0777)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
TPrintln("DatabaseFile<main>: " + dbfile1)
|
|
|
|
TPrintln("DatabaseFile<requests>: " + dbfile2)
|
|
|
|
TPrintln("DatabaseFile<logs>: " + dbfile3)
|
|
|
|
|
|
|
|
scn.Conf = CreateTestConfig(t, dbfile1, dbfile2, dbfile3)
|
|
|
|
|
|
|
|
stop := func() {
|
|
|
|
_ = os.Remove(dbfile1)
|
|
|
|
_ = os.Remove(dbfile2)
|
|
|
|
_ = os.Remove(dbfile3)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbfile1, dbfile2, dbfile3, scn.Conf, stop
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateTestConfig(t *testing.T, dbfile1 string, dbfile2 string, dbfile3 string) scn.Config {
|
|
|
|
conf, ok := scn.GetConfig("local-host")
|
|
|
|
if !ok {
|
|
|
|
TestFail(t, "conf not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.ServerPort = "0" // simply choose a free port
|
|
|
|
conf.DBMain.File = dbfile1
|
|
|
|
conf.DBLogs.File = dbfile2
|
|
|
|
conf.DBRequests.File = dbfile3
|
|
|
|
conf.DBMain.Timeout = 500 * time.Millisecond
|
|
|
|
conf.DBLogs.Timeout = 500 * time.Millisecond
|
|
|
|
conf.DBRequests.Timeout = 500 * time.Millisecond
|
|
|
|
conf.DBMain.ConnMaxLifetime = 1 * time.Second
|
|
|
|
conf.DBLogs.ConnMaxLifetime = 1 * time.Second
|
|
|
|
conf.DBRequests.ConnMaxLifetime = 1 * time.Second
|
|
|
|
conf.DBMain.ConnMaxIdleTime = 1 * time.Second
|
|
|
|
conf.DBLogs.ConnMaxIdleTime = 1 * time.Second
|
|
|
|
conf.DBRequests.ConnMaxIdleTime = 1 * time.Second
|
|
|
|
conf.RequestMaxRetry = 32
|
|
|
|
conf.RequestRetrySleep = 100 * time.Millisecond
|
|
|
|
conf.ReturnRawErrors = true
|
|
|
|
conf.DummyFirebase = true
|
|
|
|
|
|
|
|
return conf
|
|
|
|
}
|