package main import ( "bytes" "context" "errors" "net/http" "os" "os/exec" "reflect" "strings" "testing" ) func TestSystemVerbKnowsBoxAndGroup(t *testing.T) { for _, verb := range []string{"box", "group"} { got, rest, _ := systemVerb([]string{verb, "role", "x", "legacy"}) if got != verb || len(rest) != 3 || rest[0] != "role" { t.Fatalf("systemVerb(%s) = %q %q", verb, got, rest) } } } // carlos system box publish-record reaches the box verb, not the console // role call: run in a child on a fleet box, it prints the fleet refusal // (or, as root, the root refusal) and exits 1 before any store, console // or IMDS is touched. func TestSystemBoxPublishRecordDispatch(t *testing.T) { if os.Getenv("CARLOS_TEST_PUBLISH_RECORD_CHILD") == "1" { runSystem([]string{"box", "publish-record"}) os.Exit(0) } cmd := exec.Command(os.Args[0], "-test.run=^TestSystemBoxPublishRecordDispatch$") scratch := t.TempDir() cmd.Dir = scratch cmd.Env = []string{"PATH=" + os.Getenv("PATH"), "HOME=" + scratch, "CARLOS_TEST_PUBLISH_RECORD_CHILD=1", "CARLOS_FLEET=acme", "CARLOS_FLEET_BOX=kass-1"} var stdout, stderr bytes.Buffer cmd.Stdout, cmd.Stderr = &stdout, &stderr err := cmd.Run() var exit *exec.ExitError if !errors.As(err, &exit) || exit.ExitCode() != 1 { t.Fatalf("exit: %v; stdout %q stderr %q", err, stdout.String(), stderr.String()) } want := "this is a fleet box. fleet boxes publish no identity record, so nothing was written" if os.Geteuid() == 0 { // root is refused first (census ruling R1) want = "carlos system box publish-record runs as the agent user, not root. run it with systemd-run -p User=carlos." } if got := strings.TrimSpace(stderr.String()); got != want { t.Fatalf("stderr = %q", got) } } // fakeRolePost answers one role call with status and body, recording what // was asked. type fakeRolePost struct { status int body string path string in any } func (f *fakeRolePost) post(_ context.Context, path string, in any) (int, []byte, error) { f.path, f.in = path, in return f.status, []byte(f.body), nil } func TestSetBoxRole(t *testing.T) { f := &fakeRolePost{status: http.StatusOK, body: `{"box":"i-0abc","role":"legacy"}`} line, err := setBoxRole(context.Background(), f.post, "old box", "legacy") if err != nil || line != "i-0abc now has the legacy role" { t.Fatalf("line %q, %v", line, err) } if f.path != "/api/cli/system/boxes/old%20box/role" || !reflect.DeepEqual(f.in, map[string]string{"role": "legacy"}) { t.Fatalf("asked %s %v", f.path, f.in) } f = &fakeRolePost{status: http.StatusBadRequest, body: `{"error":"role must be default, legacy or platform"}`} if _, err := setBoxRole(context.Background(), f.post, "x", "boss"); err == nil || err.Error() != "role must be default, legacy or platform" { t.Fatalf("refusal: %v", err) } failing := func(context.Context, string, any) (int, []byte, error) { return 0, nil, errors.New("offline") } if _, err := setBoxRole(context.Background(), failing, "x", "legacy"); err == nil { t.Fatal("a failed call printed a line") } } func TestSetGroupRole(t *testing.T) { f := &fakeRolePost{status: http.StatusOK, body: `{"region":"ie","group":"routers","role":"legacy","boxes":["i-1","i-2"]}`} lines, err := setGroupRole(context.Background(), f.post, "ie", "legacy") if err != nil { t.Fatal(err) } want := []string{"the group in ie now has the legacy role", "i-1 now has the legacy role", "i-2 now has the legacy role"} if !reflect.DeepEqual(lines, want) || f.path != "/api/cli/system/groups/ie/role" { t.Fatalf("lines %q path %s", lines, f.path) } f = &fakeRolePost{status: http.StatusOK, body: `{"region":"ie","group":"routers","role":"default","boxes":[]}`} if lines, _ := setGroupRole(context.Background(), f.post, "ie", "default"); len(lines) != 1 { t.Fatalf("an empty group: %q", lines) } }