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 |
| 16 | |
| 17 | import fixtures |
| 18 | |
| 19 | from tempest.cmd import init |
Matthew Treinish | ffad78a | 2016-04-16 14:39:52 -0400 | [diff] [blame] | 20 | from tempest.tests import base |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class TestTempestInit(base.TestCase): |
| 24 | |
Masayuki Igawa | ac0b171 | 2018-03-09 16:20:42 +0900 | [diff] [blame^] | 25 | def test_generate_stestr_conf(self): |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 26 | # Create fake conf dir |
| 27 | conf_dir = self.useFixture(fixtures.TempDir()) |
| 28 | |
| 29 | init_cmd = init.TempestInit(None, None) |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 30 | init_cmd.generate_stestr_conf(conf_dir.path) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 31 | |
| 32 | # Generate expected file contents |
| 33 | top_level_path = os.path.dirname(os.path.dirname(init.__file__)) |
| 34 | discover_path = os.path.join(top_level_path, 'test_discover') |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 35 | stestr_conf_file = init.STESTR_CONF % (discover_path, top_level_path) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 36 | |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 37 | conf_path = conf_dir.join('.stestr.conf') |
zhang.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 38 | with open(conf_path, 'r') as conf_file: |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 39 | self.assertEqual(conf_file.read(), stestr_conf_file) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 40 | |
ghanshyam | f001084 | 2015-10-09 15:46:41 +0900 | [diff] [blame] | 41 | def test_generate_sample_config(self): |
| 42 | local_dir = self.useFixture(fixtures.TempDir()) |
| 43 | etc_dir_path = os.path.join(local_dir.path, 'etc/') |
| 44 | os.mkdir(etc_dir_path) |
ghanshyam | f001084 | 2015-10-09 15:46:41 +0900 | [diff] [blame] | 45 | init_cmd = init.TempestInit(None, None) |
| 46 | local_sample_conf_file = os.path.join(etc_dir_path, |
| 47 | 'tempest.conf.sample') |
Matthew Treinish | 3c39bb6 | 2016-08-09 14:44:44 -0400 | [diff] [blame] | 48 | |
ghanshyam | f001084 | 2015-10-09 15:46:41 +0900 | [diff] [blame] | 49 | # Verify no sample config file exist |
| 50 | self.assertFalse(os.path.isfile(local_sample_conf_file)) |
Matthew Treinish | bdef1c7 | 2016-06-21 18:06:49 -0400 | [diff] [blame] | 51 | init_cmd.generate_sample_config(local_dir.path) |
ghanshyam | f001084 | 2015-10-09 15:46:41 +0900 | [diff] [blame] | 52 | |
| 53 | # Verify sample config file exist with some content |
| 54 | self.assertTrue(os.path.isfile(local_sample_conf_file)) |
| 55 | self.assertGreater(os.path.getsize(local_sample_conf_file), 0) |
| 56 | |
Matthew Treinish | 3c39bb6 | 2016-08-09 14:44:44 -0400 | [diff] [blame] | 57 | def test_update_local_conf(self): |
| 58 | local_dir = self.useFixture(fixtures.TempDir()) |
| 59 | etc_dir_path = os.path.join(local_dir.path, 'etc/') |
| 60 | os.mkdir(etc_dir_path) |
| 61 | lock_dir = os.path.join(local_dir.path, 'tempest_lock') |
| 62 | config_path = os.path.join(etc_dir_path, 'tempest.conf') |
| 63 | log_dir = os.path.join(local_dir.path, 'logs') |
| 64 | |
| 65 | init_cmd = init.TempestInit(None, None) |
| 66 | |
| 67 | # Generate the config file |
| 68 | init_cmd.generate_sample_config(local_dir.path) |
| 69 | |
| 70 | # Create a conf file with populated values |
| 71 | config_parser_pre = init_cmd.get_configparser(config_path) |
| 72 | with open(config_path, 'w+') as conf_file: |
| 73 | # create the same section init will check for and add values to |
| 74 | config_parser_pre.add_section('oslo_concurrency') |
| 75 | config_parser_pre.set('oslo_concurrency', 'TEST', local_dir.path) |
| 76 | # create a new section |
| 77 | config_parser_pre.add_section('TEST') |
| 78 | config_parser_pre.set('TEST', 'foo', "bar") |
| 79 | config_parser_pre.write(conf_file) |
| 80 | |
| 81 | # Update the config file the same way tempest init does |
| 82 | init_cmd.update_local_conf(config_path, lock_dir, log_dir) |
| 83 | |
| 84 | # parse the new config file to verify it |
| 85 | config_parser_post = init_cmd.get_configparser(config_path) |
| 86 | |
| 87 | # check that our value in oslo_concurrency wasn't overwritten |
| 88 | self.assertTrue(config_parser_post.has_section('oslo_concurrency')) |
| 89 | self.assertEqual(config_parser_post.get('oslo_concurrency', 'TEST'), |
| 90 | local_dir.path) |
| 91 | # check that the lock directory was set correctly |
| 92 | self.assertEqual(config_parser_post.get('oslo_concurrency', |
| 93 | 'lock_path'), lock_dir) |
| 94 | |
| 95 | # check that our new section still exists and wasn't modified |
| 96 | self.assertTrue(config_parser_post.has_section('TEST')) |
| 97 | self.assertEqual(config_parser_post.get('TEST', 'foo'), 'bar') |
| 98 | |
| 99 | # check that the DEFAULT values are correct |
| 100 | # NOTE(auggy): has_section ignores DEFAULT |
| 101 | self.assertEqual(config_parser_post.get('DEFAULT', 'log_dir'), log_dir) |
| 102 | |
Marc Koderer | 090b5dc | 2015-11-04 10:35:48 +0100 | [diff] [blame] | 103 | def test_create_working_dir_with_existing_local_dir_non_empty(self): |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 104 | fake_local_dir = self.useFixture(fixtures.TempDir()) |
| 105 | fake_local_conf_dir = self.useFixture(fixtures.TempDir()) |
Marc Koderer | 090b5dc | 2015-11-04 10:35:48 +0100 | [diff] [blame] | 106 | open("%s/foo" % fake_local_dir.path, 'w').close() |
| 107 | |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 108 | _init = init.TempestInit(None, None) |
| 109 | self.assertRaises(OSError, |
| 110 | _init.create_working_dir, |
| 111 | fake_local_dir.path, |
| 112 | fake_local_conf_dir.path) |
| 113 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 114 | def test_create_working_dir(self): |
| 115 | fake_local_dir = self.useFixture(fixtures.TempDir()) |
| 116 | fake_local_conf_dir = self.useFixture(fixtures.TempDir()) |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 117 | os.rmdir(fake_local_dir.path) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 118 | # Create a fake conf file |
| 119 | fake_file = fake_local_conf_dir.join('conf_file.conf') |
| 120 | open(fake_file, 'w').close() |
| 121 | init_cmd = init.TempestInit(None, None) |
| 122 | init_cmd.create_working_dir(fake_local_dir.path, |
| 123 | fake_local_conf_dir.path) |
| 124 | # Assert directories are created |
| 125 | lock_path = os.path.join(fake_local_dir.path, 'tempest_lock') |
| 126 | etc_dir = os.path.join(fake_local_dir.path, 'etc') |
| 127 | log_dir = os.path.join(fake_local_dir.path, 'logs') |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 128 | stestr_dir = os.path.join(fake_local_dir.path, '.stestr') |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 129 | self.assertTrue(os.path.isdir(lock_path)) |
| 130 | self.assertTrue(os.path.isdir(etc_dir)) |
| 131 | self.assertTrue(os.path.isdir(log_dir)) |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 132 | self.assertTrue(os.path.isdir(stestr_dir)) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 133 | # Assert file creation |
| 134 | fake_file_moved = os.path.join(etc_dir, 'conf_file.conf') |
| 135 | local_conf_file = os.path.join(etc_dir, 'tempest.conf') |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 136 | local_stestr_conf = os.path.join(fake_local_dir.path, '.stestr.conf') |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 137 | self.assertTrue(os.path.isfile(fake_file_moved)) |
| 138 | self.assertTrue(os.path.isfile(local_conf_file)) |
Chandan Kumar | 8a4396e | 2017-09-15 12:18:10 +0530 | [diff] [blame] | 139 | self.assertTrue(os.path.isfile(local_stestr_conf)) |
Masayuki Igawa | f802641 | 2016-09-12 17:12:35 +0900 | [diff] [blame] | 140 | |
| 141 | def test_take_action_fails(self): |
| 142 | class ParsedArgs(object): |
| 143 | workspace_dir = self.useFixture(fixtures.TempDir()).path |
| 144 | workspace_path = os.path.join(workspace_dir, 'workspace.yaml') |
| 145 | name = 'test' |
| 146 | dir_base = self.useFixture(fixtures.TempDir()).path |
| 147 | dir = os.path.join(dir_base, 'foo', 'bar') |
| 148 | config_dir = self.useFixture(fixtures.TempDir()).path |
| 149 | show_global_dir = False |
| 150 | pa = ParsedArgs() |
| 151 | init_cmd = init.TempestInit(None, None) |
| 152 | self.assertRaises(OSError, init_cmd.take_action, pa) |
| 153 | # one more trying should be a same error not "workspace already exists" |
| 154 | self.assertRaises(OSError, init_cmd.take_action, pa) |