4 # Copyright 2016 RIFT.IO Inc
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
25 import tornado
.platform
.asyncio
26 import tornado
.testing
33 from rift
.package
.handler
import FileRestApiHandler
36 gi
.require_version('NsdYang', '1.0')
37 gi
.require_version('VnfdYang', '1.0')
39 from gi
.repository
import (
45 class FileServerTestCase(tornado
.testing
.AsyncHTTPTestCase
):
47 self
._log
= logging
.getLogger(__file__
)
48 self
._loop
= asyncio
.get_event_loop()
51 self
._port
= self
.get_http_port()
53 def get_new_ioloop(self
):
54 return tornado
.platform
.asyncio
.AsyncIOMainLoop()
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()
67 self
.path
= create_mock_structure()
70 return tornado
.web
.Application([
71 (r
"/api/package/vnfd/(.*)", FileRestApiHandler
, {"path": self
.path
}),
74 def test_get_file(self
):
75 response
= self
.fetch("/api/package/vnfd/pong_vnfd.xml")
76 assert response
.code
== 200
78 def test_get_folder(self
):
79 response
= self
.fetch("/api/package/vnfd/")
80 assert response
.code
== 200
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
88 def main(argv
=sys
.argv
[1:]):
89 logging
.basicConfig(format
='TEST %(message)s')
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')
96 args
, unknown
= parser
.parse_known_args(argv
)
100 # Set the global logging level
101 logging
.getLogger().setLevel(logging
.DEBUG
if args
.verbose
else logging
.ERROR
)
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
)
108 if __name__
== '__main__':