Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 1 | # 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 | |
| 15 | import os |
ghanshyam | f001084 | 2015-10-09 15:46:41 +0900 | [diff] [blame] | 16 | import shutil |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 17 | |
| 18 | import fixtures |
| 19 | |
| 20 | from tempest.cmd import init |
Matthew Treinish | ffad78a | 2016-04-16 14:39:52 -0400 | [diff] [blame] | 21 | from tempest.tests import base |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class 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.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 39 | with open(conf_path, 'r') as conf_file: |
| 40 | self.assertEqual(conf_file.read(), testr_conf_file) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 41 | |
ghanshyam | f001084 | 2015-10-09 15:46:41 +0900 | [diff] [blame] | 42 | 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 Koderer | 090b5dc | 2015-11-04 10:35:48 +0100 | [diff] [blame] | 60 | def test_create_working_dir_with_existing_local_dir_non_empty(self): |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 61 | fake_local_dir = self.useFixture(fixtures.TempDir()) |
| 62 | fake_local_conf_dir = self.useFixture(fixtures.TempDir()) |
Marc Koderer | 090b5dc | 2015-11-04 10:35:48 +0100 | [diff] [blame] | 63 | open("%s/foo" % fake_local_dir.path, 'w').close() |
| 64 | |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 65 | _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 Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 71 | def test_create_working_dir(self): |
| 72 | fake_local_dir = self.useFixture(fixtures.TempDir()) |
| 73 | fake_local_conf_dir = self.useFixture(fixtures.TempDir()) |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 74 | os.rmdir(fake_local_dir.path) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 75 | # 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)) |