2021-06-04 14:33:22 +00:00
import subprocess
2021-06-09 13:53:16 +00:00
from helpers . cluster import run_and_check
2021-06-04 14:33:22 +00:00
import pytest
import logging
2021-06-09 13:53:16 +00:00
import os
2017-05-19 18:54:05 +00:00
from helpers . test_tools import TSV
2021-06-04 14:33:22 +00:00
from helpers . network import _NetworkManager
2017-05-19 18:54:05 +00:00
2021-06-04 14:33:22 +00:00
@pytest.fixture ( autouse = True , scope = " session " )
def cleanup_environment ( ) :
_NetworkManager . clean_all_user_iptables_rules ( )
try :
2021-06-09 13:53:16 +00:00
result = run_and_check ( [ ' docker ps | wc -l ' ] , shell = True )
if int ( result ) > 1 :
if int ( os . environ . get ( " PYTEST_CLEANUP_CONTAINERS " ) ) != 1 :
logging . warning ( f " Docker containters( { int ( result ) } ) are running before tests run. They can be left from previous pytest run and cause test failures. \n " \
" You can set env PYTEST_CLEANUP_CONTAINERS=1 or use runner with --cleanup-containers argument to enable automatic containers cleanup. " )
2021-06-04 15:00:59 +00:00
else :
2021-06-09 13:53:16 +00:00
logging . debug ( " Trying to kill unstopped containers... " )
run_and_check ( [ f ' docker kill $(docker container list --all --quiet) ' ] , shell = True , nothrow = True )
run_and_check ( [ f ' docker rm $docker container list --all --quiet) ' ] , shell = True , nothrow = True )
logging . debug ( " Unstopped containers killed " )
r = run_and_check ( [ ' docker-compose ' , ' ps ' , ' --services ' , ' --all ' ] )
logging . debug ( f " Docker ps before start: { r . stdout } " )
else :
logging . debug ( f " No running containers " )
2021-06-04 15:00:59 +00:00
except Exception as e :
2021-06-09 13:53:16 +00:00
logging . exception ( f " cleanup_environment: { str ( e ) } " )
2021-06-04 14:33:22 +00:00
pass
2020-09-16 04:26:10 +00:00
2021-06-21 08:14:26 +00:00
yield
def pytest_addoption ( parser ) :
parser . addoption ( " --run-id " , default = " " , help = " run-id is used as postfix in _instances_ {} directory " )
def pytest_configure ( config ) :
os . environ [ ' INTEGRATION_TESTS_RUN_ID ' ] = config . option . run_id