Skip to content

Commit

Permalink
backend: fix copr-backend-unknown-resalloc-tickets
Browse files Browse the repository at this point in the history
And add a test-case now.
  • Loading branch information
praiskup committed Oct 11, 2024
1 parent 916f82f commit b86e1c3
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/copr-backend.spec
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ BuildRequires: python3-zstandard
BuildRequires: python3-cachetools
BuildRequires: modulemd-tools >= 0.6
BuildRequires: prunerepo >= %prunerepo_version
BuildRequires: resalloc-server
BuildRequires: dnf

Requires: (copr-selinux if selinux-policy-targeted)
Expand Down
Empty file added backend/fake_executables
Empty file.
5 changes: 4 additions & 1 deletion backend/run/copr-backend-unknown-resalloc-tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from copr_common.helpers import script_requires_user


def used_ids():
"""
Return a `set()` of ticket IDs currently in use by
Expand All @@ -34,7 +35,8 @@ def used_ids():
output = output.strip()
tickets = set()
for ticket_id in output.split("\n"):
if ticket_id: # ignore empty lines
ticket_id = ticket_id.strip()
if not ticket_id: # ignore empty lines
continue
assert ticket_id.isdigit()
tickets.add(int(ticket_id))
Expand Down Expand Up @@ -64,6 +66,7 @@ def print_once(message):
print(message)
storage[message] = True


def _main():
script_requires_user("resalloc")

Expand Down
25 changes: 25 additions & 0 deletions backend/tests/fake_executables/unknown_tickets/ps
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /bin/sh

cat <<EOF
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 0.0 76688 27632 ? Ss Aug20 164:48 /usr/lib/systemd/systemd --switched-root --system --deserialize=32
root 2 0.0 0.0 0 0 ? S Aug20 0:19 [kthreadd]
root 3 0.0 0.0 0 0 ? S Aug20 0:00 [pool_workqueue_release]
copr 25312 0.2 0.1 575840 114196 ? Sl 07:12 0:12 Builder for task 8112858-fedora-40-x86_64: Job 8112858-fedora-40-x86_64, host info: ResallocHost, ticket_id=5273994, hostname=2620:52:3:1:dead:beef:cafe:c197 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112858 --chroot fedora-40-x86_64 --worker-id rpm_build_worker:8112858-fedora-40-x86_64)
copr 25314 0.2 0.1 575840 114580 ? Sl 07:12 0:12 Builder for task 8112859-fedora-rawhide-x86_64: Job 8112859-fedora-rawhide-x86_64, host info: ResallocHost, ticket_id=5273992, hostname=2620:52:3:1:dead:beef:cafe:c15b (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112859 --chroot fedora-rawhide-x86_64 --worker-id rpm_build_worker:8112859-fedora-rawhide-x86_64)
copr 25332 0.2 0.1 575832 113908 ? Sl 07:12 0:12 Builder for task 8112858-fedora-rawhide-x86_64: Job 8112858-fedora-rawhide-x86_64, host info: ResallocHost, ticket_id=5273997, hostname=2620:52:3:1:dead:beef:cafe:c192 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112858 --chroot fedora-rawhide-x86_64 --worker-id rpm_build_worker:8112858-fedora-rawhide-x86_64)
copr 25358 0.2 0.1 575840 114256 ? Sl 07:12 0:12 Builder for task 8112858-fedora-41-x86_64: Job 8112858-fedora-41-x86_64, host info: ResallocHost, ticket_id=5274000, hostname=2620:52:3:1:dead:beef:cafe:c1da (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112858 --chroot fedora-41-x86_64 --worker-id rpm_build_worker:8112858-fedora-41-x86_64)
copr 25362 0.2 0.1 575828 114220 ? Sl 07:12 0:12 Builder for task 8112858-fedora-39-x86_64: Job 8112858-fedora-39-x86_64, host info: ResallocHost, ticket_id=5273999, hostname=2620:52:3:1:dead:beef:cafe:c1d8 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112858 --chroot fedora-39-x86_64 --worker-id rpm_build_worker:8112858-fedora-39-x86_64)
copr 26017 0.0 0.0 11720 8064 ? S 07:12 0:00 ssh -F /home/copr/.ssh/config mockbuilder@2620:52:3:1:dead:beef:cafe:c15b copr-rpmbuild-log
copr 26055 0.0 0.0 11724 7880 ? S 07:12 0:00 ssh -F /home/copr/.ssh/config mockbuilder@2620:52:3:1:dead:beef:cafe:c1da copr-rpmbuild-log
copr 26059 0.0 0.0 11724 7800 ? S 07:12 0:00 ssh -F /home/copr/.ssh/config mockbuilder@2620:52:3:1:dead:beef:cafe:c1d8 copr-rpmbuild-log
copr 26061 0.0 0.0 11724 7476 ? S 07:12 0:00 ssh -F /home/copr/.ssh/config mockbuilder@2620:52:3:1:dead:beef:cafe:c192 copr-rpmbuild-log
copr 166357 0.1 0.1 575832 114212 ? Sl 07:46 0:07 Builder for task 8112918-fedora-41-ppc64le: Job 8112918-fedora-41-ppc64le, host info: ResallocHost, ticket_id=5274329, hostname=2620:52:3:1:dead:beef:cafe:c20e (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112918 --chroot fedora-41-ppc64le --worker-id rpm_build_worker:8112918-fedora-41-ppc64le)
root 300588 0.0 0.0 0 0 ? I 08:23 0:01 [kworker/u33:5-btrfs-delalloc]
copr 414485 0.4 0.1 575828 114420 ? Sl 08:45 0:01 Builder for task 8112989-epel-9-aarch64: Job 8112989-epel-9-aarch64, host info: ResallocHost, ticket_id=5274764, hostname=50.17.171.12 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112989 --chroot epel-9-aarch64 --worker-id rpm_build_worker:8112989-epel-9-aarch64)
copr 414486 0.4 0.1 575828 114432 ? Sl 08:45 0:01 Builder for task 8112989-epel-9-x86_64: Job 8112989-epel-9-x86_64, host info: ResallocHost, ticket_id=5274768, hostname=2620:52:3:1:dead:beef:cafe:c150 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112989 --chroot epel-9-x86_64 --worker-id rpm_build_worker:8112989-epel-9-x86_64)
copr 414526 0.4 0.1 575840 114448 ? Sl 08:45 0:01 Builder for task 8112989-fedora-41-x86_64: Job 8112989-fedora-41-x86_64, host info: ResallocHost, ticket_id=5274772, hostname=2620:52:3:1:dead:beef:cafe:c154 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112989 --chroot fedora-41-x86_64 --worker-id rpm_build_worker:8112989-fedora-41-x86_64)
copr 414573 0.4 0.1 575832 114104 ? Sl 08:45 0:02 Builder for task 8112989-fedora-39-x86_64: Job 8112989-fedora-39-x86_64, host info: ResallocHost, ticket_id=5274773, hostname=2620:52:3:1:dead:beef:cafe:c112 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112989 --chroot fedora-39-x86_64 --worker-id rpm_build_worker:8112989-fedora-39-x86_64)
copr 414574 0.3 0.1 575700 114300 ? Sl 08:45 0:01 Builder for task 8112989-epel-8-s390x: Job 8112989-epel-8-s390x, host info: ResallocHost, ticket_id=5274775, hostname=150.239.87.207 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112989 --chroot epel-8-s390x --worker-id rpm_build_worker:8112989-epel-8-s390x)
copr 414575 0.3 0.1 575700 114064 ? Sl 08:45 0:01 Builder for task 8112989-fedora-41-s390x: Job 8112989-fedora-41-s390x, host info: ResallocHost, ticket_id=5274774, hostname=169.59.165.222 (command: /usr/bin/copr-backend-process-build --daemon --build-id 8112989 --chroot fedora-41-s390x --worker-id rpm_build_worker:8112989-fedora-41-s390x)
EOF
52 changes: 52 additions & 0 deletions backend/tests/test_unknown_resalloc_tickets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Test the ./run/copr-backend-unknown-resalloc-tickets.py script
"""

