def test_unzip_from_uri_reads_AWS_CA_BUNDLE_env_var( self, os_patch, open_patch, requests_patch, progressbar_patch, path_patch, unzip_patch): get_request_mock = Mock() get_request_mock.headers = {"Content-length": "200"} get_request_mock.iter_content.return_value = [b"data1"] requests_patch.get.return_value = get_request_mock file_mock = Mock() open_patch.return_value.__enter__.return_value = file_mock progressbar_mock = Mock() progressbar_patch.return_value.__enter__.return_value = progressbar_mock path_mock = Mock() path_mock.exists.return_value = True path_patch.return_value = path_mock os_patch.environ.get.return_value = "/some/path/on/the/system" unzip_from_uri("uri", "layer_zip_path", "output_zip_dir", "layer_arn") requests_patch.get.assert_called_with( "uri", stream=True, verify="/some/path/on/the/system") get_request_mock.iter_content.assert_called_with(chunk_size=None) open_patch.assert_called_with("layer_zip_path", "wb") file_mock.write.assert_called_with(b"data1") progressbar_mock.update.assert_called_with(5) path_patch.assert_called_with("layer_zip_path") path_mock.unlink.assert_called() unzip_patch.assert_called_with("layer_zip_path", "output_zip_dir", permission=0o700) os_patch.environ.get.assert_called_with("AWS_CA_BUNDLE", True)
def test_successfully_unzip_from_uri(self, os_patch, open_patch, requests_patch, progressbar_patch, path_patch, unzip_patch): get_request_mock = Mock() get_request_mock.headers = {"Content-length": "200"} get_request_mock.iter_content.return_value = [b'data1'] requests_patch.get.return_value = get_request_mock file_mock = Mock() open_patch.return_value.__enter__.return_value = file_mock progressbar_mock = Mock() progressbar_patch.return_value.__enter__.return_value = progressbar_mock path_mock = Mock() path_mock.exists.return_value = True path_patch.return_value = path_mock os_patch.environ.get.return_value = True unzip_from_uri('uri', 'layer_zip_path', 'output_zip_dir', 'layer_arn') requests_patch.get.assert_called_with('uri', stream=True, verify=True) get_request_mock.iter_content.assert_called_with(chunk_size=None) open_patch.assert_called_with('layer_zip_path', 'wb') file_mock.write.assert_called_with(b'data1') progressbar_mock.update.assert_called_with(5) path_patch.assert_called_with('layer_zip_path') path_mock.unlink.assert_called() unzip_patch.assert_called_with('layer_zip_path', 'output_zip_dir', permission=0o700) os_patch.environ.get.assert_called_with('AWS_CA_BUNDLE', True)
def test_successfully_unzip_from_uri(self, open_patch, requests_patch, progressbar_patch, path_patch, unzip_patch): get_request_mock = Mock() get_request_mock.headers = {"Content-length": "200"} get_request_mock.iter_content.return_value = [b'data1'] requests_patch.get.return_value = get_request_mock file_mock = Mock() open_patch.return_value.__enter__.return_value = file_mock progressbar_mock = Mock() progressbar_patch.return_value.__enter__.return_value = progressbar_mock path_mock = Mock() path_mock.exists.return_value = True path_patch.return_value = path_mock unzip_from_uri('uri', 'layer_zip_path', 'output_zip_dir', 'layer_arn') requests_patch.get.assert_called_with('uri', stream=True) get_request_mock.iter_content.assert_called_with(chunk_size=None) open_patch.assert_called_with('layer_zip_path', 'wb') file_mock.write.assert_called_with(b'data1') progressbar_mock.update.assert_called_with(5) path_patch.assert_called_with('layer_zip_path') path_mock.unlink.assert_called() unzip_patch.assert_called_with('layer_zip_path', 'output_zip_dir', permission=0o700)
def test_not_unlink_file_when_file_doesnt_exist(self, open_patch, requests_patch, progressbar_patch, path_patch, unzip_patch): get_request_mock = Mock() get_request_mock.headers = {"Content-length": "200"} get_request_mock.iter_content.return_value = [b'data1'] requests_patch.get.return_value = get_request_mock file_mock = Mock() open_patch.return_value.__enter__.return_value = file_mock progressbar_mock = Mock() progressbar_patch.return_value.__enter__.return_value = progressbar_mock path_mock = Mock() path_mock.exists.return_value = False path_patch.return_value = path_mock unzip_from_uri('uri', 'layer_zip_path', 'output_zip_dir', 'layer_arn') requests_patch.get.assert_called_with('uri', stream=True) get_request_mock.iter_content.assert_called_with(chunk_size=None) open_patch.assert_called_with('layer_zip_path', 'wb') file_mock.write.assert_called_with(b'data1') progressbar_mock.update.assert_called_with(5) path_patch.assert_called_with('layer_zip_path') path_mock.unlink.assert_not_called() unzip_patch.assert_called_with('layer_zip_path', 'output_zip_dir', permission=0o700)
def download(self, layer: LayerVersion, force=False) -> LayerVersion: """ Download a given layer to the local cache. Parameters ---------- layer samcli.commands.local.lib.provider.Layer Layer representing the layer to be downloaded. force bool True to download the layer even if it exists already on the system Returns ------- Path Path object that represents where the layer is download to """ if layer.is_defined_within_template: LOG.info("%s is a local Layer in the template", layer.name) # the template file containing the layer might not be in the same directory as root template file # therefore we need to join the path of template directory and codeuri in case codeuri is a relative path. try: stack = next(stack for stack in self._stacks if stack.stack_path == layer.stack_path) except StopIteration as ex: raise RuntimeError( f"Cannot find stack that matches layer's stack_path {layer.stack_path}" ) from ex codeuri = (layer.codeuri if os.path.isabs(layer.codeuri) else os.path.normpath( os.path.join(os.path.dirname(stack.location), layer.codeuri))) layer.codeuri = resolve_code_path(self.cwd, codeuri) return layer layer_path = Path(self.layer_cache).resolve().joinpath(layer.name) is_layer_downloaded = self._is_layer_cached(layer_path) layer.codeuri = str(layer_path) if is_layer_downloaded and not force: LOG.info("%s is already cached. Skipping download", layer.arn) return layer layer_zip_path = layer.codeuri + ".zip" layer_zip_uri = self._fetch_layer_uri(layer) unzip_from_uri( layer_zip_uri, layer_zip_path, unzip_output_dir=layer.codeuri, progressbar_label="Downloading {}".format(layer.layer_arn), ) return layer
def download(self, layer, force=False): """ Download a given layer to the local cache. Parameters ---------- layer samcli.commands.local.lib.provider.Layer Layer representing the layer to be downloaded. force bool True to download the layer even if it exists already on the system Returns ------- Path Path object that represents where the layer is download to """ if layer.is_defined_within_template: LOG.info("%s is a local Layer in the template", layer.name) layer.codeuri = resolve_code_path(self.cwd, layer.codeuri) return layer LayerDownloader._create_cache(self.layer_cache) # disabling no-member due to https://github.com/PyCQA/pylint/issues/1660 layer_path = Path(self.layer_cache).joinpath(layer.name).resolve() # pylint: disable=no-member is_layer_downloaded = self._is_layer_cached(layer_path) layer.codeuri = str(layer_path) if is_layer_downloaded and not force: LOG.info("%s is already cached. Skipping download", layer.arn) return layer layer_zip_path = layer.codeuri + '.zip' layer_zip_uri = self._fetch_layer_uri(layer) unzip_from_uri(layer_zip_uri, layer_zip_path, unzip_output_dir=layer.codeuri, progressbar_label='Downloading {}'.format( layer.layer_arn)) return layer
def download(self, layer: LayerVersion, force=False) -> LayerVersion: """ Download a given layer to the local cache. Parameters ---------- layer samcli.commands.local.lib.provider.Layer Layer representing the layer to be downloaded. force bool True to download the layer even if it exists already on the system Returns ------- Path Path object that represents where the layer is download to """ if layer.is_defined_within_template: LOG.info("%s is a local Layer in the template", layer.name) layer.codeuri = resolve_code_path(self.cwd, layer.codeuri) return layer layer_path = Path(self.layer_cache).resolve().joinpath(layer.name) is_layer_downloaded = self._is_layer_cached(layer_path) layer.codeuri = str(layer_path) if is_layer_downloaded and not force: LOG.info("%s is already cached. Skipping download", layer.arn) return layer layer_zip_path = layer.codeuri + ".zip" layer_zip_uri = self._fetch_layer_uri(layer) unzip_from_uri( layer_zip_uri, layer_zip_path, unzip_output_dir=layer.codeuri, progressbar_label="Downloading {}".format(layer.layer_arn), ) return layer