From a88a727c2a1840f979b34f12bcce3d55dcd7ea6e Mon Sep 17 00:00:00 2001 From: Laurent Stacul Date: Sat, 27 Feb 2021 10:06:20 +0000 Subject: [PATCH] [tests] Try several times to connect the ssh server Sometimes, as the OCI container is run in detached mode, it is possible the actual server is not ready yet to handle SSH traffic. The goal of this PR is to try several times (max 3). The mechanism is the same as for the connection to the docker machine. --- tests/openssh_fixture.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/tests/openssh_fixture.c b/tests/openssh_fixture.c index 2d6cb29..0480c5d 100644 --- a/tests/openssh_fixture.c +++ b/tests/openssh_fixture.c @@ -208,6 +208,15 @@ static int is_running_inside_a_container() #endif } +static unsigned int portable_sleep(unsigned int seconds) +{ +#ifdef WIN32 + Sleep(seconds); +#else + sleep(seconds); +#endif +} + static int ip_address_from_container(char *container_id, char **ip_address_out) { const char *active_docker_machine = docker_machine_name(); @@ -232,11 +241,7 @@ static int ip_address_from_container(char *container_id, char **ip_address_out) return -1; } else { -#ifdef WIN32 - Sleep(wait_time); -#else - sleep(wait_time); -#endif + portable_sleep(wait_time); ++attempt_no; wait_time *= 2; } @@ -283,6 +288,7 @@ static int open_socket_to_container(char *container_id) unsigned long hostaddr; int sock; struct sockaddr_in sin; + int counter = 0; int ret = ip_address_from_container(container_id, &ip_address); if(ret != 0) { @@ -325,16 +331,26 @@ static int open_socket_to_container(char *container_id) sin.sin_port = htons((short)strtol(port_string, NULL, 0)); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr *)(&sin), - sizeof(struct sockaddr_in)) != 0) { + for(counter = 0; counter < 3; ++counter) { + if(connect(sock, (struct sockaddr *)(&sin), + sizeof(struct sockaddr_in)) != 0) { + ret = -1; + fprintf(stderr, + "Connection to %s:%s attempt #%d failed: retrying...\n", + ip_address, port_string, counter); + portable_sleep(1 + 2*counter); + } + else { + ret = sock; + break; + } + } + if(ret == -1) { fprintf(stderr, "Failed to connect to %s:%s\n", ip_address, port_string); - ret = -1; goto cleanup; } - ret = sock; - cleanup: free(ip_address); free(port_string);