Skip to content

Commit

Permalink
test: update tests for gitlab 16.8 functionality
Browse files Browse the repository at this point in the history
- use programmatic dates for expires_at in tokens tests
- set PAT for 16.8 into tests

Signed-off-by: Tim Knight <tim.knight1@engineering.digital.dwp.gov.uk>
  • Loading branch information
TimKnight-DWP authored and nejch committed Apr 25, 2024
1 parent ea8c4c2 commit f8283ae
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 79 deletions.
7 changes: 6 additions & 1 deletion docs/cli-examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ CLI examples
CI Lint
-------

**ci-lint has been Removed in Gitlab 16, use project-ci-lint instead**

Lint a CI YAML configuration from a string:

.. note::
.. note::

To see output, you will need to use the ``-v``/``--verbose`` flag.

Expand Down Expand Up @@ -39,6 +41,9 @@ Validate a CI YAML configuration from a file (lints and exits with non-zero on f
$ gitlab ci-lint validate --content @.gitlab-ci.yml
Project CI Lint
---------------

Lint a project's CI YAML configuration:

.. code-block:: console
Expand Down
8 changes: 6 additions & 2 deletions tests/functional/api/test_deploy_tokens.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from datetime import date


def test_project_deploy_tokens(gl, project):
today = date.today().isoformat()
deploy_token = project.deploytokens.create(
{
"name": "foo",
"username": "bar",
"expires_at": "2022-01-01",
"expires_at": today,
"scopes": ["read_registry"],
}
)
Expand All @@ -12,7 +16,7 @@ def test_project_deploy_tokens(gl, project):

deploy_token = project.deploytokens.get(deploy_token.id)
assert deploy_token.name == "foo"
assert deploy_token.expires_at == "2022-01-01T00:00:00.000Z"
assert deploy_token.expires_at == today + "T00:00:00.000Z"
assert deploy_token.scopes == ["read_registry"]
assert deploy_token.username == "bar"

Expand Down
4 changes: 2 additions & 2 deletions tests/functional/api/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def test_groups(gl):
"email": "user@test.com",
"username": "user",
"name": "user",
"password": "user_pass",
"password": "E4596f8be406Bc3a14a4ccdb1df80587",
}
)
user2 = gl.users.create(
{
"email": "user2@test.com",
"username": "user2",
"name": "user2",
"password": "user2_pass",
"password": "E4596f8be406Bc3a14a4ccdb1df80587$2",
}
)
group1 = gl.groups.create(
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/api/test_merge_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_merge_requests(project):
}
)

source_branch = "branch1"
source_branch = "branch-merge-request-api"
project.branches.create({"branch": source_branch, "ref": "main"})

project.files.create(
Expand All @@ -28,7 +28,7 @@ def test_merge_requests(project):
}
)
project.mergerequests.create(
{"source_branch": "branch1", "target_branch": "main", "title": "MR readme2"}
{"source_branch": source_branch, "target_branch": "main", "title": "MR readme2"}
)


Expand Down
2 changes: 1 addition & 1 deletion tests/functional/api/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_create_user(gl, fixture_dir):
"email": "foo@bar.com",
"username": "foo",
"name": "foo",
"password": "foo_password",
"password": "E4596f8be406Bc3a14a4ccdb1df80587$3",
"avatar": open(fixture_dir / "avatar.png", "rb"),
}
)
Expand Down
22 changes: 20 additions & 2 deletions tests/functional/cli/test_cli_repository.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import time


Expand Down Expand Up @@ -31,9 +32,13 @@ def test_list_all_commits(gitlab_cli, project):
data = {
"branch": "new-branch",
"start_branch": "main",
"commit_message": "New commit on new branch",
"commit_message": "chore: test commit on new branch",
"actions": [
{"action": "create", "file_path": "new-file", "content": "new content"}
{
"action": "create",
"file_path": "test-cli-repo.md",
"content": "new content",
}
],
}
commit = project.commits.create(data)
Expand Down Expand Up @@ -76,6 +81,19 @@ def test_commit_merge_requests(gitlab_cli, project, merge_request, wait_for_side
"""This tests the `project-commit merge-requests` command and also tests
that we can print the result using the `json` formatter"""
# Merge the MR first
wait_for_sidekiq(timeout=60)

logging.info(f"MR status: {merge_request.state}")
logging.info(f"MR merge status: {merge_request.detailed_merge_status}")

if merge_request.detailed_merge_status == "not_approved":
logging.info("Approving Merge Request")

merge_request.approve()

logging.info(f"MR merge status: {merge_request.detailed_merge_status}")
time.sleep(5)

merge_result = merge_request.merge(should_remove_source_branch=True)
wait_for_sidekiq(timeout=60)

Expand Down
7 changes: 5 additions & 2 deletions tests/functional/cli/test_cli_resource_access_tokens.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pytest
from datetime import date


def test_list_project_access_tokens(gitlab_cli, project):
Expand All @@ -18,13 +18,14 @@ def test_create_project_access_token_with_scopes(gitlab_cli, project):
"test-token",
"--scopes",
"api,read_repository",
"--expires-at",
date.today().isoformat(),
]
ret = gitlab_cli(cmd)

assert ret.success


