diff options
| author | Michal Arbet <michal.arbet@ultimum.io> | 2026-05-25 09:56:17 +0200 |
|---|---|---|
| committer | Michal Arbet <michal.arbet@ultimum.io> | 2026-07-02 17:28:39 +0200 |
| commit | ebc7eb82969af107f5a54b29b8ef898cd759a997 (patch) | |
| tree | cc197428c022f7ae1d863b741f1cc6601e331504 /tests/test_set_config.py | |
| parent | 575baf05c4964d46f0c8f13bcdf193ce5e863c82 (diff) | |
| download | kolla-ebc7eb82969af107f5a54b29b8ef898cd759a997.tar.gz kolla-ebc7eb82969af107f5a54b29b8ef898cd759a997.zip | |
set_configs: Add support for creating files and setting perms
Change-Id: I74a6d05007488b69fa040c40d01a07afae369794
Needed-By: https://review.opendev.org/c/openstack/kolla-ansible/+/989850
Signed-off-by: Michal Arbet <michal.arbet@ultimum.io>
Diffstat (limited to 'tests/test_set_config.py')
| -rw-r--r-- | tests/test_set_config.py | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/tests/test_set_config.py b/tests/test_set_config.py index 45f71bdd4..50c38c087 100644 --- a/tests/test_set_config.py +++ b/tests/test_set_config.py @@ -990,6 +990,19 @@ class ValidateConfigDirectoriesTest(base.BaseTestCase): } set_configs.validate_config(config) + def test_validate_config_directories_glob_rejected(self): + config = { + 'command': '/bin/true', + 'directories': [{'path': '/var/log/*', + 'owner': 'myuser', + 'perm': '0755'}] + } + self.assertRaisesRegex( + set_configs.InvalidConfig, + 'directories config does not support glob paths', + set_configs.validate_config, + config) + class CreateDirectoriesTest(base.BaseTestCase): @@ -1062,6 +1075,174 @@ class CreateDirectoriesTest(base.BaseTestCase): self.assertEqual(mock_handle_permissions.call_count, 2) +class ValidateConfigFilesTest(base.BaseTestCase): + + def test_validate_config_files_missing_path(self): + config = { + 'command': '/bin/true', + 'files': [{'owner': 'myuser', 'perm': '0644'}] + } + self.assertRaisesRegex( + set_configs.InvalidConfig, + 'files config is missing required keys', + set_configs.validate_config, + config) + + def test_validate_config_files_glob_rejected(self): + config = { + 'command': '/bin/true', + 'files': [{'path': '/var/log/*.log', + 'owner': 'myuser', 'perm': '0644'}] + } + self.assertRaisesRegex( + set_configs.InvalidConfig, + 'files config does not support glob paths', + set_configs.validate_config, + config) + + def test_validate_config_files_valid(self): + config = { + 'command': '/bin/true', + 'files': [ + {'path': '/var/log/foo.log', + 'owner': 'myuser', 'perm': '0644'} + ] + } + set_configs.validate_config(config) + + def test_validate_config_files_null_path_rejected(self): + config = { + 'command': '/bin/true', + 'files': [{'path': None, + 'owner': 'myuser', + 'perm': '0644'}] + } + self.assertRaisesRegex( + set_configs.InvalidConfig, + 'files config key "path" must not be null', + set_configs.validate_config, + config) + + +class CreateFilesTest(base.BaseTestCase): + + @mock.patch.object(set_configs, 'handle_permissions') + @mock.patch('builtins.open', new_callable=mock.mock_open) + @mock.patch('os.path.exists', return_value=False) + @mock.patch('os.path.isdir', side_effect=[False, True]) + @mock.patch('os.path.islink', return_value=False) + def test_create_file(self, mock_islink, mock_isdir, mock_exists, + mock_open, mock_handle_permissions): + config = { + 'files': [ + {'path': '/var/log/foo.log', + 'owner': 'myuser', 'perm': '0644'} + ] + } + set_configs.create_files(config) + + mock_open.assert_called_once_with('/var/log/foo.log', 'a') + mock_handle_permissions.assert_called_once_with( + {'permissions': [ + {'path': '/var/log/foo.log', + 'owner': 'myuser', 'perm': '0644'} + ]} + ) + + @mock.patch.object(set_configs, 'handle_permissions') + @mock.patch('builtins.open', new_callable=mock.mock_open) + @mock.patch('os.path.exists', return_value=True) + @mock.patch('os.path.isdir', side_effect=[False, True]) + @mock.patch('os.path.islink', return_value=False) + def test_create_file_already_exists(self, mock_islink, mock_isdir, + mock_exists, mock_open, + mock_handle_permissions): + config = { + 'files': [ + {'path': '/var/log/foo.log', + 'owner': 'myuser', 'perm': '0644'} + ] + } + + set_configs.create_files(config) + + mock_open.assert_not_called() + mock_handle_permissions.assert_called_once_with( + {'permissions': [ + {'path': '/var/log/foo.log', + 'owner': 'myuser', 'perm': '0644'} + ]} + ) + + @mock.patch('os.path.islink', return_value=True) + def test_create_file_rejects_symlink(self, mock_islink): + + config = { + 'files': [ + {'path': '/var/log/foo.log', + 'owner': 'myuser', + 'perm': '0644'} + ] + } + + self.assertRaisesRegex( + OSError, + 'Path is a symbolic link', + set_configs.create_files, + config) + + @mock.patch('os.path.isdir', return_value=True) + @mock.patch('os.path.islink', return_value=False) + def test_create_file_rejects_directory(self, mock_islink, mock_isdir): + + config = { + 'files': [ + {'path': '/var/log/somefile', + 'owner': 'myuser', + 'perm': '0644'} + ] + } + + self.assertRaisesRegex( + OSError, + 'Path is a directory', + set_configs.create_files, + config) + + @mock.patch('os.path.isdir', side_effect=[False, False]) + @mock.patch('os.path.islink', return_value=False) + def test_create_file_rejects_missing_parent(self, mock_islink, + mock_isdir): + config = { + 'files': [ + {'path': '/var/log/foo.log', + 'owner': 'myuser', + 'perm': '0644'} + ] + } + + self.assertRaisesRegex( + OSError, + 'Parent directory does not exist', + set_configs.create_files, + config) + + @mock.patch.object(set_configs, 'handle_permissions') + @mock.patch('os.path.exists') + @mock.patch('os.path.isdir') + @mock.patch('os.path.islink') + def test_create_files_no_files_key(self, mock_islink, mock_isdir, + mock_exists, + mock_handle_permissions): + config = {'command': '/bin/true'} + set_configs.create_files(config) + + mock_islink.assert_not_called() + mock_isdir.assert_not_called() + mock_exists.assert_not_called() + mock_handle_permissions.assert_not_called() + + class ExecuteConfigCheckStateMismatchTest(base.BaseTestCase): @mock.patch.object(set_configs, 'get_defaults_state') |
