1+ import { spawnSync } from "node:child_process" ;
2+ import { mkdtemp , rm , writeFile } from "node:fs/promises" ;
3+ import { tmpdir } from "node:os" ;
4+ import { join } from "node:path" ;
15import { afterEach , describe , expect , it , vi } from "vitest" ;
26import { ExeImageVerificationError , ExeVmBackend , isOwnedVmName , tagsFromInventory , vmNameFor } from "../src/exe-vm-backend" ;
37import type { ExecutionBackend , ExecutionObservation , LaunchRequest , RunHandle } from "../src/execution" ;
@@ -96,7 +100,7 @@ describe("ExeVmBackend", () => {
96100 expect ( requests [ 1 ] ) . toContain ( "--uid=exedev --gid=exedev" ) ;
97101 expect ( requests [ 1 ] ) . toContain ( "--property=Type=exec" ) ;
98102 expect ( requests [ 1 ] ) . toContain ( "--property=RemainAfterExit=yes" ) ;
99- expect ( requests [ 1 ] ) . toContain ( "--property=StandardInput=/dev/ null" ) ;
103+ expect ( requests [ 1 ] ) . toContain ( "--property=StandardInput=null" ) ;
100104 expect ( requests [ 1 ] ) . toContain ( "--property=StandardOutput=append:/tmp/factorize.log" ) ;
101105 expect ( requests [ 1 ] ) . toContain ( "--property=StandardError=append:/tmp/factorize.stderr" ) ;
102106 expect ( launchPayload ( requests [ 1 ] ) . traceSources . primary . path ) . toBe ( "/tmp/trace/events.jsonl" ) ;
@@ -106,6 +110,31 @@ describe("ExeVmBackend", () => {
106110 expect ( requests [ 1 ] ) . toContain ( "systemctl show" ) ;
107111 } ) ;
108112
113+ it ( "generates standard I/O settings accepted by systemd" , async ( ) => {
114+ const requests : string [ ] = [ ] ;
115+ vi . stubGlobal ( "fetch" , vi . fn ( async ( _url : string , init : RequestInit ) => {
116+ requests . push ( String ( init . body ) ) ;
117+ return new Response ( requests . length === 1 ? "{}" : "started" , { headers : { "X-Exe-Exit" : "0" } } ) ;
118+ } ) ) ;
119+ await new ExeVmBackend ( connection ) . launch ( { runId : "stdin-regression" , prompt : "" , code : { script : "true" , context : { } } } ) ;
120+ const command = requests . find ( value => value . includes ( "sudo systemd-run" ) ) ! ;
121+ const properties = [ ...command . matchAll ( / - - p r o p e r t y = ( S t a n d a r d (?: I n p u t | O u t p u t | E r r o r ) = [ ^ \s ] + ) / g) ] . map ( match => match [ 1 ] ) ;
122+ expect ( properties ) . toHaveLength ( 3 ) ;
123+ const directory = await mkdtemp ( join ( tmpdir ( ) , "factorize-systemd-" ) ) ;
124+ try {
125+ const unit = join ( directory , "launch.service" ) ;
126+ await writeFile ( unit , `[Service]\nType=exec\n${ properties . join ( "\n" ) } \nExecStart=/bin/true\n` ) ;
127+ const verified = spawnSync ( "systemd-analyze" , [ "verify" , unit ] , { encoding : "utf8" , timeout : 10_000 } ) ;
128+ expect ( verified . error ) . toBeUndefined ( ) ;
129+ expect ( verified . status ) . toBe ( 0 ) ;
130+ // Invalid StandardInput values are warnings in unit files but fatal to
131+ // systemd-run's transient-unit API. Reject parser warnings as well.
132+ expect ( verified . stderr ) . toBe ( "" ) ;
133+ } finally {
134+ await rm ( directory , { recursive : true , force : true } ) ;
135+ }
136+ } ) ;
137+
109138 it ( "quotes a snapshotted public image in the exe.dev VM creation command" , async ( ) => {
110139 const requests : string [ ] = [ ] ;
111140 vi . stubGlobal ( "fetch" , vi . fn ( async ( _url : string , init : RequestInit ) => { requests . push ( String ( init . body ) ) ; return new Response ( requests . length === 1 ? "{}" : "started" , { headers : { "X-Exe-Exit" : "0" } } ) ; } ) ) ;
0 commit comments