def test_for_container_specific_port(self): """ The ``for_container()`` class method returns a container client that connects to the container port specified. """ ch = self.make_helper() container = ContainerDefinition('first_port', IMG, create_kwargs={ 'ports': { '8080/tcp': ('127.0.0.1', None), '5353/udp': ('127.0.0.1', None), } }, helper=ch) container.setup() self.addCleanup(container.teardown) client = ContainerHttpClient.for_container(container, container_port='8080') self.addCleanup(client.close) response = client.request('GET', '/foo') self.assertEqual(response.status_code, 200) response_lines = response.text.splitlines() self.assertIn('HTTP/1.1 GET /foo', response_lines) addr, port = container.get_host_port('8080') self.assertIn('Host: {}:{}'.format(addr, port), response_lines)
def test_wait_timeout_default(self): """ When wait_timeout isn't passed to the constructor, the default timeout is used. """ container = ContainerDefinition('timeout', IMG_WAIT) self.assertEqual(container.wait_timeout, ContainerDefinition.WAIT_TIMEOUT)
def test_wait_timeout_override(self): """ When wait_timeout is passed to the constructor, it is used in place of the default. """ timeout = ContainerDefinition.WAIT_TIMEOUT + 10.0 container = ContainerDefinition('timeout', IMG_WAIT, wait_timeout=timeout) self.assertEqual(container.wait_timeout, timeout)
def run_logs_container(self, logs, wait=True, delay=0.01): # Sleep some amount between lines to ensure ordering across stdout and # stderr. script = '\nsleep {}\n'.format(delay).join(logs) script_con = self.with_cleanup( ContainerDefinition('script', IMG_SCRIPT, helper=self.helper)) script_con.run(fetch_image=False, command=['sh', '-c', script]) # Wait for the output to arrive. if wait: # Wait a minimum of 100ms to avoid jitter with small intervals. time.sleep(max(0.1, len(logs) * delay)) return script_con
def test_setup_teardown(self, request, docker_helper): """ The fixture should yield a started container, and afterwards stop and remove the container. """ fixture = resource_fixture(ContainerDefinition(name='test', image=IMG), 'test') fixture_gen = fixture(request, docker_helper) container = next(fixture_gen) assert isinstance(container, ContainerDefinition) assert container.inner().status == 'running' # Test things are torn down with pytest.raises(StopIteration): next(fixture_gen) # Container has been stopped and removed assert not container.created
def test_clean_fixtures(self, request, docker_helper): """ The fixture returned by the ``pytest_clean_fixture()`` method should yield a started container, and afterwards stop and remove the container. """ raw_fixture, fixture = ContainerDefinition( name='test', image=IMG).pytest_clean_fixtures('test') fixture_gen = raw_fixture(request, docker_helper) # TODO: Assert on cleaning fixture container = next(fixture_gen) assert isinstance(container, ContainerDefinition) assert container.inner().status == 'running' # Test things are torn down with pytest.raises(StopIteration): next(fixture_gen) # Container has been stopped and removed assert not container.created
def test_dependencies(self, request, docker_helper): """ When the fixture depends on other fixtures, those fixtures should be setup when the fixture is used. """ container_fixture = resource_fixture( ContainerDefinition(name='test', image=IMG), 'container_test', dependencies=('volume_test', 'network_test')) fixture_gen = container_fixture(request, docker_helper) container = next(fixture_gen) assert isinstance(container, ContainerDefinition) assert container.status() == 'running' assert volume.created assert network.created # Test things are torn down with pytest.raises(StopIteration): next(fixture_gen) # Container has been stopped and removed assert not container.created
from seaworthy.definitions import ContainerDefinition container = ContainerDefinition('gem-molo-unicore-redirect', 'praekeltfoundation/gem-molo-unicore-redirect', create_kwargs={'ports': { '80/tcp': None }}) fixture = container.pytest_fixture('redirect_container') def test_bare_domain_redirect(redirect_container): redirect = redirect_container.http_client().get( '/', allow_redirects=False, headers={'Host': 'gem.molo.unicore.io'}, ) assert redirect.status_code == 301 assert redirect.headers['Location'] == 'http://id.heyspringster.com/' def test_country_code_redirect(redirect_container): redirect = redirect_container.http_client().get( '/', allow_redirects=False, headers={'Host': 'za.gem.molo.unicore.io'}, ) assert redirect.status_code == 301 assert redirect.headers['Location'] == 'http://za.heyspringster.com/'
def make_definition(self, name): return ContainerDefinition(name, IMG)
def make_definition(self, name, helper=None): return ContainerDefinition(name, IMG_WAIT, helper=helper)
'SRV_SSL': 'true', 'SRV_SSL_AUTO': 'true', 'SRV_SSL_NAME': 'custom', }) f5 = httpd_fixture( 'httpd_htaccess', { 'SRV_ENABLE_MODULE': 'rewrite', 'SRV_ALLOW_OVERRIDE': 'true', 'SRV_DOCROOT': '/var/www/html' }, {Path().absolute().as_posix() + '/test/www': '/var/www/html'}) f6_php = ContainerDefinition( "php_for_httpd24", "php:7.2-fpm-alpine", create_kwargs={ "volumes": { Path().absolute().as_posix() + '/test/www': '/var/www/html', } }).pytest_fixture("php_container") f6 = httpd_fixture_with_deps( 'httpd_php', [ 'php_container', ], { 'SRV_PHP': 'true', 'SRV_PHP_HOST': 'php_for_httpd24', 'SRV_DOCROOT': '/var/www/html' }, { Path().absolute().as_posix() + '/test/www': '/var/www/html', })