update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / common / python / test / utest_project.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2017 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 import argparse
20 import asyncio
21 import gi
22 import logging
23 import os
24 import sys
25 import unittest
26 import xmlrunner
27
28 from rift.mano.utils import project
29 gi.require_version('RwKeyspec', '1.0')
30 from gi.repository.RwKeyspec import quoted_key
31
32
33 NAME = 'test'
34 XPATH = "/rw-project:project[rw-project:name={}]".format(quoted_key(NAME))
35
36 class TestCase(unittest.TestCase):
37 log = None
38
39 @classmethod
40 def set_logger(cls, log):
41 cls.log = log
42
43 def setUp(self):
44 if not TestCase.log:
45 log = logging.getLogger()
46 log.setLevel( logging.ERROR)
47
48 def test_create_from_xpath(self):
49 """
50 Asserts:
51 1. Instance of project from xpath
52 2. project name in instance is correct
53 """
54 proj = project.ManoProject.create_from_xpath(XPATH, TestCase.log)
55 assert proj
56 assert NAME == proj.name
57 assert XPATH == proj.prefix
58
59 obj = project.ManoProject.create_from_xpath(proj.prefix, TestCase.log)
60 assert obj
61 assert NAME == obj.name
62 assert XPATH == obj.prefix
63
64 def test_create(self):
65 """
66 Asserts:
67 1. Instance of project
68 2. project name in instance is correct
69 """
70 proj = project.ManoProject(TestCase.log, name=NAME)
71 assert proj
72 assert NAME == proj.name
73 assert XPATH == proj.prefix
74
75 obj = project.ManoProject.create_from_xpath(proj.prefix, TestCase.log)
76 assert obj
77 assert NAME == obj.name
78 assert XPATH == obj.prefix
79
80 def test_create_update(self):
81 """
82 Asserts:
83 1. Instance of project
84 2. Set project name later
85 3. project name in instance is correct
86 """
87 proj = project.ManoProject(TestCase.log)
88 assert proj
89 assert None == proj.name
90
91 proj.name = NAME
92 assert NAME == proj.name
93 assert XPATH == proj.prefix
94
95 try:
96 proj.name = 'testing'
97 except project.ManoProjNameSetErr as e:
98 TestCase.log.debug("Expected exception: {}".format(e))
99 else:
100 assert False
101
102 obj = project.ManoProject.create_from_xpath(proj.prefix, TestCase.log)
103 assert obj
104 assert NAME == obj.name
105 assert XPATH == obj.prefix
106
107 def test_update_from_xpath(self):
108 """
109 Asserts:
110 1. Instance of project
111 2. Update from XPATH
112 2. project name in instance is correct
113 """
114 proj = project.ManoProject(TestCase.log)
115 assert proj
116 assert proj.name is None
117
118 proj.set_from_xpath(XPATH)
119 assert NAME == proj.name
120 assert XPATH == proj.prefix
121
122 try:
123 proj.set_from_xpath(XPATH)
124 except project.ManoProjNameSetErr as e:
125 TestCase.log.debug("Expected exception: {}".format(e))
126 else:
127 assert False
128
129 obj = project.ManoProject.create_from_xpath(proj.prefix, TestCase.log)
130 assert obj
131 assert NAME == obj.name
132 assert XPATH == obj.prefix
133
134 def test_create_from_xpath1(self):
135 """
136 Asserts:
137 1. Instance of project from xpath
138 2. project name in instance is correct
139 """
140 xpath = XPATH + '/rw:project/rw-project:project/rw-project:project/rw-project:project/rw-project:project/project-nsd:nsd-catalog/project-nsd:nsd[id=\'1232334\']'
141 proj = project.ManoProject.create_from_xpath(xpath, TestCase.log)
142 assert proj
143 assert NAME == proj.name
144 assert XPATH == proj.prefix
145
146 def test_create_from_xpath2(self):
147 """
148 Asserts:
149 1. Instance of project from xpath
150 2. project name in instance is correct
151 """
152 xpath = '/rw-project:project[name={}]'.format(quoted_key(NAME))
153 proj = project.ManoProject.create_from_xpath(xpath, TestCase.log)
154 assert proj
155 assert NAME == proj.name
156 assert XPATH == proj.prefix
157
158 def test_create_from_xpath_invalid(self):
159 """
160 Asserts:
161 1. Exception due to invalid XPATH format for extracting project
162 """
163 xpath = '/'
164 try:
165 proj = project.ManoProject.create_from_xpath(xpath, TestCase.log)
166 except project.ManoProjXpathNoProjErr as e:
167 TestCase.log.debug("Expected exception: {}".format(e))
168 else:
169 assert False
170
171 def test_create_from_xpath_invalid1(self):
172 """
173 Asserts:
174 1. Exception due to invalid XPATH format for extracting project
175 """
176 xpath = '/rw-project:project/{}'.format(NAME)
177 try:
178 proj = project.ManoProject.create_from_xpath(xpath, TestCase.log)
179 except project.ManoProjXpathKeyErr as e:
180 TestCase.log.debug("Expected exception: {}".format(e))
181 else:
182 assert False
183
184 def test_create_from_xpath_invalid2(self):
185 """
186 Asserts:
187 1. Exception due to invalid XPATH format for extracting project
188 """
189 xpath = '/rw-project:project[id=test]'
190 try:
191 proj = project.ManoProject.create_from_xpath(xpath, TestCase.log)
192 except project.ManoProjXpathKeyErr as e:
193 TestCase.log.debug("Expected exception: {}".format(e))
194 else:
195 assert False
196
197 def tearDown(self):
198 pass
199
200
201 def main(argv=sys.argv[1:]):
202 logging.basicConfig(format='TEST %(message)s')
203
204 runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
205 parser = argparse.ArgumentParser()
206 parser.add_argument('-v', '--verbose', action='store_true')
207 parser.add_argument('-n', '--no-runner', action='store_true')
208
209 args, unknown = parser.parse_known_args(argv)
210 if args.no_runner:
211 runner = None
212
213 # Set the global logging level
214 log = logging.getLogger()
215 log.setLevel(logging.DEBUG if args.verbose else logging.ERROR)
216 TestCase.set_logger(log)
217
218 # The unittest framework requires a program name, so use the name of this
219 # file instead (we do not want to have to pass a fake program name to main
220 # when this is called from the interpreter).
221 unittest.main(argv=[__file__] + unknown + ["-v"], testRunner=runner)
222
223 if __name__ == '__main__':
224 main()