def test_resolve_bundle_configuration_success(self): custom_settings = Mock() first_resolver_mock = Mock() first_resolver_mock.load_bundle_configuration_from_cache = MagicMock(return_value=(False, None, None)) first_resolver_mock.resolve_bundle_configuration = MagicMock(return_value=(False, None, None)) second_resolver_mock = Mock() second_resolver_mock.load_bundle_configuration_from_cache = MagicMock(return_value=(False, None, None)) second_resolver_mock.resolve_bundle_configuration = MagicMock(return_value=(True, 'bundle_name', 'mock bundle_file')) resolver_chain_mock = MagicMock(return_value=[first_resolver_mock, second_resolver_mock]) with patch('conductr_cli.resolver.resolver_chain', resolver_chain_mock): bundle_name, bundle_file = resolver.resolve_bundle_configuration( custom_settings, '/some-cache-dir', '/some-bundle-path') self.assertEqual('bundle_name', bundle_name) self.assertEqual('mock bundle_file', bundle_file) resolver_chain_mock.assert_called_with(custom_settings) first_resolver_mock.load_bundle_configuration_from_cache('/some-cache-dir', '/some-bundle-path') first_resolver_mock.resolve_bundle_configuration.assert_called_with('/some-cache-dir', '/some-bundle-path') second_resolver_mock.load_bundle_configuration_from_cache('/some-cache-dir', '/some-bundle-path') second_resolver_mock.resolve_bundle_configuration.assert_called_with('/some-cache-dir', '/some-bundle-path')
def test_resolve_bundle_configuration_from_cache(self): custom_settings = Mock() first_resolver_mock = Mock() first_resolver_mock.load_bundle_configuration_from_cache = MagicMock( return_value=(True, 'bundle_name', 'mock bundle_file')) resolver_chain_mock = MagicMock(return_value=[first_resolver_mock]) with patch('conductr_cli.resolver.resolver_chain', resolver_chain_mock): bundle_name, bundle_file = resolver.resolve_bundle_configuration( custom_settings, '/some-cache-dir', '/some-bundle-path') self.assertEqual('bundle_name', bundle_name) self.assertEqual('mock bundle_file', bundle_file) resolver_chain_mock.assert_called_with(custom_settings, False) first_resolver_mock.load_bundle_configuration_from_cache( '/some-cache-dir', '/some-bundle-path')
def load_v1(args): log = logging.getLogger(__name__) log.info('Retrieving bundle..') custom_settings = args.custom_settings bundle_resolve_cache_dir = args.bundle_resolve_cache_dir configuration_cache_dir = args.configuration_resolve_cache_dir validate_cache_dir_permissions(bundle_resolve_cache_dir, configuration_cache_dir, log) initial_bundle_file_name, bundle_file = resolver.resolve_bundle( custom_settings, bundle_resolve_cache_dir, args.bundle, args.offline_mode) configuration_file_name, configuration_file = (None, None) if args.configuration is not None: log.info('Retrieving configuration..') configuration_file_name, configuration_file = \ resolver.resolve_bundle_configuration(custom_settings, configuration_cache_dir, args.configuration, args.offline_mode) bundle_conf_text = bundle_utils.conf(bundle_file) bundle_conf = ConfigFactory.parse_string(bundle_conf_text) bundle_file_name, bundle_open_file = open_bundle(initial_bundle_file_name, bundle_file, bundle_conf_text) overlay_bundle_conf = None if configuration_file is None else \ ConfigFactory.parse_string(bundle_utils.conf(configuration_file)) with_bundle_configurations = partial(apply_to_configurations, bundle_conf, overlay_bundle_conf) url = conduct_url.url('bundles', args) files = get_payload(bundle_file_name, bundle_open_file, with_bundle_configurations) if configuration_file is not None: open_configuration_file, config_digest = bundle_utils.digest_extract_and_open( configuration_file) files.append(('configuration', (configuration_file_name, open_configuration_file))) # TODO: Delete the bundle configuration file. # Currently, this results into a permission error on Windows. # Therefore, the deletion is disabled for now. # Issue: https://github.com/typesafehub/conductr-cli/issues/175 # if configuration_file and os.path.exists(configuration_file): # os.remove(configuration_file) log.info('Loading bundle to ConductR..') multipart = create_multipart(log, files) response = conduct_request.post( args.dcos_mode, conductr_host(args), url, data=multipart, auth=args.conductr_auth, verify=args.server_verification_file, headers={'Content-Type': multipart.content_type}) validation.raise_for_status_inc_3xx(response) if log.is_verbose_enabled(): log.verbose(validation.pretty_json(response.text)) response_json = json.loads(response.text) bundle_id = response_json[ 'bundleId'] if args.long_ids else bundle_utils.short_id( response_json['bundleId']) if not args.no_wait: bundle_installation.wait_for_installation(response_json['bundleId'], args) cleanup_old_bundles(bundle_resolve_cache_dir, bundle_file_name, excluded=bundle_file) log.info('Bundle loaded.') if not args.disable_instructions: log.info('Start bundle with: {} run{} {}'.format( args.command, args.cli_parameters, bundle_id)) log.info('Unload bundle with: {} unload{} {}'.format( args.command, args.cli_parameters, bundle_id)) log.info('Print ConductR info with: {} info{}'.format( args.command, args.cli_parameters)) log.info('Print bundle info with: {} info{} {}'.format( args.command, args.cli_parameters, bundle_id)) if not log.is_info_enabled() and log.is_quiet_enabled(): log.quiet(response_json['bundleId']) return True
def load_v1(args): log = logging.getLogger(__name__) log.info('Retrieving bundle..') custom_settings = args.custom_settings resolve_cache_dir = args.resolve_cache_dir validate_cache_dir_permissions(resolve_cache_dir, log) bundle_file_name, bundle_file = resolver.resolve_bundle(custom_settings, resolve_cache_dir, args.bundle) configuration_file_name, configuration_file = (None, None) if args.configuration is not None: log.info('Retrieving configuration..') configuration_file_name, configuration_file = resolver.resolve_bundle_configuration(custom_settings, resolve_cache_dir, args.configuration) bundle_conf = ConfigFactory.parse_string(bundle_utils.conf(bundle_file)) overlay_bundle_conf = None if configuration_file is None else \ ConfigFactory.parse_string(bundle_utils.conf(configuration_file)) with_bundle_configurations = partial(apply_to_configurations, bundle_conf, overlay_bundle_conf) url = conduct_url.url('bundles', args) files = get_payload(bundle_file_name, bundle_file, with_bundle_configurations) if configuration_file is not None: files.append(('configuration', (configuration_file_name, open(configuration_file, 'rb')))) # TODO: Delete the bundle configuration file. # Currently, this results into a permission error on Windows. # Therefore, the deletion is disabled for now. # Issue: https://github.com/typesafehub/conductr-cli/issues/175 # if configuration_file and os.path.exists(configuration_file): # os.remove(configuration_file) log.info('Loading bundle to ConductR..') multipart = create_multipart(log, files) response = conduct_request.post(args.dcos_mode, conductr_host(args), url, data=multipart, auth=args.conductr_auth, verify=args.server_verification_file, headers={'Content-Type': multipart.content_type}) validation.raise_for_status_inc_3xx(response) if log.is_verbose_enabled(): log.verbose(validation.pretty_json(response.text)) response_json = json.loads(response.text) bundle_id = response_json['bundleId'] if args.long_ids else bundle_utils.short_id(response_json['bundleId']) if not args.no_wait: bundle_installation.wait_for_installation(response_json['bundleId'], args) cleanup_old_bundles(resolve_cache_dir, bundle_file_name, excluded=bundle_file) log.info('Bundle loaded.') if not args.disable_instructions: log.info('Start bundle with: {} run{} {}'.format(args.command, args.cli_parameters, bundle_id)) log.info('Unload bundle with: {} unload{} {}'.format(args.command, args.cli_parameters, bundle_id)) log.info('Print ConductR info with: {} info{}'.format(args.command, args.cli_parameters)) if not log.is_info_enabled() and log.is_quiet_enabled(): log.quiet(response_json['bundleId']) return True