| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
import QUnit from "qunit-tests"; |
| 4 |
|
| 5 |
QUnit.test("simple process", async assert => { |
| 6 |
const resp = await cockpit.spawn(["/bin/sh", "-c", "echo hi"]); |
| 7 |
assert.equal(resp, "hi\n", "returned output"); |
| 8 |
}); |
| 9 |
|
| 10 |
QUnit.test("path", async assert => { |
| 11 |
const resp = await cockpit.spawn(["true"]); |
| 12 |
assert.equal(resp, "", "found executable"); |
| 13 |
}); |
| 14 |
|
| 15 |
QUnit.test("directory", async assert => { |
| 16 |
const resp = await cockpit.spawn(["pwd"], { directory: "/tmp" }); |
| 17 |
assert.equal(resp, "/tmp\n", "was right"); |
| 18 |
}); |
| 19 |
|
| 20 |
QUnit.test("error log", async assert => { |
| 21 |
const resp = await cockpit.spawn(["/bin/sh", "-c", "echo hi; echo yo >&2"]); |
| 22 |
assert.equal(resp, "hi\n", "produced no output"); |
| 23 |
}); |
| 24 |
|
| 25 |
QUnit.test("error output", async assert => { |
| 26 |
const resp = await cockpit.spawn(["/bin/sh", "-c", "echo hi; echo yo >&2"], { err: "out" }); |
| 27 |
assert.equal(resp, "hi\nyo\n", "showed up"); |
| 28 |
}); |
| 29 |
|
| 30 |
QUnit.test("error message", async assert => { |
| 31 |
const proc = cockpit.spawn(["/bin/sh", "-c", "echo hi; echo yo >&2"], { err: "message" }); |
| 32 |
proc.done(function(resp, message) { |
| 33 |
assert.equal(resp, "hi\n", "produced output"); |
| 34 |
assert.equal(message, "yo\n", "produced message"); |
| 35 |
}); |
| 36 |
await proc; |
| 37 |
assert.equal(proc.state(), "resolved", "didn't fail"); |
| 38 |
}); |
| 39 |
|
| 40 |
QUnit.test("error message fail", async assert => { |
| 41 |
const proc = cockpit.spawn(["/bin/sh", "-c", "echo hi; echo yo >&2; exit 2"], { err: "message" }); |
| 42 |
proc.fail(function(ex, resp) { |
| 43 |
assert.equal(resp, "hi\n", "produced output"); |
| 44 |
assert.equal(ex.message, "yo", "produced message"); |
| 45 |
}); |
| 46 |
await assert.rejects(proc); |
| 47 |
assert.equal(proc.state(), "rejected", "did fail"); |
| 48 |
}); |
| 49 |
|
| 50 |
QUnit.test("nonexisting executable", assert => { |
| 51 |
assert.rejects(cockpit.spawn(["/bin/nonexistent"]), |
| 52 |
(ex: cockpit.BasicError) => ex.problem == "not-found"); |
| 53 |
}); |
| 54 |
|
| 55 |
QUnit.test("permission denied", assert => { |
| 56 |
assert.rejects(cockpit.spawn(["/etc/hostname"]), |
| 57 |
(ex: cockpit.BasicError) => ex.problem == "access-denied"); |
| 58 |
}); |
| 59 |
|
| 60 |
QUnit.test("write eof read", async assert => { |
| 61 |
const proc = cockpit.spawn(["/usr/bin/sort"]); |
| 62 |
proc.input("2\n", true); |
| 63 |
proc.input("3\n1\n"); |
| 64 |
assert.equal(await proc, "1\n2\n3\n", "output"); |
| 65 |
}); |
| 66 |
|
| 67 |
QUnit.test("stream", async assert => { |
| 68 |
let streamed = 0; |
| 69 |
let result = ""; |
| 70 |
const resp = await cockpit.spawn(["/bin/cat"]) |
| 71 |
.input("11\n", true) |
| 72 |
.input("22\n", true) |
| 73 |
.input("33\n") |
| 74 |
.stream(resp => { |
| 75 |
result += resp; |
| 76 |
streamed += 1; |
| 77 |
}); |
| 78 |
assert.equal(resp, "", "no then data"); |
| 79 |
assert.equal(result, "11\n22\n33\n", "stream data"); |
| 80 |
assert.ok(streamed > 0, "stream handler called"); |
| 81 |
}); |
| 82 |
|
| 83 |
QUnit.test("stream packets", async assert => { |
| 84 |
let streamed = ""; |
| 85 |
const resp = await cockpit.spawn(["/bin/cat"]) |
| 86 |
.input("11\n", true) |
| 87 |
.input("22\n", true) |
| 88 |
.input("33\n") |
| 89 |
.stream(resp => { streamed += resp }); |
| 90 |
|
| 91 |
assert.equal(resp, "", "no then data"); |
| 92 |
assert.equal(streamed, "11\n22\n33\n", "stream data"); |
| 93 |
}); |
| 94 |
|
| 95 |
QUnit.test("stream replaced", async assert => { |
| 96 |
let first = false; |
| 97 |
let second = false; |
| 98 |
|
| 99 |
await cockpit.spawn(["/bin/cat"]) |
| 100 |
.input("11\n", true) |
| 101 |
.input("22\n", true) |
| 102 |
.input("33\n") |
| 103 |
.stream(() => { first = true }) |
| 104 |
.stream(() => { second = true }); |
| 105 |
|
| 106 |
assert.ok(!first, "first stream handler not called"); |
| 107 |
assert.ok(second, "second stream handler called"); |
| 108 |
}); |
| 109 |
|
| 110 |
QUnit.test("stream partial", async assert => { |
| 111 |
let streamed = ""; |
| 112 |
const resp = await cockpit.spawn(["/bin/cat"]) |
| 113 |
.input("1234") |
| 114 |
.stream(chunk => { |
| 115 |
if (chunk.length > 0) { |
| 116 |
streamed += chunk[0]; |
| 117 |
return 1; |
| 118 |
} |
| 119 |
}); |
| 120 |
assert.equal(resp, "234", "right then data"); |
| 121 |
assert.equal(streamed, "1", "stream data"); |
| 122 |
}); |
| 123 |
|
| 124 |
QUnit.test("stream partial binary", async assert => { |
| 125 |
const streamed: number[] = []; |
| 126 |
const resp = await cockpit.spawn(["/bin/cat"], { binary: true }) |
| 127 |
.input(new Uint8Array([0, 1, 2, 3])) |
| 128 |
.stream(chunk => { |
| 129 |
if (chunk.length > 0) { |
| 130 |
streamed.push(chunk[0]); |
| 131 |
return 1; |
| 132 |
} |
| 133 |
}); |
| 134 |
assert.deepEqual(resp, new Uint8Array([1, 2, 3]), "unconsumed data"); |
| 135 |
assert.deepEqual(streamed, [0], "stream got first byte"); |
| 136 |
}); |
| 137 |
|
| 138 |
QUnit.test("script with input", async assert => { |
| 139 |
const script = "#!/bin/sh\n\n# Test\n/usr/bin/sort\necho $2\necho $1"; |
| 140 |
const proc = cockpit.script(script, ["5", "4"]); |
| 141 |
proc.input("2\n", true); |
| 142 |
proc.input("3\n1\n"); |
| 143 |
assert.equal(await proc, "1\n2\n3\n4\n5\n", "output matched"); |
| 144 |
}); |
| 145 |
|
| 146 |
QUnit.test("script with options", async assert => { |
| 147 |
const script = "#!/bin/sh\n\n# Test\n/usr/bin/sort\necho $2\necho $1 >&2"; |
| 148 |
const proc = cockpit.script(script, ["5", "4"], { err: "out" }); |
| 149 |
proc.input("2\n", true); |
| 150 |
proc.input("3\n1\n"); |
| 151 |
assert.equal(await proc, "1\n2\n3\n4\n5\n", "output matched"); |
| 152 |
}); |
| 153 |
|
| 154 |
QUnit.test("script without args", async assert => { |
| 155 |
const script = "#!/bin/sh\n\n# Test\n/usr/bin/sort >&2"; |
| 156 |
const proc = cockpit.script(script, { err: "out" }); |
| 157 |
proc.input("2\n", true); |
| 158 |
proc.input("3\n1\n"); |
| 159 |
assert.equal(await proc, "1\n2\n3\n", "output matched"); |
| 160 |
}); |
| 161 |
|
| 162 |
QUnit.test("pty", async assert => { |
| 163 |
const proc = cockpit.spawn(['sh', '-c', "tty; test -t 0"], { pty: true }); |
| 164 |
const output = await proc; |
| 165 |
assert.equal(output.indexOf('/dev/pts'), 0, 'TTY is a pty: ' + output); |
| 166 |
}); |
| 167 |
|
| 168 |
QUnit.test("pty window size", async assert => { |
| 169 |
const proc = cockpit.spawn(['tput', 'lines', 'cols'], { |
| 170 |
pty: true, |
| 171 |
environ: ["TERM=vt100"], |
| 172 |
window: { rows: 77, cols: 88 } |
| 173 |
}); |
| 174 |
assert.equal(await proc, '77\r\n88\r\n', 'Correct rows and columns'); |
| 175 |
}); |
| 176 |
|
| 177 |
QUnit.test("pty window size limits", async assert => { |
| 178 |
let proc = cockpit.spawn(['stty', 'size'], { |
| 179 |
pty: true, |
| 180 |
environ: ["TERM=vt100"], |
| 181 |
window: { rows: -1, cols: 65538 } |
| 182 |
}); |
| 183 |
assert.equal(await proc, '0 65535\r\n', 'Clamps to 0x65535'); |
| 184 |
|
| 185 |
proc = cockpit.spawn(['stty', 'size'], { |
| 186 |
pty: true, |
| 187 |
environ: ["TERM=vt100"], |
| 188 |
// HACK: tput fallback to 80 if cols are 0 |
| 189 |
window: { rows: 65538, cols: 0 } |
| 190 |
}); |
| 191 |
assert.equal(await proc, '65535 0\r\n', 'Clamps to 65538x0'); |
| 192 |
}); |
| 193 |
|
| 194 |
QUnit.test("stream large output", async assert => { |
| 195 |
let lastblock = ""; |
| 196 |
const resp = await cockpit.spawn(["seq", "10000000"]) |
| 197 |
.stream(resp => { |
| 198 |
if (lastblock === "") |
| 199 |
assert.equal(resp.slice(0, 4), "1\n2\n", "stream data starts with first numbers"); |
| 200 |
lastblock = resp; |
| 201 |
}); |
| 202 |
assert.equal(resp, "", "no then data"); |
| 203 |
assert.equal(lastblock.slice(-18), "\n9999999\n10000000\n", "stream data has last numbers"); |
| 204 |
}); |
| 205 |
|
| 206 |
QUnit.test("cancel process", async assert => { |
| 207 |
const proc = cockpit.spawn(["sleep", "418"]); |
| 208 |
await cockpit.script("until pgrep -af [s]leep.*418; do sleep 0.1; done"); |
| 209 |
proc.close("cancelled"); |
| 210 |
await cockpit.script("timeout 5 sh -ec 'while pgrep -af [s]leep.*418; do sleep 0.1; done'"); |
| 211 |
try { |
| 212 |
await proc; |
| 213 |
assert.ok(false, "proc should have failed"); |
| 214 |
} catch (ex) { |
| 215 |
assert.equal((ex as cockpit.BasicError).problem, "cancelled", "proc failed with correct problem"); |
| 216 |
} |
| 217 |
}); |
| 218 |
|
| 219 |
QUnit.start(); |