1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
import dataclasses
import datetime
import logging
import pathlib
import tempfile
import time
import uuid
from subprocess import check_output
from typing import Optional
import pytest
import requests
import gitlab
import gitlab.base
from tests.functional import helpers
from tests.functional.fixtures.docker import * # noqa
SLEEP_TIME = 10
@dataclasses.dataclass
class GitlabVersion:
major: int
minor: int
patch: str
revision: str
def __post_init__(self):
self.major, self.minor = int(self.major), int(self.minor)
@pytest.fixture(scope="session")
def gitlab_version(gl) -> GitlabVersion:
version, revision = gl.version()
major, minor, patch = version.split(".")
return GitlabVersion(major=major, minor=minor, patch=patch, revision=revision)
@pytest.fixture(scope="session")
def fixture_dir(test_dir: pathlib.Path) -> pathlib.Path:
return test_dir / "functional" / "fixtures"
@pytest.fixture(scope="session")
def gitlab_service_name() -> str:
"""The "service" name is the one defined in the `docker-compose.yml` file"""
return "gitlab"
@pytest.fixture(scope="session")
def gitlab_container_name() -> str:
"""The "container" name is the one defined in the `docker-compose.yml` file
for the "gitlab" service"""
return "gitlab-test"
@pytest.fixture(scope="session")
def gitlab_docker_port(docker_services, gitlab_service_name: str) -> int:
port: int = docker_services.port_for(gitlab_service_name, container_port=80)
return port
@pytest.fixture(scope="session")
def gitlab_url(docker_ip: str, gitlab_docker_port: int) -> str:
return f"http://{docker_ip}:{gitlab_docker_port}"
def reset_gitlab(gl: gitlab.Gitlab) -> None:
"""Delete resources (such as projects, groups, users) that shouldn't
exist."""
if helpers.get_gitlab_plan(gl):
logging.info("GitLab EE detected")
# NOTE(jlvillal, timknight): By default in GitLab EE it will wait 7 days before
# deleting a group or project.
# In GL 16.0 we need to call delete with `permanently_remove=True` for projects and sub groups
# (handled in helpers.py safe_delete)
settings = gl.settings.get()
modified_settings = False
if settings.deletion_adjourned_period != 1:
logging.info("Setting `deletion_adjourned_period` to 1 Day")
settings.deletion_adjourned_period = 1
modified_settings = True
if modified_settings:
settings.save()
for project in gl.projects.list():
for deploy_token in project.deploytokens.list():
logging.info(
f"Deleting deploy token: {deploy_token.username!r} in "
f"project: {project.path_with_namespace!r}"
)
helpers.safe_delete(deploy_token)
logging.info(f"Deleting project: {project.path_with_namespace!r}")
helpers.safe_delete(project)
for group in gl.groups.list():
# skip deletion of a descendant group to prevent scenarios where parent group
# gets deleted leaving a dangling descendant whose deletion will throw 404s.
if group.parent_id:
logging.info(
f"Skipping deletion of {group.full_path} as it is a descendant "
f"group and will be removed when the parent group is deleted"
)
continue
for deploy_token in group.deploytokens.list():
logging.info(
f"Deleting deploy token: {deploy_token.username!r} in "
f"group: {group.path_with_namespace!r}"
)
helpers.safe_delete(deploy_token)
logging.info(f"Deleting group: {group.full_path!r}")
helpers.safe_delete(group)
for topic in gl.topics.list():
logging.info(f"Deleting topic: {topic.name!r}")
helpers.safe_delete(topic)
for variable in gl.variables.list():
logging.info(f"Deleting variable: {variable.key!r}")
helpers.safe_delete(variable)
for user in gl.users.list():
if user.username not in ["root", "ghost"]:
logging.info(f"Deleting user: {user.username!r}")
helpers.safe_delete(user)
def set_token(container: str, fixture_dir: pathlib.Path) -> str:
logging.info("Creating API token.")
set_token_rb = fixture_dir / "set_token.rb"
with open(set_token_rb, "r", encoding="utf-8") as f:
set_token_command = f.read().strip()
rails_command = [
"docker",
"exec",
container,
"gitlab-rails",
"runner",
set_token_command,
]
output = check_output(rails_command).decode().strip()
logging.info("Finished creating API token.")
return output
def pytest_report_collectionfinish(config, startdir, items):
return [
"",
"Starting GitLab container.",
"Waiting for GitLab to reconfigure.",
"This will take a few minutes.",
]
def pytest_addoption(parser):
parser.addoption(
"--keep-containers",
action="store_true",
help="Keep containers running after testing",
)
@pytest.fixture(scope="session")
def temp_dir() -> pathlib.Path:
return pathlib.Path(tempfile.gettempdir())
@pytest.fixture(scope="session")
def check_is_alive():
"""
Return a healthcheck function fixture for the GitLab container spinup.
"""
def _check(
*,
container: str,
start_time: float,
gitlab_url: str,
) -> bool:
setup_time = time.perf_counter() - start_time
minutes, seconds = int(setup_time / 60), int(setup_time % 60)
logging.info(
f"Checking if GitLab container is up. "
f"Have been checking for {minutes} minute(s), {seconds} seconds ..."
)
logs = ["docker", "logs", container]
if "gitlab Reconfigured!" not in check_output(logs).decode():
return False
logging.debug("GitLab has finished reconfiguring.")
for check in ("health", "readiness", "liveness"):
url = f"{gitlab_url}/-/{check}"
logging.debug(f"Checking {check!r} endpoint at: {url}")
try:
result = requests.get(url, timeout=1.0)
except requests.exceptions.Timeout:
logging.info(f"{check!r} check timed out")
return False
if result.status_code != 200:
logging.info(f"{check!r} check did not return 200: {result!r}")
return False
logging.debug(f"{check!r} check passed: {result!r}")
logging.debug(f"Sleeping for {SLEEP_TIME}")
time.sleep(SLEEP_TIME)
return True
return _check
@pytest.fixture(scope="session")
def gitlab_token(
check_is_alive,
gitlab_container_name: str,
gitlab_url: str,
docker_services,
fixture_dir: pathlib.Path,
) -> str:
start_time = time.perf_counter()
logging.info("Waiting for GitLab container to become ready.")
docker_services.wait_until_responsive(
timeout=300,
pause=10,
check=lambda: check_is_alive(
container=gitlab_container_name,
start_time=start_time,
gitlab_url=gitlab_url,
),
)
setup_time = time.perf_counter() - start_time
minutes, seconds = int(setup_time / 60), int(setup_time % 60)
logging.info(
f"GitLab container is now ready after {minutes} minute(s), {seconds} seconds"
)
return set_token(gitlab_container_name, fixture_dir=fixture_dir)
@pytest.fixture(scope="session")
def gitlab_config(gitlab_url: str, gitlab_token: str, temp_dir: pathlib.Path):
config_file = temp_dir / "python-gitlab.cfg"
config = f"""[global]
default = local
timeout = 60
[local]
url = {gitlab_url}
private_token = {gitlab_token}
api_version = 4"""
with open(config_file, "w", encoding="utf-8") as f:
f.write(config)
return config_file
@pytest.fixture(scope="session")
def gl(gitlab_url: str, gitlab_token: str) -> gitlab.Gitlab:
"""Helper instance to make fixtures and asserts directly via the API."""
logging.info("Instantiating python-gitlab gitlab.Gitlab instance")
instance = gitlab.Gitlab(gitlab_url, private_token=gitlab_token)
logging.info("Reset GitLab")
reset_gitlab(instance)
return instance
@pytest.fixture(scope="session")
def gitlab_plan(gl: gitlab.Gitlab) -> Optional[str]:
return helpers.get_gitlab_plan(gl)
@pytest.fixture(autouse=True)
def gitlab_premium(gitlab_plan, request) -> None:
if gitlab_plan in ("premium", "ultimate"):
return
if request.node.get_closest_marker("gitlab_ultimate"):
pytest.skip("Test requires GitLab Premium plan")
@pytest.fixture(autouse=True)
def gitlab_ultimate(gitlab_plan, request) -> None:
if gitlab_plan == "ultimate":
return
if request.node.get_closest_marker("gitlab_ultimate"):
pytest.skip("Test requires GitLab Ultimate plan")
@pytest.fixture(scope="session")
def gitlab_runner(gl):
container = "gitlab-runner-test"
runner_name = "python-gitlab-runner"
token = "registration-token"
url = "http://gitlab"
docker_exec = ["docker", "exec", container, "gitlab-runner"]
register = [
"register",
"--run-untagged",
"--non-interactive",
"--registration-token",
token,
"--name",
runner_name,
"--url",
url,
"--clone-url",
url,
"--executor",
"shell",
]
unregister = ["unregister", "--name", runner_name]
yield check_output(docker_exec + register).decode()
check_output(docker_exec + unregister).decode()
@pytest.fixture(scope="module")
def group(gl):
"""Group fixture for group API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"test-group-{_id}",
"path": f"group-{_id}",
}
group = gl.groups.create(data)
yield group
helpers.safe_delete(group)
@pytest.fixture(scope="module")
def project(gl):
"""Project fixture for project API resource tests."""
_id = uuid.uuid4().hex
name = f"test-project-{_id}"
project = gl.projects.create(name=name)
yield project
helpers.safe_delete(project)
@pytest.fixture(scope="function")
def make_merge_request(project):
"""Fixture factory used to create a merge_request.
It will create a branch, add a commit to the branch, and then create a
merge request against project.default_branch. The MR will be returned.
When finished any created merge requests and branches will be deleted.
NOTE: No attempt is made to restore project.default_branch to its previous
state. So if the merge request is merged then its content will be in the
project.default_branch branch.
"""
to_delete = []
def _make_merge_request(*, source_branch: str, create_pipeline: bool = False):
# Wait for processes to be done before we start...
# NOTE(jlvillal): Sometimes the CI would give a "500 Internal Server
# Error". Hoping that waiting until all other processes are done will
# help with that.
# Pause to let GL catch up (happens on hosted too, sometimes takes a while for server to be ready to merge)
time.sleep(30)
project.refresh() # Gets us the current default branch
logging.info(f"Creating branch {source_branch}")
mr_branch = project.branches.create(
{"branch": source_branch, "ref": project.default_branch}
)
# NOTE(jlvillal): Must create a commit in the new branch before we can
# create an MR that will work.
project.files.create(
{
"file_path": f"README.{source_branch}",
"branch": source_branch,
"content": "Initial content",
"commit_message": "New commit in new branch",
}
)
if create_pipeline:
project.files.create(
{
"file_path": ".gitlab-ci.yml",
"branch": source_branch,
"content": """
test:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
script:
- sleep 24h # We don't expect this to finish
""",
"commit_message": "Add a simple pipeline",
}
)
mr = project.mergerequests.create(
{
"source_branch": source_branch,
"target_branch": project.default_branch,
"title": "Should remove source branch",
"remove_source_branch": True,
}
)
# Pause to let GL catch up (happens on hosted too, sometimes takes a while for server to be ready to merge)
time.sleep(5)
mr_iid = mr.iid
for _ in range(60):
mr = project.mergerequests.get(mr_iid)
if (
mr.detailed_merge_status == "checking"
or mr.detailed_merge_status == "unchecked"
):
time.sleep(0.5)
else:
break
assert mr.detailed_merge_status != "checking"
assert mr.detailed_merge_status != "unchecked"
to_delete.extend([mr, mr_branch])
return mr
yield _make_merge_request
for object in to_delete:
helpers.safe_delete(object)
@pytest.fixture(scope="function")
def merge_request(make_merge_request, project):
_id = uuid.uuid4().hex
return make_merge_request(source_branch=f"branch-{_id}")
@pytest.fixture(scope="function")
def merge_request_with_pipeline(make_merge_request, project):
_id = uuid.uuid4().hex
return make_merge_request(source_branch=f"branch-{_id}", create_pipeline=True)
@pytest.fixture(scope="module")
def project_file(project):
"""File fixture for tests requiring a project with files and branches."""
project_file = project.files.create(
{
"file_path": "README",
"branch": "main",
"content": "Initial content",
"commit_message": "Initial commit",
}
)
return project_file
@pytest.fixture(scope="function")
def release(project, project_file):
_id = uuid.uuid4().hex
name = f"we_have_a_slash/test-release-{_id}"
project.refresh() # Gets us the current default branch
release = project.releases.create(
{
"name": name,
"tag_name": _id,
"description": "description",
"ref": project.default_branch,
}
)
return release
@pytest.fixture(scope="function")
def service(project):
"""This is just a convenience fixture to make test cases slightly prettier. Project
services are not idempotent. A service cannot be retrieved until it is enabled.
After it is enabled the first time, it can never be fully deleted, only disabled."""
service = project.services.update("asana", {"api_key": "api_key"})
yield service
try:
project.services.delete("asana")
except gitlab.exceptions.GitlabDeleteError as e:
print(f"Service already disabled: {e}")
@pytest.fixture(scope="module")
def user(gl):
"""User fixture for user API resource tests."""
_id = uuid.uuid4().hex
email = f"user{_id}@email.com"
username = f"user{_id}"
name = f"User {_id}"
password = "E4596f8be406Bc3a14a4ccdb1df80587"
user = gl.users.create(email=email, username=username, name=name, password=password)
yield user
helpers.safe_delete(user)
@pytest.fixture(scope="module")
def issue(project):
"""Issue fixture for issue API resource tests."""
_id = uuid.uuid4().hex
data = {"title": f"Issue {_id}", "description": f"Issue {_id} description"}
return project.issues.create(data)
@pytest.fixture(scope="module")
def milestone(project):
_id = uuid.uuid4().hex
data = {"title": f"milestone{_id}"}
return project.milestones.create(data)
@pytest.fixture(scope="module")
def label(project):
"""Label fixture for project label API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"prjlabel{_id}",
"description": f"prjlabel1 {_id} description",
"color": "#112233",
}
return project.labels.create(data)
@pytest.fixture(scope="module")
def group_label(group):
"""Label fixture for group label API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"grplabel{_id}",
"description": f"grplabel1 {_id} description",
"color": "#112233",
}
return group.labels.create(data)
@pytest.fixture(scope="module")
def epic(group):
"""Fixture for group epic API resource tests."""
_id = uuid.uuid4().hex
return group.epics.create({"title": f"epic-{_id}", "description": f"Epic {_id}"})
@pytest.fixture(scope="module")
def variable(project):
"""Variable fixture for project variable API resource tests."""
_id = uuid.uuid4().hex
data = {"key": f"var{_id}", "value": f"Variable {_id}"}
return project.variables.create(data)
@pytest.fixture(scope="module")
def deploy_token(project):
"""Deploy token fixture for project deploy token API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"token-{_id}",
"username": "root",
"expires_at": datetime.date.today().isoformat(),
"scopes": "read_registry",
}
return project.deploytokens.create(data)
@pytest.fixture(scope="module")
def group_deploy_token(group):
"""Deploy token fixture for group deploy token API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"group-token-{_id}",
"username": "root",
"expires_at": datetime.date.today().isoformat(),
"scopes": "read_registry",
}
return group.deploytokens.create(data)
@pytest.fixture(scope="session")
def GPG_KEY():
return """-----BEGIN PGP PUBLIC KEY BLOCK-----
mQENBFn5mzYBCADH6SDVPAp1zh/hxmTi0QplkOfExBACpuY6OhzNdIg+8/528b3g
Y5YFR6T/HLv/PmeHskUj21end1C0PNG2T9dTx+2Vlh9ISsSG1kyF9T5fvMR3bE0x
Dl6S489CXZrjPTS9SHk1kF+7dwjUxLJyxF9hPiSihFefDFu3NeOtG/u8vbC1mewQ
ZyAYue+mqtqcCIFFoBz7wHKMWjIVSJSyTkXExu4OzpVvy3l2EikbvavI3qNz84b+
Mgkv/kiBlNoCy3CVuPk99RYKZ3lX1vVtqQ0OgNGQvb4DjcpyjmbKyibuZwhDjIOh
au6d1OyEbayTntd+dQ4j9EMSnEvm/0MJ4eXPABEBAAG0G0dpdGxhYlRlc3QxIDxm
YWtlQGZha2UudGxkPokBNwQTAQgAIQUCWfmbNgIbAwULCQgHAgYVCAkKCwIEFgID
AQIeAQIXgAAKCRBgxELHf8f3hF3yB/wNJlWPKY65UsB4Lo0hs1OxdxCDqX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Ga
-----END PGP PUBLIC KEY BLOCK-----"""
@pytest.fixture(scope="session")
def SSH_KEY():
return (
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZAjAX8vTiHD7Yi3/EzuVaDChtih"
"79HyJZ6H9dEqxFfmGA1YnncE0xujQ64TCebhkYJKzmTJCImSVkOu9C4hZgsw6eE76n"
"+Cg3VwEeDUFy+GXlEJWlHaEyc3HWioxgOALbUp3rOezNh+d8BDwwqvENGoePEBsz5l"
"a6WP5lTi/HJIjAl6Hu+zHgdj1XVExeH+S52EwpZf/ylTJub0Bl5gHwf/siVE48mLMI"
"sqrukXTZ6Zg+8EHAIvIQwJ1dKcXe8P5IoLT7VKrbkgAnolS0I8J+uH7KtErZJb5oZh"
"S4OEwsNpaXMAr+6/wWSpircV2/e7sFLlhlKBC4Iq1MpqlZ7G3p foo@bar"
)
@pytest.fixture(scope="session")
def DEPLOY_KEY():
return (
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDFdRyjJQh+1niBpXqE2I8dzjG"
"MXFHlRjX9yk/UfOn075IdaockdU58sw2Ai1XIWFpZpfJkW7z+P47ZNSqm1gzeXI"
"rtKa9ZUp8A7SZe8vH4XVn7kh7bwWCUirqtn8El9XdqfkzOs/+FuViriUWoJVpA6"
"WZsDNaqINFKIA5fj/q8XQw+BcS92L09QJg9oVUuH0VVwNYbU2M2IRmSpybgC/gu"
"uWTrnCDMmLItksATifLvRZwgdI8dr+q6tbxbZknNcgEPrI2jT0hYN9ZcjNeWuyv"
"rke9IepE7SPBT41C+YtUX4dfDZDmczM1cE0YL/krdUCfuZHMa4ZS2YyNd6slufc"
"vn bar@foo"
)
|