diff --git a/tests/test_vm/data/koji.png b/tests/test_vm/data/koji.png new file mode 100644 index 00000000..2a7b508c Binary files /dev/null and b/tests/test_vm/data/koji.png differ diff --git a/tests/test_vm/test_kojikamid.py b/tests/test_vm/test_kojikamid.py new file mode 100644 index 00000000..ffff1adf --- /dev/null +++ b/tests/test_vm/test_kojikamid.py @@ -0,0 +1,65 @@ +import base64 +import filecmp +import importlib.machinery +import importlib.util +import os +import tempfile +from subprocess import check_call + +VM_TESTS_DIR = os.path.dirname(os.path.abspath(__file__)) +LOGO_FIXTURE = os.path.join(VM_TESTS_DIR, 'data', 'koji.png') +TESTS_DIR = os.path.dirname(VM_TESTS_DIR) +VM_DIR = os.path.join(os.path.dirname(TESTS_DIR), 'vm') + +# Generate the kojikamid file before importing. +tmpfile = tempfile.NamedTemporaryFile(prefix='kojikamid.') +check_call(['bash', 'fix_kojikamid.sh'], cwd=VM_DIR, stdout=tmpfile) +print(tmpfile.name) + +# Dynamically import our temporary kojikamid file. +loader = importlib.machinery.SourceFileLoader('kojikamid', tmpfile.name) +spec = importlib.util.spec_from_loader(loader.name, loader) +kojikamid = importlib.util.module_from_spec(spec) +loader.exec_module(kojikamid) + + +class FakeServer(object): + def getTaskInfo(self): + # Koji's getTaskInfo(..., request=True) returns the task's request + # data. kojivmd strips this down to the second element of that request + # and returns it here: + return ['git://example.com/ceph.git#abc123', + 'ceph-6.1-win-build', + {'repo_id': 2, + 'winspec': 'git://example.com/pkg.git?ceph#def456'}] + + def getFile(self, buildinfo, archiveinfo, offset, length, type): + # Return a base64-encoded static fixture (a PNG image). + offset = int(offset) + length = int(length) + with open(LOGO_FIXTURE, 'rb') as fileobj: + try: + fileobj.seek(offset) + data = fileobj.read(length) + encoded = base64.b64encode(data).decode() + del data + return encoded + finally: + fileobj.close() + + +def test_fetch_file(tmpdir): + server = FakeServer() + build = kojikamid.WindowsBuild(server) + basedir = str(tmpdir) + buildinfo = { + 'name': 'wnbd', + } + fileinfo = { + 'localpath': 'koji.png', + 'checksum_type': 2, # sha256 + 'checksum': 'f78bc62287eec7641a85a7d1c0435c995672e7f46e33de72a82775b1fb16a93f', + } + build.fetchFile(basedir, buildinfo, fileinfo, 'win') + fetched = str(tmpdir.join('koji.png')) + assert filecmp.cmp(fetched, LOGO_FIXTURE) diff --git a/vm/kojikamid.py b/vm/kojikamid.py index f5a7460a..08c9a783 100755 --- a/vm/kojikamid.py +++ b/vm/kojikamid.py @@ -335,7 +335,7 @@ class WindowsBuild(object): raise BuildError('Unknown checksum type %s for %s' % ( # noqa: F821 checksum_type, os.path.basename(fileinfo['localpath']))) - with koji._open_text_file(destpath, 'wt') as destfile: + with open(destpath, 'wb') as destfile: offset = 0 while True: encoded = self.server.getFile(buildinfo, fileinfo, encode_int(offset), 1048576,