def test_serve_dir(self, mload, mos, mbackend, mHTTPServer, mprint, mexit): mload.return_value = ResourceContainer('od') mos.path.exists.return_value = True jujuresources.cli.resources(['serve', '-d', 'od']) mload.assert_called_once_with('resources.yaml', 'od') mos.path.exists.assert_called_once_with('od') mbackend.PyPIResource.build_pypi_indexes.assert_called_with('od') mos.chdir.assert_called_once_with('od') self.assertIs(mHTTPServer.allow_reuse_address, True) mHTTPServer.assert_called_once_with(('', 8080), jujuresources.cli.SimpleHTTPRequestHandler)
def _load(resources_yaml, output_dir=None): if (resources_yaml, output_dir) not in resources_cache: url = resources_yaml parsed_url = urlparse(url) if not parsed_url.scheme: url = 'file://%s' % os.path.join(os.getcwd(), url) with contextlib.closing(urlopen(url)) as fp: resdefs = yaml.load(fp) _output_dir = output_dir or resdefs.get('options', {}).get( 'output_dir', 'resources') resources = ResourceContainer(_output_dir) for name, resource in resdefs.get('resources', {}).items(): resources.add_required(name, resource) for name, resource in resdefs.get('optional_resources', {}).items(): resources.add_optional(name, resource) resources_cache[(resources_yaml, output_dir)] = resources return resources_cache[(resources_yaml, output_dir)]
def setUp(self): self.resources = ResourceContainer('resources') self.resources.add_required('valid', { 'url': 'valid', 'filename': 'valid', 'destination': 'res-defaults.yaml', 'hash': '4f08575d804517cea2265a7d43022771', 'hash_type': 'md5', }) self.resources.add_required('invalid', { 'url': 'invalid', 'filename': 'invalid', 'destination': 'res-defaults.yaml', 'hash': 'deadbeef', 'hash_type': 'md5', }) self.resources.add_optional('opt-invalid', { 'url': 'opt-invalid', 'filename': 'opt-invalid', 'destination': 'res-defaults.yaml', 'hash': 'deadbeef', 'hash_type': 'md5', }) def mep(name, target): m = mock.Mock() m.name = name m.load.return_value = target return m self._piep = mock.patch.object(jujuresources.cli, 'iter_entry_points') self._miep = self._piep.start() self._miep.return_value = [ mep('fetch', jujuresources.cli.fetch), mep('verify', jujuresources.cli.verify), mep('install', jujuresources.cli.install), mep('resource_path', jujuresources.cli.resource_path), mep('resource_spec', jujuresources.cli.resource_spec), mep('serve', jujuresources.cli.serve), ]
def test_serve_no_fetch(self, mload, mos, msys, mexit): mload.return_value = ResourceContainer('od') mos.path.exists.return_value = False jujuresources.cli.resources(['serve', '-d', 'od']) msys.stderr.write.assert_called_once_with("Resources dir 'od' not found. Did you fetch?\n") mexit.assert_called_once_with(1)