8287c119c8e4dd094ab133ad4a6e6a27bbb0a131
[osm/SO.git] / common / python / test / utest_config_data.py
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 logging
22 import io
23 import os
24 import sys
25 import tempfile
26 import unittest
27 import xmlrunner
28 import yaml
29
30
31 from rift.mano.config_data import config
32
33 import gi
34 gi.require_version('VnfdYang', '1.0')
35 gi.require_version('RwYang', '1.0')
36
37 from gi.repository import (
38 VnfdYang,
39 RwYang,
40 )
41
42 class InitialPrimitiveReaderTest(unittest.TestCase):
43 def test_read_valid_config(self):
44 input_prim_data = [
45 {
46 "name": "prim_1",
47 "parameter": {
48 "hostname": "pe1",
49 #"pass": "6windos"
50 # Hard to compare with multiple elements because ordering of list
51 # element is not deterministic.
52 }
53 },
54 {
55 "name": "prim_2",
56 # No, parameters (use default values)
57 },
58 ]
59
60 with io.StringIO() as yaml_hdl:
61 yaml_hdl.write(yaml.safe_dump(input_prim_data))
62 yaml_hdl.seek(0)
63 reader = config.VnfInitialConfigPrimitiveReader.from_yaml_file_hdl(yaml_hdl)
64
65 expected_primitives = [
66 VnfdYang.InitialConfigPrimitive.from_dict({
67 "name": "prim_1", "seq": 0, "parameter": [
68 {
69 "name": "hostname",
70 "value": "pe1",
71 },
72 ]
73 }),
74 VnfdYang.InitialConfigPrimitive.from_dict({
75 "name": "prim_2", "seq": 1
76 }),
77 ]
78
79 for i, prim in enumerate(reader.primitives):
80 logging.debug("Expected: %s", str(expected_primitives[i]))
81 logging.debug("Got: %s", str(prim))
82 self.assertEqual(expected_primitives[i], prim)
83
84
85 def main(argv=sys.argv[1:]):
86 logging.basicConfig(format='TEST %(message)s')
87
88 runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
89 parser = argparse.ArgumentParser()
90 parser.add_argument('-v', '--verbose', action='store_true')
91 parser.add_argument('-n', '--no-runner', action='store_true')
92
93 args, unknown = parser.parse_known_args(argv)
94 if args.no_runner:
95 runner = None
96
97 # Set the global logging level
98 logging.getLogger().setLevel(logging.DEBUG if args.verbose else logging.ERROR)
99
100 # The unittest framework requires a program name, so use the name of this
101 # file instead (we do not want to have to pass a fake program name to main
102 # when this is called from the interpreter).
103 unittest.main(argv=[__file__] + unknown + ["-v"], testRunner=runner)
104
105 if __name__ == '__main__':
106 main()