1
1
Fork 0

[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.
This commit is contained in:
Laurent Stacul 2021-02-27 10:06:20 +00:00 committed by Marc Hörsken
parent 3709037b26
commit a88a727c2a
1 changed files with 26 additions and 10 deletions

View File

@ -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);