From e4cc41873a3c7afd5d9b6c2ed858b04a7a39e32f Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Mon, 27 Aug 2018 14:38:23 +0200 Subject: [PATCH] Add unittest for os_service_types.data module. Change-Id: I1d0e68f361e6fb9ddd578debeaaf0b6b5c0879fd --- os_service_types/tests/base.py | 18 +++++++++ os_service_types/tests/test_data.py | 61 +++++++++++++++++++++++++++++ test-requirements.txt | 1 + 3 files changed, 80 insertions(+) create mode 100644 os_service_types/tests/test_data.py diff --git a/os_service_types/tests/base.py b/os_service_types/tests/base.py index 38f0444..9751578 100644 --- a/os_service_types/tests/base.py +++ b/os_service_types/tests/base.py @@ -17,6 +17,8 @@ import copy import datetime +import os +import tempfile from oslotest import base @@ -231,3 +233,19 @@ class ServiceDataMixin(object): self.assertEqual( data, 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) diff --git a/os_service_types/tests/test_data.py b/os_service_types/tests/test_data.py new file mode 100644 index 0000000..2ba3f48 --- /dev/null +++ b/os_service_types/tests/test_data.py @@ -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 diff --git a/test-requirements.txt b/test-requirements.txt index b17ee11..6e94910 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -15,3 +15,4 @@ openstackdocstheme>=1.18.1 # Apache-2.0 keystoneauth1>=3.4.0 # Apache-2.0 # releasenotes reno>=2.5.0 # Apache-2.0 +six>=1.10.0 # MIT