blob: e56ec043be3a5041383b565ce1bded35f1ad301b [file] [log] [blame]
velandy6364d012017-01-04 19:25:07 +00001#!/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
20import argparse
21import asyncio
22import logging
23import os
24import sys
25import tornado.platform.asyncio
26import tornado.testing
27import tornado.web
28import tempfile
29import unittest
30import json
31import xmlrunner
32
33from rift.package.handler import FileRestApiHandler
34
35import gi
36gi.require_version('NsdYang', '1.0')
37gi.require_version('VnfdYang', '1.0')
38
39from gi.repository import (
40 NsdYang,
41 VnfdYang,
42 )
43
44
45class 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
88def 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
108if __name__ == '__main__':
109 main()