#!/usr/bin/env python

import os
import subprocess as sp
import sys
import time

print('Building the image...')
sp.check_call(['bazel', 'run', '//:assemble-docker'])

print('Starting the image...')
sp.check_call(['docker', 'run', '-v', '{}:/grakn-core-all-linux/logs/'.format(os.getcwd()), '--name', 'grakn','-d', '--rm', '-ti', '-p', '127.0.0.1:48555:48555/tcp', 'bazel:assemble-docker'])
print('Docker status:')
sp.check_call(['docker', 'ps'])

sys.stdout.write('Waiting for the instance to be ready')
sys.stdout.flush()
timeout = 0 # TODO: add timeout
# TODO: fail if the docker image is dead
# upon a successful gRPC connection, the curl returns 0 in linux, and 8 in mac
while sp.call(['curl', '--output', '/dev/null', '--silent', '--head', '--fail', 'localhost:48555']) not in {0, 8}:
    sys.stdout.write('.')
    sys.stdout.flush()
    time.sleep(1)
print()

print('Running the test...')
sp.check_call(['bazel', 'test', '//test/common:grakn-application-test', '--test_output=streamed',
                          '--spawn_strategy=standalone', '--cache_test_results=no'])

print('Stopping the container...')
sp.check_call(['docker', 'kill', 'grakn'])
print('Done!')
@@ -26,16 +26,30 @@
 port os%0A
+import socket%0A
 import s
@@ -88,16 +88,535 @@
 t time%0A%0A
+%0Adef wait_for_port(port, host='localhost', timeout=30.0):%0A    start_time = time.perf_counter()%0A    while True:%0A        try:%0A            with socket.create_connection((host, port), timeout=timeout):%0A                break%0A        except OSError as ex:%0A            time.sleep(0.01)%0A            if time.perf_counter() - start_time %3E= timeout:%0A                raise TimeoutError('Waited too long for the port %7B%7D on host %7B%7D to start accepting '%0A                                   'connections.'.format(port, host)) from ex%0A%0A%0A
 print('B
@@ -1126,275 +1126,27 @@
 ead%0A
-# upon a successful gRPC connection, the curl returns 0 in linux, and 8 in mac%0Awhile sp.call(%5B'curl', '--output', '/dev/null', '--silent', '--head', '--fail', 'localhost:48555'%5D) not in %7B0, 8%7D:%0A    sys.stdout.write('.')%0A    sys.stdout.flush()%0A    time.sleep(1)%0Aprint(
+wait_for_port(48555
 )%0A%0Ap
@@ -1449,8 +1449,9 @@
 'Done!')
+%0A

Fix Docker image test (#5846)