package cli import ( "bytes" "os" "path/filepath" "strings" "testing" ) func TestAPeerIsAddedToTheMirrorsUnitOnce(t *testing.T) { path := filepath.Join(t.TempDir(), "llmgw-mirror.service") unit := mirrorUnitFor("/srv/models", ":8082", "http://10.0.0.10:8081", []string{"10.0.0.10"}, nil, "test") if err := os.WriteFile(path, []byte(unit), 0o644); err != nil { t.Fatal(err) } changed, err := addToMirrorUnit(path, "http://10.0.0.12:8082", "10.0.0.12") if err != nil || !changed { t.Fatalf("changed %v %v", changed, err) } b, _ := os.ReadFile(path) if !strings.Contains(string(b), "--peer http://10.0.0.12:8082") || !strings.Contains(string(b), "--api-allow 10.0.0.12") { t.Errorf("unit:\n%s", b) } if changed, _ := addToMirrorUnit(path, "http://10.0.0.12:8082", "10.0.0.12"); changed { t.Error("added twice") } // A mirror whose API is on loopback alone is not opened by the back door. lo := mirrorUnitFor("/srv/models", "127.0.0.1:8082", "http://10.0.0.10:8081", nil, nil, "test") _ = os.WriteFile(path, []byte(lo), 0o644) _, _ = addToMirrorUnit(path, "http://10.0.0.12:8082", "10.0.0.12") if b, _ := os.ReadFile(path); strings.Contains(string(b), "--api-allow") { t.Errorf("a loopback mirror was opened:\n%s", b) } } func TestASecondMirrorIsNamedInTheSettings(t *testing.T) { path := filepath.Join(t.TempDir(), "server.yaml") if err := os.WriteFile(path, []byte("listen: \":8080\"\nmirror:\n api: http://10.0.0.10:8082\n"), 0o600); err != nil { t.Fatal(err) } for range 2 { if err := addMirrorPeerSetting(path, "http://10.0.0.12:8082"); err != nil { t.Fatal(err) } } b, _ := os.ReadFile(path) if strings.Count(string(b), "10.0.0.12:8082") != 1 || !strings.Contains(string(b), "peers") { t.Errorf("server.yaml:\n%s", b) } } func TestTheSetupOffersASecondMirrorOnAStoreMachine(t *testing.T) { in := newDefaultServerInit() f := newInstallForm(&in, "ctl", []string{"10.0.0.5"}, true) f.secondMirror.Choice = 1 if !f.secondMirror.Show() { t.Fatal("not offered with the mirror on this machine") } f.mirrorWhere.Choice = 1 if f.secondMirror.Show() { t.Error("offered with the mirror elsewhere") } f.mirrorWhere.Choice = 0 in.secondMirror = 1 in.patroni = []string{"ubuntu@10.0.0.11", "ubuntu@10.0.0.12"} if got := strings.Join(in.initFlags(), " "); !strings.Contains(got, "--second-mirror 1") { t.Errorf("flags %s", got) } // Without standbys this mirror answers itself alone: the second could not read it. in.standbys = false if err := in.ensureSecondMirror(t.Context()); err == nil || !strings.Contains(err.Error(), "standbys") { t.Errorf("without standbys: %v", err) } } func TestAMirrorOnAMachineOfItsOwnSaysWhatToRunOnAControlPlane(t *testing.T) { var out bytes.Buffer if err := nameMirrorPeer(&out, filepath.Join(t.TempDir(), "server.yaml"), "http://10.0.0.12:8082"); err != nil { t.Fatal(err) } if !strings.Contains(out.String(), "llmgw ctl config set mirror.peers '[http://10.0.0.12:8082]'") { t.Errorf("%s", out.String()) } }