| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "os/exec" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestDesktopFileRequestsReportTransportFailureAsUnavailable(t *testing.T) { |
| 12 | if _, err := exec.LookPath(sshExecutable()); err != nil { |
| 13 | t.Skip("SSH unavailable") |
| 14 | } |
| 15 | s := testServer(t) |
| 16 | s.ready = true |
| 17 | |
| 18 | |
| 19 | w := request(s, "GET", "/api/input/files/desktop/requests", "", "", true) |
| 20 | if w.Code != http.StatusServiceUnavailable { |
| 21 | t.Fatalf("transport failure became bad user input: %d %s", w.Code, w.Body.String()) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestDesktopFilePollDistinguishesUnopenedAndBrokenDesktop(t *testing.T) { |
| 26 | bash, err := exec.LookPath("bash") |
| 27 | if err != nil { |
| 28 | t.Skip("bash unavailable") |
| 29 | } |
| 30 | fixture := `systemctl() { |
| 31 | printf '%s\n' "$DESKTOP_STATE" |
| 32 | [ "$DESKTOP_STATE" = active ] |
| 33 | } |
| 34 | ` |
| 35 | for _, state := range []string{"inactive", "activating", "active", "failed", ""} { |
| 36 | t.Run(state, func(t *testing.T) { |
| 37 | |
| 38 | |
| 39 | script := "DESKTOP_STATE='" + state + "'\n" + fixture + desktopFilesPoll + "printf broker-contacted" |
| 40 | cmd := exec.Command(bash, "--noprofile", "--norc", "-s") |
| 41 | cmd.Stdin = strings.NewReader(script) |
| 42 | out, err := cmd.CombinedOutput() |
| 43 | switch state { |
| 44 | case "inactive", "activating": |
| 45 | var result map[string]any |
| 46 | if err != nil || json.Unmarshal(out, &result) != nil || result["available"] != false { |
| 47 | t.Fatalf("unopened desktop became error: %s %v", out, err) |
| 48 | } |
| 49 | case "active": |
| 50 | if err != nil || string(out) != "broker-contacted" { |
| 51 | t.Fatalf("live broker not polled: %s %v", out, err) |
| 52 | } |
| 53 | default: |
| 54 | if err == nil || strings.Contains(string(out), "broker-contacted") { |
| 55 | t.Fatalf("broken service hidden: %s %v", out, err) |
| 56 | } |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | } |