From 9cf0703f20f47143385260a6b63189f1c780f73e Mon Sep 17 00:00:00 2001
From: Matthew Treinish <mtreinish@kortar.org>
Date: Thu, 26 Oct 2017 17:46:55 -0400
Subject: [PATCH] Add unit tests to check for CONF getattr during import

Since the early days in tempest we've been fighting getattrs on CONF
during imports. We're able to get around this during test runs by lazy
loading the conf file. However, in things like the tempest commands this
doesn't work because we rely on the config file not being parsed to set
the config file path. This commit adds unit tests to check the import
of the command files for getattrs on CONF. This should prevent future
regressions.

While not strictly necessary because of the lazy loading this also gives
a framework to potentially address the CONF getatrr on discovery. The
first revision of this patch includes the discovery test, for reference.
But we have 212 cases of getattr during import (which includes lots of
skip decorators) so it's unlikely to change any time soon.

Change-Id: Ib2c15dbd06ca810cc899258758cc8a297055fdf8
Closes-Bug: #1726357
---
 tempest/tests/test_imports.py | 69 +++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 tempest/tests/test_imports.py

diff --git a/tempest/tests/test_imports.py b/tempest/tests/test_imports.py
new file mode 100644
index 0000000000..6f1cfca709
--- /dev/null
+++ b/tempest/tests/test_imports.py
@@ -0,0 +1,69 @@
+# Copyright 2017 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 mock
+
+from tempest.tests import base
+
+
+class ConfCounter(object):
+
+    def __init__(self, *args, **kwargs):
+        self.count = 0
+
+    def __getattr__(self, key):
+        self.count += 1
+        return mock.MagicMock()
+
+    def get_counts(self):
+        return self.count
+
+
+class TestImports(base.TestCase):
+    def setUp(self):
+        super(TestImports, self).setUp()
+        self.conf_mock = self.patch('tempest.config.CONF',
+                                    new_callable=ConfCounter)
+
+    def test_account_generator_command_import(self):
+        from tempest.cmd import account_generator  # noqa
+        self.assertEqual(0, self.conf_mock.get_counts())
+
+    def test_cleanup_command_import(self):
+        from tempest.cmd import cleanup  # noqa
+        self.assertEqual(0, self.conf_mock.get_counts())
+
+    def test_init_command_import(self):
+        from tempest.cmd import init  # noqa
+        self.assertEqual(0, self.conf_mock.get_counts())
+
+    def test_list_plugins_command_import(self):
+        from tempest.cmd import list_plugins  # noqa
+        self.assertEqual(0, self.conf_mock.get_counts())
+
+    def test_run_command_import(self):
+        from tempest.cmd import run  # noqa
+        self.assertEqual(0, self.conf_mock.get_counts())
+
+    def test_subunit_descibe_command_import(self):
+        from tempest.cmd import subunit_describe_calls  # noqa
+        self.assertEqual(0, self.conf_mock.get_counts())
+
+    def test_verify_tempest_config_command_import(self):
+        from tempest.cmd import verify_tempest_config  # noqa
+        self.assertEqual(0, self.conf_mock.get_counts())
+
+    def test_workspace_command_import(self):
+        from tempest.cmd import workspace  # noqa
+        self.assertEqual(0, self.conf_mock.get_counts())