package tezt

  1. Overview
  2. Docs

Source file runner.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2021-2022 Nomadic Labs <contact@nomadic-labs.com>           *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type t = {
  id : int;
  address : string;
  ssh_alias : string option;
  ssh_user : string option;
  ssh_port : int option;
  ssh_id : string option;
}

let local_public_ip = ref "127.0.0.1"

let get_local_public_ip () = !local_public_ip

let set_local_public_ip ip = local_public_ip := ip

let next_id = ref 0

let create ?ssh_alias ?ssh_user ?ssh_port ?ssh_id ~address () =
  let id = !next_id in
  incr next_id ;
  {id; address; ssh_alias; ssh_user; ssh_port; ssh_id}

let address ?(hostname = false) ?from runner =
  match (from, runner) with
  | None, None -> if hostname then "localhost" else "127.0.0.1"
  | None, Some host -> host.address
  | Some _peer, None -> get_local_public_ip ()
  | Some peer, Some host ->
      if peer.address = host.address then "127.0.0.1" else host.address

(* With ssh-agent, the environment variables SSH_AGENT_PID and SSH_AUTH_SOCK
   must be added in the environment. *)
let ssh_env () =
  match (Sys.getenv_opt "SSH_AGENT_PID", Sys.getenv_opt "SSH_AUTH_SOCK") with
  | Some agent, Some sock ->
      [|"SSH_AGENT_PID=" ^ agent; "SSH_AUTH_SOCK=" ^ sock|]
  | _ ->
      (* Here, we assume we don't have an agent running. *)
      [||]

module Shell = struct
  type command = {
    local_env : (string * string) list;
    name : string;
    arguments : string list;
  }

  type t =
    | Cmd of command
    | Seq of t * t
    | Echo_pid
    | Redirect_stdout of t * string
    | Redirect_stderr of t * string
    | Or_echo_false of t

  let string_of_env env =
    List.fold_right
      (fun (var, value) str_env -> var ^ "=" ^ value ^ " " ^ str_env)
      env
      ""

  let string_of_cmd cmd =
    let env = string_of_env cmd.local_env in
    let cmd = Log.quote_shell_command cmd.name cmd.arguments in
    env ^ cmd

  let rec to_string ?(context = `top) shell =
    let with_parentheses s =
      match context with
      | `top ->
          (* No need for parentheses in the toplevel context. *)
          s
      | `operator -> "(" ^ s ^ ")"
    in
    let in_operator = to_string ~context:`operator in
    match shell with
    | Cmd cmd ->
        (* Commands do not need parentheses, they have the highest precedence. *)
        string_of_cmd cmd
    | Seq (cmd1, cmd2) ->
        with_parentheses @@ in_operator cmd1 ^ " && " ^ in_operator cmd2
    | Echo_pid -> "echo $$"
    | Redirect_stdout (cmd, file) ->
        with_parentheses @@ in_operator cmd ^ " > " ^ Log.quote_shell file
    | Redirect_stderr (cmd, file) ->
        with_parentheses @@ in_operator cmd ^ " 2> " ^ Log.quote_shell file
    | Or_echo_false cmd ->
        with_parentheses @@ in_operator cmd ^ " || echo false"

  let cmd local_env name arguments = Cmd {local_env; name; arguments}

  let seq cmd_1 cmd_2 = Seq (cmd_1, cmd_2)

  let redirect_stdout shell file = Redirect_stdout (shell, file)

  let redirect_stderr shell file = Redirect_stderr (shell, file)

  let or_echo_false shell = Or_echo_false shell
end

let wrap_with_ssh runner shell =
  let cmd = Shell.to_string shell in
  let ssh_args =
    (match runner.ssh_alias with
    | None -> [runner.address]
    | Some alias -> [alias])
    @ (match runner.ssh_user with None -> [] | Some user -> ["-l"; user])
    @ (match runner.ssh_port with
      | None -> []
      | Some port -> ["-p"; string_of_int port])
    @ match runner.ssh_id with None -> [] | Some id -> ["-i"; id]
  in
  ("ssh", ssh_args @ [cmd])

let wrap_with_ssh_pid runner shell =
  let open Shell in
  wrap_with_ssh
    runner
    (seq Echo_pid (cmd shell.local_env "exec" (shell.name :: shell.arguments)))

module Sys = struct
  type error = {
    address : string option;
    command : string;
    exit_code : Unix.process_status;
    stderr : string;
  }

  exception Remote_error of error

  let show_error {address; command; exit_code; stderr} =
    let address_msg =
      match address with
      | None -> "on localhost"
      | Some address -> "on " ^ address
    in
    let exit_msg =
      match exit_code with
      | Unix.WEXITED code -> "exited with code " ^ string_of_int code
      | Unix.WSIGNALED signal -> "was killed by signal " ^ string_of_int signal
      | Unix.WSTOPPED signal -> "was stopped by signal " ^ string_of_int signal
    in
    let stderr_msg = if stderr = "" then "" else " (" ^ stderr ^ ")" in
    command ^ " " ^ exit_msg ^ stderr_msg ^ " " ^ address_msg ^ "."

  let () =
    Printexc.register_printer @@ function
    | Remote_error error -> Some (show_error error)
    | _ -> None

  (* WARNING: synchronous method so it can block. *)
  let run_unix_with_ssh runner shell =
    let ssh, ssh_args = wrap_with_ssh runner shell in
    let unix_cmd = String.concat " " (ssh :: ssh_args) in
    let ssh_env = ssh_env () in
    Unix.open_process_full unix_cmd ssh_env

  let get_stderr (_, _, stderr) = try input_line stderr with End_of_file -> ""

  let get_stdout (stdout, _, _) = try input_line stdout with End_of_file -> ""

  let file_exists ?runner file =
    match runner with
    | None -> Sys.file_exists file
    | Some runner -> (
        let command = "test" in
        let arguments = ["-e"; file] in
        let shell = Shell.or_echo_false (Shell.cmd [] command arguments) in
        let process = run_unix_with_ssh runner shell in
        let stdout = get_stdout process in
        let stderr = get_stderr process in
        let status = Unix.close_process_full process in
        match status with
        | Unix.WEXITED 0 -> stdout <> "false"
        | error ->
            let error =
              {
                command;
                address = Some runner.address;
                exit_code = error;
                stderr;
              }
            in
            raise (Remote_error error))

  let is_directory ?runner path =
    match runner with
    | None -> Sys.is_directory path
    | Some runner -> (
        let command = "test" in
        let arguments = ["-d"; path] in
        let shell = Shell.or_echo_false (Shell.cmd [] command arguments) in
        let process = run_unix_with_ssh runner shell in
        let stdout = get_stdout process in
        let stderr = get_stderr process in
        let status = Unix.close_process_full process in
        match status with
        | Unix.WEXITED 0 -> stdout <> "false"
        | error ->
            let error =
              {
                command;
                address = Some runner.address;
                exit_code = error;
                stderr;
              }
            in
            raise (Remote_error error))

  let mkdir ?runner ?(perms = 0o755) dir =
    match runner with
    | None -> Unix.mkdir dir perms
    | Some runner -> (
        let command = "mkdir" in
        let arguments = ["-m"; Format.sprintf "%o" perms; dir] in
        let shell = Shell.cmd [] command arguments in
        let process = run_unix_with_ssh runner shell in
        let stderr = get_stderr process in
        let status = Unix.close_process_full process in
        match status with
        | Unix.WEXITED 0 -> ()
        | error ->
            let error =
              {
                command;
                address = Some runner.address;
                exit_code = error;
                stderr;
              }
            in
            raise (Remote_error error))

  let readdir ?runner dir =
    match runner with
    | None -> Sys.readdir dir
    | Some runner -> (
        let command = "ls" in
        let arguments = ["-1"; dir] in
        let shell = Shell.cmd [] command arguments in
        let ((stdout, _, _) as process) = run_unix_with_ssh runner shell in
        let rec read_until_eof input acc =
          try
            let line = input_line input in
            read_until_eof input (line :: acc)
          with End_of_file -> Array.of_list acc
        in
        let files = read_until_eof stdout [] in
        let stderr = get_stderr process in
        let status = Unix.close_process_full process in
        match status with
        | Unix.WEXITED 0 -> files
        | error ->
            let error =
              {
                command;
                address = Some runner.address;
                exit_code = error;
                stderr;
              }
            in
            raise (Remote_error error))

  let remove ?runner file =
    match runner with
    | None -> Sys.remove file
    | Some runner -> (
        let command = "rm" in
        let arguments = [file] in
        let shell = Shell.cmd [] command arguments in
        let process = run_unix_with_ssh runner shell in
        let stderr = get_stderr process in
        let status = Unix.close_process_full process in
        match status with
        | Unix.WEXITED 0 -> ()
        | error ->
            let error =
              {
                command;
                address = Some runner.address;
                exit_code = error;
                stderr;
              }
            in
            raise (Remote_error error))

  let rm_rf runner file =
    let command = "rm" in
    let arguments = ["-rf"; file] in
    let shell = Shell.cmd [] command arguments in
    let process = run_unix_with_ssh runner shell in
    let stderr = get_stderr process in
    let status = Unix.close_process_full process in
    match status with
    | Unix.WEXITED 0 -> ()
    | error ->
        let error =
          {command; address = Some runner.address; exit_code = error; stderr}
        in
        raise (Remote_error error)

  let rmdir ?runner dir =
    match runner with
    | None -> Unix.rmdir dir
    | Some runner -> (
        let command = "rmdir" in
        let arguments = [dir] in
        let shell = Shell.cmd [] command arguments in
        let process = run_unix_with_ssh runner shell in
        let stderr = get_stderr process in
        let status = Unix.close_process_full process in
        match status with
        | Unix.WEXITED 0 -> ()
        | error ->
            let error =
              {
                command;
                address = Some runner.address;
                exit_code = error;
                stderr;
              }
            in
            raise (Remote_error error))

  let mkfifo ?runner ?(perms = 755) pipe =
    match runner with
    | None -> Unix.mkfifo pipe perms
    | Some runner -> (
        let command = "mkfifo" in
        let arguments = ["-m"; Format.sprintf "%o" perms; pipe] in
        let shell = Shell.cmd [] command arguments in
        let process = run_unix_with_ssh runner shell in
        let stderr = get_stderr process in
        let status = Unix.close_process_full process in
        match status with
        | Unix.WEXITED 0 -> ()
        | error ->
            let error =
              {
                command;
                address = Some runner.address;
                exit_code = error;
                stderr;
              }
            in
            raise (Remote_error error))
end
OCaml

Innovation. Community. Security.