def test_dl_via_requests_200(self, copyfileobj, requests_get): if os.name.lower() in ['nt']: # Unable to get permission to write/access file in Windows, so don't bother... print(f'OS: {os.name.lower()}') raise SkipTest() temp_file = tempfile.NamedTemporaryFile(mode='r', delete=True) print(f"Created temp file: {temp_file.name}") image_obj = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR) image_obj.dl_file_spec = temp_file.name dl_status = image_obj._dl_via_requests() temp_file.close() print(f"Closed/Removed temp file: {image_obj.dl_file_spec}") remove_temp_file(image_obj.dl_file_spec) assert requests_get.call_count == 1 assert copyfileobj.call_count == 1 assert image_obj.status == status.DownloadStatus.DOWNLOADED assert dl_status == status.DownloadStatus.DOWNLOADED assert not os.path.exists(image_obj.dl_file_spec)
def test_get_image_name_with_no_image(self): image_dl = dl.DownloadPX(image_url='https://abc.com/', url_split_token='/', dl_dir=self.DL_DIR, test=True) name = image_dl.get_image_name() assert_equals(image_dl.status, status.DownloadStatus.NOT_SET) assert_equals(name, '')
def test_get_image_name_without_url_returns_none(self): image_dl = dl.DownloadPX(image_url='', url_split_token=None, dl_dir=self.DL_DIR, test=True) name = image_dl.get_image_name() assert_equals(name, None) assert_equals(image_dl.status, status.DownloadStatus.ERROR)
def test_download_image_successful_dl(self, dl_pending_mock): dl_image = dl.DownloadPX(image_url=self.EXISTS_DUMMY_URL, dl_dir=self.DL_DIR) dl_image.RETRY_DELAY = 0 dl_status = dl_image.download_image() assert dl_status == status.DownloadStatus.DOWNLOADED assert dl_pending_mock.call_count == 1
def test_download_image_unable_to_dl(self, dl_pending_mock): dl_image = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR) dl_image.RETRY_DELAY = 0 dl_status = dl_image.download_image() assert dl_status == status.DownloadStatus.PENDING assert dl_image.status == status.DownloadStatus.PENDING assert dl_pending_mock.call_count == dl.DownloadPX.MAX_ATTEMPTS
def test_filespec_is_empty_string(self): image_obj = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR, use_wget=True) image_obj.dl_file_spec = '' result = image_obj._file_exists() assert result is False assert image_obj.status == status.DownloadStatus.ERROR
def test_filespec_does_not_exist(self): image_obj = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR, use_wget=True) image_obj.dl_file_spec = '/tmp/does_not_exist.wooba' curr_status = image_obj.status result = image_obj._file_exists() assert result is False assert image_obj.status == curr_status
def test_set_status_updates_all_statuses(self): test_status = status.DownloadStatus.DOWNLOADED image_dl = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, url_split_token=None, dl_dir=self.DL_DIR, image_info=None) image_dl.status = test_status assert_equals(image_dl.status, test_status) assert_equals(image_dl.image_info.dl_status, test_status)
def test_download_image_successful_dl_wget(self, dl_pending_mock): dl_image = dl.DownloadPX(image_url=self.EXISTS_DUMMY_URL, dl_dir=self.DL_DIR, use_wget=True, test=True) dl_image.RETRY_DELAY = 0 dl_image.dl_file_spec = os.path.sep.join( [self.DL_DIR, self.EXIST_IMAGE_NAME]) dl_status = dl_image.download_image() assert dl_status == status.DownloadStatus.DOWNLOADED assert dl_pending_mock.call_count == 1
def test_get_image_name_with_wget(self): expected_image_name = self.EXIST_IMAGE_NAME if not expected_image_name.endswith(dl.DownloadPX.EXTENSION): expected_image_name += f'.{dl.DownloadPX.EXTENSION}' image_dl = dl.DownloadPX(image_url=self.EXISTS_DUMMY_URL, url_split_token=None, dl_dir=self.DL_DIR, test=True) name = image_dl.get_image_name(use_wget=True) assert_equals(name, expected_image_name) assert_equals(image_dl.status, status.DownloadStatus.PENDING)
def test_set_status_on_provided_imagedata_obj(self): test_status = status.DownloadStatus.DOWNLOADED image_data = imageinfo.ImageData() image_data.dl_status = status.DownloadStatus.PENDING image_dl = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, url_split_token=None, dl_dir=self.DL_DIR, image_info=image_data) image_dl.status = test_status assert_equals(image_dl.status, test_status) assert_equals(image_dl.image_info.dl_status, test_status)
def test_filespec_is_valid(self): temp_file = create_temp_file(size=(dl.DownloadPX.MIN_KB + 1) * dl.DownloadPX.KILOBYTES) image_obj = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR, use_wget=True) image_obj.dl_file_spec = temp_file.name result = image_obj._file_exists() remove_temp_file(temp_file.name) assert result is True assert image_obj.status == status.DownloadStatus.EXISTS assert not os.path.exists(temp_file.name)
def test_dl_via_wget_but_wget_is_disabled(self, wget_mock): image_obj = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR, use_wget=False) image_obj.RETRY_DELAY = 0 image_obj.MAX_ATTEMPTS = 0 assert image_obj.status == status.DownloadStatus.PENDING dl_status = image_obj._dl_via_wget() print(f"Mock WGET call count: {wget_mock.call_count}") assert wget_mock.call_count == image_obj.MAX_ATTEMPTS assert dl_status == status.DownloadStatus.ERROR assert image_obj.status == status.DownloadStatus.ERROR assert image_obj.image_info.dl_status == status.DownloadStatus.ERROR remove_temp_file(wget_mock.return_value.name) assert not os.path.exists(wget_mock.return_value.name)
def test_dl_via_wget_200_with_correct_file_size(self, wget_mock): image_file = wget_mock.return_value print(f"Created temp file: {image_file}") print(f"Temp file stats:\n\tSize:{os.path.getsize(image_file)} bytes") image_obj = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR, use_wget=True) image_obj.RETRY_DELAY = 0 assert image_obj.status == status.DownloadStatus.PENDING dl_status = image_obj._dl_via_wget() remove_temp_file(wget_mock.return_value) print(f"Deleted temp file: {image_file}") print(f"Mock call count: {wget_mock.call_count}") assert wget_mock.call_count == 1 assert dl_status == status.DownloadStatus.DOWNLOADED assert image_obj.status == status.DownloadStatus.DOWNLOADED assert image_obj.image_info.dl_status == status.DownloadStatus.DOWNLOADED assert not os.path.exists(wget_mock.return_value)
def test_dl_via_requests_404(self, copyfileobj, requests_get): temp_file = tempfile.NamedTemporaryFile(mode='r', delete=True) print(f"Created temp file: {temp_file.name}") image_obj = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR) image_obj.dl_file_spec = temp_file.name assert image_obj.status == status.DownloadStatus.PENDING dl_status = image_obj._dl_via_requests() temp_file.close() print(f"Closed/removed temp file: {image_obj.dl_file_spec}") remove_temp_file(image_obj.dl_file_spec) assert requests_get.call_count == 1 assert copyfileobj.call_count == 0 assert image_obj.status == status.DownloadStatus.ERROR assert dl_status == status.DownloadStatus.ERROR assert not os.path.exists(image_obj.dl_file_spec)
def test_dl_via_wget_conn_err(self, wget_mock): image_file = wget_mock.return_value print(f"Created temp file: {image_file}") print(f"Temp file stats:\n\tSize:{os.path.getsize(image_file)} bytes") image_obj = dl.DownloadPX(image_url=self.DNE_DUMMY_URL, dl_dir=self.DL_DIR, use_wget=True) image_obj.RETRY_DELAY = 0 assert image_obj.status == status.DownloadStatus.PENDING dl_status = image_obj._dl_via_wget() print(f"Deleted temp file: {image_file}") print(f"Mock WGET call count: {wget_mock.call_count}") assert wget_mock.call_count == dl.DownloadPX.MAX_ATTEMPTS assert dl_status == status.DownloadStatus.ERROR assert image_obj.status == status.DownloadStatus.ERROR assert image_obj.image_info.dl_status == status.DownloadStatus.ERROR remove_temp_file(wget_mock.return_value) assert not os.path.exists(wget_mock.return_value)