| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # |
| 4 | # Copyright 2016 RIFT.IO Inc |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
| 19 | |
| 20 | import argparse |
| 21 | import asyncio |
| 22 | import logging |
| 23 | import os |
| 24 | import sys |
| 25 | import unittest |
| 26 | import xmlrunner |
| 27 | |
| 28 | import rift.downloader as downloader |
| 29 | |
| 30 | TEST_URL = "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell" |
| 31 | |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 32 | class UrlTestCase(unittest.TestCase): |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 33 | def setUp(self): |
| 34 | pass |
| 35 | |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 36 | @classmethod |
| 37 | def set_logger(cls, log): |
| 38 | cls.log = log |
| 39 | |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 40 | def _common_checks(self, job): |
| 41 | if job.status != "COMPLETED": |
| 42 | return |
| 43 | |
| 44 | # assert job.bytes_downloaded == job.bytes_total |
| 45 | assert job.stop_time > 0 |
| 46 | assert job.start_time > 0 |
| 47 | assert job.stop_time >= job.start_time |
| 48 | |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 49 | def _display_result(self, url_downl): |
| 50 | UrlTestCase.log.debug("URL download result: {}".format(url_downl)) |
| 51 | |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 52 | def test_file_download(self): |
| 53 | """ |
| 54 | Asserts: |
| 55 | 1. Successful download |
| 56 | 2. Model attributes (Process percent, detail, status) |
| 57 | """ |
| 58 | url_downl = downloader.UrlDownloader(TEST_URL) |
| 59 | url_downl.download() |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 60 | assert os.path.isfile(url_downl.filepath) |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 61 | |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 62 | self._display_result(url_downl) |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 63 | assert url_downl.meta.status == downloader.DownloadStatus.COMPLETED |
| 64 | # assert url_downl.job.progress_percent == 100 |
| 65 | assert "success" in url_downl.meta.detail |
| 66 | self._common_checks(url_downl.meta) |
| 67 | |
| 68 | def test_file_not_exists(self): |
| 69 | """ |
| 70 | Asserts: |
| 71 | 1. 404 download with retries |
| 72 | 2. Model attributes (Process percent, detail, status) |
| 73 | """ |
| 74 | url_downl = downloader.UrlDownloader(TEST_URL + ".blah") |
| 75 | url_downl.download() |
| 76 | |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 77 | self._display_result(url_downl) |
| 78 | assert not os.path.isfile(url_downl.filepath) |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 79 | assert url_downl.meta.status == downloader.DownloadStatus.FAILED |
| 80 | assert "Max retries" in url_downl.meta.detail or "404" in url_downl.meta.detail |
| 81 | |
| 82 | self._common_checks(url_downl.meta) |
| 83 | |
| 84 | def test_cancellation(self): |
| 85 | """ |
| 86 | Asserts: |
| 87 | 1. Cancel for a download and clean up of the downloaded file. |
| 88 | 2. Model attributes (Process percent, detail, status) |
| 89 | """ |
| chamarty | c148feb | 2017-03-06 17:04:20 +0000 | [diff] [blame] | 90 | url = "http://speedtest.ftp.otenet.gr/files/test10Mb.db" |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 91 | url_dwld = downloader.UrlDownloader(url) |
| 92 | loop = asyncio.get_event_loop() |
| 93 | fut = loop.run_in_executor(None, url_dwld.download) |
| 94 | |
| 95 | def cancel(): |
| 96 | fut.cancel() |
| 97 | url_dwld.cancel_download() |
| 98 | |
| 99 | @asyncio.coroutine |
| 100 | def sleep(): |
| 101 | yield from asyncio.sleep(2) |
| 102 | cancel() |
| 103 | yield from asyncio.sleep(2) |
| 104 | |
| 105 | loop.run_until_complete(sleep()) |
| 106 | |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 107 | self._display_result(url_dwld) |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 108 | assert url_dwld.meta.status == downloader.DownloadStatus.CANCELLED |
| 109 | assert url_dwld.meta.bytes_downloaded == url_dwld.meta.bytes_downloaded |
| 110 | assert "cancel" in url_dwld.meta.detail |
| 111 | self._common_checks(url_dwld.meta) |
| 112 | |
| 113 | def test_auth_url(self): |
| 114 | url_downl = downloader.UrlDownloader( |
| 115 | 'https://api.github.com/user') |
| 116 | |
| 117 | url_downl.download() |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 118 | self._display_result(url_downl) |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def tearDown(self): |
| 122 | pass |
| 123 | |
| 124 | |
| 125 | def main(argv=sys.argv[1:]): |
| 126 | logging.basicConfig(format='TEST %(message)s') |
| 127 | |
| 128 | runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"]) |
| 129 | parser = argparse.ArgumentParser() |
| 130 | parser.add_argument('-v', '--verbose', action='store_true') |
| 131 | parser.add_argument('-n', '--no-runner', action='store_true') |
| 132 | |
| 133 | args, unknown = parser.parse_known_args(argv) |
| 134 | if args.no_runner: |
| 135 | runner = None |
| 136 | |
| 137 | # Set the global logging level |
| chamarty | 98e0b67 | 2017-03-06 14:56:25 +0000 | [diff] [blame] | 138 | log = logging.getLogger() |
| 139 | log.setLevel(logging.DEBUG if args.verbose else logging.ERROR) |
| 140 | UrlTestCase.set_logger(log) |
| velandy | 6364d01 | 2017-01-04 19:25:07 +0000 | [diff] [blame] | 141 | |
| 142 | # The unittest framework requires a program name, so use the name of this |
| 143 | # file instead (we do not want to have to pass a fake program name to main |
| 144 | # when this is called from the interpreter). |
| 145 | unittest.main(argv=[__file__] + unknown + ["-v"], testRunner=runner) |
| 146 | |
| 147 | if __name__ == '__main__': |
| 148 | main() |