Codebase list python-os-service-types / e4cc418
Add unittest for os_service_types.data module. Change-Id: I1d0e68f361e6fb9ddd578debeaaf0b6b5c0879fd Federico Ressi 5 years ago
3 changed file(s) with 80 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1616
1717 import copy
1818 import datetime
19 import os
20 import tempfile
1921
2022 from oslotest import base
2123
230232 self.assertEqual(
231233 data,
232234 self.service_types.get_service_data(self.all_services[index]))
235
236
237 class TemporaryFileMixin(base.BaseTestCase):
238
239 def create_temp_file(self, mode='w', suffix='', prefix='tmp', dir=None,
240 text=False, delete=True):
241 fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir,
242 text=text)
243 fd = os.fdopen(fd, mode)
244 if delete:
245 self.addCleanup(self._delete_temp, fd, name)
246 return fd, name
247
248 def _delete_temp(self, fd, name):
249 fd.close()
250 os.unlink(name)
0 # -*- coding: utf-8 -*-
1
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 """
15 test_data
16 ------------
17
18 Tests for `os_service_types.data` module.
19
20 """
21
22 import json
23
24 import six
25
26 from os_service_types import data
27 from os_service_types.tests import base
28
29
30 if six.PY2:
31 # Python 2 has not FileNotFoundError exception
32 FileNotFoundError = IOError
33
34
35 class TestData(base.TestCase, base.TemporaryFileMixin):
36
37 def setUp(self):
38 super(TestData, self).setUp()
39
40 def test_load(self):
41 json_data = {'some_key': 'some_value'}
42 filename = self.create_json(json_data)
43 actual_data = data.read_data(filename)
44 self.assertEqual(json_data, actual_data)
45
46 def test_load_service_types(self):
47 json_data = data.read_data('service-types.json')
48 for key in ["all_types_by_service_type", "forward",
49 "primary_service_by_project", "reverse"]:
50 self.assertIn(key, json_data)
51
52 def test_load_non_existing(self):
53 self.assertRaises(FileNotFoundError, data.read_data,
54 '/non-existing-file')
55
56 def create_json(self, json_data):
57 fd, name = self.create_temp_file(suffix='.json')
58 with fd:
59 json.dump(json_data, fd)
60 return name
1414 keystoneauth1>=3.4.0 # Apache-2.0
1515 # releasenotes
1616 reno>=2.5.0 # Apache-2.0
17 six>=1.10.0 # MIT