def test_containers_already_running(self): stdout = MagicMock() features = [] running_containers = ['cond-0'] input_args = MagicMock(**self.default_args) with \ patch('conductr_cli.terminal.docker_images', return_value='some-image'), \ patch('conductr_cli.terminal.docker_pull', return_value=''), \ patch('conductr_cli.terminal.docker_ps', return_value=''), \ patch('conductr_cli.terminal.docker_inspect', return_value='10.10.10.10'), \ patch('conductr_cli.terminal.docker_run', return_value=''), \ patch('conductr_cli.sandbox_common.resolve_running_docker_containers', return_value=running_containers), \ patch('conductr_cli.sandbox_proxy.stop_proxy') as mock_stop_proxy, \ patch('conductr_cli.terminal.docker_rm') as mock_docker_rm: logging_setup.configure_logging(input_args, stdout) sandbox_run_docker.run(input_args, features) expected_stdout = strip_margin( """||------------------------------------------------| || Stopping ConductR | ||------------------------------------------------| |ConductR has been successfully stopped ||------------------------------------------------| || Starting ConductR | ||------------------------------------------------| |Starting container cond-0.. |""") self.assertEqual(expected_stdout, self.output(stdout)) mock_stop_proxy.assert_called_once_with() mock_docker_rm.assert_called_once_with(running_containers)
def test_multiple_container(self): stdout = MagicMock() nr_of_instances = 3 with \ patch('conductr_cli.terminal.docker_images', return_value='some-image'), \ patch('conductr_cli.terminal.docker_ps', return_value=''), \ patch('conductr_cli.terminal.docker_inspect', return_value='10.10.10.10'), \ patch('conductr_cli.terminal.docker_run', return_value='') as mock_docker_run, \ patch('conductr_cli.sandbox_proxy.stop_proxy') as mock_stop_proxy, \ patch('conductr_cli.sandbox_common.resolve_running_docker_containers', return_value=[]): args = self.default_args.copy() args.update({'nr_of_instances': nr_of_instances}) input_args = MagicMock(**args) logging_setup.configure_logging(input_args, stdout) features = [] sandbox_run_docker.run(input_args, features) expected_stdout = strip_margin( """||------------------------------------------------| || Starting ConductR | ||------------------------------------------------| |Starting container cond-0.. |Starting container cond-1.. |Starting container cond-2.. |""") expected_image = '{}:{}'.format(CONDUCTR_DEV_IMAGE, LATEST_CONDUCTR_VERSION) self.assertEqual(expected_stdout, self.output(stdout)) # Assert cond-0 mock_docker_run.assert_any_call([ '-d', '--name', 'cond-0', '-e', 'CONDUCTR_INSTANCE=0', '-e', 'CONDUCTR_NR_OF_INSTANCES=3', '-e', 'AKKA_LOGLEVEL=info', '-p', '9000:9000', '-p', '9004:9004', '-p', '9005:9005', '-p', '9006:9006' ], expected_image, self.default_positional_args) # Assert cond-1 mock_docker_run.assert_any_call([ '-d', '--name', 'cond-1', '-e', 'CONDUCTR_INSTANCE=1', '-e', 'CONDUCTR_NR_OF_INSTANCES=3', '-e', 'AKKA_LOGLEVEL=info', '-e', 'SYSLOG_IP=10.10.10.10', '-p', '9010:9000', '-p', '9014:9004', '-p', '9015:9005', '-p', '9016:9006' ], expected_image, self.default_positional_args + ['--seed', '10.10.10.10:9004']) # Assert cond-2 mock_docker_run.assert_any_call([ '-d', '--name', 'cond-2', '-e', 'CONDUCTR_INSTANCE=2', '-e', 'CONDUCTR_NR_OF_INSTANCES=3', '-e', 'AKKA_LOGLEVEL=info', '-e', 'SYSLOG_IP=10.10.10.10', '-p', '9020:9000', '-p', '9024:9004', '-p', '9025:9005', '-p', '9026:9006' ], expected_image, self.default_positional_args + ['--seed', '10.10.10.10:9004']) mock_stop_proxy.assert_called_once_with()
def test_run_options(self): stdout = MagicMock() run_options = { 'CONDUCTR_DOCKER_RUN_OPTS': "-v /etc/haproxy:/usr/local/etc/haproxy" } def run_env(key, default=None): return run_options[key] if key in run_options else os.environ.get( key, default) input_args = MagicMock(**self.default_args) features = [] with \ patch('conductr_cli.terminal.docker_images', return_value=''), \ patch('conductr_cli.terminal.docker_pull', return_value=''), \ patch('conductr_cli.terminal.docker_ps', return_value=''), \ patch('conductr_cli.terminal.docker_inspect', return_value='10.10.10.10'), \ patch('conductr_cli.terminal.docker_run', return_value='') as mock_docker_run, \ patch('conductr_cli.sandbox_proxy.stop_proxy') as mock_stop_proxy, \ patch('conductr_cli.sandbox_common.resolve_running_docker_containers', return_value=[]), \ patch('os.getenv', side_effect=run_env) as mock_get_env: logging_setup.configure_logging(input_args, stdout) sandbox_run_docker.run(input_args, features) expected_stdout = strip_margin( """|Pulling down the ConductR development image.. ||------------------------------------------------| || Starting ConductR | ||------------------------------------------------| |Starting container cond-0.. |""") expected_optional_args = self.default_general_args( 'cond-0') + self.default_env_args + self.default_port_args expected_image = '{}:{}'.format(CONDUCTR_DEV_IMAGE, LATEST_CONDUCTR_VERSION) expected_positional_args = self.default_positional_args self.assertEqual(expected_stdout, self.output(stdout)) mock_get_env.assert_any_call('CONDUCTR_DOCKER_RUN_OPTS') mock_docker_run.assert_called_once_with( expected_optional_args + ['-v', '/etc/haproxy:/usr/local/etc/haproxy'], expected_image, expected_positional_args) mock_stop_proxy.assert_called_once_with()
def test_default_args(self): stdout = MagicMock() input_args = MagicMock(**self.default_args) features = [] with \ patch('conductr_cli.terminal.docker_images', return_value=''), \ patch('conductr_cli.terminal.docker_pull', return_value=''), \ patch('conductr_cli.terminal.docker_ps', return_value=''), \ patch('conductr_cli.terminal.docker_inspect', return_value='10.10.10.10'), \ patch('conductr_cli.terminal.docker_run', return_value='') as mock_docker_run, \ patch('conductr_cli.sandbox_proxy.stop_proxy') as mock_stop_proxy, \ patch('conductr_cli.sandbox_common.resolve_running_docker_containers', return_value=[]): logging_setup.configure_logging(MagicMock(**self.default_args), stdout) sandbox_run_docker.run(input_args, features) expected_stdout = strip_margin( """|Pulling down the ConductR development image.. ||------------------------------------------------| || Starting ConductR | ||------------------------------------------------| |Starting container cond-0.. |""") expected_optional_args = self.default_general_args( 'cond-0') + self.default_env_args + self.default_port_args expected_image = '{}:{}'.format(CONDUCTR_DEV_IMAGE, LATEST_CONDUCTR_VERSION) expected_positional_args = self.default_positional_args self.assertEqual(expected_stdout, self.output(stdout)) mock_docker_run.assert_called_once_with(expected_optional_args, expected_image, expected_positional_args) mock_stop_proxy.assert_called_once_with()
def test_with_custom_args(self): stdout = MagicMock() image_version = '1.1.0' conductr_roles = [['role1', 'role2']] envs = ['key1=value1', 'key2=value2'] image = 'my-image' log_level = 'debug' nr_of_containers = 1 ports = [3000, 3001] features = [ VisualizationFeature(version_args=[], image_version=image_version, offline_mode=False), LoggingFeature(version_args=[], image_version=image_version, offline_mode=False) ] with \ patch('conductr_cli.terminal.docker_images', return_value='some-image'), \ patch('conductr_cli.terminal.docker_ps', return_value=''), \ patch('conductr_cli.terminal.docker_inspect', return_value='10.10.10.10'), \ patch('conductr_cli.terminal.docker_run', return_value='') as mock_docker_run, \ patch('conductr_cli.sandbox_proxy.stop_proxy') as mock_stop_proxy, \ patch('conductr_cli.sandbox_common.resolve_running_docker_containers', return_value=[]): args = self.default_args.copy() args.update({ 'image_version': image_version, 'conductr_roles': conductr_roles, 'envs': envs, 'image': image, 'log_level': log_level, 'nr_of_containers': nr_of_containers, 'ports': ports, 'bundle_http_port': 7222, 'features': features }) input_args = MagicMock(**args) logging_setup.configure_logging(input_args, stdout) sandbox_run_docker.run(input_args, features) expected_stdout = strip_margin( """||------------------------------------------------| || Starting ConductR | ||------------------------------------------------| |Starting container cond-0 exposing 127.0.0.1:3000, 127.0.0.1:3001, 127.0.0.1:5601, 127.0.0.1:9200, 127.0.0.1:9999.. |""") self.assertEqual(expected_stdout, self.output(stdout)) mock_docker_run.assert_called_once_with([ '-d', '--name', 'cond-0', '-e', 'key1=value1', '-e', 'key2=value2', '-e', 'CONDUCTR_INSTANCE=0', '-e', 'CONDUCTR_NR_OF_INSTANCES=1', '-e', 'AKKA_LOGLEVEL=debug', '-e', 'CONDUCTR_FEATURES=visualization,logging', '-e', 'CONDUCTR_ROLES=role1,role2', '-p', '5601:5601', '-p', '9004:9004', '-p', '9005:9005', '-p', '9006:9006', '-p', '9999:9999', '-p', '9200:9200', '-p', '7222:7222', '-p', '3000:3000', '-p', '3001:3001' ], '{}:{}'.format(image, image_version), self.default_positional_args) mock_stop_proxy.assert_called_once_with()