Add unittest for os_service_types.data module.

Change-Id: I1d0e68f361e6fb9ddd578debeaaf0b6b5c0879fd
This commit is contained in:
Federico Ressi 2018-08-27 14:38:23 +02:00
parent b6ac042ece
commit e4cc41873a
3 changed files with 80 additions and 0 deletions

View File

@ -17,6 +17,8 @@
import copy import copy
import datetime import datetime
import os
import tempfile
from oslotest import base from oslotest import base
@ -231,3 +233,19 @@ class ServiceDataMixin(object):
self.assertEqual( self.assertEqual(
data, data,
self.service_types.get_service_data(self.all_services[index])) self.service_types.get_service_data(self.all_services[index]))
class TemporaryFileMixin(base.BaseTestCase):
def create_temp_file(self, mode='w', suffix='', prefix='tmp', dir=None,
text=False, delete=True):
fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir,
text=text)
fd = os.fdopen(fd, mode)
if delete:
self.addCleanup(self._delete_temp, fd, name)
return fd, name
def _delete_temp(self, fd, name):
fd.close()
os.unlink(name)

View File

@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
test_data
------------
Tests for `os_service_types.data` module.
"""
import json
import six
from os_service_types import data
from os_service_types.tests import base
if six.PY2:
# Python 2 has not FileNotFoundError exception
FileNotFoundError = IOError
class TestData(base.TestCase, base.TemporaryFileMixin):
def setUp(self):
super(TestData, self).setUp()
def test_load(self):
json_data = {'some_key': 'some_value'}
filename = self.create_json(json_data)
actual_data = data.read_data(filename)
self.assertEqual(json_data, actual_data)
def test_load_service_types(self):
json_data = data.read_data('service-types.json')
for key in ["all_types_by_service_type", "forward",
"primary_service_by_project", "reverse"]:
self.assertIn(key, json_data)
def test_load_non_existing(self):
self.assertRaises(FileNotFoundError, data.read_data,
'/non-existing-file')
def create_json(self, json_data):
fd, name = self.create_temp_file(suffix='.json')
with fd:
json.dump(json_data, fd)
return name

View File

@ -15,3 +15,4 @@ openstackdocstheme>=1.18.1 # Apache-2.0
keystoneauth1>=3.4.0 # Apache-2.0 keystoneauth1>=3.4.0 # Apache-2.0
# releasenotes # releasenotes
reno>=2.5.0 # Apache-2.0 reno>=2.5.0 # Apache-2.0
six>=1.10.0 # MIT