def run_injector(docker_socket, docker_info_path):
    injector = DockerInjector(
        get_production_docker_wrapper(base_url=docker_socket),
        docker_info_path=docker_info_path)

    while True:
        injector.start()
        time.sleep(30)
Ejemplo n.º 2
0
def run_injector(docker_socket, docker_info_path):
    injector = DockerInjector(
        get_production_docker_wrapper(base_url=docker_socket),
        docker_info_path=docker_info_path)

    while True:
        injector.start()
        time.sleep(30)
    def test_start(self):
        expected_c1 = {'Docker container id': 'c1', 'Docker host': 'host', 'Docker container name': 'name1', 'Docker image': 'image1'}
        expected_c2 = {'Docker container id': 'c2', 'Docker host': 'host', 'Docker container name': 'name2', 'Docker image': 'image2'}
        container = {'Id': 'c1', 'Image':'image1', 'Names':['name1']}

        wrapper_mock = Mock()
        wrapper_mock.get_containers.return_value = [container]
        wrapper_mock.get_host_name.return_value='host'
        wrapper_mock.run_command.return_value = 'file already exists'
        wrapper_mock.get_events.return_value = [{'time': 1439388853, 'Id': 'c2', 'id': 'c2', 'from': 'image2', 'status': 'start'}]
        wrapper_mock.get_inspection.return_value = {'Id': 'c2', 'status': 'start', 'Config':{'Image':'image2'}, 'Name':'name2'}

        def assert_func():
            res = wrapper_mock.run_command.mock_calls
            start = time.time()
            while len(res) < 4 and time.time() - start < 100:
                time.sleep(1)
            self.assertEqual(4, len(res))

        injector = DockerInjector(docker_wrapper=wrapper_mock, docker_info_path="/path/docker.info")
        with ThreadPoolExecutor(max_workers=3) as ex:
            ex.submit(lambda: injector.start())
            result = ex.submit(lambda: assert_func())
            result.result()
            calls_argument_keys = [keys for name, args, keys in wrapper_mock.run_command.mock_calls]
            c1_calls = [d for d in calls_argument_keys if 'container' in d and 'Id' in d['container'] and d['container']['Id'] == 'c1']
            c2_calls = [d for d in calls_argument_keys if 'container' in d and 'Id' in d['container'] and d['container']['Id'] == 'c2']
            self.assertEqual(2, len(c1_calls))
            self.assertEqual(2, len(c2_calls))
            c1_data = c1_calls[1]['cmd']
            m1 = re.search('printf \'(.+)\'', c1_data)
            self.assertTrue(m1)
            actual_c1= {key:val for key,val in [token.split('=') for token in m1.group(1).strip(' ').split(',')]}
            self.assertDictEqual(expected_c1, actual_c1)
            c2_data = c2_calls[1]['cmd']
            m2 = re.search('printf \'(.+)\'', c2_data)
            self.assertTrue(m2)
            actual_c2= {key:val for key,val in [token.split('=') for token in m2.group(1).strip(' ').split(',')]}
            self.assertDictEqual(expected_c2, actual_c2)
Ejemplo n.º 4
0
    def test_start(self):
        expected_c1 = {
            'Docker container id': 'c1',
            'Docker host': 'host',
            'Docker container name': 'name1',
            'Docker image': 'image1'
        }
        expected_c2 = {
            'Docker container id': 'c2',
            'Docker host': 'host',
            'Docker container name': 'name2',
            'Docker image': 'image2'
        }
        container = {'Id': 'c1', 'Image': 'image1', 'Names': ['name1']}

        wrapper_mock = Mock()
        wrapper_mock.get_containers.return_value = [container]
        wrapper_mock.get_host_name.return_value = 'host'
        wrapper_mock.run_command.return_value = 'file already exists'
        wrapper_mock.get_events.return_value = [{
            'time': 1439388853,
            'Id': 'c2',
            'id': 'c2',
            'from': 'image2',
            'status': 'start'
        }]
        wrapper_mock.get_inspection.return_value = {
            'Id': 'c2',
            'status': 'start',
            'Config': {
                'Image': 'image2'
            },
            'Name': 'name2'
        }

        def assert_func():
            res = wrapper_mock.run_command.mock_calls
            start = time.time()
            while len(res) < 4 and time.time() - start < 100:
                time.sleep(1)
            self.assertEqual(4, len(res))

        injector = DockerInjector(docker_wrapper=wrapper_mock,
                                  docker_info_path="/path/docker.info")
        with ThreadPoolExecutor(max_workers=3) as ex:
            ex.submit(lambda: injector.start())
            result = ex.submit(lambda: assert_func())
            result.result()
            calls_argument_keys = [
                keys
                for name, args, keys in wrapper_mock.run_command.mock_calls
            ]
            c1_calls = [
                d for d in calls_argument_keys if 'container' in d
                and 'Id' in d['container'] and d['container']['Id'] == 'c1'
            ]
            c2_calls = [
                d for d in calls_argument_keys if 'container' in d
                and 'Id' in d['container'] and d['container']['Id'] == 'c2'
            ]
            self.assertEqual(2, len(c1_calls))
            self.assertEqual(2, len(c2_calls))
            c1_data = c1_calls[1]['cmd']
            m1 = re.search('printf \'(.+)\'', c1_data)
            self.assertTrue(m1)
            actual_c1 = {
                key: val
                for key, val in [
                    token.split('=')
                    for token in m1.group(1).strip(' ').split(',')
                ]
            }
            self.assertDictEqual(expected_c1, actual_c1)
            c2_data = c2_calls[1]['cmd']
            m2 = re.search('printf \'(.+)\'', c2_data)
            self.assertTrue(m2)
            actual_c2 = {
                key: val
                for key, val in [
                    token.split('=')
                    for token in m2.group(1).strip(' ').split(',')
                ]
            }
            self.assertDictEqual(expected_c2, actual_c2)