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() ac = utils.ArrayCounter() # Checking parsing of backup variables with many empty answers answers = ["", "y", "", "y", "", "y", "1", "y", "2", "y"] # utils.raw_input = lambda _: answers[self.array_counter()] utils.raw_input = lambda _: answers[ac.array_counter()] bk_vars = _backup_setup_inst.construct_backup_variables("") # Check the parsed answers self.assertIsNone(bk_vars[Backup._oldest_object_bk_key]) self.assertIsNone(bk_vars[Backup._days_to_backup_key]) self.assertIsNone(bk_vars[Backup._end_date_of_backup_key]) self.assertEqual(bk_vars[Backup._periodicity_key], 1) self.assertEqual(bk_vars[Backup._backup_length_threshold_key], 2) # Checking parsing of backup variables with all the answers given ac = 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" ] utils.raw_input = lambda _: answers[ac.array_counter()] bk_vars = _backup_setup_inst.construct_backup_variables("") # Check the parsed answers self.assertEqual(bk_vars[Backup._oldest_object_bk_key], answers[0]) self.assertEqual(bk_vars[Backup._days_to_backup_key], 2) self.assertEqual(bk_vars[Backup._end_date_of_backup_key], answers[4]) self.assertEqual(bk_vars[Backup._periodicity_key], 3) self.assertEqual(bk_vars[Backup._backup_length_threshold_key], 4)
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() ac = utils.ArrayCounter() # Checking parsing of backup variables with many empty answers answers = ["", "y", "", "y", "", "y", "1", "y", "2", "y"] # utils.raw_input = lambda _: answers[self.array_counter()] utils.raw_input = lambda _: answers[ac.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 ac = 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" ] utils.raw_input = lambda _: answers[ac.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.utils.capturing 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 ac = utils.ArrayCounter() self.seq = -1 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? utils.raw_input = lambda _: answers[ac.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 = [f for f in 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 doesn't 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")) 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)
class TestBackupScriptIntegration(AiidaTestCase): _aiida_rel_path = ".aiida" _backup_rel_path = "backup" _repo_rel_path = "repository" _bs_instance = backup_setup.BackupSetup() def test_integration(self): from aiida.utils.capturing 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 from aiida import settings from filecmp import dircmp import os from aiida.common.utils import are_dir_trees_equal source_dir = os.path.join(settings.REPOSITORY_PATH, self._repo_rel_path) dest_dir = os.path.join(backup_full_path, self._bs_instance._file_backup_folder_rel, self._repo_rel_path) self.assertTrue( are_dir_trees_equal(source_dir, dest_dir), "The backed-up repository has differences " "to the original one.") finally: shutil.rmtree(temp_folder, ignore_errors=True) def fill_repo(self): from aiida.orm import JobCalculation, CalculationFactory, Data, DataFactory extra_name = self.__class__.__name__ + "/test_with_subclasses" calc_params = { 'computer': self.computer, 'resources': { 'num_machines': 1, 'num_mpiprocs_per_machine': 1 } } TemplateReplacerCalc = CalculationFactory( 'simpleplugins.templatereplacer') ParameterData = DataFactory('parameter') a1 = JobCalculation(**calc_params).store() # To query only these nodes later a1.set_extra(extra_name, True) a2 = TemplateReplacerCalc(**calc_params).store() # To query only these nodes later a2.set_extra(extra_name, True) a3 = Data().store() a3.set_extra(extra_name, True) a4 = ParameterData(dict={'a': 'b'}).store() a4.set_extra(extra_name, True) a5 = Node().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 = JobCalculation(**calc_params) a6.store() a7 = Node() a7.store() def create_backup_scripts(self, tmp_folder): 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? utils.raw_input = lambda _: answers[ac.array_counter()] # Run the setup script self._bs_instance.run() return backup_full_path