Exemple #1
0
    def _download_file(self, url):
        """
        Download a file from http server

        :type  url: str
        :param url: url to the file to download

        :rtype: tuple
        :return: ACS verdict and local path to downloaded file
        """
        result = Global.FAILURE
        file_name = os.path.join(gettempdir(),
                                 url.rstrip('/').split('/')[-1])

        http_downloader = HttpDownloaderUtil(url=url,
                                             destination=file_name,
                                             override=True,
                                             download_timeout=self._test_timeout,
                                             logger=self._logger)

        try:
            # check args
            response = http_downloader.init()
            # then, process download
            result, _, file_name = http_downloader.download(response)
        except (SysErrors, AcsConfigException):
            msg = ("Exception occurred while Downloading {0}"
                   "Detailed exception below:\n{1}".format(url, traceback.format_exc()))
            file_name = ""
            result = Global.FAILURE
            self._logger.error(msg)

        return result, file_name
Exemple #2
0
 def test_http_downloader_download_ko_bad_header(self, mock_get):
     mock_logger = Mock()
     # empty header
     http_downloader_obj = HttpDownloaderUtil(url="",
                                              destination="",
                                              logger=mock_logger)
     fake_response = fake_request_response()
     fake_response.headers = dict()
     mock_get.return_value = fake_response
     return_value = http_downloader_obj.download()
     self.assertNotEqual(return_value[0], 0)
     self.assertEqual(return_value[2], "")
Exemple #3
0
 def test_http_downloader_download_no_md5_check(self, mock_get):
     patch("acs_test_scripts.Utilities.HttpDownloaderUtil.hashlib.md5")
     mock_logger = Mock()
     # empty header
     http_downloader_obj = HttpDownloaderUtil(url="",
                                              destination=os.path.dirname(__file__),
                                              download_timeout=1,
                                              logger=mock_logger)
     fake_response = fake_request_response()
     fake_response.headers = {'content-length': '10'}
     mock_get.return_value = fake_response
     return_value = http_downloader_obj.download()
     self.assertEqual(return_value[0], 0)
     self.assertNotEqual(return_value[2], "")
Exemple #4
0
    def test_http_downloader_download_timeout_md5_check_ko(self, mock_get, mock_md5):
        mock_logger = Mock()
        # empty header
        http_downloader_obj = HttpDownloaderUtil(url="",
                                                 destination=os.path.dirname(__file__),
                                                 download_timeout=1,
                                                 logger=mock_logger)
        fake_response = fake_request_response()
        fake_response.headers = {'content-length': '10',
                                 'X-Checksum-Md5': '123456789abc'}
        my_md5 = mock_md5()
        my_md5.hexdigest.return_value = '123456789abcNotTheSame'
        mock_get.return_value = fake_response

        return_value = http_downloader_obj.download()
        self.assertNotEqual(return_value[0], 0)
        self.assertEqual(return_value[2], "")
class HttpDownloader(TestStepBase):
    """
    Download from http server
    """
    def __init__(self, tc_conf, global_conf, ts_conf, factory):
        """
        Constructor
        """
        TestStepBase.__init__(self, tc_conf, global_conf, ts_conf, factory)
        self._http_downloader_obj = None

    def run(self, context):
        """
        Runs the test step

        :type context: TestStepContext
        :param context: test case context
        """
        TestStepBase.run(self, context)

        self._http_downloader_obj = HttpDownloaderUtil(
            url=self._pars.url,
            destination=self._pars.destination,
            proxy=self._pars.http_proxy,
            creds=self._pars.credential,
            http_timeout=self._pars.http_timeout,
            override=self._pars.override_file,
            download_timeout=self._pars.transfer_timeout,
            logger=self._logger)
        try:
            response = self._http_downloader_obj.init()
        except AcsConfigException:
            raise

        if not response:
            error_msg = "Cannot get response from server"
            raise AcsToolException(AcsToolException.OPERATION_FAILED,
                                   error_msg)

        verdict, output, _ = self._http_downloader_obj.download(response)
        if verdict != Global.SUCCESS:
            raise AcsToolException(AcsToolException.OPERATION_FAILED, output)
        else:
            self.ts_verdict_msg = output