Skip to content

Commit

Permalink
Driving client/server lifetime with python script for CI test
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfadden8 committed Jun 27, 2024
1 parent af5c5eb commit 6042455
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TARGET_LINK_LIBRARIES(test_client_server ${axl_lib})

CONFIGURE_FILE(test_axl.sh ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
CONFIGURE_FILE(test_axl_metadata.sh ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
CONFIGURE_FILE(test_driver_client_server.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)

ADD_TEST(sync_test test_axl.sh sync)

Expand Down Expand Up @@ -68,7 +69,7 @@ ENDIF(BBAPI_FOUND)

ADD_TEST(test_config test_config)

ADD_TEST(test_client_server test_client_server)
ADD_TEST(test_client_server test_driver_client_server.py)

####################
# make a verbose "test" target named "check"
Expand Down
67 changes: 46 additions & 21 deletions test/test_client_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,71 @@
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#include "axl.h"

int wait_for_service_to_complete()
{
int pid;
int wstatus = 0;
#define AXLCS_SUCCESS 0
#define AXLCS_CLIENT_INVALID 1
#define AXLCS_SERVICE_CREATION_FAILURE 1000
#define AXLCS_SERVICE_KILLED 2000
#define AXLCS_SERVICE_FAIL 3000

if ((pid = wait(&wstatus)) < 0) {
fprintf(stderr, "Wait for service process failed: [%d] - %s\n", errno, strerror(errno));
return 1;
}
static int time_to_leave = 0;

return WEXITSTATUS(wstatus);
void sigterm_handler(int sig, siginfo_t* info, void* ucontext)
{
time_to_leave++;
}

int run_service()
{
return 0;
struct sigaction act = {0};

act.sa_flags = 0;
sigemptyset(&act.sa_mask);
act.sa_sigaction = sigterm_handler;
if (sigaction(SIGTERM, &act, NULL) == -1) {
perror("sigaction");
return AXLCS_SERVICE_FAIL;
}

fprintf(stdout, "Service Started!\n");

for (int i = 0; !time_to_leave && i < 100000; ++i) {
int seconds = 2+i;
fprintf(stdout, "Sleeping %d seconds\n", seconds);
fprintf(stderr, "Just testing stderr. %d ..\n", seconds);
sleep(seconds);
}

fprintf(stdout, "Service Ending!\n");
return AXLCS_SUCCESS;
}

int run_client()
{
int client_status = 0;

return client_status + wait_for_service_to_complete();
fprintf(stdout, "Client Started!\n");
sleep(10);
fprintf(stdout, "Client Ending!\n");
return AXLCS_SUCCESS;
}

int main()
int main(int ac, char **av)
{
int pid;

if ((pid = fork()) < 0) {
fprintf(stderr, "Creation of service failed: [%d] - %s\n", errno, strerror(errno));
return 2;
fprintf(stderr, "Just testing stderr...\n");
if (ac != 2) {
fprintf(stderr, "Command count (%d) incorrect:\nUsage: test_client_server <client|server>\n", ac);
return AXLCS_CLIENT_INVALID;
}
else if (pid == 0) {

if (strcmp("server", av[1]) == 0) {
return run_service();
}
else {
else if (strcmp("client", av[1]) == 0) {
return run_client();
}

fprintf(stderr, "Unknown Argument (%s) incorrect:\nUsage: test_client_server <client|server>\n", av[1]);
return AXLCS_CLIENT_INVALID;
}
30 changes: 30 additions & 0 deletions test/test_driver_client_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3

import sys
import subprocess
import os

def wait_for_completion(procname, proc, wait_time):
try:
outs, err = proc.communicate(timeout=wait_time)
except:
proc.terminate()
outs, err = proc.communicate()

print("{} Return Code: {}".format(procname, proc.returncode))
print("stdout:\n{}".format(outs.decode("utf-8")))
print("stderr:\n{}".format(err.decode("utf-8")))

return proc.returncode, outs, err

if __name__ == '__main__':
# Launch the server then the client
server = subprocess.Popen(['./test_client_server server'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
client = subprocess.Popen(['./test_client_server client'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

# Wait for the client then the server to finish
server_ecode, server_out, server_err = wait_for_completion("axl_client", client, 120)
client_ecode, client_out, client_err = wait_for_completion("axl_server", server, 10)

if server_ecode != 0 or client_ecode != 0:
sys.exit(server_ecode + client_ecode)

0 comments on commit 6042455

Please sign in to comment.