fix(instance): never serve a container-id hostname as the instance name
os.Hostname() inside the Maestro container is the bare container id (e.g. 6079761d6e4d) — a hex-only hostname now falls back to 'maestro' so the public profile stays meaningful out of the box. DCNTRLIZED_INSTANCE_NAME and instance.json still win.
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
// 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", func(t *testing.T) {
|
||||
t.Setenv("DCNTRLIZED_INSTANCE_NAME", "edge-1")
|
||||
if id := defaultIdentity(); id.Name != "edge-1" {
|
||||
t.Fatalf("got %q", id.Name)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("hostname used when human-readable", func(t *testing.T) {
|
||||
os.Unsetenv("DCNTRLIZED_INSTANCE_NAME")
|
||||
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")
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user