a tool to implement the autorelease cycle for devuan connecting gitlab repositories and issues with dak and jenkins
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.6 KiB

import unittest
import typing
import attr
from tests.lib import GiteaRepoStoreMockup, ArgsMockup, ConfigMockup, ResponseMockup
from releasebot import ReleaseBotTask, ReleaseBotCommand, ReleaseBotRequester
# This provides the testing model that we'll provide to our classes
# If you want to add test-cases, make sure you edit responses to match
# this description as well.
tested_open_issues = [
{
"id": 10,
"title": "Build",
"descr": "Not 'pending': not assigned to releasebot",
"repository": {"name": "goodowner", "owner": "devuan",},
"labels": [],
"assignee": {"username": "koala",},
},
{
"id": 11,
"title": "Build",
"descr": "Not 'pending': bad owner",
"repository": {"name": "badstuff", "owner": "badone",},
"labels": [],
"assignee": {"username": "releasebot",},
},
{
"id": 12,
"title": "Build",
"descr": "Is 'pending': but should have no permissions",
"repository": {"name": "stuff", "owner": "devuan",},
"labels": [],
"assignee": {"username": "releasebot",},
"user": {"username": "koala"},
},
{
"id": 13,
"title": "Build",
"descr": "Is 'pending': should be collaborator",
"repository": {"name": "stuff", "owner": "devuan",},
"labels": [],
"assignee": {"username": "releasebot",},
"user": {"username": "colab"},
},
{
"id": 14,
"title": "Build",
"descr": "Is 'pending': should be member",
"repository": {"name": "stuff", "owner": "devuan",},
"labels": [],
"assignee": {"username": "releasebot",},
"user": {"username": "dev1dev"},
},
{
"id": 15,
"title": "Build",
"descr": "Is 'pending': should be admin",
"repository": {"name": "stuff", "owner": "devuan",},
"labels": [],
"assignee": {"username": "releasebot"},
"user": {"username": "toor", "is_admin": True},
},
]
def expected_pending_tasks(repo_store):
return [
ReleaseBotTask(
id="devuan/stuff/issues/12",
command=ReleaseBotCommand(name="build", package="stuff", labels=set()),
requester=ReleaseBotRequester(name="koala", permissions=[]),
repo_store=repo_store,
),
ReleaseBotTask(
id="devuan/stuff/issues/13",
command=ReleaseBotCommand(name="build", package="stuff", labels=set()),
requester=ReleaseBotRequester(
name="colab", permissions=["collaborator/stuff"]
),
repo_store=repo_store,
),
ReleaseBotTask(
id="devuan/stuff/issues/14",
command=ReleaseBotCommand(name="build", package="stuff", labels=set()),
requester=ReleaseBotRequester(name="dev1dev", permissions=["member"]),
repo_store=repo_store,
),
ReleaseBotTask(
id="devuan/stuff/issues/15",
command=ReleaseBotCommand(name="build", package="stuff", labels=set()),
requester=ReleaseBotRequester(name="toor", is_admin=True, permissions=[]),
repo_store=repo_store,
),
]
# This emulates communication to gitea by hard-coding the responses.
# Of special interest is /repos/issues/search, which is defined above.
responses = {
"get": {
# Hard-coded team_id
"/orgs/devuan/teams/search": {
"{'q': 'packages', 'include_desc': False}": ResponseMockup(
response={"data": [{"id": 42}]}
)
},
# colab is collaborator in devuan/stuff, dev1dev and koala aren't
"/repos/devuan/stuff/collaborators/colab": {"{}": ResponseMockup(ok=True),},
"/repos/devuan/stuff/collaborators/dev1dev": {"{}": ResponseMockup(ok=False),},
"/repos/devuan/stuff/collaborators/koala": {"{}": ResponseMockup(ok=False),},
"/repos/devuan/stuff/collaborators/toor": {"{}": ResponseMockup(ok=False),},
"/repos/issues/search": {
"{'state': 'open', 'q': 'build'}": ResponseMockup(
ok=False, response=tested_open_issues
),
},
# dev1dev is member of packages, colab and koala aren't
"/teams/42/members/dev1dev": {"{}": ResponseMockup(ok=True),},
"/teams/42/members/colab": {"{}": ResponseMockup(ok=False),},
"/teams/42/members/koala": {"{}": ResponseMockup(ok=False),},
"/teams/42/members/toor": {"{}": ResponseMockup(ok=False),},
},
}
class TestRepoStores(unittest.TestCase):
def test_gitea_init(self):
self.maxDiff = None
rs = GiteaRepoStoreMockup(ConfigMockup(), ArgsMockup(), responses=responses)
# In our testing this team_id is 42
self.assertEqual(42, rs.packages_team_id)
# colab is a collaborator for package "stuff"
self.assertTrue(rs.is_package_collaborator("colab", "stuff"))
# koala is not
self.assertFalse(rs.is_package_collaborator("koala", "stuff"))
# dev1dev is a member of the packages team
self.assertTrue(rs.is_packages_member("dev1dev"))
# colab and koala are not
for dev in ["colab", "koala"]:
self.assertFalse(
rs.is_packages_member(dev),
"Wrong packages_member status for: {}".format(dev),
)
# Open issues: should be verbatim
open_issues = rs._get_build_issues()
self.assertEqual(tested_open_issues, open_issues)
# Pending tasks: should be filtered by namespace / assignee
pending_tasks = list(rs.get_pending_tasks())
self.assertEqual(expected_pending_tasks(rs), pending_tasks)
# ReleaseBot