diff options
| author | Jeffrey Zhang <zhang.lei.fly@gmail.com> | 2016-08-29 15:35:26 +0800 |
|---|---|---|
| committer | Jeffrey Zhang <jeffrey.zhang@99cloud.net> | 2017-01-18 03:38:22 +0000 |
| commit | 97e17becce5dc7c2261c5e54cfdb18c02ecdcf96 (patch) | |
| tree | 7624fe7f55975e1c625da696a9b5e7c16b4c010f /tests/test_set_config.py | |
| parent | 98914c483ac5b377e04420bf8d314dee21453169 (diff) | |
| download | kolla-97e17becce5dc7c2261c5e54cfdb18c02ecdcf96.tar.gz kolla-97e17becce5dc7c2261c5e54cfdb18c02ecdcf96.zip | |
Fix the set config error
* fix wrong handle for directory
* refactor the code
Closes-Bug: #1563741
Change-Id: I83869e074cb2924f1c5b49a67e57aefef623a8b0
Diffstat (limited to 'tests/test_set_config.py')
| -rw-r--r-- | tests/test_set_config.py | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/tests/test_set_config.py b/tests/test_set_config.py index 3e946c797..e67db9bc2 100644 --- a/tests/test_set_config.py +++ b/tests/test_set_config.py @@ -10,6 +10,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import collections +import copy import imp import json import mock @@ -64,3 +66,212 @@ class LoadFromEnv(base.BaseTestCase): mock.call().write(u'/bin/true'), mock.call().__exit__(None, None, None)], mo.mock_calls) + +FAKE_CONFIG_FILES = [ + set_configs.ConfigFile( + '/var/lib/kolla/config_files/bar.conf', + '/foo/bar.conf', 'user1', '0644') +] + +FAKE_CONFIG_FILE = FAKE_CONFIG_FILES[0] + + +class ConfigFileTest(base.BaseTestCase): + + @mock.patch('os.path.exists', return_value=False) + def test_delete_path_not_exists(self, mock_exists): + + config_file = copy.deepcopy(FAKE_CONFIG_FILE) + config_file._delete_path(config_file.dest) + + mock_exists.assert_called_with(config_file.dest) + + @mock.patch('os.path.exists', return_value=True) + @mock.patch('os.path.isdir', return_value=True) + @mock.patch('shutil.rmtree') + def test_delete_path_exist_dir(self, + mock_rmtree, + mock_isdir, + mock_exists): + + config_file = copy.deepcopy(FAKE_CONFIG_FILE) + config_file._delete_path(config_file.dest) + + mock_exists.assert_called_with(config_file.dest) + mock_isdir.assert_called_with(config_file.dest) + mock_rmtree.assert_called_with(config_file.dest) + + @mock.patch('os.path.exists', return_value=True) + @mock.patch('os.path.isdir', return_value=False) + @mock.patch('os.remove') + def test_delete_path_exist_file(self, + mock_remove, + mock_isdir, + mock_exists): + + config_file = copy.deepcopy(FAKE_CONFIG_FILE) + config_file._delete_path(config_file.dest) + + mock_exists.assert_called_with(config_file.dest) + mock_isdir.assert_called_with(config_file.dest) + mock_remove.assert_called_with(config_file.dest) + + @mock.patch('os.chmod') + @mock.patch('os.chown') + @mock.patch('pwd.getpwnam') + def test_set_permission(self, + mock_getpwnam, + mock_chown, + mock_chmod): + + User = collections.namedtuple('User', ['pw_uid', 'pw_gid']) + user = User(3333, 4444) + mock_getpwnam.return_value = user + + config_file = copy.deepcopy(FAKE_CONFIG_FILE) + config_file._set_permission(config_file.dest) + + mock_getpwnam.assert_called_with(config_file.owner) + mock_chown.assert_called_with(config_file.dest, 3333, 4444) + mock_chmod.assert_called_with(config_file.dest, 420) + + @mock.patch('glob.glob', return_value=[]) + def test_copy_no_source_not_optional(self, + mock_glob): + + config_file = copy.deepcopy(FAKE_CONFIG_FILE) + + self.assertRaises(set_configs.MissingRequiredSource, + config_file.copy) + + @mock.patch('glob.glob', return_value=[]) + def test_copy_no_source_optional(self, mock_glob): + + config_file = copy.deepcopy(FAKE_CONFIG_FILE) + config_file.optional = True + + config_file.copy() + + mock_glob.assert_called_with(config_file.source) + + @mock.patch.object(set_configs.ConfigFile, '_copy_file') + @mock.patch('os.path.isdir', return_value=False) + @mock.patch.object(set_configs.ConfigFile, '_create_parent_dirs') + @mock.patch.object(set_configs.ConfigFile, '_delete_path') + @mock.patch('glob.glob') + def test_copy_one_source_file(self, mock_glob, mock_delete_path, + mock_create_parent_dirs, mock_isdir, + mock_copy_file): + config_file = copy.deepcopy(FAKE_CONFIG_FILE) + + mock_glob.return_value = [config_file.source] + + config_file.copy() + + mock_glob.assert_called_with(config_file.source) + mock_delete_path.assert_called_with(config_file.dest) + mock_create_parent_dirs.assert_called_with(config_file.dest) + mock_isdir.assert_called_with(config_file.source) + mock_copy_file.assert_called_with(config_file.source, + config_file.dest) + + @mock.patch.object(set_configs.ConfigFile, '_copy_dir') + @mock.patch('os.path.isdir', return_value=True) + @mock.patch.object(set_configs.ConfigFile, '_create_parent_dirs') + @mock.patch.object(set_configs.ConfigFile, '_delete_path') + @mock.patch('glob.glob') + def test_copy_one_source_dir(self, mock_glob, mock_delete_path, + mock_create_parent_dirs, mock_isdir, + mock_copy_dir): + config_file = copy.deepcopy(FAKE_CONFIG_FILE) + + mock_glob.return_value = [config_file.source] + + config_file.copy() + + mock_glob.assert_called_with(config_file.source) + mock_delete_path.assert_called_with(config_file.dest) + mock_create_parent_dirs.assert_called_with(config_file.dest) + mock_isdir.assert_called_with(config_file.source) + mock_copy_dir.assert_called_with(config_file.source, + config_file.dest) + + @mock.patch.object(set_configs.ConfigFile, '_copy_file') + @mock.patch('os.path.isdir', return_value=False) + @mock.patch.object(set_configs.ConfigFile, '_create_parent_dirs') + @mock.patch.object(set_configs.ConfigFile, '_delete_path') + @mock.patch('glob.glob') + def test_copy_glob_source_file(self, mock_glob, mock_delete_path, + mock_create_parent_dirs, mock_isdir, + mock_copy_file): + config_file = set_configs.ConfigFile( + '/var/lib/kolla/config_files/bar.*', '/foo/', 'user1', '0644') + + mock_glob.return_value = ['/var/lib/kolla/config_files/bar.conf', + '/var/lib/kolla/config_files/bar.yml'] + + config_file.copy() + + mock_glob.assert_called_with(config_file.source) + + self.assertEqual(mock_delete_path.mock_calls, + [mock.call('/foo/bar.conf'), + mock.call('/foo/bar.yml')]) + self.assertEqual(mock_create_parent_dirs.mock_calls, + [mock.call('/foo/bar.conf'), + mock.call('/foo/bar.yml')]) + self.assertEqual(mock_isdir.mock_calls, + [mock.call('/var/lib/kolla/config_files/bar.conf'), + mock.call('/var/lib/kolla/config_files/bar.yml')]) + self.assertEqual(mock_copy_file.mock_calls, + [mock.call('/var/lib/kolla/config_files/bar.conf', + '/foo/bar.conf'), + mock.call('/var/lib/kolla/config_files/bar.yml', + '/foo/bar.yml')]) + + @mock.patch.object(set_configs.ConfigFile, '_cmp_file') + @mock.patch('os.path.isdir', return_value=False) + @mock.patch('glob.glob') + def test_check_glob_source_file(self, mock_glob, mock_isdir, + mock_cmp_file): + config_file = set_configs.ConfigFile( + '/var/lib/kolla/config_files/bar.*', '/foo/', 'user1', '0644') + + mock_glob.return_value = ['/var/lib/kolla/config_files/bar.conf', + '/var/lib/kolla/config_files/bar.yml'] + mock_cmp_file.return_value = True + + config_file.check() + + self.assertEqual(mock_isdir.mock_calls, + [mock.call('/var/lib/kolla/config_files/bar.conf'), + mock.call('/var/lib/kolla/config_files/bar.yml')]) + self.assertEqual(mock_cmp_file.mock_calls, + [mock.call('/var/lib/kolla/config_files/bar.conf', + '/foo/bar.conf'), + mock.call('/var/lib/kolla/config_files/bar.yml', + '/foo/bar.yml')]) + + @mock.patch.object(set_configs.ConfigFile, '_cmp_file') + @mock.patch('os.path.isdir', return_value=False) + @mock.patch('glob.glob') + def test_check_glob_source_file_no_equal(self, mock_glob, mock_isdir, + mock_cmp_file): + config_file = set_configs.ConfigFile( + '/var/lib/kolla/config_files/bar.*', '/foo/', 'user1', '0644') + + mock_glob.return_value = ['/var/lib/kolla/config_files/bar.conf', + '/var/lib/kolla/config_files/bar.yml'] + mock_cmp_file.side_effect = [True, False] + + self.assertRaises(set_configs.ConfigFileBadState, + config_file.check) + + self.assertEqual(mock_isdir.mock_calls, + [mock.call('/var/lib/kolla/config_files/bar.conf'), + mock.call('/var/lib/kolla/config_files/bar.yml')]) + self.assertEqual(mock_cmp_file.mock_calls, + [mock.call('/var/lib/kolla/config_files/bar.conf', + '/foo/bar.conf'), + mock.call('/var/lib/kolla/config_files/bar.yml', + '/foo/bar.yml')]) |
