-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
86 lines (70 loc) · 2.28 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"net/http"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/smarthome-go/smarthome/core"
"github.com/smarthome-go/smarthome/core/automation"
"github.com/smarthome-go/smarthome/core/database"
"github.com/smarthome-go/smarthome/core/event"
"github.com/smarthome-go/smarthome/core/homescript"
"github.com/smarthome-go/smarthome/core/scheduler"
"github.com/smarthome-go/smarthome/core/utils"
"github.com/smarthome-go/smarthome/server/api"
"github.com/smarthome-go/smarthome/server/middleware"
"github.com/smarthome-go/smarthome/server/routes"
"github.com/smarthome-go/smarthome/server/templates"
"github.com/smarthome-go/smarthome/services/camera"
"github.com/smarthome-go/smarthome/services/reminder"
)
func TestServer(t *testing.T) {
// Create logger
log := utils.NewLogger(logrus.FatalLevel)
// Initialize module loggers
core.InitLoggers(log)
camera.InitLogger(log)
middleware.InitLogger(log)
api.InitLogger(log)
routes.InitLogger(log)
templates.InitLogger(log)
reminder.InitLogger(log)
// Simulates a typical server startup
// Initialize database, try 5 times before giving up
var dbErr error = nil
for i := 0; i <= 5; i++ {
dbErr = database.Init(database.DatabaseConfig{
Username: "smarthome",
Password: "testing",
Hostname: "localhost",
Database: "smarthome",
Port: 3330,
}, "admin")
if dbErr == nil {
break
} else {
log.Warn("Failed to connect to database, retrying in 2 seconds")
time.Sleep(time.Second * 5)
}
}
serverConfig, found, err := database.GetServerConfiguration()
assert.NoError(t, err)
assert.True(t, found)
// If the connection failed after 5 retries, give up
assert.NoError(t, dbErr)
// Run setup file if it exists
assert.NoError(t, core.RunSetup())
// Always flush old logs
assert.NoError(t, event.FlushOldLogs())
assert.NoError(t, database.SetAutomationSystemActivation(true))
hms := homescript.InitManager()
// Initializes the automation scheduler
assert.NoError(t, automation.InitManager(hms, serverConfig))
// Initializes the normal scheduler
assert.NoError(t, scheduler.InitManager(hms))
r := routes.NewRouter()
middleware.InitWithRandomKey()
assert.NoError(t, templates.LoadTemplates("./web/dist/html/*.html"))
http.Handle("/", r)
}