kojikamid: write buildrequires files as bytes

Fix a TypeError when downloading buildrequires files on Python 3.
base64.b64decode() returns bytes, so we must open the file for writing
in bytes mode, not text mode.

Add a unit test that verifies this behavior.
This commit is contained in:
Ken Dreyer 2022-11-09 10:41:15 -05:00
parent 56cea9adc2
commit 64a856f62b
3 changed files with 66 additions and 1 deletions

BIN
tests/test_vm/data/koji.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View file

@ -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)

View file

@ -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,