From 1548d82ffdc32a20e87e952c6b3c486b7c4968a9 Mon Sep 17 00:00:00 2001
From: "James E. Blair" <jeblair@redhat.com>
Date: Thu, 28 Jun 2018 16:26:54 -0700
Subject: [PATCH] Add a python unit test framework

Add a unit testing framework for python roles.  Thanks to Matthew
Treinish for the suggestion of how to perform discovery (and much of the
code which is copied from Tempest).

Change-Id: Iec95dd1026a41614def57c65c3faa0516a682a5a
---
 .gitignore                                    |  1 +
 .stestr.conf                                  |  3 ++
 .../library/__init__.py                       |  0
 roles/sphinx/library/__init__.py              |  0
 roles/tox/library/__init__.py                 |  0
 roles/upload-afs/library/__init__.py          |  0
 roles/validate-host/library/__init__.py       |  0
 test-requirements.txt                         |  1 +
 tests/__init__.py                             |  0
 tests/test_discovery.py                       | 35 +++++++++++++++++++
 tox.ini                                       |  4 +--
 11 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 .stestr.conf
 create mode 100644 roles/multi-node-known-hosts/library/__init__.py
 create mode 100644 roles/sphinx/library/__init__.py
 create mode 100644 roles/tox/library/__init__.py
 create mode 100644 roles/upload-afs/library/__init__.py
 create mode 100644 roles/validate-host/library/__init__.py
 create mode 100644 tests/__init__.py
 create mode 100644 tests/test_discovery.py

diff --git a/.gitignore b/.gitignore
index 58948d485..48bdfc8de 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@
 # Unit test / coverage reports
 .coverage
 .tox
+.stestr
 
 # Sphinx
 doc/build
diff --git a/.stestr.conf b/.stestr.conf
new file mode 100644
index 000000000..ea359caed
--- /dev/null
+++ b/.stestr.conf
@@ -0,0 +1,3 @@
+[DEFAULT]
+test_path=./tests
+top_dir=./
diff --git a/roles/multi-node-known-hosts/library/__init__.py b/roles/multi-node-known-hosts/library/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/roles/sphinx/library/__init__.py b/roles/sphinx/library/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/roles/tox/library/__init__.py b/roles/tox/library/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/roles/upload-afs/library/__init__.py b/roles/upload-afs/library/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/roles/validate-host/library/__init__.py b/roles/validate-host/library/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/test-requirements.txt b/test-requirements.txt
index d786ee5aa..d65d4dc39 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -9,3 +9,4 @@ ansible<2.4
 ansible-lint
 bashate>=0.2
 zuul-sphinx>=0.1.1
+stestr>=1.0.0 # Apache-2.0
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
new file mode 100644
index 000000000..a88ff064b
--- /dev/null
+++ b/tests/test_discovery.py
@@ -0,0 +1,35 @@
+# Copyright 2013 IBM Corp.
+#
+#    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 os
+import unittest
+
+
+def load_tests(loader, tests, pattern):
+    suite = unittest.TestSuite()
+    base_path = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
+    # Load role tests
+    for role_dir in os.listdir('roles'):
+        full_role_dir = os.path.join(base_path, 'roles', role_dir)
+        lib_dir = os.path.join(full_role_dir, 'library')
+        if not os.path.exists(lib_dir):
+            continue
+        if not pattern:
+            suite.addTests(loader.discover(lib_dir,
+                                           top_level_dir=base_path))
+        else:
+            suite.addTests(loader.discover(lib_dir, pattern=pattern,
+                           top_level_dir=base_path))
+
+    return suite
diff --git a/tox.ini b/tox.ini
index 5c71936c5..bcb350d48 100644
--- a/tox.ini
+++ b/tox.ini
@@ -7,8 +7,8 @@ envlist = linters
 basepython = python3
 install_command = pip install {opts} {packages}
 deps = -r{toxinidir}/test-requirements.txt
-commands =
-  python setup.py testr --slowest --testr-args='{posargs}'
+commands = stestr run {posargs}
+           stestr slowest
 
 [testenv:bindep]
 # Do not install any requirements. We want this to be fast and work even if