194 lines
7.7 KiB
Go
194 lines
7.7 KiB
Go
//go:build integration
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/novox/mesh-controller/internal/link"
|
|
"github.com/novox/mesh-controller/internal/testbus"
|
|
)
|
|
|
|
func TestTheTerminalRunsWithoutAVerbAndAnOrdinaryCallNamesMeshCLI(t *testing.T) {
|
|
t.Setenv(echoEnvironment, "1")
|
|
t.Setenv("MESH_VERB", "leaked-from-the-serving-process")
|
|
// The serving controller marks itself (ADR 0266); its terminal line for mesh-cli is still the terminal's.
|
|
t.Setenv(servedVar, "1")
|
|
ctx := context.Background()
|
|
|
|
a := runForMeshCLI(ctx, "control", cliAsked("operator", 1000, "status"), cliVerdict{terminal: true, why: "the terminal"})
|
|
if a.Exit != 0 || a.Refused != "" || !a.Terminal {
|
|
t.Fatalf("the terminal's line did not run: %+v", a)
|
|
}
|
|
if got := string(a.Stdout); !strings.Contains(got, `verb=""`) || !strings.Contains(got, "operator through mesh-cli on control") ||
|
|
!strings.Contains(got, "terminal=true") {
|
|
t.Fatalf("the terminal's line ran with %s", got)
|
|
}
|
|
|
|
a = runForMeshCLI(ctx, "laptop", cliAsked("operator", 1000, "status"), cliVerdict{why: "not the terminal"})
|
|
if a.Exit != 0 || a.Terminal || a.Why != "not the terminal" {
|
|
t.Fatalf("an ordinary line did not run as one: %+v", a)
|
|
}
|
|
if got := string(a.Stdout); !strings.Contains(got, `verb="mesh-cli"`) || !strings.Contains(got, "terminal=false") {
|
|
t.Fatalf("an ordinary line ran with %s", got)
|
|
}
|
|
}
|
|
|
|
func TestAnOrdinaryCallMeetsTheCommandVerbsRefusals(t *testing.T) {
|
|
t.Setenv(echoEnvironment, "1")
|
|
ctx := context.Background()
|
|
ordinary := cliVerdict{why: "not the terminal"}
|
|
a := runForMeshCLI(ctx, "laptop", cliAsked("operator", 1000, "cleanup", "delete", "x"), ordinary)
|
|
if a.Refused == "" || len(a.Stdout) != 0 || a.Exit != 1 || a.Why != "not the terminal" {
|
|
t.Fatalf("a repair without --why ran as an ordinary call: %+v", a)
|
|
}
|
|
a = runForMeshCLI(ctx, "laptop", cliAsked("operator", 1000, "settings", "set", "claude-code", "{}"), ordinary)
|
|
if a.Refused != "" || !strings.Contains(string(a.Stdout), `verb="mesh-cli"`) {
|
|
t.Fatalf("an ordinary settings set did not run through the settings verb's path with MESH_VERB set: %+v", a)
|
|
}
|
|
for _, server := range []string{"serve", "api", "board"} {
|
|
a := runForMeshCLI(ctx, "control", cliAsked("operator", 1000, server), cliVerdict{terminal: true})
|
|
if a.Refused == "" || len(a.Stdout) != 0 {
|
|
t.Fatalf("%s was run for mesh-cli: %+v", server, a)
|
|
}
|
|
}
|
|
a = runForMeshCLI(ctx, "control", cliAsked("agent", 1001, "status"), cliVerdict{refused: "agent is not answered"})
|
|
if a.Refused != "agent is not answered" || len(a.Stdout) != 0 {
|
|
t.Fatalf("a refused line ran: %+v", a)
|
|
}
|
|
}
|
|
|
|
func TestTheControllersModuleIsMovedAtTheTerminalAlone(t *testing.T) {
|
|
t.Setenv("MESH_VERB", "assign")
|
|
ctx := context.Background()
|
|
if _, err := assignWith(ctx, nil, "laptop", assignOptions{}, "zsh", "mesh-controller"); err == nil ||
|
|
!strings.Contains(err.Error(), "terminal") {
|
|
t.Fatalf("assigning the controller through a verb was not refused: %v", err)
|
|
}
|
|
if _, err := assign(ctx, nil, "laptop", "mesh-controller"); err == nil || !strings.Contains(err.Error(), "terminal") {
|
|
t.Fatalf("assigning the controller through a verb was not refused: %v", err)
|
|
}
|
|
t.Setenv("MESH_VERB", "unassign")
|
|
if _, err := unassign(ctx, nil, "control", "mesh-controller"); err == nil || !strings.Contains(err.Error(), "terminal") {
|
|
t.Fatalf("unassigning the controller through a verb was not refused: %v", err)
|
|
}
|
|
// Another module through a verb, and the controller's at the terminal, are not refused for it.
|
|
if err := refusedMovingTheController([]string{"zsh"}); err != nil {
|
|
t.Fatalf("another module was refused: %v", err)
|
|
}
|
|
t.Setenv("MESH_VERB", "")
|
|
if err := refusedMovingTheController([]string{"mesh-controller"}); err != nil {
|
|
t.Fatalf("the terminal was refused: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEveryMeshCLILineIsSaidInTheJournal(t *testing.T) {
|
|
t.Setenv(echoEnvironment, "1")
|
|
var said []string
|
|
was := cliJournal
|
|
cliJournal = func(line string) { said = append(said, line) }
|
|
t.Cleanup(func() { cliJournal = was })
|
|
ctx := link.WithCallID(context.Background(), "call-1")
|
|
runForMeshCLI(ctx, "control", cliAsked("operator", 1000, "settings", "set", "x", `{"password":"s3cret"}`),
|
|
cliVerdict{terminal: true, why: "the terminal"})
|
|
runForMeshCLI(ctx, "control", cliAsked("agent", 1001, "status"), cliVerdict{refused: "agent is not answered"})
|
|
all := strings.Join(said, "\n")
|
|
if len(said) != 2 || !strings.Contains(all, "call-1") || !strings.Contains(all, "operator on control") ||
|
|
!strings.Contains(all, "as the controller's terminal") || !strings.Contains(all, "refused") {
|
|
t.Fatalf("the journal said %q", said)
|
|
}
|
|
if strings.Contains(all, "s3cret") {
|
|
t.Fatalf("the journal carries the line's values: %q", said)
|
|
}
|
|
}
|
|
|
|
func TestAnOrdinaryLineRunsNothingTheCommandVerbWouldRefuse(t *testing.T) {
|
|
t.Setenv(echoEnvironment, "1")
|
|
for _, line := range [][]string{
|
|
{"node", "account", "control", "x"},
|
|
{"node", "agent-account", "control", "x", "--clear"},
|
|
{"token", "issue", "laptop"},
|
|
{"secret", "export", "x"},
|
|
{"operator", "key", "set", "x"},
|
|
{"assign", "laptop", "zsh"},
|
|
} {
|
|
if _, err := ordinaryLine(line); err == nil {
|
|
t.Errorf("%q composed as an ordinary line", line)
|
|
}
|
|
a := runForMeshCLI(context.Background(), "laptop", cliAsked("operator", 1000, line...), cliVerdict{why: "not the terminal"})
|
|
if a.Refused == "" || len(a.Stdout) != 0 {
|
|
t.Errorf("%q ran as an ordinary line: %+v", line, a)
|
|
}
|
|
}
|
|
if argv, err := ordinaryLine([]string{"status"}); err != nil || argv[0] != "status" {
|
|
t.Fatalf("a read was refused: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTheCallsVerbNeverShowsAMeshCLILinesAnswer(t *testing.T) {
|
|
conn, err := nats.Connect(testbus.URL(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
stop, err := link.OverNATS{Conn: conn}.ServeCLI(func(context.Context, string, link.CLIAsked) link.CLIAnswer {
|
|
return link.CLIAnswer{Stdout: []byte("s3cret-join")}
|
|
}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer stop()
|
|
body, _ := json.Marshal(link.CLIAsked{Line: []string{"token", "issue", "x"}, Account: "operator"})
|
|
msg, err := conn.Request(link.CLISubject("control"), body, 5*time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(msg.Data), base64.StdEncoding.EncodeToString([]byte("s3cret-join"))) {
|
|
t.Fatalf("the asker was not given its answer: %s", msg.Data)
|
|
}
|
|
recent, _ := link.Calls.Recent()
|
|
var id string
|
|
for _, c := range recent {
|
|
if c.Seat == link.CLISeat {
|
|
id = c.ID
|
|
break
|
|
}
|
|
}
|
|
shown, err := callsAnswer(link.Calls, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
said, _ := json.Marshal(shown)
|
|
if strings.Contains(string(said), "s3cret-join") ||
|
|
strings.Contains(string(said), base64.StdEncoding.EncodeToString([]byte("s3cret-join"))) {
|
|
t.Fatalf("calls showed a mesh-cli line's answer: %s", said)
|
|
}
|
|
}
|
|
|
|
func TestTheTerminalsMarkIsStrippedFromEveryOtherLine(t *testing.T) {
|
|
t.Setenv(echoEnvironment, "1")
|
|
t.Setenv(cliTerminalVar, "1")
|
|
t.Setenv(servedVar, "1")
|
|
for _, env := range [][]string{commandEnvironment("someone", "status", false)} {
|
|
for _, kv := range env {
|
|
if strings.HasPrefix(kv, cliTerminalVar+"=") {
|
|
t.Fatalf("a verb's line carries the terminal's mark: %s", kv)
|
|
}
|
|
}
|
|
}
|
|
a := runForMeshCLI(context.Background(), "laptop", cliAsked("operator", 1000, "status"), cliVerdict{why: "not the terminal"})
|
|
if got := string(a.Stdout); !strings.Contains(got, "terminal=false") || !strings.Contains(got, `verb="mesh-cli"`) {
|
|
t.Fatalf("an ordinary line with the mark in the serving environment ran as %s", got)
|
|
}
|
|
a = runForMeshCLI(context.Background(), "control", cliAsked("operator", 1000, "status"), cliVerdict{terminal: true})
|
|
if got := string(a.Stdout); !strings.Contains(got, "terminal=true") {
|
|
t.Fatalf("the terminal's line ran as %s", got)
|
|
}
|
|
}
|