import contextlib
import importlib
import io
import subprocess
from unittest import mock

from testlib import patch_path_run, patch_path_fake_executables, patch_getpwuid

SCRIPT_NAME = 'copr-backend-unknown-resalloc-tickets.py'
SPEC = importlib.util.spec_from_file_location("spec", "run/" + SCRIPT_NAME)
SCRIPT = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(SCRIPT)

EXPECTED_TICKETS = {5273992, 5273994, 5273997, 5273999, 5274000, 5274329,
5274764, 5274768, 5274772, 5274773, 5274774, 5274775}


def main_output(ids):
with mock.patch.object(SCRIPT, "all_ids", return_value=ids):
f = io.StringIO()
with contextlib.redirect_stdout(f):
SCRIPT._main() # pylint: disable=protected-access
return f.getvalue()


@patch_path_run
@patch_path_fake_executables("unknown_tickets")
@patch_getpwuid("resalloc")
def test_unknown_tickets():
# not ticket found in DB
assert main_output({}) == "no tickets taken\n"
assert SCRIPT.used_ids() == EXPECTED_TICKETS

# no unused ticket
assert main_output(EXPECTED_TICKETS) == ""

assert main_output(EXPECTED_TICKETS | {1}) == """\
These are old tickets, Copr only uses newer tickets, close them:
resalloc ticket-close 1
"""
assert main_output(EXPECTED_TICKETS | {6000000}) == """\
These tickets are relatively new for closing blindly, double check!
resalloc ticket-check 6000000
"""
assert main_output(EXPECTED_TICKETS | {1, 6000000}) == """\
resalloc ticket-close 1
resalloc ticket-check 6000000
"""
36 changes: 35 additions & 1 deletion backend/tests/testlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
import os
import shutil
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

from copr_backend.background_worker_build import COMMANDS
from copr_backend.sshcmd import SSHConnection, SSHConnectionError, DEFAULT_SUBPROCESS_TIMEOUT
Expand Down Expand Up @@ -262,3 +262,37 @@ def get(self, dirname, overrides=None, done=False):
self.redis.hset(key, "task", task_json)
if done:
self.redis.hset(key, "status", "success")


def patch_path(method, directory):
directory = os.path.realpath(directory)

def _wrapper(*args, **kwargs):
with patch.dict(os.environ, {'PATH': directory + ":" + os.environ['PATH']}):
method(*args, **kwargs)

return _wrapper


def patch_path_run(method):
""" Add the /backend/run/ directory on PATH """
runpath = os.path.join(__file__, "..", "..", "..", "run")
return patch_path(method, runpath)


def patch_path_fake_executables(fake_subdir):
runpath = os.path.join(__file__, "..", "..", "fake_executables", fake_subdir)

def _decorator(func):
return patch_path(func, runpath)

return _decorator


def patch_getpwuid(username):
def _decorator(func):
def _wrapper(*args, **kwargs):
with patch('copr_common.helpers.pwd.getpwuid', return_value=(username,)):
return func(*args, **kwargs)
return _wrapper
return _decorator

0 comments on commit b86e1c3

Please sign in to comment.