blob: 9042b12f3546f844504693e91725f54a50be6358 [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
16
17import fixtures
18
19from tempest.cmd import init
Matthew Treinishffad78a2016-04-16 14:39:52 -040020from tempest.tests import base
Matthew Treinishf610aca2015-06-30 15:32:34 -040021
22
23class TestTempestInit(base.TestCase):
24
Masayuki Igawaac0b1712018-03-09 16:20:42 +090025 def test_generate_stestr_conf(self):
Matthew Treinishf610aca2015-06-30 15:32:34 -040026 # Create fake conf dir
27 conf_dir = self.useFixture(fixtures.TempDir())
28
29 init_cmd = init.TempestInit(None, None)
Chandan Kumar8a4396e2017-09-15 12:18:10 +053030 init_cmd.generate_stestr_conf(conf_dir.path)
Matthew Treinishf610aca2015-06-30 15:32:34 -040031
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 Kumar8a4396e2017-09-15 12:18:10 +053035 stestr_conf_file = init.STESTR_CONF % (discover_path, top_level_path)
Matthew Treinishf610aca2015-06-30 15:32:34 -040036
Chandan Kumar8a4396e2017-09-15 12:18:10 +053037 conf_path = conf_dir.join('.stestr.conf')
zhang.leia4b1cef2016-03-01 10:50:01 +080038 with open(conf_path, 'r') as conf_file:
Chandan Kumar8a4396e2017-09-15 12:18:10 +053039 self.assertEqual(conf_file.read(), stestr_conf_file)
Matthew Treinishf610aca2015-06-30 15:32:34 -040040
ghanshyamf0010842015-10-09 15:46:41 +090041 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)
ghanshyamf0010842015-10-09 15:46:41 +090045 init_cmd = init.TempestInit(None, None)
46 local_sample_conf_file = os.path.join(etc_dir_path,
47 'tempest.conf.sample')
Matthew Treinish3c39bb62016-08-09 14:44:44 -040048
ghanshyamf0010842015-10-09 15:46:41 +090049 # Verify no sample config file exist
50 self.assertFalse(os.path.isfile(local_sample_conf_file))
Matthew Treinishbdef1c72016-06-21 18:06:49 -040051 init_cmd.generate_sample_config(local_dir.path)
ghanshyamf0010842015-10-09 15:46:41 +090052
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 Treinish3c39bb62016-08-09 14:44:44 -040057 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 Koderer090b5dc2015-11-04 10:35:48 +0100103 def test_create_working_dir_with_existing_local_dir_non_empty(self):
David Paterson0bf52d42015-04-13 21:55:58 -0400104 fake_local_dir = self.useFixture(fixtures.TempDir())
105 fake_local_conf_dir = self.useFixture(fixtures.TempDir())
Marc Koderer090b5dc2015-11-04 10:35:48 +0100106 open("%s/foo" % fake_local_dir.path, 'w').close()
107
David Paterson0bf52d42015-04-13 21:55:58 -0400108 _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 Treinishf610aca2015-06-30 15:32:34 -0400114 def test_create_working_dir(self):
115 fake_local_dir = self.useFixture(fixtures.TempDir())
116 fake_local_conf_dir = self.useFixture(fixtures.TempDir())
David Paterson0bf52d42015-04-13 21:55:58 -0400117 os.rmdir(fake_local_dir.path)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400118 # 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 Kumar8a4396e2017-09-15 12:18:10 +0530128 stestr_dir = os.path.join(fake_local_dir.path, '.stestr')
Matthew Treinishf610aca2015-06-30 15:32:34 -0400129 self.assertTrue(os.path.isdir(lock_path))
130 self.assertTrue(os.path.isdir(etc_dir))
131 self.assertTrue(os.path.isdir(log_dir))
Chandan Kumar8a4396e2017-09-15 12:18:10 +0530132 self.assertTrue(os.path.isdir(stestr_dir))
Matthew Treinishf610aca2015-06-30 15:32:34 -0400133 # 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 Kumar8a4396e2017-09-15 12:18:10 +0530136 local_stestr_conf = os.path.join(fake_local_dir.path, '.stestr.conf')
Matthew Treinishf610aca2015-06-30 15:32:34 -0400137 self.assertTrue(os.path.isfile(fake_file_moved))
138 self.assertTrue(os.path.isfile(local_conf_file))
Chandan Kumar8a4396e2017-09-15 12:18:10 +0530139 self.assertTrue(os.path.isfile(local_stestr_conf))
Masayuki Igawaf8026412016-09-12 17:12:35 +0900140
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)