| 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 tornado.platform.asyncio |
| 26 | import tornado.testing |
| 27 | import tornado.web |
| 28 | import tempfile |
| 29 | import unittest |
| 30 | import json |
| 31 | import xmlrunner |
| 32 | |
| 33 | from rift.package.handler import FileRestApiHandler |
| 34 | |
| 35 | import gi |
| 36 | gi.require_version('NsdYang', '1.0') |
| 37 | gi.require_version('VnfdYang', '1.0') |
| 38 | |
| 39 | from gi.repository import ( |
| 40 | NsdYang, |
| 41 | VnfdYang, |
| 42 | ) |
| 43 | |
| 44 | |
| 45 | class FileServerTestCase(tornado.testing.AsyncHTTPTestCase): |
| 46 | def setUp(self): |
| 47 | self._log = logging.getLogger(__file__) |
| 48 | self._loop = asyncio.get_event_loop() |
| 49 | |
| 50 | super().setUp() |
| 51 | self._port = self.get_http_port() |
| 52 | |
| 53 | def get_new_ioloop(self): |
| 54 | return tornado.platform.asyncio.AsyncIOMainLoop() |
| 55 | |
| 56 | def get_app(self): |
| 57 | |
| 58 | def create_mock_structure(): |
| 59 | path = tempfile.mkdtemp() |
| 60 | package_path = os.path.join(path, "pong_vnfd") |
| 61 | os.makedirs(package_path) |
| 62 | open(os.path.join(path, "pong_vnfd.xml"), "wb").close() |
| 63 | open(os.path.join(path, "logo.png"), "wb").close() |
| 64 | |
| 65 | return path |
| 66 | |
| 67 | self.path = create_mock_structure() |
| 68 | print (self.path) |
| 69 | |
| 70 | return tornado.web.Application([ |
| 71 | (r"/api/package/vnfd/(.*)", FileRestApiHandler, {"path": self.path}), |
| 72 | ]) |
| 73 | |
| 74 | def test_get_file(self): |
| 75 | response = self.fetch("/api/package/vnfd/pong_vnfd.xml") |
| 76 | assert response.code == 200 |
| 77 | |
| 78 | def test_get_folder(self): |
| 79 | response = self.fetch("/api/package/vnfd/") |
| 80 | assert response.code == 200 |
| 81 | |
| 82 | data = json.loads(response.body.decode("utf-8")) |
| 83 | files = [content['name'] for content in data['contents']] |
| 84 | assert "pong_vnfd.xml" in files |
| 85 | assert "logo.png" in files |
| 86 | |
| 87 | |
| 88 | def main(argv=sys.argv[1:]): |
| 89 | logging.basicConfig(format='TEST %(message)s') |
| 90 | |
| 91 | runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"]) |
| 92 | parser = argparse.ArgumentParser() |
| 93 | parser.add_argument('-v', '--verbose', action='store_true') |
| 94 | parser.add_argument('-n', '--no-runner', action='store_true') |
| 95 | |
| 96 | args, unknown = parser.parse_known_args(argv) |
| 97 | if args.no_runner: |
| 98 | runner = None |
| 99 | |
| 100 | # Set the global logging level |
| 101 | logging.getLogger().setLevel(logging.DEBUG if args.verbose else logging.ERROR) |
| 102 | |
| 103 | # The unittest framework requires a program name, so use the name of this |
| 104 | # file instead (we do not want to have to pass a fake program name to main |
| 105 | # when this is called from the interpreter). |
| 106 | unittest.main(argv=[__file__] + unknown + ["-v"], testRunner=runner) |
| 107 | |
| 108 | if __name__ == '__main__': |
| 109 | main() |