def test_backupDHCPBackend(tempDir): with getOpsiBackupArchive(tempdir=tempDir, keepArchive=True) as archive: with pytest.raises(OpsiBackupBackendNotFound): archive.restoreDHCPBackend() archiveName = archive.name for backend in archive._getBackends("dhcpd"): dhcpConfigFile = backend['config']['dhcpdConfigFile'] md5OfOriginalFile = md5sum(dhcpConfigFile) archive.backupDHCPBackend() archive.close() os.remove(dhcpConfigFile) break else: raise RuntimeError("No DHCPD backend configured!") with getOpsiBackupArchive(name=archiveName, mode="r", tempdir=tempDir) as backup: backup.restoreDHCPBackend() md5OfRestoredFile = md5sum(dhcpConfigFile) assert md5OfOriginalFile == md5OfRestoredFile
def testConfiguringDHCPDBackendWithEmptyFile(tempDir): filename = 'dhcpd_test.conf' with open(filename, 'wx'): pass oldHash = md5sum(filename) with disableSystemCallsForConfigureDHCPD(): configureDHCPD(filename) newHash = md5sum(filename) assert oldHash != newHash
def depot_createMd5SumFile(self, filename, md5sumFilename): if not os.path.exists(filename): raise BackendIOError(u"File not found: %s" % filename) logger.info(u"Creating md5sum file '%s'" % md5sumFilename) md5 = md5sum(filename) with open(md5sumFilename, 'w') as md5file: md5file.write(md5)
def depot_getMD5Sum(self, filename, forceCalculation=False): checksum = None try: if not forceBool(forceCalculation): hashFile = filename + '.md5' try: with open(hashFile) as fileHandle: checksum = fileHandle.read() logger.info(u"Using pre-calculated MD5sum from '{0}'.", hashFile) except (OSError, IOError): pass if not checksum: checksum = md5sum(filename) logger.info(u"MD5sum of file '{0}' is '{1}'", filename, checksum) return checksum except Exception as error: raise BackendIOError(u"Failed to get md5sum: %s" % error)
def testCreatingMd5sum(fileAndHash): testFile, expectedHash = fileAndHash assert expectedHash == md5sum(testFile)
def testPackageContentFileCreation(outsideFile, outsideDir): with workInTemporaryDirectory() as tempDir: content = fillDirectory(tempDir) outsideLink = 'jlink' assert outsideLink not in content for filename in (f for f, t in content.items() if t == 'f'): os.symlink(outsideFile, os.path.join(tempDir, outsideLink)) content[outsideLink] = 'f' break outsideDirLink = 'dlink' assert outsideDirLink not in content for dirname in (f for f, t in content.items() if t == 'd'): os.symlink(outsideDir, os.path.join(tempDir, outsideDirLink)) content[outsideDirLink] = 'd' break clientDataFiles = findFiles(tempDir) filename = os.path.join(tempDir, 'test.files') contentFile = PackageContentFile(filename) contentFile.setProductClientDataDir(tempDir) contentFile.setClientDataFiles(clientDataFiles) contentFile.generate() assert os.path.exists(filename) assert os.path.getsize(filename) > 10, 'Generated file is empty!' # Manual parsing of the file contents to ensure that the # format matches our requirements. with open(filename) as generatedFile: for line in generatedFile: try: entry, path, size = line.split(' ', 2) path = path.strip("'") assert entry == content.pop(path), "Type mismatch!" if path == outsideLink: assert entry == 'f' elif path == outsideDirLink: assert entry == 'd' if entry == 'd': assert int(size.strip()) == 0 elif entry == 'f': size, hashSum = size.split(' ', 1) assert os.path.getsize(path) == int(size) assert not hashSum.startswith("'") assert not hashSum.endswith("'") hashSum = hashSum.strip() assert md5sum(path) == hashSum elif entry == 'l': size, target = size.split(' ', 1) assert int(size) == 0 target = target.strip() assert target.startswith("'") assert target.endswith("'") target = target.strip("'") assert target else: raise RuntimeError( "Unexpected type {0!r}".format(entry)) except Exception: print("Processing line {0!r} failed".format(line)) raise assert not content, "Files not listed in content file: {0}".format( ', '.join(content))