Ejemplo n.º 1
0
    def __init__(self,
                 filepath,
                 description=None,
                 dst_path=None,
                 scratch_path=None):

        self.source_path = filepath
        self.orig_filename = os.path.basename(filepath)
        self.filesize = os.path.getsize(filepath)

        if dst_path is None:
            basename, ext = os.path.splitext(self.orig_filename)
            hash = hash_file(self.source_path)
            # To avoid file name collision
            self.dst_path = f"{basename}-{hash}-{self.filesize}{ext}"
        else:
            self.dst_path = dst_path

        if scratch_path:
            # will best effort make a copy of the file
            try:
                copy_of_file = os.path.join(scratch_path, self.dst_path)
                shutil.copyfile(filepath, copy_of_file)
            except Exception:
                pass
            else:
                self.source_path = copy_of_file

        super(Attachment, self).__init__(description=description)
Ejemplo n.º 2
0
    def __init__(self, filepath, description):
        self.source_path = filepath
        self.hash = path_utils.hash_file(filepath)
        self.orig_filename = os.path.basename(filepath)
        self.filesize = os.path.getsize(filepath)

        basename, ext = os.path.splitext(self.orig_filename)
        self.dst_path = "{basename}-{hash}-{filesize}{ext}".format(
            basename=basename, hash=self.hash, filesize=self.filesize, ext=ext)
        super(Attachment, self).__init__(description=description)
Ejemplo n.º 3
0
    def test_attach(self, tmpdir):
        """UT for result.attach method."""
        tmpfile = str(tmpdir.join("attach_me.txt"))
        with open(tmpfile, "w") as f:
            f.write("testplan\n" * 1000)

        result = result_mod.Result()
        assert result.attach(tmpfile, description="Attach a text file")

        assert len(result.entries) == 1
        attachment_entry = result.entries[0]

        assert attachment_entry.source_path == tmpfile
        assert attachment_entry.hash == path_utils.hash_file(tmpfile)
        assert attachment_entry.orig_filename == "attach_me.txt"
        assert attachment_entry.filesize == os.path.getsize(tmpfile)

        # The expected destination path depends on the exact hash and filesize
        # of the file we wrote.
        expected_dst_path = "attach_me-{hash}-{filesize}.txt".format(
            hash=attachment_entry.hash, filesize=attachment_entry.filesize)
        assert attachment_entry.dst_path == expected_dst_path
Ejemplo n.º 4
0
def test_hashfile(tmpdir):
    """
    Test the hash_file() utility.

    The SHA1 algorithm is used. Check that the same hash value is returned as
    given by the sha1sum utility.
    """
    tmpfile = str(tmpdir.join("hash_me.txt"))
    with open(tmpfile, 'w') as f:
        f.write("testplan\n" * 1000)

    try:
        sha_output = subprocess.check_output(
            ["sha1sum", tmpfile], universal_newlines=True)
        ref_sha = re.match(r"([0-9a-f]+)\s+.*", sha_output).group(1)
    except OSError:
        pytest.skip("Cannot call sha1sum to generate reference SHA.")
        return

    # Check that the has produced by our hash_file utility matches the
    # reference value.
    assert path.hash_file(tmpfile) == ref_sha
Ejemplo n.º 5
0
    def test_attach_in_result_group(self, tmpdir):
        """UT for result.attach method."""
        tmpfile = str(tmpdir.join("attach_me.txt"))
        with open(tmpfile, "w") as f:
            f.write("testplan\n" * 1000)

        description = "Attach a text file at level: {}"

        result = result_mod.Result(_scratch=str(tmpdir))
        assert result.attach(tmpfile, description=description.format(0))

        assert len(result.entries) == 1

        with result.group("subgroup") as subgroup:
            assert subgroup.attach(tmpfile, description=description.format(1))
            assert len(subgroup.entries) == 1

            with subgroup.group("subgroup") as subsubgroup:
                assert subsubgroup.attach(tmpfile,
                                          description=description.format(2))
                assert len(subsubgroup.entries) == 1

            assert len(subgroup.entries) == 2
            assert len(subgroup.attachments) == 2
        assert len(result.entries) == 2
        assert len(result.attachments) == 3

        file_hash = path_utils.hash_file(tmpfile)
        size = os.path.getsize(tmpfile)

        for idx, attachment in enumerate(result.attachments):
            assert attachment.source_path == os.path.join(
                os.path.dirname(tmpfile), attachment.dst_path)
            assert attachment.hash == file_hash
            assert attachment.orig_filename == "attach_me.txt"
            assert attachment.filesize == size
            assert attachment.description == description.format(idx)