#!/usr/share/ucs-test/runner python ## desc: Test if the number of LDAP backups is limited correctly ## roles: [domaincontroller_master] ## tags: [basic, apptest] ## bugs: [38554] ## packages: [univention-ldap-server] ## exposure: dangerous import os import subprocess import time from univention.config_registry import handler_set from univention.testing.ucr import UCSTestConfigRegistry from univention.testing.utils import fail BACKUP_SCRIPT = '/usr/sbin/univention-ldap-backup' BACKUP_LIMIT = 'ucr/univention-ldap-backup/limit' BACKUP_DIRECTORY = '/var/univention-backup' BACKUP_PREFIX = 'ldap-backup_' BACKUP_EXTENSION = '.ldif.gz' BACKUP_LOG_EXTENSION = '.log.gz' DATE_FORMAT = '%Y%m%d' TEST_DATE = '19000101' # long before the epoch def count_backups(): backups = [backup for backup in os.listdir(BACKUP_DIRECTORY) if backup.endswith(BACKUP_EXTENSION)] return len(backups) # Move the latest backup (and log) to a temporary directory, to allow new backup. today = time.strftime(DATE_FORMAT) backup_today = os.path.join(BACKUP_DIRECTORY, ''.join([BACKUP_PREFIX, today, BACKUP_EXTENSION])) backup_log_today = os.path.join(BACKUP_DIRECTORY, ''.join([BACKUP_PREFIX, today, BACKUP_LOG_EXTENSION])) backup_original = os.path.join(BACKUP_DIRECTORY, 'temp', os.path.basename(backup_today)) backup_log_original = os.path.join(BACKUP_DIRECTORY, 'temp', os.path.basename(backup_log_today)) if os.path.exists(backup_today): os.renames(backup_today, backup_original) if os.path.exists(backup_log_today): os.renames(backup_log_today, backup_log_original) # This file should be automatically deleted when the limit is exceeded. test_backup = os.path.join(BACKUP_DIRECTORY, ''.join([BACKUP_PREFIX, TEST_DATE, BACKUP_EXTENSION])) open(test_backup, 'a').close() backups_before = count_backups() # Set the limit to the current number of backups and make a new backup. error = None with UCSTestConfigRegistry() as ucr: handler_set(['%s=%d' % (BACKUP_LIMIT, backups_before)]) try: subprocess.check_output(BACKUP_SCRIPT) except subprocess.CalledProcessError as e: error = e backups_after = count_backups() # Cleanup if os.path.exists(backup_original): os.renames(backup_original, backup_today) if os.path.exists(backup_log_original): os.renames(backup_log_original, backup_log_today) if error: fail(error.output) if os.path.exists(test_backup): fail('The oldest backup was not deleted.') if backups_after != backups_before: fail('The total number of backups changed unexpectedly.')