def test_basic_logrotate(): """ Verify current openvstorage logrotate configuration Apply the openvstorage logrotate on custom logfile and see if it rotates as predicted Update ownership of custom file and verify logrotate raises issue """ storagerouters = GeneralStorageRouter.get_storage_routers() logrotate_content = """{0} {{ rotate 5 size 20M compress copytruncate notifempty }} {1} {{ su ovs ovs rotate 10 size 19M compress delaycompress notifempty create 666 ovs ovs postrotate /usr/bin/pkill -SIGUSR1 arakoon endscript }}""" if len(storagerouters) == 0: raise ValueError('No Storage Routers found in the model') logrotate_include_dir = '/etc/logrotate.d' logrotate_cfg_file = '/etc/logrotate.conf' logrotate_cron_file = '/etc/cron.daily/logrotate' logrotate_ovs_file = '{0}/openvstorage-logs'.format(logrotate_include_dir) expected_logrotate_content = logrotate_content.format('/var/log/ovs/*.log', '/var/log/arakoon/*/*.log') # Verify basic logrotate configurations for storagerouter in storagerouters: root_client = SSHClient(endpoint=storagerouter, username='******') assert_true(expr=root_client.file_exists(filename=logrotate_cfg_file), msg='Logrotate config {0} does not exist on Storage Router {1}'.format(logrotate_cfg_file, storagerouter.name)) assert_true(expr=root_client.file_exists(filename=logrotate_ovs_file), msg='Logrotate file {0} does not exist on Storage Router {1}'.format(logrotate_ovs_file, storagerouter.name)) assert_true(expr=root_client.file_exists(filename=logrotate_cron_file), msg='Logrotate file {0} does not exist on Storage Router {1}'.format(logrotate_cron_file, storagerouter.name)) assert_true(expr='include {0}'.format(logrotate_include_dir) in root_client.file_read(filename=logrotate_cfg_file).splitlines(), msg='Logrotate on Storage Router {0} does not include {1}'.format(storagerouter.name, logrotate_include_dir)) assert_true(expr='/usr/sbin/logrotate /etc/logrotate.conf' in root_client.file_read(filename=logrotate_cron_file).splitlines(), msg='Logrotate will not be executed on Storage Router {0}'.format(storagerouter.name)) actual_file_contents = root_client.file_read(filename=logrotate_ovs_file).rstrip('\n') assert_equal(first=expected_logrotate_content, second=actual_file_contents, msg='Logrotate contents does not match expected contents on Storage Router {0}'.format(storagerouter.name)) # Create custom logrotate file for testing purposes custom_logrotate_cfg_file = '/opt/OpenvStorage/ci/logrotate-conf' custom_logrotate_dir = '/opt/OpenvStorage/ci/logrotate' custom_logrotate_file1 = '{0}/logrotate_test_file1.log'.format(custom_logrotate_dir) custom_logrotate_file2 = '{0}/logrotate_test_file2.log'.format(custom_logrotate_dir) custom_logrotate_content = logrotate_content.format(custom_logrotate_file1, custom_logrotate_file2) local_sr = GeneralStorageRouter.get_local_storagerouter() root_client = SSHClient(endpoint=local_sr, username='******') root_client.file_write(filename=custom_logrotate_cfg_file, contents=custom_logrotate_content) # No logfile present --> logrotate should fail assert_raises(excClass=CalledProcessError, callableObj=root_client.run, command='logrotate {0}'.format(custom_logrotate_cfg_file)) ########################################## # Test 1st logrotate configuration entry # ########################################## root_client.dir_create(directories=custom_logrotate_dir) root_client.dir_chown(directories=custom_logrotate_dir, user='******', group='ovs', recursive=True) root_client.run(command='touch {0}'.format(custom_logrotate_file1)) root_client.run(command='touch {0}'.format(custom_logrotate_file2)) root_client.file_chmod(filename=custom_logrotate_file1, mode=666) root_client.file_chmod(filename=custom_logrotate_file2, mode=666) # Write data to the file less than size for rotation and verify rotation GeneralVDisk.write_to_volume(location=custom_logrotate_file1, count=15, bs='1M', input_type='zero', root_client=root_client) root_client.run('logrotate {0}'.format(custom_logrotate_cfg_file)) assert_equal(first=len(root_client.file_list(directory=custom_logrotate_dir)), second=2, msg='More files than expected present in {0}'.format(custom_logrotate_dir)) # Write data to file larger than size in configuration and verify amount of rotations files_to_delete = [] for counter in range(7): expected_file = '{0}.{1}.gz'.format(custom_logrotate_file1, counter + 1 if counter < 5 else 5) GeneralVDisk.write_to_volume(location=custom_logrotate_file1, count=30, bs='1M', input_type='zero', root_client=root_client) root_client.run('logrotate {0}'.format(custom_logrotate_cfg_file)) assert_equal(first=len(root_client.file_list(directory=custom_logrotate_dir)), second=counter + 3 if counter < 5 else 7, msg='Not the expected amount of files present in {0}'.format(custom_logrotate_dir)) assert_true(expr=root_client.file_exists(filename=expected_file), msg='Logrotate did not create the expected file {0}'.format(expected_file)) user_info = General.get_owner_group_for_path(path=expected_file, root_client=root_client) assert_equal(first='root', second=user_info['user']['name'], msg='Expected file to be owned by user "root", but instead its owned by "{0}"'.format(user_info['user']['name'])) assert_equal(first='root', second=user_info['group']['name'], msg='Expected file to be owned by group "root", but instead its owned by "{0}"'.format(user_info['group']['name'])) files_to_delete.append(expected_file) root_client.file_delete(filenames=files_to_delete) ########################################## # Test 2nd logrotate configuration entry # ########################################## root_client.file_chown(filenames=custom_logrotate_file2, user='******', group='ovs') # Write data to the file less than size for rotation and verify rotation GeneralVDisk.write_to_volume(location=custom_logrotate_file2, count=15, bs='1M', input_type='zero', root_client=root_client) root_client.run('logrotate {0}'.format(custom_logrotate_cfg_file)) assert_equal(first=len(root_client.file_list(directory=custom_logrotate_dir)), second=2, msg='More files than expected present in {0}'.format(custom_logrotate_dir)) # Write data to file larger than size in configuration and verify amount of rotations for counter in range(12): if counter == 0: # Delaycompress --> file is not compressed during initial cycle expected_file = '{0}.1'.format(custom_logrotate_file2) else: expected_file = '{0}.{1}.gz'.format(custom_logrotate_file2, counter + 1 if counter < 10 else 10) GeneralVDisk.write_to_volume(location=custom_logrotate_file2, count=30, bs='1M', input_type='zero', root_client=root_client) root_client.run('logrotate {0}'.format(custom_logrotate_cfg_file)) assert_equal(first=len(root_client.file_list(directory=custom_logrotate_dir)), second=counter + 3 if counter < 10 else 12, msg='Not the expected amount of files present in {0}'.format(custom_logrotate_dir)) assert_true(expr=root_client.file_exists(filename=expected_file), msg='Logrotate did not create the expected file {0}'.format(expected_file)) user_info = General.get_owner_group_for_path(path=expected_file, root_client=root_client) assert_equal(first='ovs', second=user_info['user']['name'], msg='Expected file to be owned by user "root", but instead its owned by "{0}"'.format(user_info['user']['name'])) assert_equal(first='ovs', second=user_info['group']['name'], msg='Expected file to be owned by group "root", but instead its owned by "{0}"'.format(user_info['group']['name'])) root_client.dir_delete(directories=custom_logrotate_dir) root_client.file_delete(filenames=custom_logrotate_cfg_file)