@pytest.mark.skip(reason="Requires GitLab 14.7")
def test_list_group_access_tokens(gitlab_cli, group):
cmd = ["group-access-token", "list", "--group-id", group.id]
ret = gitlab_cli(cmd)
Expand All @@ -42,6 +43,8 @@ def test_create_group_access_token_with_scopes(gitlab_cli, group):
"test-token",
"--scopes",
"api,read_repository",
"--expires-at",
date.today().isoformat(),
]
ret = gitlab_cli(cmd)

Expand Down
5 changes: 5 additions & 0 deletions tests/functional/cli/test_cli_users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from datetime import date


def test_create_user_impersonation_token_with_scopes(gitlab_cli, user):
cmd = [
"user-impersonation-token",
Expand All @@ -8,6 +11,8 @@ def test_create_user_impersonation_token_with_scopes(gitlab_cli, user):
"test-token",
"--scopes",
"api,read_user",
"--expires-at",
date.today().isoformat(),
]
ret = gitlab_cli(cmd)

Expand Down
68 changes: 32 additions & 36 deletions tests/functional/cli/test_cli_v4.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import logging
import os
import time
from datetime import date

branch = "branch-cli-v4"


def test_create_project(gitlab_cli):
Expand All @@ -22,28 +26,6 @@ def test_update_project(gitlab_cli, project):
assert description in ret.stdout


def test_create_ci_lint(gitlab_cli, valid_gitlab_ci_yml):
cmd = ["ci-lint", "create", "--content", valid_gitlab_ci_yml]
ret = gitlab_cli(cmd)

assert ret.success


def test_validate_ci_lint(gitlab_cli, valid_gitlab_ci_yml):
cmd = ["ci-lint", "validate", "--content", valid_gitlab_ci_yml]
ret = gitlab_cli(cmd)

assert ret.success


def test_validate_ci_lint_invalid_exits_non_zero(gitlab_cli, invalid_gitlab_ci_yml):
cmd = ["ci-lint", "validate", "--content", invalid_gitlab_ci_yml]
ret = gitlab_cli(cmd)

assert not ret.success
assert "CI YAML Lint failed (Invalid configuration format)" in ret.stderr


def test_validate_project_ci_lint(gitlab_cli, project, valid_gitlab_ci_yml):
cmd = [
"project-ci-lint",
Expand Down Expand Up @@ -103,7 +85,7 @@ def test_create_user(gitlab_cli, gl):
email = "fake@email.com"
username = "user1"
name = "User One"
password = "fakepassword"
password = "E4596f8be406Bc3a14a4ccdb1df80587"

cmd = [
"user",
Expand Down Expand Up @@ -215,8 +197,6 @@ def test_create_issue_note(gitlab_cli, issue):


def test_create_branch(gitlab_cli, project):
branch = "branch1"

cmd = [
"project-branch",
"create",
Expand All @@ -233,7 +213,6 @@ def test_create_branch(gitlab_cli, project):


def test_create_merge_request(gitlab_cli, project):
branch = "branch1"

cmd = [
"project-merge-request",
Expand All @@ -257,23 +236,43 @@ def test_accept_request_merge(gitlab_cli, project, wait_for_sidekiq):
mr = project.mergerequests.list()[0]
file_data = {
"branch": mr.source_branch,
"file_path": "README2",
"file_path": "test-cli-v4.md",
"content": "Content",
"commit_message": "Pre-merge commit",
"commit_message": "chore: test-cli-v4 change",
}
project.files.create(file_data)
time.sleep(2)
time.sleep(30)
wait_for_sidekiq(timeout=60)

cmd = [
logging.info(f"MR status: {mr.state}")
logging.info(f"MR merge status: {mr.detailed_merge_status}")

if mr.detailed_merge_status == "not_approved":
logging.info("Approving Merge Request")

approve_cmd = [
"project-merge-request",
"approve",
"--project-id",
project.id,
"--iid",
mr.iid,
]
gitlab_cli(approve_cmd)

time.sleep(5)
logging.info(f"MR merge status: {mr.detailed_merge_status}")

time.sleep(0.5)
approve_cmd = [
"project-merge-request",
"merge",
"--project-id",
project.id,
"--iid",
mr.iid,
]
ret = gitlab_cli(cmd)
ret = gitlab_cli(approve_cmd)

assert ret.success

Expand Down Expand Up @@ -501,9 +500,6 @@ def test_delete_project_variable(gitlab_cli, variable):


def test_delete_branch(gitlab_cli, project):
# TODO: branch fixture
branch = "branch1"

cmd = ["project-branch", "delete", "--project-id", project.id, "--name", branch]
ret = gitlab_cli(cmd)

Expand Down Expand Up @@ -590,7 +586,7 @@ def test_create_project_with_values_at_prefixed(gitlab_cli, tmpdir):
def test_create_project_deploy_token(gitlab_cli, project):
name = "project-token"
username = "root"
expires_at = "2021-09-09"
expires_at = date.today().isoformat()
scopes = "read_registry"

cmd = [
Expand Down Expand Up @@ -666,7 +662,7 @@ def test_delete_project_deploy_token(gitlab_cli, deploy_token):
def test_create_group_deploy_token(gitlab_cli, group):
name = "group-token"
username = "root"
expires_at = "2021-09-09"
expires_at = date.today().isoformat()
scopes = "read_registry"

cmd = [
Expand Down

0 comments on commit f8283ae

Please sign in to comment.