84 lines
2.8 KiB
Go
84 lines
2.8 KiB
Go
package instance_test
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
catalogdata "git.zaynet.fr/DoGaMa/DoGaMa-serv/catalog"
|
|
"git.zaynet.fr/DoGaMa/DoGaMa-serv/internal/catalog"
|
|
"git.zaynet.fr/DoGaMa/DoGaMa-serv/internal/instance"
|
|
)
|
|
|
|
func TestBuildPreviewIsDeterministicAndRedactsSecrets(t *testing.T) {
|
|
snapshots, err := catalog.LoadFS(catalogdata.Files, ".")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := instance.PreviewRequest{
|
|
DisplayName: "Family Palworld",
|
|
Slug: "family-palworld",
|
|
HostPorts: map[string]int{"game": 8211},
|
|
MountPaths: map[string]string{"saved": filepath.Join(string(filepath.Separator), "srv", "game-servers", "family-palworld", "saved")},
|
|
DataOrigin: "new",
|
|
BackupRetention: 7,
|
|
}
|
|
first, err := instance.BuildPreview(snapshots[0], request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := instance.BuildPreview(snapshots[0], request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first.PlanDigest != second.PlanDigest || first.CanonicalJSON != second.CanonicalJSON {
|
|
t.Fatal("preview is not deterministic")
|
|
}
|
|
for _, setting := range first.Settings {
|
|
if setting.Secret && setting.Default != nil {
|
|
t.Fatalf("secret default leaked: %#v", setting)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildPreviewRejectsPrivatePortPublicationAndLowResources(t *testing.T) {
|
|
snapshots, err := catalog.LoadFS(catalogdata.Files, ".")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
base := instance.PreviewRequest{
|
|
DisplayName: "Family Palworld", Slug: "family-palworld",
|
|
HostPorts: map[string]int{"game": 8211, "rest_api": 8212},
|
|
MountPaths: map[string]string{"saved": "/srv/game-servers/family-palworld/saved"},
|
|
DataOrigin: "new", BackupRetention: 7,
|
|
}
|
|
if _, err := instance.BuildPreview(snapshots[0], base); err == nil {
|
|
t.Fatal("private integration port unexpectedly published")
|
|
}
|
|
base.HostPorts = map[string]int{"game": 8211}
|
|
base.Resources = catalog.Resources{CPUCores: 1, MemoryMB: 128, StorageGB: 1}
|
|
if _, err := instance.BuildPreview(snapshots[0], base); err == nil {
|
|
t.Fatal("below-minimum resources unexpectedly accepted")
|
|
}
|
|
}
|
|
|
|
func TestBuildPreviewRejectsUnknownPortAndMountIDs(t *testing.T) {
|
|
snapshots, err := catalog.LoadFS(catalogdata.Files, ".")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
base := instance.PreviewRequest{
|
|
DisplayName: "Family Palworld", Slug: "family-palworld",
|
|
HostPorts: map[string]int{"game": 8211, "unknown": 8212},
|
|
MountPaths: map[string]string{"saved": "/srv/game-servers/family-palworld/saved"},
|
|
DataOrigin: "new", BackupRetention: 7,
|
|
}
|
|
if _, err := instance.BuildPreview(snapshots[0], base); err == nil {
|
|
t.Fatal("unknown port ID unexpectedly accepted")
|
|
}
|
|
base.HostPorts = map[string]int{"game": 8211}
|
|
base.MountPaths["unknown"] = "/srv/game-servers/family-palworld/unknown"
|
|
if _, err := instance.BuildPreview(snapshots[0], base); err == nil {
|
|
t.Fatal("unknown mount ID unexpectedly accepted")
|
|
}
|
|
}
|