add test for forking app
Stefan Schuermans

Stefan Schuermans commited on 2021-03-09 19:21:47
Showing 5 changed files, with 67 additions and 0 deletions.

... ...
@@ -1,3 +1,4 @@
1 1
 add_subdirectory(first)
2
+add_subdirectory(fork)
2 3
 add_subdirectory(pylint)
3 4
 add_subdirectory(trace_build)
... ...
@@ -0,0 +1,11 @@
1
+add_executable(
2
+  forkapp
3
+  forkapp.c
4
+)
5
+
6
+add_test(
7
+  NAME
8
+  fork
9
+  COMMAND
10
+  ${CMAKE_CURRENT_SOURCE_DIR}/forktest.bash ${CMAKE_BINARY_DIR}
11
+)
... ...
@@ -0,0 +1,26 @@
1
+#include <stdlib.h>
2
+#include <sys/types.h>
3
+#include <sys/wait.h>
4
+#include <unistd.h>
5
+
6
+void run(char const *path) {
7
+  pid_t child = fork();
8
+  if (child == 0) {
9
+    execl(path, path, NULL);
10
+    exit(EXIT_FAILURE);
11
+  } else if (child > 0) {
12
+    waitpid(child, NULL, 0);
13
+  }
14
+}
15
+
16
+int main() {
17
+  run("/bin/ls");
18
+  pid_t child = fork();
19
+  if (child == 0) {
20
+    run("/bin/uptime");
21
+  } else if (child > 0) {
22
+    waitpid(child, NULL, 0);
23
+    run("/bin/hostname");
24
+  }
25
+  return EXIT_SUCCESS;
26
+}
... ...
@@ -0,0 +1,5 @@
1
+__UPT_HOME__/tests/fork/forkapp
2
+  /bin/ls
3
+  ???
4
+    /bin/uptime
5
+  /bin/hostname
... ...
@@ -0,0 +1,24 @@
1
+#! /bin/bash
2
+
3
+set -eux -o pipefail
4
+
5
+if (( $# < 1 ))
6
+then
7
+  echo "usage: $0 <UPT_HOME>" >&2
8
+  exit 2
9
+fi
10
+UPT_HOME="$1"
11
+
12
+SCRIPT_DIR="$(dirname "$0")"
13
+
14
+source "$UPT_HOME/exports"
15
+
16
+rm -rf forkapp.upt
17
+
18
+upt-trace forkapp.upt "$UPT_HOME/tests/fork/forkapp"
19
+
20
+upt-tool forkapp.upt pstree | tee forkapp.pstree
21
+
22
+sed -e "s%__UPT_HOME__%$UPT_HOME%" "$SCRIPT_DIR/forkapp.pstree_ref" \
23
+  >forkapp.pstree_ref
24
+diff -u forkapp.pstree forkapp.pstree_ref
0 25