Skip to content

Commit a998d5a

Browse files
committed
test(supervisor/posix): initial test structure
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 2231ac4 commit a998d5a

3 files changed

Lines changed: 158 additions & 0 deletions

File tree

src/samples/supervisor/posix/server/ocred.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "../zcbor_helpers.h"
2727
#include "waiters.h"
28+
#include "handlers.h"
2829

2930
#define DEFAULT_PID_FILE "ocre.pid"
3031

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# @copyright Copyright (c) contributors to Project Ocre,
2+
# which has been established as Project Ocre a Series of LF Projects, LLC
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
cmake_minimum_required(VERSION 3.20.0)
7+
8+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
9+
10+
# set(CMAKE_C_COMPILER /usr/bin/clang)
11+
12+
list(APPEND OCRE_SDK_PRELOADED_IMAGES
13+
"return0.wasm"
14+
"return1.wasm"
15+
"sleep_5_return_0.wasm"
16+
)
17+
18+
if (SANITIZER)
19+
string(APPEND CMAKE_C_FLAGS
20+
"-fsanitize=address"
21+
)
22+
endif()
23+
24+
project(OcreSystemTestPosixIPC)
25+
26+
add_subdirectory(../../.. ocre)
27+
28+
add_library(Unity STATIC
29+
../../Unity/src/unity.c
30+
)
31+
32+
target_include_directories(Unity PUBLIC
33+
../../Unity/src
34+
)
35+
36+
list(APPEND OCRE_TESTS
37+
context
38+
container
39+
)
40+
41+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/src/ocre/var/lib/ocre/images)
42+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/src/ocre/var/lib/ocre/containers)
43+
44+
foreach(test ${OCRE_TESTS})
45+
add_executable(test_${test}
46+
../${test}.c
47+
setup.c
48+
)
49+
50+
set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/../${test}.c PROPERTIES COMPILE_FLAGS
51+
"-DOCRE_TEST_OTHER_CONTEXT_PATH=\\\"./othercontext\\\" -DtearDown=test_tearDown -DsetUp=test_setUp"
52+
)
53+
54+
target_link_libraries(test_${test}
55+
OcreCommon
56+
OcreClient
57+
Unity
58+
)
59+
60+
add_custom_command(
61+
OUTPUT test_${test}.log
62+
COMMAND cd ocre && LLVM_PROFILE_FILE=../test_${test}.profraw ../test_${test} > ../test_${test}.log
63+
COMMAND rm -f test_${test}.testpass test_${test}.testfail
64+
COMMAND sh -c "[ \"$(tail -1 test_${test}.log)\" = \"OK\" ] && cp test_${test}.log test_${test}.testpass || cp test_${test}.log test_${test}.testfail"
65+
DEPENDS
66+
test_${test}
67+
VERBATIM
68+
)
69+
endforeach()
70+
71+
add_custom_target(run-systests
72+
COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/../../Unity/auto/unity_test_summary.py ${CMAKE_CURRENT_BINARY_DIR}
73+
DEPENDS
74+
test_context.log
75+
test_container.log
76+
)

tests/system/posix-ipc/setup.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
#include <spawn.h>
5+
#include <signal.h>
6+
#include <sys/types.h>
7+
#include <sys/wait.h>
8+
#include <sys/socket.h>
9+
#include <sys/un.h>
10+
11+
extern void test_setUp(void);
12+
extern void test_tearDown(void);
13+
14+
static pid_t ocred_pid;
15+
16+
void setUp(void)
17+
{
18+
19+
#if 1
20+
if (chdir("ocre")) {
21+
perror("chdir");
22+
}
23+
24+
if (unlink("/tmp/ocre.sock") == -1) {
25+
perror("setUp unlink");
26+
}
27+
28+
char *argv[] = {"./src/samples/supervisor/posix/server/ocred", NULL};
29+
char *envp[] = {NULL};
30+
printf("will spawn ocred\n");
31+
int rc = posix_spawnp(&ocred_pid, "./src/samples/supervisor/posix/server/ocred", NULL, NULL, argv, envp);
32+
printf("spawned ocred PID %d\n", rc);
33+
34+
// try to connect to socket
35+
36+
int s;
37+
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
38+
perror("socket");
39+
exit(1);
40+
}
41+
while (1) {
42+
struct sockaddr_un remote = {
43+
.sun_family = AF_UNIX,
44+
};
45+
46+
strcpy(remote.sun_path, "/tmp/ocre.sock");
47+
size_t len = strlen(remote.sun_path) + sizeof(remote.sun_family);
48+
fprintf(stderr, "connecting to %s\n", remote.sun_path);
49+
if (connect(s, (struct sockaddr *)&remote, len) == -1) {
50+
perror("connect");
51+
usleep(100000);
52+
continue;
53+
}
54+
55+
fprintf(stderr, "connected to %s\n", remote.sun_path);
56+
break;
57+
}
58+
59+
close(s);
60+
61+
if (chdir("..")) {
62+
perror("chdir");
63+
}
64+
#endif
65+
// sleep(1);
66+
67+
test_setUp();
68+
}
69+
70+
void tearDown(void)
71+
{
72+
test_tearDown();
73+
#if 1
74+
printf("sending SIGTERM to ocred\n");
75+
kill(ocred_pid, SIGTERM);
76+
waitpid(ocred_pid, NULL, 0);
77+
if (unlink("/tmp/ocre.sock") == -1) {
78+
perror("tearDown unlink");
79+
}
80+
#endif
81+
}

0 commit comments

Comments
 (0)