diff options
-rwxr-xr-x | tools/build_test_env.sh | 16 | ||||
-rwxr-xr-x | tools/cli_test_v4.sh | 291 | ||||
-rw-r--r-- | tools/functional/conftest.py | 171 | ||||
-rw-r--r-- | tools/functional/test_cli_v4.py | 715 | ||||
-rwxr-xr-x | tools/functional_tests.sh | 2 | ||||
-rw-r--r-- | tox.ini | 2 |
6 files changed, 889 insertions, 308 deletions
diff --git a/tools/build_test_env.sh b/tools/build_test_env.sh index 91c2896..adab24d 100755 --- a/tools/build_test_env.sh +++ b/tools/build_test_env.sh @@ -104,20 +104,6 @@ letsencrypt['enable'] = false "$GITLAB_IMAGE:$GITLAB_TAG" >/dev/null fi -LOGIN='root' -PASSWORD='5iveL!fe' -GITLAB() { gitlab --config-file "$CONFIG" "$@"; } -GREEN='\033[0;32m' -NC='\033[0m' -OK() { printf "${GREEN}OK${NC}\\n"; } -testcase() { - testname=$1; shift - testscript=$1; shift - printf %s "Testing ${testname}... " - eval "${testscript}" || fatal "test failed" - OK -} - if [ -z "$NOVENV" ]; then log "Creating Python virtualenv..." try $VENV_CMD "$VENV" @@ -130,7 +116,7 @@ if [ -z "$NOVENV" ]; then try pip install -e . # to run generate_token.py - pip install requests-html + pip install requests-html pytest-console-scripts fi log "Waiting for gitlab to come online... " diff --git a/tools/cli_test_v4.sh b/tools/cli_test_v4.sh deleted file mode 100755 index 725e418..0000000 --- a/tools/cli_test_v4.sh +++ /dev/null @@ -1,291 +0,0 @@ -#!/bin/sh -# Copyright (C) 2015 Gauvain Pocentek <gauvain@pocentek.net> -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -testcase "project creation" ' - OUTPUT=$(try GITLAB project create --name test-project1) || exit 1 - PROJECT_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d" " -f2) - OUTPUT=$(try GITLAB project list) || exit 1 - pecho "${OUTPUT}" | grep -q test-project1 -' - -testcase "project update" ' - GITLAB project update --id "$PROJECT_ID" --description "My New Description" -' - -testcase "group creation" ' - OUTPUT=$(try GITLAB group create --name test-group1 --path group1) || exit 1 - GROUP_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d" " -f2) - OUTPUT=$(try GITLAB group list) || exit 1 - pecho "${OUTPUT}" | grep -q test-group1 -' - -testcase "group update" ' - GITLAB group update --id "$GROUP_ID" --description "My New Description" -' - -testcase "user creation" ' - OUTPUT=$(GITLAB user create --email fake@email.com --username user1 \ - --name "User One" --password fakepassword) -' -USER_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d' ' -f2) - -testcase "user get (by id)" ' - GITLAB user get --id $USER_ID >/dev/null 2>&1 -' - -testcase "verbose output" ' - OUTPUT=$(try GITLAB -v user list) || exit 1 - pecho "${OUTPUT}" | grep -q avatar-url -' - -testcase "CLI args not in output" ' - OUTPUT=$(try GITLAB -v user list) || exit 1 - pecho "${OUTPUT}" | grep -qv config-file -' - -testcase "adding member to a project" ' - GITLAB project-member create --project-id "$PROJECT_ID" \ - --user-id "$USER_ID" --access-level 40 >/dev/null 2>&1 -' - -testcase "listing user memberships" ' - GITLAB user-membership list --user-id "$USER_ID" >/dev/null 2>&1 -' - -testcase "file creation" ' - GITLAB project-file create --project-id "$PROJECT_ID" \ - --file-path README --branch master --content "CONTENT" \ - --commit-message "Initial commit" >/dev/null 2>&1 -' - -testcase "issue creation" ' - OUTPUT=$(GITLAB project-issue create --project-id "$PROJECT_ID" \ - --title "my issue" --description "my issue description") -' -ISSUE_ID=$(pecho "${OUTPUT}" | grep ^iid: | cut -d' ' -f2) - -testcase "note creation" ' - GITLAB project-issue-note create --project-id "$PROJECT_ID" \ - --issue-iid "$ISSUE_ID" --body "the body" >/dev/null 2>&1 -' - -testcase "branch creation" ' - GITLAB project-branch create --project-id "$PROJECT_ID" \ - --branch branch1 --ref master >/dev/null 2>&1 -' - -GITLAB project-file create --project-id "$PROJECT_ID" \ - --file-path README2 --branch branch1 --content "CONTENT" \ - --commit-message "second commit" >/dev/null 2>&1 - -testcase "merge request creation" ' - OUTPUT=$(GITLAB project-merge-request create \ - --project-id "$PROJECT_ID" \ - --source-branch branch1 --target-branch master \ - --title "Update README") -' -MR_ID=$(pecho "${OUTPUT}" | grep ^iid: | cut -d' ' -f2) - -testcase "merge request validation" ' - GITLAB project-merge-request merge --project-id "$PROJECT_ID" \ - --iid "$MR_ID" >/dev/null 2>&1 -' - -# Test revert commit -COMMITS=$(GITLAB -v project-commit list --project-id "${PROJECT_ID}") -COMMIT_ID=$(pecho "${COMMITS}" | grep -m1 '^id:' | cut -d' ' -f2) - -testcase "revert commit" ' - GITLAB project-commit revert --project-id "$PROJECT_ID" \ - --id "$COMMIT_ID" --branch master -' - -# Test commit signature -testcase "attempt to get signature of unsigned commit" ' - OUTPUT=$(GITLAB project-commit signature --project-id "$PROJECT_ID" \ - --id "$COMMIT_ID" 2>&1 || exit 0) - echo "$OUTPUT" | grep -q "404 Signature Not Found" -' - -# Test project labels -testcase "create project label" ' - OUTPUT=$(GITLAB -v project-label create --project-id $PROJECT_ID \ - --name prjlabel1 --description "prjlabel1 description" --color "#112233") -' - -testcase "list project label" ' - OUTPUT=$(GITLAB -v project-label list --project-id $PROJECT_ID) -' - -testcase "update project label" ' - OUTPUT=$(GITLAB -v project-label update --project-id $PROJECT_ID \ - --name prjlabel1 --new-name prjlabel2 --description "prjlabel2 description" --color "#332211") -' - -testcase "delete project label" ' - OUTPUT=$(GITLAB -v project-label delete --project-id $PROJECT_ID \ - --name prjlabel2) -' - -# Test group labels -testcase "create group label" ' - OUTPUT=$(GITLAB -v group-label create --group-id $GROUP_ID \ - --name grplabel1 --description "grplabel1 description" --color "#112233") -' - -testcase "list group label" ' - OUTPUT=$(GITLAB -v group-label list --group-id $GROUP_ID) -' - -testcase "update group label" ' - OUTPUT=$(GITLAB -v group-label update --group-id $GROUP_ID \ - --name grplabel1 --new-name grplabel2 --description "grplabel2 description" --color "#332211") -' - -testcase "delete group label" ' - OUTPUT=$(GITLAB -v group-label delete --group-id $GROUP_ID \ - --name grplabel2) -' - -# Test project variables -testcase "create project variable" ' - OUTPUT=$(GITLAB -v project-variable create --project-id $PROJECT_ID \ - --key junk --value car) -' - -testcase "get project variable" ' - OUTPUT=$(GITLAB -v project-variable get --project-id $PROJECT_ID \ - --key junk) -' - -testcase "update project variable" ' - OUTPUT=$(GITLAB -v project-variable update --project-id $PROJECT_ID \ - --key junk --value bus) -' - -testcase "list project variable" ' - OUTPUT=$(GITLAB -v project-variable list --project-id $PROJECT_ID) -' - -testcase "delete project variable" ' - OUTPUT=$(GITLAB -v project-variable delete --project-id $PROJECT_ID \ - --key junk) -' - -testcase "branch deletion" ' - GITLAB project-branch delete --project-id "$PROJECT_ID" \ - --name branch1 >/dev/null 2>&1 -' - -testcase "project upload" ' - GITLAB project upload --id "$PROJECT_ID" \ - --filename '$(basename $0)' --filepath '$0' >/dev/null 2>&1 -' - -testcase "application settings get" ' - GITLAB application-settings get >/dev/null 2>&1 -' - -testcase "application settings update" ' - GITLAB application-settings update --signup-enabled false >/dev/null 2>&1 -' - -cat > /tmp/gitlab-project-description << EOF -Multi line - -Data -EOF -testcase "values from files" ' - OUTPUT=$(GITLAB -v project create --name fromfile \ - --description @/tmp/gitlab-project-description) - echo $OUTPUT | grep -q "Multi line" -' - -# Test deploy tokens -CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v project-deploy-token create --project-id $PROJECT_ID \ - --name foo --username root --expires-at "2021-09-09" --scopes "read_registry") -CREATED_DEPLOY_TOKEN_ID=$(echo "$CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT" | grep ^id: | cut -d" " -f2) -testcase "create project deploy token (name)" ' - echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep -q "name: foo" -' -testcase "create project deploy token (expires-at)" ' - echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep -q "expires-at: 2021-09-09T00:00:00.000Z" -' -testcase "create project deploy token (scopes)" ' - echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep "scopes: " | grep -q "read_registry" -' - -testcase "create project deploy token (username)" ' - echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep -q "username: root" -' - -LIST_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v deploy-token list) -testcase "list all deploy tokens" ' - echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -q "name: foo" -' -testcase "list all deploy tokens" ' - echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -q "id: $CREATED_DEPLOY_TOKEN_ID" -' -testcase "list all deploy tokens" ' - echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -q "expires-at: 2021-09-09T00:00:00.000Z" -' -testcase "list all deploy tokens" ' - echo $LIST_DEPLOY_TOKEN_OUTPUT | grep "scopes: " | grep -q "read_registry" -' - -testcase "list project deploy tokens" ' - OUTPUT=$(GITLAB -v project-deploy-token list --project-id $PROJECT_ID) - echo $OUTPUT | grep -q "id: $CREATED_DEPLOY_TOKEN_ID" -' -testcase "delete project deploy token" ' - GITLAB -v project-deploy-token delete --project-id $PROJECT_ID --id $CREATED_DEPLOY_TOKEN_ID - LIST_PROJECT_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v project-deploy-token list --project-id $PROJECT_ID) - echo $LIST_PROJECT_DEPLOY_TOKEN_OUTPUT | grep -qv "id: $CREATED_DEPLOY_TOKEN_ID" -' -# Uncomment once https://gitlab.com/gitlab-org/gitlab/-/issues/212523 is fixed -#testcase "delete project deploy token" ' -# LIST_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v deploy-token list) -# echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -qv "id: $CREATED_DEPLOY_TOKEN_ID" -#' - -CREATE_GROUP_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v group-deploy-token create --group-id $GROUP_ID \ - --name bar --username root --expires-at "2021-09-09" --scopes "read_repository") -CREATED_DEPLOY_TOKEN_ID=$(echo "$CREATE_GROUP_DEPLOY_TOKEN_OUTPUT" | grep ^id: | cut -d" " -f2) -testcase "create group deploy token" ' - echo $CREATE_GROUP_DEPLOY_TOKEN_OUTPUT | grep -q "name: bar" -' -testcase "list group deploy tokens" ' - OUTPUT=$(GITLAB -v group-deploy-token list --group-id $GROUP_ID) - echo $OUTPUT | grep -q "id: $CREATED_DEPLOY_TOKEN_ID" -' -testcase "delete group deploy token" ' - GITLAB -v group-deploy-token delete --group-id $GROUP_ID --id $CREATED_DEPLOY_TOKEN_ID - LIST_GROUP_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v group-deploy-token list --group-id $GROUP_ID) - echo $LIST_GROUP_DEPLOY_TOKEN_OUTPUT | grep -qv "id: $CREATED_DEPLOY_TOKEN_ID" -' -# Uncomment once https://gitlab.com/gitlab-org/gitlab/-/issues/212523 is fixed -#testcase "delete group deploy token" ' -# LIST_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v deploy-token list) -# echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -qv "id: $CREATED_DEPLOY_TOKEN_ID" -#' - -testcase "project deletion" ' - GITLAB project delete --id "$PROJECT_ID" -' - -testcase "group deletion" ' - OUTPUT=$(try GITLAB group delete --id $GROUP_ID) -' diff --git a/tools/functional/conftest.py b/tools/functional/conftest.py new file mode 100644 index 0000000..bd99fa9 --- /dev/null +++ b/tools/functional/conftest.py @@ -0,0 +1,171 @@ +from random import randint + +import pytest + +import gitlab + + +def random_id(): + """ + Helper to ensure new resource creation does not clash with + existing resources, for example when a previous test deleted a + resource but GitLab is still deleting it asynchronously in the + background. TODO: Expand to make it 100% safe. + """ + return randint(9, 9999) + + +@pytest.fixture(scope="session") +def CONFIG(): + return "/tmp/python-gitlab.cfg" + + +@pytest.fixture +def gitlab_cli(script_runner, CONFIG): + """Wrapper fixture to help make test cases less verbose.""" + + def _gitlab_cli(subcommands): + """ + Return a script_runner.run method that takes a default gitlab + command, and subcommands passed as arguments inside test cases. + """ + command = ["gitlab", "--config-file", CONFIG] + + for subcommand in subcommands: + # ensure we get strings (e.g from IDs) + command.append(str(subcommand)) + + return script_runner.run(*command) + + return _gitlab_cli + + +@pytest.fixture(scope="session") +def gl(CONFIG): + """Helper instance to make fixtures and asserts directly via the API.""" + return gitlab.Gitlab.from_config("local", [CONFIG]) + + +@pytest.fixture(scope="module") +def group(gl): + """Group fixture for group API resource tests.""" + _id = random_id() + data = { + "name": f"test-group-{_id}", + "path": f"group-{_id}", + } + group = gl.groups.create(data) + + yield group + + try: + group.delete() + except gitlab.exceptions.GitlabDeleteError as e: + print(f"Group already deleted: {e}") + + +@pytest.fixture(scope="module") +def project(gl): + """Project fixture for project API resource tests.""" + _id = random_id() + name = f"test-project-{_id}" + + project = gl.projects.create(name=name) + + yield project + + try: + project.delete() + except gitlab.exceptions.GitlabDeleteError as e: + print(f"Project already deleted: {e}") + + +@pytest.fixture(scope="module") +def user(gl): + """User fixture for user API resource tests.""" + _id = random_id() + email = f"user{_id}@email.com" + username = f"user{_id}" + name = f"User {_id}" + password = "fakepassword" + + user = gl.users.create(email=email, username=username, name=name, password=password) + + yield user + + try: + user.delete() + except gitlab.exceptions.GitlabDeleteError as e: + print(f"User already deleted: {e}") + + +@pytest.fixture(scope="module") +def issue(project): + """Issue fixture for issue API resource tests.""" + _id = random_id() + data = {"title": f"Issue {_id}", "description": f"Issue {_id} description"} + + return project.issues.create(data) + + +@pytest.fixture(scope="module") +def label(project): + """Label fixture for project label API resource tests.""" + _id = random_id() + 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 = random_id() + data = { + "name": f"grplabel{_id}", + "description": f"grplabel1 {_id} description", + "color": "#112233", + } + + return group.labels.create(data) + + +@pytest.fixture(scope="module") +def variable(project): + """Variable fixture for project variable API resource tests.""" + _id = random_id() + 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 = random_id() + data = { + "name": f"token-{_id}", + "username": "root", + "expires_at": "2021-09-09", + "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 = random_id() + data = { + "name": f"group-token-{_id}", + "username": "root", + "expires_at": "2021-09-09", + "scopes": "read_registry", + } + + return group.deploytokens.create(data) diff --git a/tools/functional/test_cli_v4.py b/tools/functional/test_cli_v4.py new file mode 100644 index 0000000..c4d2413 --- /dev/null +++ b/tools/functional/test_cli_v4.py @@ -0,0 +1,715 @@ +import os +import time + + +def test_create_project(gitlab_cli): + name = "test-project1" + + cmd = ["project", "create", "--name", name] + ret = gitlab_cli(cmd) + + assert ret.success + assert name in ret.stdout + + +def test_update_project(gitlab_cli, project): + description = "My New Description" + + cmd = ["project", "update", "--id", project.id, "--description", description] + ret = gitlab_cli(cmd) + + assert ret.success + assert description in ret.stdout + + +def test_create_group(gitlab_cli): + name = "test-group1" + path = "group1" + + cmd = ["group", "create", "--name", name, "--path", path] + ret = gitlab_cli(cmd) + + assert ret.success + assert name in ret.stdout + assert path in ret.stdout + + +def test_update_group(gitlab_cli, gl, group): + description = "My New Description" + + cmd = ["group", "update", "--id", group.id, "--description", description] + ret = gitlab_cli(cmd) + + assert ret.success + + group = gl.groups.list(description=description)[0] + assert group.description == description + + +def test_create_user(gitlab_cli, gl): + email = "fake@email.com" + username = "user1" + name = "User One" + password = "fakepassword" + + cmd = [ + "user", + "create", + "--email", + email, + "--username", + username, + "--name", + name, + "--password", + password, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + user = gl.users.list(username=username)[0] + + assert user.email == email + assert user.username == username + assert user.name == name + + +def test_get_user_by_id(gitlab_cli, user): + cmd = ["user", "get", "--id", user.id] + ret = gitlab_cli(cmd) + + assert ret.success + assert str(user.id) in ret.stdout + + +def test_list_users_verbose_output(gitlab_cli): + cmd = ["-v", "user", "list"] + ret = gitlab_cli(cmd) + + assert ret.success + assert "avatar-url" in ret.stdout + + +def test_cli_args_not_in_output(gitlab_cli): + cmd = ["-v", "user", "list"] + ret = gitlab_cli(cmd) + + assert "config-file" not in ret.stdout + + +def test_add_member_to_project(gitlab_cli, project, user): + access_level = "40" + + cmd = [ + "project-member", + "create", + "--project-id", + project.id, + "--user-id", + user.id, + "--access-level", + access_level, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_list_user_memberships(gitlab_cli, user): + cmd = ["user-membership", "list", "--user-id", user.id] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_project_create_file(gitlab_cli, project): + file_path = "README" + branch = "master" + content = "CONTENT" + commit_message = "Initial commit" + + cmd = [ + "project-file", + "create", + "--project-id", + project.id, + "--file-path", + file_path, + "--branch", + branch, + "--content", + content, + "--commit-message", + commit_message, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_create_project_issue(gitlab_cli, project): + title = "my issue" + description = "my issue description" + + cmd = [ + "project-issue", + "create", + "--project-id", + project.id, + "--title", + title, + "--description", + description, + ] + ret = gitlab_cli(cmd) + + assert ret.success + assert title in ret.stdout + + +def test_create_issue_note(gitlab_cli, issue): + body = "body" + + cmd = [ + "project-issue-note", + "create", + "--project-id", + issue.project_id, + "--issue-iid", + issue.id, + "--body", + body, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_create_branch(gitlab_cli, project): + branch = "branch1" + + cmd = [ + "project-branch", + "create", + "--project-id", + project.id, + "--branch", + branch, + "--ref", + "master", + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_create_merge_request(gitlab_cli, project): + branch = "branch1" + + cmd = [ + "project-merge-request", + "create", + "--project-id", + project.id, + "--source-branch", + branch, + "--target-branch", + "master", + "--title", + "Update README", + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_accept_request_merge(gitlab_cli, project): + # MR needs at least 1 commit before we can merge + mr = project.mergerequests.list()[0] + file_data = { + "branch": mr.source_branch, + "file_path": "README2", + "content": "Content", + "commit_message": "Pre-merge commit", + } + project.files.create(file_data) + time.sleep(2) + + cmd = [ + "project-merge-request", + "merge", + "--project-id", + project.id, + "--iid", + mr.iid, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_revert_commit(gitlab_cli, project): + commit = project.commits.list()[0] + + cmd = [ + "project-commit", + "revert", + "--project-id", + project.id, + "--id", + commit.id, + "--branch", + "master", + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_get_commit_signature_not_found(gitlab_cli, project): + commit = project.commits.list()[0] + + cmd = ["project-commit", "signature", "--project-id", project.id, "--id", commit.id] + ret = gitlab_cli(cmd) + + assert not ret.success + assert "404 Signature Not Found" in ret.stderr + + +def test_create_project_label(gitlab_cli, project): + name = "prjlabel1" + description = "prjlabel1 description" + color = "#112233" + + cmd = [ + "-v", + "project-label", + "create", + "--project-id", + project.id, + "--name", + name, + "--description", + description, + "--color", + color, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_list_project_labels(gitlab_cli, project): + cmd = ["-v", "project-label", "list", "--project-id", project.id] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_update_project_label(gitlab_cli, label): + new_label = "prjlabel2" + new_description = "prjlabel2 description" + new_color = "#332211" + + cmd = [ + "-v", + "project-label", + "update", + "--project-id", + label.project_id, + "--name", + label.name, + "--new-name", + new_label, + "--description", + new_description, + "--color", + new_color, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_delete_project_label(gitlab_cli, label): + # TODO: due to update above, we'd need a function-scope label fixture + label_name = "prjlabel2" + + cmd = [ + "-v", + "project-label", + "delete", + "--project-id", + label.project_id, + "--name", + label_name, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_create_group_label(gitlab_cli, group): + name = "grouplabel1" + description = "grouplabel1 description" + color = "#112233" + + cmd = [ + "-v", + "group-label", + "create", + "--group-id", + group.id, + "--name", + name, + "--description", + description, + "--color", + color, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_list_group_labels(gitlab_cli, group): + cmd = ["-v", "group-label", "list", "--group-id", group.id] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_update_group_label(gitlab_cli, group_label): + new_label = "grouplabel2" + new_description = "grouplabel2 description" + new_color = "#332211" + + cmd = [ + "-v", + "group-label", + "update", + "--group-id", + group_label.group_id, + "--name", + group_label.name, + "--new-name", + new_label, + "--description", + new_description, + "--color", + new_color, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_delete_group_label(gitlab_cli, group_label): + # TODO: due to update above, we'd need a function-scope label fixture + new_label = "grouplabel2" + + cmd = [ + "-v", + "group-label", + "delete", + "--group-id", + group_label.group_id, + "--name", + new_label, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_create_project_variable(gitlab_cli, project): + key = "junk" + value = "car" + + cmd = [ + "-v", + "project-variable", + "create", + "--project-id", + project.id, + "--key", + key, + "--value", + value, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_get_project_variable(gitlab_cli, variable): + cmd = [ + "-v", + "project-variable", + "get", + "--project-id", + variable.project_id, + "--key", + variable.key, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_update_project_variable(gitlab_cli, variable): + new_value = "bus" + + cmd = [ + "-v", + "project-variable", + "update", + "--project-id", + variable.project_id, + "--key", + variable.key, + "--value", + new_value, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_list_project_variables(gitlab_cli, project): + cmd = ["-v", "project-variable", "list", "--project-id", project.id] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_delete_project_variable(gitlab_cli, variable): + cmd = [ + "-v", + "project-variable", + "delete", + "--project-id", + variable.project_id, + "--key", + variable.key, + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +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) + + assert ret.success + + +def test_project_upload_file(gitlab_cli, project): + cmd = [ + "project", + "upload", + "--id", + project.id, + "--filename", + __file__, + "--filepath", + os.path.realpath(__file__), + ] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_get_application_settings(gitlab_cli): + cmd = ["application-settings", "get"] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_update_application_settings(gitlab_cli): + cmd = ["application-settings", "update", "--signup-enabled", "false"] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_create_project_with_values_from_file(gitlab_cli, tmpdir): + name = "gitlab-project-from-file" + description = "Multiline\n\nData\n" + from_file = tmpdir.join(name) + from_file.write(description) + from_file_path = f"@{str(from_file)}" + + cmd = [ + "-v", + "project", + "create", + "--name", + name, + "--description", + from_file_path, + ] + ret = gitlab_cli(cmd) + + assert ret.success + assert description in ret.stdout + + +def test_create_project_deploy_token(gitlab_cli, project): + name = "project-token" + username = "root" + expires_at = "2021-09-09" + scopes = "read_registry" + + cmd = [ + "-v", + "project-deploy-token", + "create", + "--project-id", + project.id, + "--name", + name, + "--username", + username, + "--expires-at", + expires_at, + "--scopes", + scopes, + ] + ret = gitlab_cli(cmd) + + assert ret.success + assert name in ret.stdout + assert username in ret.stdout + assert expires_at in ret.stdout + assert scopes in ret.stdout + + +def test_list_all_deploy_tokens(gitlab_cli, deploy_token): + cmd = ["-v", "deploy-token", "list"] + ret = gitlab_cli(cmd) + + assert ret.success + assert deploy_token.name in ret.stdout + assert str(deploy_token.id) in ret.stdout + assert deploy_token.username in ret.stdout + assert deploy_token.expires_at in ret.stdout + assert deploy_token.scopes[0] in ret.stdout + + +def test_list_project_deploy_tokens(gitlab_cli, deploy_token): + cmd = [ + "-v", + "project-deploy-token", + "list", + "--project-id", + deploy_token.project_id, + ] + ret = gitlab_cli(cmd) + + assert ret.success + assert deploy_token.name in ret.stdout + assert str(deploy_token.id) in ret.stdout + assert deploy_token.username in ret.stdout + assert deploy_token.expires_at in ret.stdout + assert deploy_token.scopes[0] in ret.stdout + + +def test_delete_project_deploy_token(gitlab_cli, deploy_token): + cmd = [ + "-v", + "project-deploy-token", + "delete", + "--project-id", + deploy_token.project_id, + "--id", + deploy_token.id, + ] + ret = gitlab_cli(cmd) + + assert ret.success + # TODO assert not in list + + +def test_create_group_deploy_token(gitlab_cli, group): + name = "group-token" + username = "root" + expires_at = "2021-09-09" + scopes = "read_registry" + + cmd = [ + "-v", + "group-deploy-token", + "create", + "--group-id", + group.id, + "--name", + name, + "--username", + username, + "--expires-at", + expires_at, + "--scopes", + scopes, + ] + ret = gitlab_cli(cmd) + + assert ret.success + assert name in ret.stdout + assert username in ret.stdout + assert expires_at in ret.stdout + assert scopes in ret.stdout + + +def test_list_group_deploy_tokens(gitlab_cli, group_deploy_token): + cmd = [ + "-v", + "group-deploy-token", + "list", + "--group-id", + group_deploy_token.group_id, + ] + ret = gitlab_cli(cmd) + + assert ret.success + assert group_deploy_token.name in ret.stdout + assert str(group_deploy_token.id) in ret.stdout + assert group_deploy_token.username in ret.stdout + assert group_deploy_token.expires_at in ret.stdout + assert group_deploy_token.scopes[0] in ret.stdout + + +def test_delete_group_deploy_token(gitlab_cli, group_deploy_token): + cmd = [ + "-v", + "group-deploy-token", + "delete", + "--group-id", + group_deploy_token.group_id, + "--id", + group_deploy_token.id, + ] + ret = gitlab_cli(cmd) + + assert ret.success + # TODO assert not in list + + +def test_delete_project(gitlab_cli, project): + cmd = ["project", "delete", "--id", project.id] + ret = gitlab_cli(cmd) + + assert ret.success + + +def test_delete_group(gitlab_cli, group): + cmd = ["group", "delete", "--id", group.id] + ret = gitlab_cli(cmd) + + assert ret.success diff --git a/tools/functional_tests.sh b/tools/functional_tests.sh index 4123d87..b86be3a 100755 --- a/tools/functional_tests.sh +++ b/tools/functional_tests.sh @@ -18,4 +18,4 @@ setenv_script=$(dirname "$0")/build_test_env.sh || exit 1 BUILD_TEST_ENV_AUTO_CLEANUP=true . "$setenv_script" "$@" || exit 1 -. $(dirname "$0")/cli_test_v${API_VER}.sh +pytest "$(dirname "$0")/functional/test_cli_v4.py" @@ -25,7 +25,7 @@ deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt black commands = - black {posargs} gitlab + black {posargs} gitlab tools/functional [testenv:venv] commands = {posargs} |