Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,10 @@ against a disposable GitLab instance running as a Docker container OR use your o

```
./dev/run_gitlab_in_docker.sh
export GITLAB_URL=$(cat gitlab_url.txt)
export GITLAB_TOKEN=$(cat gitlab_token.txt)
```

2. Run `py.test gitlabform/gitlabform/test` to start the tests
2. Run `py.test gitlabform/gitlabform/test` to start all tests.
To run only a single class with tests run f.e. `py.test gitlabform/gitlabform/test -k "TestArchiveProject"`.

##### Running integration tests using your own GitLab instance

Expand All @@ -119,7 +118,8 @@ export GITLAB_URL="https://mygitlab.company.com"
export GITLAB_TOKEN="<my admin user API token>"
```

2. Run `py.test gitlabform/gitlabform/test` to start the tests
2. Run `py.test gitlabform/gitlabform/test` to start all tests
To run only a single class with tests run f.e. `py.test gitlabform/gitlabform/test -k "TestArchiveProject"`.

#### General coding guidelines

Expand Down
11 changes: 3 additions & 8 deletions dev/run_gitlab_in_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cecho() {
tput sgr0
}

if [[ -n $1 ]] ; then
if [[ $# == 1 ]] ; then
gitlab_version="$1"
else
gitlab_version="latest"
Expand Down Expand Up @@ -90,19 +90,14 @@ echo ''
cecho b 'GitLab web UI URL (user: root, password: password)'
echo 'http://localhost'
echo ''
cecho b 'Run these commands to set the env variables needed by the tests to use this GitLab instance:'
# shellcheck disable=SC2016
cecho g 'export GITLAB_URL=$(cat gitlab_url.txt)'
# shellcheck disable=SC2016
cecho g 'export GITLAB_TOKEN=$(cat gitlab_token.txt)'
echo ''
alias stop_gitlab='existing_gitlab_container_id=$(docker ps -a -f "name=gitlab" --format "{{.ID}}"); docker stop --time=30 $existing_gitlab_container_id ; docker rm $existing_gitlab_container_id'
cecho b 'Run this command to stop GitLab container:'
cecho r 'stop_gitlab'
echo ''
cecho b 'To start GitLab container again, re-run this script. Note that GitLab will reuse existing ./data, ./config'
cecho b 'and ./logs dirs. To start new GitLab instance from scratch please delete them.'
echo ''
cecho b 'Run this to start the integration tests:'
cecho b 'Run this to start the integration tests (it will automatically load GITLAB_URL from gitlab_url.txt'
cecho b 'and GITLAB_TOKEN from gitlab_token.txt created by this script):'
echo ''
cecho y 'py.test gitlabform/gitlabform/test'
8 changes: 8 additions & 0 deletions gitlabform/gitlabform/processors/abstract_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def process(
project_or_project_and_group,
configuration.get(self.__configuration_name),
)
elif (
configuration.get("project|archive")
and self.__configuration_name != "project"
):
logging.info(
"Skipping %s - it is configured to be archived."
% self.__configuration_name
)
else:
logging.info("Processing %s" % self.__configuration_name)
self._process_configuration(project_or_project_and_group, configuration)
Expand Down
23 changes: 23 additions & 0 deletions gitlabform/gitlabform/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from gitlabform.gitlab import GitLab
from gitlabform.gitlab.core import NotFoundException, UnexpectedResponseException

Expand All @@ -7,6 +9,27 @@
api_version: 4
"""

# automate reading files created by run_gitlab_in_docker.sh to run tests in PyCharm / IntelliJ
# (workaround for lack of this feature: https://youtrack.jetbrains.com/issue/PY-5543 )

env_vars_and_file_paths = {
"GITLAB_URL": "dev/gitlab_url.txt",
"GITLAB_TOKEN": "dev/gitlab_token.txt",
}

for env_var, file_path in env_vars_and_file_paths.items():
if env_var not in os.environ:
print(f"{env_var} not set - trying to read it from {file_path} ...")
if os.path.isfile(file_path):
try:
with open(file_path, "r") as file:
os.environ[env_var] = file.read().replace("\n", "")
print(f"{env_var} set!")
except Exception as e:
print(f"Failed to read {file_path}: {e}")
else:
print(f"{file_path} doesn't exist.")

GROUP_NAME = "gitlabform_tests_group"

DEVELOPER_ACCESS = 30
Expand Down
128 changes: 128 additions & 0 deletions gitlabform/gitlabform/test/test_archive_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import time

import pytest

from gitlabform.gitlabform import GitLabForm
from gitlabform.gitlabform.test import (
create_group,
create_project_in_group,
create_readme_in_project,
get_gitlab,
GROUP_NAME,
)

PROJECT_NAME = "archive_project"
GROUP_AND_PROJECT_NAME = GROUP_NAME + "/" + PROJECT_NAME


@pytest.fixture(scope="function")
def gitlab(request):
gl = get_gitlab()

create_group(GROUP_NAME)
create_project_in_group(GROUP_NAME, PROJECT_NAME)
create_readme_in_project(GROUP_AND_PROJECT_NAME) # in main branch

def fin():
gl.delete_project(GROUP_AND_PROJECT_NAME)
# TODO: find some smarter way to avoid 400 when trying to create project while it is still being deleted
time.sleep(15)

request.addfinalizer(fin)
return gl # provide fixture value


archive_project = """
gitlab:
api_version: 4

project_settings:
gitlabform_tests_group/archive_project:
project:
archive: true
"""

unarchive_project = """
gitlab:
api_version: 4

project_settings:
gitlabform_tests_group/archive_project:
project:
archive: false
"""

edit_archived_project = """
gitlab:
api_version: 4

# the project has to be configured as archived
# for other configs for it to be ignored
project_settings:
gitlabform_tests_group/archive_project:
project:
archive: true

group_settings:
gitlabform_tests_group:
files:
README.md:
overwrite: true
branches:
- main
content: |
Some other content that the default one
"""


class TestArchiveProject:
def test__archive_project(self, gitlab):
gf = GitLabForm(
config_string=archive_project,
project_or_group=GROUP_AND_PROJECT_NAME,
)
gf.main()

project = gitlab.get_project(GROUP_AND_PROJECT_NAME)

assert project["archived"] is True

def test__unarchive_project(self, gitlab):
gf = GitLabForm(
config_string=archive_project,
project_or_group=GROUP_AND_PROJECT_NAME,
)
gf.main()

project = gitlab.get_project(GROUP_AND_PROJECT_NAME)

assert project["archived"] is True

gf = GitLabForm(
config_string=unarchive_project,
project_or_group=GROUP_AND_PROJECT_NAME,
)
gf.main()

project = gitlab.get_project(GROUP_AND_PROJECT_NAME)

assert project["archived"] is False

def test__dont_edit_archived_project(self, gitlab):
gf = GitLabForm(
config_string=archive_project,
project_or_group=GROUP_AND_PROJECT_NAME,
)
gf.main()

project = gitlab.get_project(GROUP_AND_PROJECT_NAME)

assert project["archived"] is True

gf = GitLabForm(
config_string=edit_archived_project,
project_or_group=GROUP_NAME,
)
gf.main()

# if we tried to edit an archived project, then we will get an exception here