Beispiel #1
0
def verify_image_checksum(image_location, expected_checksum):
    """Verifies checksum (md5) of image file against the expected one.

    This method generates the checksum of the image file on the fly and
    verifies it against the expected checksum provided as argument.

    :param image_location: location of image file whose checksum is verified.
    :param expected_checksum: checksum to be checked against
    :raises: ImageRefValidationFailed, if invalid file path or
             verification fails.
    """
    try:
        actual_checksum = fileutils.compute_file_checksum(image_location,
                                                          algorithm='md5')
    except IOError as e:
        LOG.error("Error opening file: %(file)s", {'file': image_location})
        raise exception.ImageRefValidationFailed(image_href=image_location,
                                                 reason=e)

    if actual_checksum != expected_checksum:
        msg = (_('Error verifying image checksum. Image %(image)s failed to '
                 'verify against checksum %(checksum)s. Actual checksum is: '
                 '%(actual_checksum)s') % {
                     'image': image_location,
                     'checksum': expected_checksum,
                     'actual_checksum': actual_checksum
                 })
        LOG.error(msg)
        raise exception.ImageRefValidationFailed(image_href=image_location,
                                                 reason=msg)
Beispiel #2
0
    def test_compute_checksum_default_algorithm(self):
        path = fileutils.write_to_tempfile(self.content)
        self.assertTrue(os.path.exists(path))
        self.check_file_content(self.content, path)

        expected_checksum = hashlib.sha256()
        expected_checksum.update(self.content)

        actual_checksum = fileutils.compute_file_checksum(path)

        self.assertEqual(expected_checksum.hexdigest(), actual_checksum)
Beispiel #3
0
    def test_compute_checksum_default_algorithm(self):
        path = fileutils.write_to_tempfile(self.content)
        self.assertTrue(os.path.exists(path))
        self.check_file_content(self.content, path)

        expected_checksum = hashlib.sha256()
        expected_checksum.update(self.content)

        actual_checksum = fileutils.compute_file_checksum(path)

        self.assertEqual(expected_checksum.hexdigest(), actual_checksum)
Beispiel #4
0
def file_has_content(path, content, hash_algo='sha256'):
    """Checks that content of the file is the same as provided reference.

    :param path: path to file
    :param content: reference content to check against
    :param hash_algo: hashing algo from hashlib to use, default is 'sha256'
    :returns: True if the hash of reference content is the same as
        the hash of file's content, False otherwise
    """
    file_hash_hex = fileutils.compute_file_checksum(path, algorithm=hash_algo)
    ref_hash = _get_hash_object(hash_algo)
    encoded_content = (content.encode(
        encoding='utf-8') if isinstance(content, str) else content)
    ref_hash.update(encoded_content)
    return file_hash_hex == ref_hash.hexdigest()
Beispiel #5
0
def file_has_content(path, content, hash_algo='md5'):
    """Checks that content of the file is the same as provided reference.

    :param path: path to file
    :param content: reference content to check against
    :param hash_algo: hashing algo from hashlib to use, default is 'md5'
    :returns: True if the hash of reference content is the same as
        the hash of file's content, False otherwise
    """
    file_hash_hex = fileutils.compute_file_checksum(path, algorithm=hash_algo)
    ref_hash = _get_hash_object(hash_algo)
    encoded_content = (content.encode(encoding='utf-8')
                       if isinstance(content, six.string_types) else content)
    ref_hash.update(encoded_content)
    return file_hash_hex == ref_hash.hexdigest()
Beispiel #6
0
    def test_compute_checksum_sleep_0_called(self):
        path = fileutils.write_to_tempfile(self.content)
        self.assertTrue(os.path.exists(path))
        self.check_file_content(self.content, path)

        expected_checksum = hashlib.sha256()
        expected_checksum.update(self.content)

        with mock.patch.object(time, "sleep") as sleep_mock:
            actual_checksum = fileutils.compute_file_checksum(path,
                                                              read_chunksize=4)

        sleep_mock.assert_has_calls([mock.call(0)] * 3)
        # Just to make sure that there were exactly 3 calls
        self.assertEqual(3, sleep_mock.call_count)
        self.assertEqual(expected_checksum.hexdigest(), actual_checksum)
Beispiel #7
0
def compute_image_checksum(image_path, algorithm='md5'):
    """Compute checksum by given image path and algorithm."""
    time_start = time.time()
    LOG.debug('Start computing %(algo)s checksum for image %(image)s.', {
        'algo': algorithm,
        'image': image_path
    })
    checksum = fileutils.compute_file_checksum(image_path, algorithm=algorithm)
    time_elapsed = time.time() - time_start
    LOG.debug(
        'Computed %(algo)s checksum for image %(image)s in '
        '%(delta).2f seconds, checksum value: %(checksum)s.', {
            'algo': algorithm,
            'image': image_path,
            'delta': time_elapsed,
            'checksum': checksum
        })
    return checksum
Beispiel #8
0
from oslo_utils import fileutils
import os
import shutil

path = 'path/to/here'
fileutils.ensure_tree(path)

file = fileutils.write_to_tempfile('hello fileutils', path)
print("template file " + file)

for n in ['sha1', 'md5', 'sha256']:
    print(
        fileutils.compute_file_checksum(file,
                                        read_chunksize=65536,
                                        algorithm=n))

print(fileutils.last_bytes(file, 1))
print(fileutils.last_bytes(file, 10000))

fileutils.delete_if_exists(file)

# any exception will cause the path to be deleted.
#fileutils.remove_path_on_error(path)

# fileutils have no routine for directory :(
shutil.rmtree('path')