def test_construct_backup_variables(self): """ Test that checks that the backup variables are populated as it should by the construct_backup_variables by asking the needed questions. A lambda function is used to simulate the user input. """ _backup_setup_inst = backup_setup.BackupSetup() counter = utils.ArrayCounter() # Checking parsing of backup variables with many empty answers answers = ['', 'y', '', 'y', '', 'y', '1', 'y', '2', 'y'] backup_utils.input = lambda _: answers[counter.array_counter()] bk_vars = _backup_setup_inst.construct_backup_variables('') # Check the parsed answers self.assertIsNone(bk_vars[AbstractBackup.OLDEST_OBJECT_BK_KEY]) self.assertIsNone(bk_vars[AbstractBackup.DAYS_TO_BACKUP_KEY]) self.assertIsNone(bk_vars[AbstractBackup.END_DATE_OF_BACKUP_KEY]) self.assertEqual(bk_vars[AbstractBackup.PERIODICITY_KEY], 1) self.assertEqual(bk_vars[AbstractBackup.BACKUP_LENGTH_THRESHOLD_KEY], 2) # Checking parsing of backup variables with all the answers given counter = utils.ArrayCounter() answers = [ '2013-07-28 20:48:53.197537+02:00', 'y', '2', 'y', '2015-07-28 20:48:53.197537+02:00', 'y', '3', 'y', '4', 'y' ] backup_utils.input = lambda _: answers[counter.array_counter()] bk_vars = _backup_setup_inst.construct_backup_variables('') # Check the parsed answers self.assertEqual(bk_vars[AbstractBackup.OLDEST_OBJECT_BK_KEY], answers[0]) self.assertEqual(bk_vars[AbstractBackup.DAYS_TO_BACKUP_KEY], 2) self.assertEqual(bk_vars[AbstractBackup.END_DATE_OF_BACKUP_KEY], answers[4]) self.assertEqual(bk_vars[AbstractBackup.PERIODICITY_KEY], 3) self.assertEqual(bk_vars[AbstractBackup.BACKUP_LENGTH_THRESHOLD_KEY], 4)
def test_full_backup_setup_script(self): """ This method is a full test of the backup setup script. It launches it, replies to all the question as the user would do and in the end it checks that the correct files were created with the right content. """ from aiida.common.utils import Capturing # Create a temp folder where the backup files will be placed temp_folder = tempfile.mkdtemp() try: temp_aiida_folder = os.path.join(temp_folder, '.aiida') # The predefined answers for the setup script counter = utils.ArrayCounter() answers = [ temp_aiida_folder, # the backup folder path '', # should the folder be created? '', # destination folder of the backup '', # should the folder be created? 'n', # print config explanation? '', # configure the backup conf file now? '2014-07-18 13:54:53.688484+00:00', # start date of backup? '', # is it correct? '', # days to backup? '', # is it correct? '2015-04-11 13:55:53.688484+00:00', # end date of backup '', # is it correct? '1', # periodicity '', # is it correct? '2', # threshold? '' # is it correct? ] backup_utils.input = lambda _: answers[counter.array_counter()] # Run the setup script and catch the sysout with Capturing(): backup_setup.BackupSetup().run() # Get the backup configuration files & dirs backup_conf_records = os.listdir(temp_aiida_folder) # Check if all files & dirs are there self.assertTrue( backup_conf_records is not None and len(backup_conf_records) == 4 and 'backup_dest' in backup_conf_records and 'backup_info.json.tmpl' in backup_conf_records and 'start_backup.py' in backup_conf_records and 'backup_info.json' in backup_conf_records, 'The created backup folder does not have the expected files. It contains: {}.' ''.format(backup_conf_records)) # Check the content of the main backup configuration file with open(os.path.join(temp_aiida_folder, 'backup_info.json'), encoding='utf8') as conf_jfile: conf_cont = json.load(conf_jfile) self.assertEqual( conf_cont[AbstractBackup.OLDEST_OBJECT_BK_KEY], '2014-07-18 13:54:53.688484+00:00') self.assertEqual(conf_cont[AbstractBackup.DAYS_TO_BACKUP_KEY], None) self.assertEqual( conf_cont[AbstractBackup.END_DATE_OF_BACKUP_KEY], '2015-04-11 13:55:53.688484+00:00') self.assertEqual(conf_cont[AbstractBackup.PERIODICITY_KEY], 1) self.assertEqual( conf_cont[AbstractBackup.BACKUP_LENGTH_THRESHOLD_KEY], 2) finally: shutil.rmtree(temp_folder, ignore_errors=True)
def setUpClass(cls, *args, **kwargs): super().setUpClass(*args, **kwargs) cls._bs_instance = backup_setup.BackupSetup()
class TestBackupScriptIntegration(AiidaTestCase): """Integration tests for the Backup classes.""" _aiida_rel_path = '.aiida' _backup_rel_path = 'backup' _repo_rel_path = 'repository' _bs_instance = backup_setup.BackupSetup() def test_integration(self): """Test integration""" from aiida.common.utils import Capturing # Fill in the repository with data self.fill_repo() try: # Create a temp folder where the backup files will be placed # and the backup will be stored temp_folder = tempfile.mkdtemp() # Capture the sysout of the following command with Capturing(): # Create the backup scripts backup_full_path = self.create_backup_scripts(temp_folder) # Put the backup folder in the path sys.path.append(backup_full_path) # Import the backup script - this action will also run it # It is assumed that the backup script ends with .py importlib.import_module(self._bs_instance._script_filename[:-3]) # Check the backup import os from aiida.manage.configuration import get_profile from aiida.common.utils import are_dir_trees_equal dirpath_repository = get_profile().repository_path source_dir = os.path.join(dirpath_repository, self._repo_rel_path) dest_dir = os.path.join(backup_full_path, self._bs_instance._file_backup_folder_rel, self._repo_rel_path) res, msg = are_dir_trees_equal(source_dir, dest_dir) self.assertTrue( res, 'The backed-up repository has differences to the original one. ' + str(msg) + '. If the test fails, report it in issue #2134.') finally: shutil.rmtree(temp_folder, ignore_errors=True) def fill_repo(self): """Utility function to create repository nodes""" from aiida.orm import CalcJobNode, Data, Dict extra_name = self.__class__.__name__ + '/test_with_subclasses' resources = {'num_machines': 1, 'num_mpiprocs_per_machine': 1} a1 = CalcJobNode(computer=self.computer) a1.set_option('resources', resources) a1.store() # To query only these nodes later a1.set_extra(extra_name, True) a3 = Data().store() a3.set_extra(extra_name, True) a4 = Dict(dict={'a': 'b'}).store() a4.set_extra(extra_name, True) a5 = Data().store() a5.set_extra(extra_name, True) # I don't set the extras, just to be sure that the filtering works # The filtering is needed because other tests will put stuff int he DB a6 = CalcJobNode(computer=self.computer) a6.set_option('resources', resources) a6.store() a7 = Data() a7.store() def create_backup_scripts(self, tmp_folder): """Utility function to create backup scripts""" backup_full_path = '{}/{}/{}/'.format(tmp_folder, self._aiida_rel_path, self._backup_rel_path) # The predefined answers for the setup script ac = utils.ArrayCounter() answers = [ backup_full_path, # the backup folder path '', # should the folder be created? '', # destination folder of the backup '', # should the folder be created? 'n', # print config explanation? '', # configure the backup conf file now? '', # start date of backup? '', # is it correct? '', # days to backup? '', # is it correct? '', # end date of backup '', # is it correct? '1', # periodicity '', # is it correct? '0', # threshold? '' # is it correct? ] backup_utils.input = lambda _: answers[ac.array_counter()] # Run the setup script self._bs_instance.run() return backup_full_path