1
1

[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.
Этот коммит содержится в:
Laurent Stacul 2021-02-27 10:06:20 +00:00 коммит произвёл Marc Hörsken
родитель 3709037b26
Коммит a88a727c2a

Просмотреть файл

@ -208,6 +208,15 @@ static int is_running_inside_a_container()
#endif #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) static int ip_address_from_container(char *container_id, char **ip_address_out)
{ {
const char *active_docker_machine = docker_machine_name(); 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; return -1;
} }
else { else {
#ifdef WIN32 portable_sleep(wait_time);
Sleep(wait_time);
#else
sleep(wait_time);
#endif
++attempt_no; ++attempt_no;
wait_time *= 2; wait_time *= 2;
} }
@ -283,6 +288,7 @@ static int open_socket_to_container(char *container_id)
unsigned long hostaddr; unsigned long hostaddr;
int sock; int sock;
struct sockaddr_in sin; struct sockaddr_in sin;
int counter = 0;
int ret = ip_address_from_container(container_id, &ip_address); int ret = ip_address_from_container(container_id, &ip_address);
if(ret != 0) { 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_port = htons((short)strtol(port_string, NULL, 0));
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if(connect(sock, (struct sockaddr *)(&sin), for(counter = 0; counter < 3; ++counter) {
sizeof(struct sockaddr_in)) != 0) { 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", fprintf(stderr, "Failed to connect to %s:%s\n",
ip_address, port_string); ip_address, port_string);
ret = -1;
goto cleanup; goto cleanup;
} }
ret = sock;
cleanup: cleanup:
free(ip_address); free(ip_address);
free(port_string); free(port_string);