def test_file_writable_with_stout(): """ Creates a new util.FileWritable object using stdout. Checks the util.FileWritable.file attribute is set to stdout. """ test_writable = util.FileWritable("-") assert_equals(test_writable.file, sys.stdout)
def test_file_writable(): """ Creates a new util.FileWritable object using a temporary filename. Writes a string to it using util.FileWritable.write(). The checksum of this file is compared to a good known checksum. The temporary file is only removed if the test is successful. """ temp_file = tempfile.NamedTemporaryFile() test_writable = util.FileWritable(temp_file.name) test_writable.write("onionperf") test_writable.close() expected_checksum = "5001ed4ab25b52543946fa63da829d4eeab1bd254c89ffdad0877186e074b385" with open(temp_file.name, 'rb') as f: file_bytes = f.read() file_checksum = hashlib.sha256(file_bytes).hexdigest() assert_equals(file_checksum, expected_checksum) temp_file.close()
def test_file_writable_compressed(): """ Creates a new util.FileWritable object using a temporary filename and compression. Writes a string to it using util.FileWritable.write(). The checksum of this file is compared to a good known checksum. The temporary file is only removed if the test is successful. """ temp_file = tempfile.NamedTemporaryFile(suffix=".xz") test_writable = util.FileWritable(temp_file.name, True) test_writable.write("onionperf") test_writable.close() expected_checksum = "3556b3bee6bb56d0a42676cbbf5784ebe4151fe65b0797f42260f93212e2df11" with open(temp_file.name, 'rb') as f: file_bytes = f.read() file_checksum = hashlib.sha256(file_bytes).hexdigest() assert_equals(file_checksum, expected_checksum) temp_file.close()
def test_file_writable_compressed(): """ Creates a new util.FileWritable object using a temporary filename and compression. Writes a string to it using util.FileWritable.write(). The checksum of this file is compared to a good known checksum. The temporary file is only removed if the test is successful. """ temp_file = tempfile.NamedTemporaryFile(suffix=".xz") test_writable = util.FileWritable(temp_file.name, True) test_writable.write("onionperf") test_writable.close() expected_checksum = "66a6256bc4b04529c7123fa9573d30de659ffaa0cce1cc9b189817c8bf30e813" with open(temp_file.name) as f: file_bytes = f.read() file_checksum = hashlib.sha256(file_bytes).hexdigest() assert_equals(file_checksum, expected_checksum) temp_file.close()
def test_file_writable_rotate_file(): """ Creates a temporary working directory. Creates a new util.FileWritable object in the working directory. Rotates file using util.FileWritable.rotate_file with a fixed date and time. Checks path log_archive has been created in the working directory. Checks path log_archive is a directory. Checks file with the appropiate name has been rotated in the log_archive directory. Removes working directory only if successful. """ work_dir = tempfile.mkdtemp() test_writable = util.FileWritable(os.path.join(work_dir, "logfile")) test_writable.write("onionperf") test_writable.rotate_file(datetime.datetime(2018, 11, 27, 0, 0, 0)) created_dir = os.path.join(work_dir, "log_archive") rotated_file = os.path.join(created_dir, "logfile_2018-11-27_00:00:00.gz") assert(os.path.exists(created_dir)) assert(os.path.isdir(created_dir)) assert(os.path.exists(rotated_file)) shutil.rmtree(work_dir)