Skip to content

Commit 538b2c9

Browse files
committed
feat(testing): incorporate pytest-gitlab plugin code
Code is taken from tag v0.3.1.dev0 (8aaa35d2). - remove `__version__.py` file from pytest-gitlab - maintain python-gitlab docker/.env - adjust imports to use `gitlab.testing` instead of `pytest-gitlab` - make `plugin.py` imports relative - add mypy override for `gitlab.testing.*` - create a `testing` optional dependency - add pytest plugin entrypoint `gitlab` - change from single quotes to double quotes (thanks to black formatting) Signed-off-by: Patrick Talbert <ptalbert@redhat.com>
1 parent 1819181 commit 538b2c9

File tree

17 files changed

+958
-0
lines changed

17 files changed

+958
-0
lines changed

gitlab/testing/__init__.py

Whitespace-only changes.

gitlab/testing/docker/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
GITLAB_IMAGE=gitlab/gitlab-ee
2+
GITLAB_TAG=17.8.2-ee.0
3+
GITLAB_RUNNER_IMAGE=gitlab/gitlab-runner
4+
GITLAB_RUNNER_TAG=96856197

gitlab/testing/docker/__init__.py

Whitespace-only changes.

gitlab/testing/docker/avatar.png

592 Bytes
Loading
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# NOTE: As of 2022-06-01 the GitLab Enterprise Edition License has the following
2+
# section:
3+
# Notwithstanding the foregoing, you may copy and modify the Software for development
4+
# and testing purposes, without requiring a subscription.
5+
#
6+
# https://gitlab.com/gitlab-org/gitlab/-/blob/29503bc97b96af8d4876dc23fc8996e3dab7d211/ee/LICENSE
7+
#
8+
# This code is strictly intended for use in the testing framework of python-gitlab
9+
10+
# Code inspired by MIT licensed code at: https://github.com/CONIGUERO/gitlab-license.git
11+
12+
require 'openssl'
13+
require 'gitlab/license'
14+
15+
# Generate a 2048 bit key pair.
16+
license_encryption_key = OpenSSL::PKey::RSA.generate(2048)
17+
18+
# Save the private key
19+
File.open("/.license_encryption_key", "w") { |f| f.write(license_encryption_key.to_pem) }
20+
# Save the public key
21+
public_key = license_encryption_key.public_key
22+
File.open("/.license_encryption_key.pub", "w") { |f| f.write(public_key.to_pem) }
23+
File.open("/opt/gitlab/embedded/service/gitlab-rails/.license_encryption_key.pub", "w") { |f| f.write(public_key.to_pem) }
24+
25+
Gitlab::License.encryption_key = license_encryption_key
26+
27+
# Build a new license.
28+
license = Gitlab::License.new
29+
30+
license.licensee = {
31+
"Name" => "python-gitlab-ci",
32+
"Company" => "python-gitlab-ci",
33+
"Email" => "python-gitlab-ci@example.com",
34+
}
35+
36+
# The date the license starts.
37+
license.starts_at = Date.today
38+
# Want to make sure we get at least 1 day of usage. Do two days after because if CI
39+
# started at 23:59 we could be expired in one minute if we only did one next_day.
40+
license.expires_at = Date.today.next_day.next_day
41+
42+
# Use 'ultimate' plan so that we can test all features in the CI
43+
license.restrictions = {
44+
:plan => "ultimate",
45+
:id => rand(1000..99999999)
46+
}
47+
48+
# Export the license, which encrypts and encodes it.
49+
data = license.export
50+
51+
File.open("/python-gitlab-ci.gitlab-license", 'w') { |file| file.write(data) }
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
version: '3.5'
2+
3+
networks:
4+
gitlab-network:
5+
name: gitlab-network
6+
7+
services:
8+
gitlab:
9+
image: '${GITLAB_IMAGE:-gitlab/gitlab-ee}:${GITLAB_TAG:-latest}'
10+
container_name: 'gitlab-test'
11+
hostname: 'gitlab.test'
12+
privileged: true # Just in case https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/1350
13+
environment:
14+
GITLAB_ROOT_PASSWORD: 5iveL!fe
15+
GITLAB_OMNIBUS_CONFIG: |
16+
external_url 'http://127.0.0.1:8080'
17+
registry['enable'] = false
18+
nginx['redirect_http_to_https'] = false
19+
nginx['listen_port'] = 80
20+
nginx['listen_https'] = false
21+
pages_external_url 'http://pages.gitlab.lxd'
22+
gitlab_pages['enable'] = true
23+
gitlab_pages['inplace_chroot'] = true
24+
prometheus['enable'] = false
25+
alertmanager['enable'] = false
26+
node_exporter['enable'] = false
27+
redis_exporter['enable'] = false
28+
postgres_exporter['enable'] = false
29+
pgbouncer_exporter['enable'] = false
30+
gitlab_exporter['enable'] = false
31+
letsencrypt['enable'] = false
32+
gitlab_rails['initial_license_file'] = '/python-gitlab-ci.gitlab-license'
33+
gitlab_rails['monitoring_whitelist'] = ['0.0.0.0/0']
34+
entrypoint:
35+
- /bin/sh
36+
- -c
37+
- ruby /create_license.rb && /assets/wrapper
38+
volumes:
39+
- ./create_license.rb:/create_license.rb
40+
ports:
41+
- '8080:80'
42+
- '2222:22'
43+
networks:
44+
- gitlab-network
45+
46+
gitlab-runner:
47+
image: '${GITLAB_RUNNER_IMAGE:-gitlab/gitlab-runner}:${GITLAB_RUNNER_TAG:-latest}'
48+
container_name: 'gitlab-runner-test'
49+
depends_on:
50+
- gitlab
51+
networks:
52+
- gitlab-network

gitlab/testing/docker/docker.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
pytest-docker fixture overrides.
3+
See https://github.com/avast/pytest-docker#available-fixtures.
4+
"""
5+
6+
import pytest
7+
import pytest_docker
8+
9+
10+
@pytest.fixture(scope="session")
11+
def docker_compose_project_name():
12+
"""Set a consistent project name to enable optional reuse of containers."""
13+
return "pytest-python-gitlab"
14+
15+
16+
pytest_docker.docker_compose_project_name = docker_compose_project_name
17+
18+
19+
@pytest.fixture(scope="session")
20+
def docker_compose_file(fixture_dir):
21+
return fixture_dir / "docker-compose.yml"
22+
23+
24+
pytest_docker.docker_compose_file = docker_compose_file
25+
26+
27+
@pytest.fixture(scope="session")
28+
def docker_cleanup(request):
29+
"""Conditionally keep containers around by overriding the cleanup command."""
30+
if request.config.getoption("--keep-containers-running"):
31+
# Print version and exit.
32+
return "-v"
33+
if request.config.getoption("--keep-containers"):
34+
# Stop the containers.
35+
return "stop"
36+
return "down"
37+
38+
39+
pytest_docker.docker_cleanup = docker_cleanup
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apt-get update
2+
apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common
3+
curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
4+
apt-get update
5+
echo \
6+
"deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu \
7+
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
8+
tee /etc/apt/sources.list.d/docker.list > /dev/null
9+
apt-get update
10+
apt-get install -y docker-ce docker-compose
11+
usermod -aG docker gitlab-runner
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[test]
2+
url = https://gitlab.com
3+
private_token = abc123
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[test]
2+
api_version = 3
3+
url = https://gitlab.example.com

0 commit comments

Comments
 (0)