Create test framework for python with stestr.

Add add the first unit test for fm-rest-api/fm/fm/common/timeutils.
Add tox task for py27/35 as zuul checking and gating jobs.

Story: 2007082
Task: 38077

Change-Id: I2f259dcf2178f42546966063c0d724bc17e2a804
Signed-off-by: chenyan <yan.chen@intel.com>
This commit is contained in:
chenyan 2020-01-13 15:02:48 +08:00 committed by SidneyAn
parent 3a58bf68a1
commit 385f274a8f
11 changed files with 166 additions and 4 deletions

2
.gitignore vendored
View File

@ -22,6 +22,8 @@ develop-eggs
dist dist
eggs eggs
sdist sdist
.stestr
.ipynb_checkpoints
# Sphinx documentation # Sphinx documentation
doc/build doc/build

View File

@ -11,10 +11,14 @@
- openstack-tox-linters - openstack-tox-linters
- openstack-tox-pep8 - openstack-tox-pep8
- stx-fault-build - stx-fault-build
- fault-rest-api-py27
- fault-rest-api-py35
gate: gate:
jobs: jobs:
- openstack-tox-linters - openstack-tox-linters
- openstack-tox-pep8 - openstack-tox-pep8
- fault-rest-api-py27
- fault-rest-api-py35
# Perform just a build # Perform just a build
- job: - job:
@ -48,3 +52,23 @@
fault: https://opendev.org/starlingx/fault fault: https://opendev.org/starlingx/fault
integ: https://opendev.org/starlingx/integ integ: https://opendev.org/starlingx/integ
update: https://opendev.org/starlingx/update update: https://opendev.org/starlingx/update
- job:
name: fault-rest-api-py27
parent: tox
description: |
Run py27 test for fm-rest-api
nodeset: ubuntu-xenial
vars:
tox_envlist: py27
tox_extra_args: -c fm-rest-api/fm/tox.ini
- job:
name: fault-rest-api-py35
parent: tox
description: |
Run py35 test for fm-rest-api
nodeset: ubuntu-xenial
vars:
tox_envlist: py35
tox_extra_args: -c fm-rest-api/fm/tox.ini

View File

@ -0,0 +1,5 @@
[DEFAULT]
test_path=./fm/tests
top_dir=./
#parallel_class=True

View File

@ -46,9 +46,9 @@ def parse_isotime(timestr):
try: try:
return iso8601.parse_date(timestr) return iso8601.parse_date(timestr)
except iso8601.ParseError as e: except iso8601.ParseError as e:
raise ValueError(e.message) raise ValueError(str(e))
except TypeError as e: except TypeError as e:
raise ValueError(e.message) raise ValueError(str(e))
def strtime(at=None, fmt=PERFECT_TIME_FORMAT): def strtime(at=None, fmt=PERFECT_TIME_FORMAT):

View File

@ -0,0 +1,14 @@
# Copyright 2020 Intel Corporation.
# All Rights Reserved.
#
# 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.

View File

@ -0,0 +1,33 @@
# Copyright 2020 Intel Corporation.
# All Rights Reserved.
#
# 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.
"""Base classes for our unit tests.
Allows overriding of config for use of fakes, and some black magic for
inline callbacks.
"""
import testtools
class TestCase(testtools.TestCase):
"""Test case base class for all unit tests."""
def setUp(self):
"""Run before each test method to initialize test environment."""
super(TestCase, self).setUp()
def tearDown(self):
super(TestCase, self).tearDown()

View File

@ -0,0 +1,34 @@
# Copyright 2020 Intel Corporation.
# All Rights Reserved.
#
# 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.
import datetime
from fm.common import timeutils
from fm.tests import base
class FaultTimeUtilsTestCase(base.TestCase):
def test_isotime(self):
isotimestr = timeutils.isotime()
isotime = timeutils.parse_isotime(isotimestr)
self.assertTrue(isinstance(isotime, datetime.datetime))
isotimestr = timeutils.isotime(subsecond=True)
isotime = timeutils.parse_isotime(isotimestr)
self.assertTrue(isinstance(isotime, datetime.datetime))
self.assertRaises(ValueError, timeutils.parse_isotime, "bad input")
self.assertRaises(ValueError, timeutils.parse_isotime, isotime)

View File

@ -1 +1,10 @@
-r requirements.txt hacking!=0.13.0,<0.14,>=0.12.0
bashate >= 0.2
PyYAML >= 3.1.0
yamllint >= 0.5.2
stestr
testtools!=1.2.0,>=0.9.36
iso8601
stestr
mock
cython

33
fm-rest-api/fm/tox.ini Normal file
View File

@ -0,0 +1,33 @@
[tox]
envlist = py27,py35
minversion = 2.3
skipsdist = True
stxdir = {toxinidir}/../../../
[testenv]
install_command = pip install -U {opts} {packages}
setenv = VIRTUAL_ENV={envdir}
OS_STDOUT_CAPTURE=1
OS_STDERR_CAPTURE=1
OS_TEST_TIMEOUT=60
deps = -r{toxinidir}/test-requirements.txt
[testenv:venv]
basepython = python3
commands = {posargs}
[testenv:py27]
basepython = python2.7
commands =
stestr run {posargs}
stestr slowest
[testenv:py35]
basepython = python3.5
commands =
stestr run {posargs}
stestr slowest

View File

@ -2,4 +2,10 @@ hacking!=0.13.0,<0.14,>=0.12.0
bashate >= 0.2 bashate >= 0.2
PyYAML >= 3.1.0 PyYAML >= 3.1.0
yamllint >= 0.5.2 yamllint >= 0.5.2
spec_cleaner>=1.0.9 #spec_cleaner>=1.0.9
stestr
testtools!=1.2.0,>=0.9.36
iso8601
stestr
mock
cython

View File

@ -2,6 +2,7 @@
envlist = linters,pep8,rpm-packaging-lint envlist = linters,pep8,rpm-packaging-lint
minversion = 2.3 minversion = 2.3
skipsdist = True skipsdist = True
stxdir = {toxinidir}/../
[testenv] [testenv]
install_command = pip install -U {opts} {packages} install_command = pip install -U {opts} {packages}
@ -128,3 +129,4 @@ commands = {toxinidir}/devstack/build.sh
basepython = python3 basepython = python3
whitelist_externals = cat whitelist_externals = cat
commands = cat /etc/fm/fm.conf commands = cat /etc/fm/fm.conf