def hashsum_changed(self): new_hashsum = get_checksum(self.entries) check = new_hashsum != self.hashsum logger.debug( f"DATABASE:CHECK_HASHSUM ({new_hashsum} != {self.hashsum}) => {check}" ) return check
def verify_checksum(): """Verfies that the backup checksum matches the imported data checksum. If verification fails we halt the temp file cleanup so the user can re-check the checksum files and decide how to proceed. """ if cloudsql_importer_constants.VERIFY_CHECKSUM: checksumed_tables = checksum.get_checksum(cloudsql_importer_constants) checksum_file_name = (cloudsql_importer_constants.DATABASE_NAME + cloudsql_importer_constants.CHECKSUM_FILE_SUFFIX) import_checksum_path = os.path.join( cloudsql_importer_constants.DATABASE_TEMP_FOLDER, checksum_file_name) backup_checksum_path = os.path.join( cloudsql_importer_constants.MYSQL_BACKUP_FOLDER, checksum_file_name) file_writer.write(import_checksum_path, checksumed_tables) checksum.compare_checksum_files(import_checksum_path, backup_checksum_path) else: logging.info("Skipping checksum verification")
def checksum_tables(): """Get the checksums for the Database's tables and stores them in a file.""" logging.info("Checksumming exported tables") checksumed_tables = checksum.get_checksum(mysql_exporter_constants) checksum_path = os.path.join( mysql_exporter_constants.TEMP_FOLDER, mysql_exporter_constants.DATABASE_NAME + mysql_exporter_constants.CHECKSUM_FILE_SUFFIX) file_writer.write(checksum_path, checksumed_tables)
def load(self): logger.info("DATABASE:LOAD") with open(self.path, "r") as f: database_string = f.read() raw_database = bytearray.fromhex(database_string).decode() self.entries = self.process_entries(raw_database) self.loaded = True self.hashsum = get_checksum(self.entries) logger.debug(f"DATABASE:HASHSUM => {self.hashsum}")
def test_checksum(self, sql_wrapper_mock): database_tables = "table1\ntable2\ntable3\n" tables_to_checksum = list(filter(bool, database_tables.split("\n"))) sql_wrapper_mock.call_show_tables.return_value = database_tables sql_wrapper_mock.call_checksum.return_value = EXPECTED_CHECKSUMS actual_checksums = checksum.get_checksum(self.constants_mock) self.assertEqual(EXPECTED_CHECKSUMS, actual_checksums) sql_wrapper_mock.call_show_tables.assert_called_with( self.constants_mock) sql_wrapper_mock.call_checksum.assert_called_with( tables_to_checksum, self.constants_mock)
def test_cloudsql_importer(): """Compares cloudsql_importer's checksum versus the raw import's checksum.""" logging.info("Creating database from raw dump and getting checksum") sql_wrapper.call_create_database(cloudsql_importer_constants) end_to_end_runner.load_database_from_raw_dump(cloudsql_importer_constants) raw_checksum = checksum.get_checksum(cloudsql_importer_constants) sql_wrapper.call_drop_database(cloudsql_importer_constants) cloudsql_importer.main() importer_checksum = checksum.get_checksum(cloudsql_importer_constants) if raw_checksum == importer_checksum: logging.info("Checksums are equal!") else: logging.error("Checksums don't match!") logging.error("Raw Checksum: %s", raw_checksum) logging.error("Importer Checksum: %s", importer_checksum) logging.info("Cleaning temp files") end_to_end_runner.clean_temp_folder( cloudsql_importer_constants.TEMP_FOLDER) assert raw_checksum == importer_checksum
def test_one_double(self): self.assertEqual(get_checksum(['aa','b']), 0)
def test_something(self): self.assertEqual(get_checksum(['a']), 0)
def test_bigger_mix(self): self.assertEqual(get_checksum(['f', 'ee','bbb', 'pp', 'fff', 'c', 'ddd', 'qiee', 'poppld', 'hjhjhjhj', 'qqbq']), 15)
def test_mix(self): self.assertEqual(get_checksum(['f', 'ee','bbb', 'pp', 'fff', 'c']), 4)
def test_triple_and_double(self): self.assertEqual(get_checksum(['ee','bbb', 'c']), 1)
def test_two_triple(self): self.assertEqual(get_checksum(['eee','bbb', 'c']), 0)
def test_one_triple(self): self.assertEqual(get_checksum(['a','bbb']), 0)
def test_two_doubles(self): self.assertEqual(get_checksum(['aa','dd']), 0)
def day_two(): dir_path = os.path.dirname(os.path.realpath(__file__)) freqs = read_file_to_list(os.path.join(dir_path,'day_two_input.txt')) print(get_checksum(freqs))