Fix init command

Init command was not working properly as it could not locate the
configuration sample file.
Steps taken:
- Move config-generator.tempest.conf to etc so it will be installed
with pbr.
- Update all files with new path to config-generator-tempest.conf as
needed.
- Refactor init command so if it detects we are not in a virtual
environment, try to find the global config directory /etc/tempest.
If that fails fallback to [sys.prefix]/etc/tempest.

Closes-Bug: #1491058
Closes-Bug: #1490670
Change-Id: I960bc711ff78ac2b0441ef63dff8ec4fb268fd3a
diff --git a/README.rst b/README.rst
index 7108eaf..bf513bd 100644
--- a/README.rst
+++ b/README.rst
@@ -168,7 +168,7 @@
 
     $> cd $TEMPEST_ROOT_DIR
     $> oslo-config-generator --config-file \
-        tools/config/config-generator.tempest.conf \
+        etc/config-generator.tempest.conf \
         --output-file etc/tempest.conf
 
 After that, open up the ``etc/tempest.conf`` file and edit the
diff --git a/doc/source/conf.py b/doc/source/conf.py
index f85899b..12d1d40 100644
--- a/doc/source/conf.py
+++ b/doc/source/conf.py
@@ -34,7 +34,7 @@
               'oslo_config.sphinxconfiggen',
              ]
 
-config_generator_config_file = '../../tools/config/config-generator.tempest.conf'
+config_generator_config_file = '../../etc/config-generator.tempest.conf'
 sample_config_basename = '_static/tempest'
 
 todo_include_todos = True
diff --git a/tools/config/config-generator.tempest.conf b/etc/config-generator.tempest.conf
similarity index 100%
rename from tools/config/config-generator.tempest.conf
rename to etc/config-generator.tempest.conf
diff --git a/tempest/cmd/init.py b/tempest/cmd/init.py
index 289b978..af8f270 100644
--- a/tempest/cmd/init.py
+++ b/tempest/cmd/init.py
@@ -52,17 +52,21 @@
     real_prefix = getattr(sys, 'real_prefix', None)
     base_prefix = getattr(sys, 'base_prefix', None)
     prefix = sys.prefix
-    if real_prefix is None and base_prefix is None:
-        # Not running in a virtual environnment of any kind
-        return '/etc/tempest'
-    elif (real_prefix is None and base_prefix is not None and
-            base_prefix == prefix):
-        # Probably not running in a virtual environment
+    if (real_prefix is None and
+            (base_prefix is None or base_prefix == prefix)):
+        # Probably not running in a virtual environment.
         # NOTE(andreaf) we cannot distinguish this case from the case of
         # a virtual environment created with virtualenv, and running python3.
-        return '/etc/tempest'
+        # Also if it appears we are not in virtual env and fail to find
+        # global config: '/etc/tempest', fall back to
+        # '[sys.prefix]/etc/tempest'
+        global_conf_dir = '/etc/tempest'
+        if os.path.isdir(global_conf_dir):
+            return global_conf_dir
+        else:
+            return os.path.join(prefix, 'etc/tempest')
     else:
-        return os.path.join(sys.prefix, 'etc/tempest')
+        return os.path.join(prefix, 'etc/tempest')
 
 
 class TempestInit(command.Command):
@@ -99,9 +103,12 @@
     def copy_config(self, etc_dir, config_dir):
         shutil.copytree(config_dir, etc_dir)
 
-    def generate_sample_config(self, local_dir):
+    def generate_sample_config(self, local_dir, config_dir):
+        conf_generator = os.path.join(config_dir,
+                                      'config-generator.tempest.conf')
+
         subprocess.call(['oslo-config-generator', '--config-file',
-                         'tools/config/config-generator.tempest.conf'],
+                         conf_generator],
                         cwd=local_dir)
 
     def create_working_dir(self, local_dir, config_dir):
@@ -109,6 +116,10 @@
         if not os.path.isdir(local_dir):
             LOG.debug('Creating local working dir: %s' % local_dir)
             os.mkdir(local_dir)
+        else:
+            raise OSError("Directory you are trying to initialize already "
+                          "exists: %s" % local_dir)
+
         lock_dir = os.path.join(local_dir, 'tempest_lock')
         etc_dir = os.path.join(local_dir, 'etc')
         config_path = os.path.join(etc_dir, 'tempest.conf')
@@ -125,7 +136,7 @@
         # Create and copy local etc dir
         self.copy_config(etc_dir, config_dir)
         # Generate the sample config file
-        self.generate_sample_config(local_dir)
+        self.generate_sample_config(local_dir, config_dir)
         # Update local confs to reflect local paths
         self.update_local_conf(config_path, lock_dir, log_dir)
         # Generate a testr conf file
diff --git a/tempest/tests/cmd/test_tempest_init.py b/tempest/tests/cmd/test_tempest_init.py
index 6b5af7e..2b868be 100644
--- a/tempest/tests/cmd/test_tempest_init.py
+++ b/tempest/tests/cmd/test_tempest_init.py
@@ -12,6 +12,7 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
+# import mock
 import os
 
 import fixtures
@@ -39,9 +40,19 @@
         self.addCleanup(conf_file.close)
         self.assertEqual(conf_file.read(), testr_conf_file)
 
+    def test_create_working_dir_with_existing_local_dir(self):
+        fake_local_dir = self.useFixture(fixtures.TempDir())
+        fake_local_conf_dir = self.useFixture(fixtures.TempDir())
+        _init = init.TempestInit(None, None)
+        self.assertRaises(OSError,
+                          _init.create_working_dir,
+                          fake_local_dir.path,
+                          fake_local_conf_dir.path)
+
     def test_create_working_dir(self):
         fake_local_dir = self.useFixture(fixtures.TempDir())
         fake_local_conf_dir = self.useFixture(fixtures.TempDir())
+        os.rmdir(fake_local_dir.path)
         # Create a fake conf file
         fake_file = fake_local_conf_dir.join('conf_file.conf')
         open(fake_file, 'w').close()
diff --git a/tox.ini b/tox.ini
index 3250344..09c8626 100644
--- a/tox.ini
+++ b/tox.ini
@@ -24,7 +24,7 @@
          bash tools/pretty_tox.sh '{posargs}'
 
 [testenv:genconfig]
-commands = oslo-config-generator --config-file tools/config/config-generator.tempest.conf
+commands = oslo-config-generator --config-file etc/config-generator.tempest.conf
 
 [testenv:cover]
 setenv = OS_TEST_PATH=./tempest/tests