databag/net/server/internal/main_test.go

90 lines
2.2 KiB
Go
Raw Normal View History

2022-01-15 08:07:00 +00:00
package databag
2022-01-11 17:36:16 +00:00
2022-01-12 07:04:27 +00:00
import (
"os"
2022-01-12 07:04:27 +00:00
"testing"
2022-01-12 21:12:40 +00:00
"databag/internal/store"
2022-01-12 07:04:27 +00:00
)
2022-01-11 17:36:16 +00:00
2022-01-15 08:07:00 +00:00
func TestMain(m *testing.M) {
2022-01-12 07:04:27 +00:00
// SetHideLog(true)
2022-01-19 23:03:06 +00:00
SetKeySize(2048)
os.Remove("databag.db")
2022-02-28 22:59:29 +00:00
os.RemoveAll("testdata")
2022-03-01 08:28:36 +00:00
os.RemoveAll("testscripts")
2022-02-28 22:59:29 +00:00
store.SetPath("databag.db")
2022-02-28 22:59:29 +00:00
if err := os.Mkdir("testdata", os.ModePerm); err != nil {
panic("failed to create testdata path")
}
2022-03-01 08:28:36 +00:00
if err := os.Mkdir("testscripts", os.ModePerm); err != nil {
panic("failed to create testscripts path")
}
P01 := []byte("#!/bin/bash\n echo \"P01 $1 $2 $3\"\n")
if err := os.WriteFile("testscripts/P01.sh", P01, 0555); err != nil {
panic("failed to create P01 script")
}
2022-01-15 08:07:00 +00:00
2022-02-08 20:24:42 +00:00
r, w, _ := NewRequest("GET", "/admin/status", nil)
GetNodeStatus(w, r)
2022-01-17 21:27:48 +00:00
var available bool
if ReadResponse(w, &available) != nil {
panic("server not claimable")
2022-01-12 07:04:27 +00:00
}
2022-01-11 17:36:16 +00:00
2022-01-17 22:25:39 +00:00
// claim server
2022-02-08 20:24:42 +00:00
r, w, _ = NewRequest("PUT", "/admin/status", nil)
2022-01-17 21:27:48 +00:00
SetCredentials(r, "admin:pass");
2022-02-08 20:24:42 +00:00
SetNodeStatus(w, r)
2022-01-17 21:27:48 +00:00
if ReadResponse(w, nil) != nil {
panic("failed to claim server")
2022-01-12 07:16:08 +00:00
}
2022-03-01 08:28:36 +00:00
// config data path
scripts := &store.Config{ ConfigId: CONFIG_SCRIPTPATH, StrValue: "./testscripts" }
if err := store.DB.Save(scripts).Error; err != nil {
panic("failed to configure scripts path")
}
2022-02-28 22:59:29 +00:00
// config data path
path := &store.Config{ ConfigId: CONFIG_ASSETPATH, StrValue: "./testdata" }
if err := store.DB.Save(path).Error; err != nil {
2022-03-01 08:28:36 +00:00
panic("failed to configure data path")
2022-02-28 22:59:29 +00:00
}
2022-01-17 22:25:39 +00:00
// config server
2022-01-15 08:07:00 +00:00
config := NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096}
2022-01-17 22:25:39 +00:00
r, w, _ = NewRequest("PUT", "/admin/config", &config)
2022-01-17 21:27:48 +00:00
SetBasicAuth(r, "admin:pass")
SetNodeConfig(w, r)
if ReadResponse(w, nil) != nil {
panic("failed to set config")
2022-01-12 21:12:40 +00:00
}
2022-01-15 05:27:43 +00:00
2022-01-17 22:25:39 +00:00
// check config
r, w, _ = NewRequest("GET", "/admin/config", nil)
2022-01-17 21:27:48 +00:00
SetBasicAuth(r, "admin:pass")
GetNodeConfig(w, r)
2022-01-17 22:25:39 +00:00
var check NodeConfig
if ReadResponse(w, &check) != nil {
2022-01-15 08:07:00 +00:00
panic("failed to get node config")
2022-01-15 05:27:43 +00:00
}
2022-01-17 22:25:39 +00:00
if check.Domain != "example.com" {
2022-01-15 08:07:00 +00:00
panic("failed to set config domain");
2022-01-15 05:27:43 +00:00
}
2022-01-17 22:25:39 +00:00
if check.PublicLimit != 1024 {
2022-01-15 08:07:00 +00:00
panic("failed to set public limit");
2022-01-15 05:27:43 +00:00
}
2022-01-17 22:25:39 +00:00
if check.AccountStorage != 4096 {
2022-01-15 08:07:00 +00:00
panic("failed to set account storage");
2022-01-15 05:27:43 +00:00
}
2022-01-17 07:00:08 +00:00
2022-01-27 19:45:01 +00:00
go SendNotifications()
2022-01-17 22:25:39 +00:00
m.Run()
2022-01-27 19:45:01 +00:00
ExitNotifications()
2022-01-17 07:00:08 +00:00
}
2022-01-17 22:25:39 +00:00