blob: 685a0b3e182baaf4a2a102c1e43707cea5610ce4 [file] [log] [blame]
Matthew Treinishf610aca2015-06-30 15:32:34 -04001# Copyright 2015 Hewlett-Packard Development Company, L.P.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import os
ghanshyamf0010842015-10-09 15:46:41 +090016import shutil
Matthew Treinishf610aca2015-06-30 15:32:34 -040017
18import fixtures
19
20from tempest.cmd import init
Matthew Treinishffad78a2016-04-16 14:39:52 -040021from tempest.tests import base
Matthew Treinishf610aca2015-06-30 15:32:34 -040022
23
24class TestTempestInit(base.TestCase):
25
26 def test_generate_testr_conf(self):
27 # Create fake conf dir
28 conf_dir = self.useFixture(fixtures.TempDir())
29
30 init_cmd = init.TempestInit(None, None)
31 init_cmd.generate_testr_conf(conf_dir.path)
32
33 # Generate expected file contents
34 top_level_path = os.path.dirname(os.path.dirname(init.__file__))
35 discover_path = os.path.join(top_level_path, 'test_discover')
36 testr_conf_file = init.TESTR_CONF % (top_level_path, discover_path)
37
38 conf_path = conf_dir.join('.testr.conf')
zhang.leia4b1cef2016-03-01 10:50:01 +080039 with open(conf_path, 'r') as conf_file:
40 self.assertEqual(conf_file.read(), testr_conf_file)
Matthew Treinishf610aca2015-06-30 15:32:34 -040041
ghanshyamf0010842015-10-09 15:46:41 +090042 def test_generate_sample_config(self):
43 local_dir = self.useFixture(fixtures.TempDir())
44 etc_dir_path = os.path.join(local_dir.path, 'etc/')
45 os.mkdir(etc_dir_path)
46 tmp_dir = self.useFixture(fixtures.TempDir())
47 config_dir = os.path.join(tmp_dir.path, 'config/')
48 shutil.copytree('etc/', config_dir)
49 init_cmd = init.TempestInit(None, None)
50 local_sample_conf_file = os.path.join(etc_dir_path,
51 'tempest.conf.sample')
52 # Verify no sample config file exist
53 self.assertFalse(os.path.isfile(local_sample_conf_file))
54 init_cmd.generate_sample_config(local_dir.path, config_dir)
55
56 # Verify sample config file exist with some content
57 self.assertTrue(os.path.isfile(local_sample_conf_file))
58 self.assertGreater(os.path.getsize(local_sample_conf_file), 0)
59
Marc Koderer090b5dc2015-11-04 10:35:48 +010060 def test_create_working_dir_with_existing_local_dir_non_empty(self):
David Paterson0bf52d42015-04-13 21:55:58 -040061 fake_local_dir = self.useFixture(fixtures.TempDir())
62 fake_local_conf_dir = self.useFixture(fixtures.TempDir())
Marc Koderer090b5dc2015-11-04 10:35:48 +010063 open("%s/foo" % fake_local_dir.path, 'w').close()
64
David Paterson0bf52d42015-04-13 21:55:58 -040065 _init = init.TempestInit(None, None)
66 self.assertRaises(OSError,
67 _init.create_working_dir,
68 fake_local_dir.path,
69 fake_local_conf_dir.path)
70
Matthew Treinishf610aca2015-06-30 15:32:34 -040071 def test_create_working_dir(self):
72 fake_local_dir = self.useFixture(fixtures.TempDir())
73 fake_local_conf_dir = self.useFixture(fixtures.TempDir())
David Paterson0bf52d42015-04-13 21:55:58 -040074 os.rmdir(fake_local_dir.path)
Matthew Treinishf610aca2015-06-30 15:32:34 -040075 # Create a fake conf file
76 fake_file = fake_local_conf_dir.join('conf_file.conf')
77 open(fake_file, 'w').close()
78 init_cmd = init.TempestInit(None, None)
79 init_cmd.create_working_dir(fake_local_dir.path,
80 fake_local_conf_dir.path)
81 # Assert directories are created
82 lock_path = os.path.join(fake_local_dir.path, 'tempest_lock')
83 etc_dir = os.path.join(fake_local_dir.path, 'etc')
84 log_dir = os.path.join(fake_local_dir.path, 'logs')
85 testr_dir = os.path.join(fake_local_dir.path, '.testrepository')
86 self.assertTrue(os.path.isdir(lock_path))
87 self.assertTrue(os.path.isdir(etc_dir))
88 self.assertTrue(os.path.isdir(log_dir))
89 self.assertTrue(os.path.isdir(testr_dir))
90 # Assert file creation
91 fake_file_moved = os.path.join(etc_dir, 'conf_file.conf')
92 local_conf_file = os.path.join(etc_dir, 'tempest.conf')
93 local_testr_conf = os.path.join(fake_local_dir.path, '.testr.conf')
94 self.assertTrue(os.path.isfile(fake_file_moved))
95 self.assertTrue(os.path.isfile(local_conf_file))
96 self.assertTrue(os.path.isfile(local_testr_conf))