feat: PostgreSQL as the panel's database, chosen in Настройки, the SQLite data copied over on its first start; tested on a real PostgreSQL
This commit is contained in:
1 parent
055020c3bb
commit
187dc2ae97
3 files changed
+106
-6
No files matched your search
@@ -0,0 +1,85 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/x-grk/longpolling-remote-access/internal/proto"
|
||||
)
|
||||
|
||||
// On a real PostgreSQL (LPRA_TEST_PG: {"host","port","name","user","password",
|
||||
// "sslmode"} of an empty database): the first start copies the SQLite data
|
||||
// over, and the panel's queries work there.
|
||||
func TestPostgres(t *testing.T) {
|
||||
raw := os.Getenv("LPRA_TEST_PG")
|
||||
if raw == "" {
|
||||
t.Skip("LPRA_TEST_PG not set")
|
||||
}
|
||||
var c PGConf
|
||||
if err := json.Unmarshal([]byte(raw), &c); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// SQLite with a user, a machine, a rule, a saved host and some talk.
|
||||
s, _ := newTestServer(t)
|
||||
a, _ := s.db.AdminByUsername("adm")
|
||||
if _, err := s.db.CreateForwardRule(ForwardRule{AgentID: "x1", ClientPort: 80, HostPort: 18080, ListenAddr: "0.0.0.0", Proto: "tcp", NoUfw: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.saveSSHHost(a.ID, savedHost{Label: "web", Host: "h", Port: 22, User: "root", Auth: "password"}, sshSecret{Password: "pw"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.aiAppend(a.ID, "user", "привет")
|
||||
_ = s.db.SetUserAgents(a.ID, []string{"x1"})
|
||||
|
||||
pg, err := OpenPostgres(c, s.db.path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pg.Close()
|
||||
// Clean up for the next run.
|
||||
defer func() {
|
||||
for i := len(copyTables) - 1; i >= 0; i-- {
|
||||
_, _ = pg.sql.Exec(`DROP TABLE IF EXISTS ` + copyTables[i] + ` CASCADE`)
|
||||
}
|
||||
}()
|
||||
p := &Server{db: pg}
|
||||
if got, _ := pg.AdminByUsername("adm"); got == nil || got.ID != a.ID {
|
||||
t.Fatalf("admin not copied: %+v", got)
|
||||
}
|
||||
if ag, _ := pg.GetAgent("x1"); ag == nil {
|
||||
t.Fatal("agent not copied")
|
||||
}
|
||||
rules, _ := pg.ListForwardRules()
|
||||
if len(rules) != 1 || !rules[0].NoUfw {
|
||||
t.Fatalf("rules: %+v", rules)
|
||||
}
|
||||
tg := &proto.SSHTarget{Saved: p.listSSHHosts(a.ID)[0].ID}
|
||||
if err := p.fillSaved(a.ID, tg); err != nil || tg.Password != "pw" {
|
||||
t.Fatalf("saved host: %+v %v", tg, err)
|
||||
}
|
||||
if h := p.aiHistory(a.ID, 10); len(h) != 1 || h[0].Content != "привет" {
|
||||
t.Fatalf("talk: %+v", h)
|
||||
}
|
||||
// New rows go on after the copied ids.
|
||||
id, err := pg.CreateUser("bob", "x", RoleUser)
|
||||
if err != nil || id <= a.ID {
|
||||
t.Fatalf("new user id %d, %v", id, err)
|
||||
}
|
||||
if err := pg.SetConfig("k", "v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pg.SetConfig("k", "v2"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v, _ := pg.GetConfig("k"); v != "v2" {
|
||||
t.Fatalf("config upsert: %q", v)
|
||||
}
|
||||
if err := pg.SetUserAgents(id, []string{"x1", "x1"}); err != nil {
|
||||
t.Fatalf("grants: %v", err)
|
||||
}
|
||||
p.aiAppend(a.ID, "assistant", "ответ")
|
||||
if h := p.aiHistory(a.ID, 10); len(h) != 2 {
|
||||
t.Fatalf("talk after: %+v", h)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user