def test_http_download_run_compressed(tmpdir): def reader(): yield b"\xfd7zXZ\x00\x00\x04\xe6\xd6\xb4F\x02\x00!\x01\x16\x00\x00" yield b"\x00t/\xe5\xa3\x01\x00\x0bhello world\n\x00\xa1\xf2\xff\xc4j" yield b"\x7f\xbf\xcf\x00\x01$\x0c\xa6\x18\xd8\xd8\x1f\xb6\xf3}\x01" yield b"\x00\x00\x00\x00\x04YZ" action = HttpDownloadAction( "rootfs", str(tmpdir), urlparse("https://example.com/rootfs.xz") ) action.job = Job(1234, {}, None) action.url = urlparse("https://example.com/rootfs.xz") action.parameters = { "to": "download", "rootfs": { "url": "https://example.com/rootfs.xz", "compression": "xz", "md5sum": "0107d527acf9b8de628b7b4d103c89d1", "sha256sum": "3275a39be7b717d548b66f3c8f23d940603a63b0f13d84a596d979a7f66feb2c", "sha512sum": "d0850c3e0c45bdf74995907a04f69806a070d79a4f0b2dd82d6b96adafdbfd85ce6c1daaff916ff089bdf9b04eba7805041c49afecdbeabca69fef802e60de35", }, "namespace": "common", } action.params = action.parameters["rootfs"] action.reader = reader action.size = 68 action.fname = str(tmpdir / "rootfs/rootfs") action.run(None, 4212) data = "" with open(str(tmpdir / "rootfs/rootfs")) as f_in: data = f_in.read() assert data == "hello world\n" assert dict(action.results) == { "success": { "sha512": "d0850c3e0c45bdf74995907a04f69806a070d79a4f0b2dd82d6b96adafdbfd85ce6c1daaff916ff089bdf9b04eba7805041c49afecdbeabca69fef802e60de35" }, "label": "rootfs", "size": 68, "md5sum": "0107d527acf9b8de628b7b4d103c89d1", "sha256sum": "3275a39be7b717d548b66f3c8f23d940603a63b0f13d84a596d979a7f66feb2c", "sha512sum": "d0850c3e0c45bdf74995907a04f69806a070d79a4f0b2dd82d6b96adafdbfd85ce6c1daaff916ff089bdf9b04eba7805041c49afecdbeabca69fef802e60de35", } assert action.data == { "common": { "download-action": { "rootfs": { "decompressed": True, "file": "%s/rootfs/rootfs" % str(tmpdir), "md5": "0107d527acf9b8de628b7b4d103c89d1", "sha256": "3275a39be7b717d548b66f3c8f23d940603a63b0f13d84a596d979a7f66feb2c", "sha512": "d0850c3e0c45bdf74995907a04f69806a070d79a4f0b2dd82d6b96adafdbfd85ce6c1daaff916ff089bdf9b04eba7805041c49afecdbeabca69fef802e60de35", }, "file": {"rootfs": "%s/rootfs/rootfs" % str(tmpdir)}, } } }
def test_http_download_reader(mocker): # Working class DummyResponse: status_code = requests.codes.OK headers = {"content-length": "4212"} def iter_content(self, size): assert size == HTTP_DOWNLOAD_CHUNK_SIZE yield b"hello" def close(self): pass def dummyget(url, allow_redirects, stream, headers): assert allow_redirects is True assert stream is True assert url == "https://example.com/dtb" return DummyResponse() mocker.patch("requests.get", dummyget) action = HttpDownloadAction( "image", "/path/to/file", urlparse("https://example.com/dtb") ) action.url = urlparse("https://example.com/dtb") ite = action.reader() assert next(ite) == b"hello" with pytest.raises(StopIteration): next(ite) # Not working def dummygetraise(url, allow_redirects, stream, headers): raise requests.RequestException("error") mocker.patch("requests.get", dummygetraise) action = HttpDownloadAction( "image", "/path/to/file", urlparse("https://example.com/dtb") ) action.url = urlparse("https://example.com/dtb") ite = action.reader() with pytest.raises(InfrastructureError) as exc: next(ite) assert exc.match("Unable to download 'https://example.com/dtb': error")
def test_http_download_run(tmpdir): def reader(): yield b"hello" yield b"world" action = HttpDownloadAction("dtb", str(tmpdir), urlparse("https://example.com/dtb")) action.job = Job(1234, {"dispatcher": {}}, None) action.url = urlparse("https://example.com/dtb") action.parameters = { "to": "download", "images": { "dtb": { "url": "https://example.com/dtb", "md5sum": "fc5e038d38a57032085441e7fe7010b0", "sha256sum": "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af", "sha512sum": "1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60", } }, "namespace": "common", } action.params = action.parameters["images"]["dtb"] action.reader = reader action.fname = str(tmpdir / "dtb/dtb") action.run(None, 4212) data = "" with open(str(tmpdir / "dtb/dtb")) as f_in: data = f_in.read() assert data == "helloworld" assert dict(action.results) == { "success": { "sha512": "1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60" }, "label": "dtb", "size": 10, "md5sum": "fc5e038d38a57032085441e7fe7010b0", "sha256sum": "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af", "sha512sum": "1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60", } assert action.data == { "common": { "download-action": { "dtb": { "decompressed": False, "file": "%s/dtb/dtb" % str(tmpdir), "md5": "fc5e038d38a57032085441e7fe7010b0", "sha256": "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af", "sha512": "1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60", }, "file": {"dtb": "%s/dtb/dtb" % str(tmpdir)}, } } }