def test_success_with_configuration(self): tmpdir, config_file = create_temp_bundle_with_contents({ 'bundle.conf': '{name="overlaid-name"}', 'config.sh': 'echo configuring' }) urlretrieve_mock = MagicMock(side_effect=[(self.bundle_file, ()), (config_file, ())]) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) with patch('conductr_cli.conduct_load.urlretrieve', urlretrieve_mock), \ patch('requests.post', http_method), \ patch('sys.stdout', stdout), \ patch('builtins.open', open_mock): args = self.default_args.copy() args.update({'configuration': config_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( open_mock.call_args_list, [call(self.bundle_file, 'rb'), call(config_file, 'rb')] ) expected_files = self.default_files + [('configuration', ('bundle.zip', 1))] expected_files[4] = ('bundleName', 'overlaid-name') http_method.assert_called_with(self.default_url, files=expected_files) self.assertEqual(self.default_output(downloading_configuration='Retrieving configuration...\n'), self.output(stdout))
def test_failure_no_bundle(self): urlretrieve_mock = MagicMock(side_effect=URLError('no_such.bundle')) stderr = MagicMock() with patch('conductr_cli.conduct_load.urlretrieve', urlretrieve_mock), patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': 'no_such.bundle'}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: File not found: no_such.bundle |"""), self.output(stderr))
def base_test_success(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock() cleanup_old_bundles_mock = MagicMock() input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('conductr_cli.conduct_load.cleanup_old_bundles', cleanup_old_bundles_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1'}) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) cleanup_old_bundles_mock.assert_called_with(self.bundle_resolve_cache_dir, self.bundle_file_name, excluded=self.bundle_file) self.assertEqual(self.default_output(), self.output(stdout))
def base_test_failure_no_response(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.raise_read_timeout_error('test reason', self.default_url) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(input_args, stdout, stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1'}) self.assertEqual( as_error( strip_margin("""|Error: Timed out waiting for response from the server: test reason |Error: One possible issue may be that there are not enough resources or machines with the roles that your bundle requires |""")), self.output(stderr))
def base_test_failure_invalid_address(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.raise_connection_error('test reason', self.default_url) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(input_args, stdout, stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1'}) self.assertEqual( self.default_connection_error.format(self.default_url), self.output(stderr))
def base_test_success_verbose(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock() args = self.default_args.copy() args.update({'verbose': True}) input_args = MagicMock(**args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual(self.default_output(verbose=self.default_response), self.output(stdout))
def base_test_failure_install_timeout(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.respond_with(200, self.default_response) stderr = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock(side_effect=WaitTimeoutError('test timeout')) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, err_output=stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual( as_error(strip_margin("""|Error: Timed out: test timeout |""")), self.output(stderr))
def base_test_failure_no_configuration(self): resolve_bundle_mock = MagicMock(side_effect=[(self.bundle_name, self.bundle_file), BundleResolutionError('some message')]) stdout = MagicMock() stderr = MagicMock() with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock): args = self.default_args.copy() args.update({'configuration': 'no_such.conf'}) logging_setup.configure_logging(MagicMock(**args), stdout, stderr) result = conduct_load.load(MagicMock(**args)) self.assertFalse(result) self.assertEqual( resolve_bundle_mock.call_args_list, [ call(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file), call(self.custom_settings, self.bundle_resolve_cache_dir, 'no_such.conf') ] ) self.assertEqual( as_error(strip_margin("""|Error: Bundle not found: some message |""")), self.output(stderr))
def base_test_failure_install_timeout(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.respond_with(200, self.default_response) stderr = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock(side_effect=WaitTimeoutError('test timeout')) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, err_output=stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1'}) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual( as_error(strip_margin("""|Error: Timed out: test timeout |""")), self.output(stderr))
def test_failure_roles_not_a_list(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |roles = {} |""").format(self.nr_of_cpus, self.memory, self.disk_space, '-'.join(self.roles))) resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, bundle_file)) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock): args = self.default_args.copy() args.update({'bundle': bundle_file}) logging_setup.configure_logging(MagicMock(**args), err_output=stderr) result = conduct_load.load(MagicMock(**args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, bundle_file, self.offline_mode) self.assertEqual( as_error( strip_margin("""|Error: Unable to parse bundle.conf. |Error: roles has type 'str' rather than 'list'. |""")), self.output(stderr)) shutil.rmtree(tmpdir)
def base_test_failure_invalid_address(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.raise_connection_error('test reason', self.default_url) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(input_args, stdout, stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1'}) self.assertEqual( self.default_connection_error.format(self.default_url), self.output(stderr))
def test_failure_no_bundle_conf(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) conf_mock = MagicMock(return_value=None) stderr = MagicMock() with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.bundle_utils.conf', conf_mock): args = self.default_args.copy() logging_setup.configure_logging(MagicMock(**args), err_output=stderr) result = conduct_load.load(MagicMock(**args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) self.assertEqual( as_error( strip_margin( """|Error: Problem with the bundle: Unable to find bundle.conf within the bundle file |""")), self.output(stderr)) conf_mock.assert_called_with(self.bundle_file)
def base_test_failure_no_configuration(self): resolve_bundle_mock = MagicMock( side_effect=[(self.bundle_name, self.bundle_file), BundleResolutionError('some message')]) stdout = MagicMock() stderr = MagicMock() with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock): args = self.default_args.copy() args.update({'configuration': 'no_such.conf'}) logging_setup.configure_logging(MagicMock(**args), stdout, stderr) result = conduct_load.load(MagicMock(**args)) self.assertFalse(result) self.assertEqual(resolve_bundle_mock.call_args_list, [ call(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file), call(self.custom_settings, self.bundle_resolve_cache_dir, 'no_such.conf') ]) self.assertEqual( as_error( strip_margin("""|Error: Bundle not found: some message |""")), self.output(stderr))
def base_test_success(self): request_headers_mock = MagicMock(return_value=self.mock_headers) resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock() input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.conduct_url.request_headers', request_headers_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) request_headers_mock.assert_called_with(input_args) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT, headers=self.mock_headers) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual(self.default_output(), self.output(stdout))
def base_test_success(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock() cleanup_old_bundles_mock = MagicMock() input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('conductr_cli.conduct_load.cleanup_old_bundles', cleanup_old_bundles_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1'}) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) cleanup_old_bundles_mock.assert_called_with(self.bundle_resolve_cache_dir, self.bundle_file_name, excluded=self.bundle_file) self.assertEqual(self.default_output(), self.output(stdout))
def base_test_failure_invalid_address(self): request_headers_mock = MagicMock(return_value=self.mock_headers) resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.raise_connection_error('test reason', self.default_url) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_url.request_headers', request_headers_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(input_args, stdout, stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) request_headers_mock.assert_called_with(input_args) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT, headers=self.mock_headers) self.assertEqual( self.default_connection_error.format(self.default_url), self.output(stderr))
def base_test_failure_no_response(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.raise_read_timeout_error('test reason', self.default_url) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(input_args, stdout, stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1'}) self.assertEqual( as_error( strip_margin("""|Error: Timed out waiting for response from the server: test reason |Error: One possible issue may be that there are not enough resources or machines with the roles that your bundle requires |""")), self.output(stderr))
def test_failure_no_disk_space(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin( """|nrOfCpus = {} |memory = {} |roles = [{}] |""" ).format(self.nr_of_cpus, self.memory, ", ".join(self.roles)) ) resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, bundle_file)) with patch("conductr_cli.resolver.resolve_bundle", resolve_bundle_mock): args = self.default_args.copy() args.update({"bundle": bundle_file}) logging_setup.configure_logging(MagicMock(**args), err_output=stderr) result = conduct_load.load(MagicMock(**args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, bundle_file) self.assertEqual( as_error( strip_margin( """|Error: Unable to parse bundle.conf. |Error: No configuration setting found for key diskSpace. |""" ) ), self.output(stderr), ) shutil.rmtree(tmpdir)
def test_success_with_configuration(self): tmpdir, config_file = create_temp_bundle_with_contents({ 'bundle.conf': '{name="overlaid-name"}', 'config.sh': 'echo configuring' }) request_headers_mock = MagicMock(return_value=self.mock_headers) resolve_bundle_mock = MagicMock(side_effect=[(self.bundle_name, self.bundle_file), ('config.zip', config_file)]) zip_entry_mock = MagicMock(side_effect=['mock bundle.conf', 'mock bundle.conf overlay']) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock() args = self.default_args.copy() args.update({'configuration': config_file}) input_args = MagicMock(**args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.bundle_utils.zip_entry', zip_entry_mock), \ patch('conductr_cli.conduct_url.request_headers', request_headers_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) self.assertEqual( resolve_bundle_mock.call_args_list, [ call(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file), call(self.custom_settings, self.bundle_resolve_cache_dir, config_file) ] ) self.assertEqual( open_mock.call_args_list, [call(self.bundle_file, 'rb'), call(config_file, 'rb')] ) request_headers_mock.assert_called_with(input_args) expected_files = [ ('bundleConf', ('bundle.conf', 'mock bundle.conf')), ('bundleConfOverlay', ('bundle.conf', 'mock bundle.conf overlay')), ('bundle', ('bundle.zip', 1)), ('configuration', ('config.zip', 1)) ] http_method.assert_called_with(self.default_url, files=expected_files, timeout=LOAD_HTTP_TIMEOUT, headers=self.mock_headers) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual(self.default_output(downloading_configuration='Retrieving configuration...\n'), self.output(stdout)) self.assertEqual( zip_entry_mock.call_args_list, [call('bundle.conf', self.bundle_file), call('bundle.conf', config_file)] )
def base_test_failure_no_response(self): request_headers_mock = MagicMock(return_value=self.mock_headers) resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.raise_read_timeout_error('test reason', self.default_url) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_url.request_headers', request_headers_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(input_args, stdout, stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) request_headers_mock.assert_called_with(input_args) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT, headers=self.mock_headers) self.assertEqual( as_error( strip_margin( """|Error: Timed out waiting for response from the server: test reason |Error: One possible issue may be that there are not enough resources or machines with the roles that your bundle requires |""")), self.output(stderr))
def test_success(self): urlretrieve_mock = MagicMock(return_value=(self.bundle_file, ())) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) with patch('conductr_cli.conduct_load.urlretrieve', urlretrieve_mock), \ patch('requests.post', http_method), \ patch('sys.stdout', stdout), \ patch('builtins.open', open_mock): conduct_load.load(MagicMock(**self.default_args)) open_mock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual(self.default_output(), self.output(stdout))
def base_test_failure_bad_zip(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) stderr = MagicMock() bundle_open_mock = MagicMock( side_effect=BadZipFile('test bad zip error')) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.open_bundle', bundle_open_mock): logging_setup.configure_logging(MagicMock(**self.default_args), err_output=stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) bundle_open_mock.assert_called_with( self.bundle_file_name, self.bundle_file, bundle_utils.conf(self.bundle_file)) self.assertEqual( as_error( strip_margin( """|Error: Problem with the bundle: test bad zip error |""")), self.output(stderr))
def test_failure_roles_not_a_list(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |roles = {} |""").format(self.nr_of_cpus, self.memory, self.disk_space, '-'.join(self.roles))) resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, bundle_file)) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock): args = self.default_args.copy() args.update({'bundle': bundle_file}) logging_setup.configure_logging(MagicMock(**args), err_output=stderr) result = conduct_load.load(MagicMock(**args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, bundle_file) self.assertEqual( as_error(strip_margin("""|Error: Unable to parse bundle.conf. |Error: roles has type 'str' rather than 'list'. |""")), self.output(stderr)) shutil.rmtree(tmpdir)
def test_failure_invalid_address(self): urlretrieve_mock = MagicMock(return_value=(self.bundle_file, ())) http_method = self.raise_connection_error('test reason', self.default_url) stderr = MagicMock() open_mock = MagicMock(return_value=1) with patch('conductr_cli.conduct_load.urlretrieve', urlretrieve_mock), \ patch('requests.post', http_method), \ patch('sys.stderr', stderr), \ patch('builtins.open', open_mock): conduct_load.load(MagicMock(**self.default_args)) open_mock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual( self.default_connection_error.format(self.default_url), self.output(stderr))
def test_success_long_ids(self): urlretrieve_mock = MagicMock(return_value=(self.bundle_file, ())) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) with patch('conductr_cli.conduct_load.urlretrieve', urlretrieve_mock), \ patch('requests.post', http_method), \ patch('sys.stdout', stdout), \ patch('builtins.open', open_mock): args = self.default_args.copy() args.update({'long_ids': True}) conduct_load.load(MagicMock(**args)) open_mock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual(self.default_output(bundle_id='45e0c477d3e5ea92aa8d85c0d8f3e25c'), self.output(stdout))
def test_success_with_configuration(self): tmpdir, config_file = create_temp_bundle_with_contents({ 'bundle.conf': '{name="overlaid-name"}', 'config.sh': 'echo configuring' }) request_headers_mock = MagicMock(return_value=self.mock_headers) resolve_bundle_mock = MagicMock( side_effect=[(self.bundle_name, self.bundle_file), ('config.zip', config_file)]) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock() args = self.default_args.copy() args.update({'configuration': config_file}) input_args = MagicMock(**args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_url.request_headers', request_headers_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) self.assertEqual( open_mock.call_args_list, [call(self.bundle_file, 'rb'), call(config_file, 'rb')]) self.assertEqual(resolve_bundle_mock.call_args_list, [ call(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file), call(self.custom_settings, self.bundle_resolve_cache_dir, config_file) ]) request_headers_mock.assert_called_with(input_args) expected_files = self.default_files + [('configuration', ('config.zip', 1))] expected_files[4] = ('bundleName', 'overlaid-name') http_method.assert_called_with(self.default_url, files=expected_files, timeout=LOAD_HTTP_TIMEOUT, headers=self.mock_headers) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual( self.default_output( downloading_configuration='Retrieving configuration...\n'), self.output(stdout))
def test_failure(self): urlretrieve_mock = MagicMock(return_value=(self.bundle_file, ())) http_method = self.respond_with(404) stderr = MagicMock() open_mock = MagicMock(return_value=1) with patch('conductr_cli.conduct_load.urlretrieve', urlretrieve_mock), \ patch('requests.post', http_method), \ patch('sys.stderr', stderr), \ patch('builtins.open', open_mock): conduct_load.load(MagicMock(**self.default_args)) open_mock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual( strip_margin("""|ERROR: 404 Not Found |"""), self.output(stderr))
def test_success_custom_ip_port(self): urlretrieve_mock = MagicMock(return_value=(self.bundle_file, ())) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) cli_parameters = ' --ip 127.0.1.1 --port 9006' with patch('conductr_cli.conduct_load.urlretrieve', urlretrieve_mock),\ patch('requests.post', http_method), \ patch('sys.stdout', stdout), \ patch('builtins.open', open_mock): args = self.default_args.copy() args.update({'cli_parameters': cli_parameters}) conduct_load.load(MagicMock(**args)) open_mock.assert_called_with(self.bundle_file, 'rb') http_method.assert_called_with(self.default_url, files=self.default_files) self.assertEqual( self.default_output(params=cli_parameters), self.output(stdout))
def test_failure_no_roles(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |""").format(self.nr_of_cpus, self.memory, self.disk_space)) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: No configuration setting found for key roles. |"""), self.output(stderr)) shutil.rmtree(tmpdir)
def base_test_success_dcos_mode(self): self.default_args['dcos_mode'] = True self.default_args['command'] = 'dcos conduct' resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() bundle_open_mock = MagicMock(side_effect=lambda p1, p2, p3: (p1, 1)) wait_for_installation_mock = MagicMock() cleanup_old_bundles_mock = MagicMock() input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('conductr_cli.conduct_load.cleanup_old_bundles', cleanup_old_bundles_mock), \ patch('dcos.http.post', http_method), \ patch('conductr_cli.conduct_load.open_bundle', bundle_open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) bundle_open_mock.assert_called_with( self.bundle_file_name, self.bundle_file, bundle_utils.conf(self.bundle_file)) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={ 'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1' }) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) cleanup_old_bundles_mock.assert_called_with( self.bundle_resolve_cache_dir, self.bundle_file_name, excluded=self.bundle_file) self.assertEqual( self.default_output(command=self.default_args['command']), self.output(stdout))
def test_failure_roles_not_a_list(self): stderr = MagicMock() tmpdir, bundle_file = create_temp_bundle( strip_margin("""|nrOfCpus = {} |memory = {} |diskSpace = {} |roles = {} |""").format(self.nr_of_cpus, self.memory, self.disk_space, '-'.join(self.roles))) with patch('sys.stderr', stderr): args = self.default_args.copy() args.update({'bundle': bundle_file}) conduct_load.load(MagicMock(**args)) self.assertEqual( strip_margin("""|ERROR: Unable to parse bundle.conf. |ERROR: roles has type 'str' rather than 'list'. |"""), self.output(stderr)) shutil.rmtree(tmpdir)
def test_success_with_configuration(self): tmpdir, config_file = create_temp_bundle_with_contents( {"bundle.conf": '{name="overlaid-name"}', "config.sh": "echo configuring"} ) resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) resolve_bundle_configuration_mock = MagicMock(return_value=("config.zip", config_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock() args = self.default_args.copy() args.update({"configuration": config_file}) input_args = MagicMock(**args) with patch("conductr_cli.resolver.resolve_bundle", resolve_bundle_mock), patch( "conductr_cli.resolver.resolve_bundle_configuration", resolve_bundle_configuration_mock ), patch("conductr_cli.conduct_load.create_multipart", create_multipart_mock), patch( "requests.post", http_method ), patch( "builtins.open", open_mock ), patch( "conductr_cli.bundle_installation.wait_for_installation", wait_for_installation_mock ): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) self.assertEqual(open_mock.call_args_list, [call(self.bundle_file, "rb"), call(config_file, "rb")]) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) resolve_bundle_configuration_mock.assert_called_with( self.custom_settings, self.bundle_resolve_cache_dir, config_file ) expected_files = self.default_files + [("configuration", ("config.zip", 1))] expected_files[4] = ("bundleName", "overlaid-name") create_multipart_mock.assert_called_with(self.conduct_load_logger, expected_files) http_method.assert_called_with( self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={"Content-Type": self.multipart_content_type, "Host": "127.0.0.1"}, ) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual( self.default_output(downloading_configuration="Retrieving configuration..\n"), self.output(stdout) )
def test_success_with_configuration(self): tmpdir, config_file = create_temp_bundle_with_contents({ 'bundle.conf': '{name="overlaid-name"}', 'config.sh': 'echo configuring' }) resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) resolve_bundle_configuration_mock = MagicMock(return_value=('config.zip', config_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock() args = self.default_args.copy() args.update({'configuration': config_file}) input_args = MagicMock(**args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.resolver.resolve_bundle_configuration', resolve_bundle_configuration_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) self.assertEqual( open_mock.call_args_list, [call(self.bundle_file, 'rb'), call(config_file, 'rb')] ) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) resolve_bundle_configuration_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, config_file, self.offline_mode) expected_files = self.default_files + [('configuration', ('config.zip', 1))] expected_files[4] = ('bundleName', 'overlaid-name') create_multipart_mock.assert_called_with(self.conduct_load_logger, expected_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1'}) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual(self.default_output(downloading_configuration='Retrieving configuration..\n'), self.output(stdout))
def base_test_failure_no_file_url_error(self): resolve_bundle_mock = MagicMock(side_effect=URLError('reason', None)) stderr = MagicMock() with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock): logging_setup.configure_logging(MagicMock(**self.default_args), err_output=stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) self.assertEqual( as_error(strip_margin("""|Error: File not found: reason |""")), self.output(stderr))
def base_test_failure_no_file_http_error(self): add_info_mock = MagicMock() resolve_bundle_mock = MagicMock(side_effect=HTTPError('url', 'code', 'message', 'headers', add_info_mock)) stderr = MagicMock() with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock): logging_setup.configure_logging(MagicMock(**self.default_args), err_output=stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) self.assertEqual( as_error(strip_margin("""|Error: Resource not found: url |""")), self.output(stderr))
def base_test_failure_no_file_url_error(self): resolve_bundle_mock = MagicMock(side_effect=URLError('reason', None)) stderr = MagicMock() with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock): logging_setup.configure_logging(MagicMock(**self.default_args), err_output=stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) self.assertEqual( as_error(strip_margin("""|Error: File not found: reason |""")), self.output(stderr))
def base_test_failure_no_file_http_error(self): add_info_mock = MagicMock() resolve_bundle_mock = MagicMock(side_effect=HTTPError('url', 'code', 'message', 'headers', add_info_mock)) stderr = MagicMock() with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock): logging_setup.configure_logging(MagicMock(**self.default_args), err_output=stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) self.assertEqual( as_error(strip_margin("""|Error: Resource not found: url |""")), self.output(stderr))
def base_test_failure_bad_zip(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) stderr = MagicMock() open_mock = MagicMock(side_effect=BadZipFile('test bad zip error')) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('builtins.open', open_mock): logging_setup.configure_logging(MagicMock(**self.default_args), err_output=stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) open_mock.assert_called_with(self.bundle_file, 'rb') self.assertEqual( as_error(strip_margin("""|Error: Problem with the bundle: test bad zip error |""")), self.output(stderr))
def test_failure_no_bundle_conf(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) zip_entry_mock = MagicMock(return_value=None) stderr = MagicMock() with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.bundle_utils.zip_entry', zip_entry_mock): args = self.default_args.copy() logging_setup.configure_logging(MagicMock(**args), err_output=stderr) result = conduct_load.load(MagicMock(**args)) self.assertFalse(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) self.assertEqual( as_error(strip_margin("""|Error: Problem with the bundle: Unable to find bundle.conf within the bundle file |""")), self.output(stderr)) zip_entry_mock.assert_called_with('bundle.conf', self.bundle_file)
def base_test_failure(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.respond_with(404) stdout = MagicMock() stderr = MagicMock() bundle_open_mock = MagicMock(side_effect=lambda p1, p2, p3: (p1, 1)) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('requests.post', http_method), \ patch('conductr_cli.conduct_load.open_bundle', bundle_open_mock): logging_setup.configure_logging(input_args, stdout, stderr) result = conduct_load.load(input_args) self.assertFalse(result) bundle_open_mock.assert_called_with( self.bundle_file_name, self.bundle_file, bundle_utils.conf(self.bundle_file)) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) create_multipart_mock.assert_called_with(self.conduct_load_logger, self.default_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={ 'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1' }) self.assertEqual( as_error( strip_margin("""|Error: 404 Not Found |""")), self.output(stderr))
def base_test_failure_invalid_address(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.raise_connection_error('test reason', self.default_url) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(MagicMock(**self.default_args), stdout, stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT) self.assertEqual( self.default_connection_error.format(self.default_url), self.output(stderr))
def base_test_failure(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.respond_with(404) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(MagicMock(**self.default_args), stdout, stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT) self.assertEqual( as_error(strip_margin("""|Error: 404 Not Found |""")), self.output(stderr))
def base_test_failure_install_timeout(self): request_headers_mock = MagicMock(return_value=self.mock_headers) resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.respond_with(200, self.default_response) stderr = MagicMock() open_mock = MagicMock(return_value=1) wait_for_installation_mock = MagicMock( side_effect=WaitTimeoutError('test timeout')) input_args = MagicMock(**self.default_args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.conduct_url.request_headers', request_headers_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, err_output=stderr) result = conduct_load.load(input_args) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) request_headers_mock.assert_called_with(input_args) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT, headers=self.mock_headers) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual( as_error( strip_margin("""|Error: Timed out: test timeout |""")), self.output(stderr))
def base_test_failure_no_response(self): resolve_bundle_mock = MagicMock(return_value=(self.bundle_name, self.bundle_file)) http_method = self.raise_read_timeout_error('test reason', self.default_url) stdout = MagicMock() stderr = MagicMock() open_mock = MagicMock(return_value=1) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('requests.post', http_method), \ patch('builtins.open', open_mock): logging_setup.configure_logging(MagicMock(**self.default_args), stdout, stderr) result = conduct_load.load(MagicMock(**self.default_args)) self.assertFalse(result) open_mock.assert_called_with(self.bundle_file, 'rb') resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file) http_method.assert_called_with(self.default_url, files=self.default_files, timeout=LOAD_HTTP_TIMEOUT) self.assertEqual( as_error( strip_margin("""|Error: Timed out waiting for response from the server: test reason |Error: One possible issue may be that there are not enough resources or machines with the roles that your bundle requires |""")), self.output(stderr))
def test_success_with_configuration_no_bundle_conf(self): tmpdir, config_file = create_temp_bundle_with_contents( {'config.sh': 'echo configuring'}) resolve_bundle_mock = MagicMock(return_value=(self.bundle_file_name, self.bundle_file)) resolve_bundle_configuration_mock = MagicMock( return_value=('config.zip', config_file)) conf_mock = MagicMock(side_effect=['mock bundle.conf', None]) string_io_mock = MagicMock( return_value='mock bundle.conf - string i/o') create_multipart_mock = MagicMock(return_value=self.multipart_mock) http_method = self.respond_with(200, self.default_response) stdout = MagicMock() open_mock = MagicMock(return_value=(1, None)) bundle_open_mock = MagicMock(side_effect=lambda p1, p2, p3: (p1, 1)) wait_for_installation_mock = MagicMock() args = self.default_args.copy() args.update({'configuration': config_file}) input_args = MagicMock(**args) with patch('conductr_cli.resolver.resolve_bundle', resolve_bundle_mock), \ patch('conductr_cli.resolver.resolve_bundle_configuration', resolve_bundle_configuration_mock), \ patch('conductr_cli.bundle_utils.conf', conf_mock), \ patch('conductr_cli.conduct_load.string_io', string_io_mock), \ patch('conductr_cli.conduct_load.create_multipart', create_multipart_mock), \ patch('requests.post', http_method), \ patch('conductr_cli.bundle_utils.digest_extract_and_open', open_mock), \ patch('conductr_cli.conduct_load.open_bundle', bundle_open_mock), \ patch('conductr_cli.bundle_installation.wait_for_installation', wait_for_installation_mock): logging_setup.configure_logging(input_args, stdout) result = conduct_load.load(input_args) self.assertTrue(result) resolve_bundle_mock.assert_called_with(self.custom_settings, self.bundle_resolve_cache_dir, self.bundle_file, self.offline_mode) resolve_bundle_configuration_mock.assert_called_with( self.custom_settings, self.configuration_resolve_cache_dir, config_file, self.offline_mode) self.assertEqual( conf_mock.call_args_list, [call(self.bundle_file), call(config_file)]) string_io_mock.assert_called_with('mock bundle.conf') self.assertEqual(bundle_open_mock.call_args_list, [ call(self.bundle_file_name, self.bundle_file, 'mock bundle.conf') ]) self.assertEqual(open_mock.call_args_list, [call(config_file)]) expected_files = [('bundleConf', ('bundle.conf', 'mock bundle.conf - string i/o')), ('bundle', ('bundle.zip', 1)), ('configuration', ('config.zip', 1))] create_multipart_mock.assert_called_with(self.conduct_load_logger, expected_files) http_method.assert_called_with(self.default_url, data=self.multipart_mock, auth=self.conductr_auth, verify=self.server_verification_file, headers={ 'Content-Type': self.multipart_content_type, 'Host': '127.0.0.1' }) wait_for_installation_mock.assert_called_with(self.bundle_id, input_args) self.assertEqual( self.default_output( downloading_configuration='Retrieving configuration..\n'), self.output(stdout))