Files
Maestro/src/lib/instance/instance_test.go
T
Admin 17d5307110 [refactor](maestro): retire the PKI and federation identity layer
Contract rev 9 removes the instance CA, client certificates, the peer listener and the trust store fleet-wide. Internal traffic is plaintext HTTP on each service single :8080 listener, so there is no peer identity left to authorize: delete lib/pki, lib/federation, devboot, the per-service database role model and the /api/peers* and /api/instances surfaces, and drop the console Trusted services UI. Config delivery now rides a dedicated ES256 token from lib/configsign rather than mTLS, and the instance fingerprint is derived from a persisted random key and is informational only. main.go also wires the surfaces introduced by the later commits, which this change is what makes possible.
2026-10-19 02:39:15 +01:00

103 lines
3.3 KiB
Go

// Copyright (C) 2026 Dcntrlized
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package instance
import (
"os"
"path/filepath"
"testing"
)
func TestLoadPrefersFileOverEnv(t *testing.T) {
dir := t.TempDir()
if err := Save(dir, Identity{Name: "home-node", Title: "Home"}); err != nil {
t.Fatal(err)
}
t.Setenv("DCNTRLIZED_INSTANCE_NAME", "from-env")
id := Load(dir)
if id.Name != "home-node" || id.Title != "Home" {
t.Fatalf("got %+v", id)
}
}
func TestDefaultIdentityFallbacks(t *testing.T) {
orig := osHostname
defer func() { osHostname = orig }()
t.Run("env wins over domain", func(t *testing.T) {
t.Setenv("DCNTRLIZED_INSTANCE_NAME", "edge-1")
t.Setenv("DCNTRLIZED_INSTANCE_DOMAIN", "maestro.dcntrlized.org")
if id := defaultIdentity(); id.Name != "edge-1" {
t.Fatalf("got %q", id.Name)
}
})
t.Run("announced domain used before hostname", func(t *testing.T) {
t.Setenv("DCNTRLIZED_INSTANCE_DOMAIN", "maestro.dcntrlized.org")
osHostname = func() (string, error) { return "archlinux", nil }
if id := defaultIdentity(); id.Name != "maestro.dcntrlized.org" {
t.Fatalf("got %q", id.Name)
}
})
t.Run("hostname used when human-readable", func(t *testing.T) {
os.Unsetenv("DCNTRLIZED_INSTANCE_NAME")
os.Unsetenv("DCNTRLIZED_INSTANCE_DOMAIN")
osHostname = func() (string, error) { return "archlinux", nil }
if id := defaultIdentity(); id.Name != "archlinux" {
t.Fatalf("got %q", id.Name)
}
})
t.Run("container-id hostname replaced", func(t *testing.T) {
os.Unsetenv("DCNTRLIZED_INSTANCE_NAME")
os.Unsetenv("DCNTRLIZED_INSTANCE_DOMAIN")
for _, host := range []string{"6079761d6e4d", "6079761d6e4dabcdef1234567890abcdef1234567890abcdef1234567890abcd"} {
osHostname = func() (string, error) { return host, nil }
if id := defaultIdentity(); id.Name != "Maestro" {
t.Fatalf("host %q: got %q", host, id.Name)
}
}
})
}
func TestLooksLikeContainerID(t *testing.T) {
cases := map[string]bool{
"6079761d6e4d": true,
"ABCDEF0123456789abcdef0123456789": true,
"archlinux": false,
"maestro-1": false,
"abc": false,
"": false,
}
for s, want := range cases {
if got := looksLikeContainerID(s); got != want {
t.Errorf("looksLikeContainerID(%q) = %v, want %v", s, got, want)
}
}
}
func TestSaveRejectsEmptyName(t *testing.T) {
dir := t.TempDir()
if err := Save(dir, Identity{}); err == nil {
t.Fatal("expected error for empty name")
}
if _, err := os.Stat(filepath.Join(dir, "instance.json")); !os.IsNotExist(err) {
t.Fatal("no file should be written for an invalid identity")
}
}