Skip to content
Draft
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
Empty file added gitlab/testing/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions gitlab/testing/docker/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GITLAB_IMAGE=gitlab/gitlab-ee
GITLAB_TAG=18.7.0-ee.0
GITLAB_RUNNER_IMAGE=gitlab/gitlab-runner
GITLAB_RUNNER_TAG=96856197
Empty file.
51 changes: 51 additions & 0 deletions gitlab/testing/docker/create_license.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# NOTE: As of 2022-06-01 the GitLab Enterprise Edition License has the following
# section:
# Notwithstanding the foregoing, you may copy and modify the Software for development
# and testing purposes, without requiring a subscription.
#
# https://gitlab.com/gitlab-org/gitlab/-/blob/29503bc97b96af8d4876dc23fc8996e3dab7d211/ee/LICENSE
#
# This code is strictly intended for use in the testing framework of python-gitlab

# Code inspired by MIT licensed code at: https://github.com/CONIGUERO/gitlab-license.git

require 'openssl'
require 'gitlab/license'

# Generate a 2048 bit key pair.
license_encryption_key = OpenSSL::PKey::RSA.generate(2048)

# Save the private key
File.open("/.license_encryption_key", "w") { |f| f.write(license_encryption_key.to_pem) }
# Save the public key
public_key = license_encryption_key.public_key
File.open("/.license_encryption_key.pub", "w") { |f| f.write(public_key.to_pem) }
File.open("/opt/gitlab/embedded/service/gitlab-rails/.license_encryption_key.pub", "w") { |f| f.write(public_key.to_pem) }

Gitlab::License.encryption_key = license_encryption_key

# Build a new license.
license = Gitlab::License.new

license.licensee = {
"Name" => "python-gitlab-ci",
"Company" => "python-gitlab-ci",
"Email" => "[email protected]",
}

# The date the license starts.
license.starts_at = Date.today
# Want to make sure we get at least 1 day of usage. Do two days after because if CI
# started at 23:59 we could be expired in one minute if we only did one next_day.
license.expires_at = Date.today.next_day.next_day

# Use 'ultimate' plan so that we can test all features in the CI
license.restrictions = {
:plan => "ultimate",
:id => rand(1000..99999999)
}

# Export the license, which encrypts and encodes it.
data = license.export

File.open("/python-gitlab-ci.gitlab-license", 'w') { |file| file.write(data) }
52 changes: 52 additions & 0 deletions gitlab/testing/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
version: '3.5'

networks:
gitlab-network:
name: gitlab-network

services:
gitlab:
image: '${GITLAB_IMAGE:-gitlab/gitlab-ee}:${GITLAB_TAG:-latest}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe extract the preparation changes (everything that can be applied without moving the plugin) into a separate PR that we merge first? Just so we can focus on integration aspects here. Let me know otherwise I can also try that on my end.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize there was so much drift between the fixtures in pytest-gitlab and python-gitlab fixtures.

To hopefully simplify things, I've redone this PR using the current python-gitlab fixtures as the starting point. It adds the pytest-gitlab plugin but stops short of having the existing tests use any of it. A follow up PR can switch the tests over.

Hopefully this makes more sense. Let me know.

container_name: 'gitlab-test'
hostname: 'gitlab.test'
privileged: true # Just in case https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/1350
environment:
GITLAB_ROOT_PASSWORD: 5iveL!fe
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://127.0.0.1:8080'
registry['enable'] = false
nginx['redirect_http_to_https'] = false
nginx['listen_port'] = 80
nginx['listen_https'] = false
pages_external_url 'http://pages.gitlab.lxd'
gitlab_pages['enable'] = true
gitlab_pages['inplace_chroot'] = true
prometheus['enable'] = false
alertmanager['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
pgbouncer_exporter['enable'] = false
gitlab_exporter['enable'] = false
letsencrypt['enable'] = false
gitlab_rails['initial_license_file'] = '/python-gitlab-ci.gitlab-license'
gitlab_rails['monitoring_whitelist'] = ['0.0.0.0/0']
entrypoint:
- /bin/sh
- -c
- ruby /create_license.rb && /assets/init-container
volumes:
- ${PWD}/tests/functional/fixtures/create_license.rb:/create_license.rb
ports:
- '8080:80'
- '2222:22'
networks:
- gitlab-network

gitlab-runner:
image: '${GITLAB_RUNNER_IMAGE:-gitlab/gitlab-runner}:${GITLAB_RUNNER_TAG:-latest}'
container_name: 'gitlab-runner-test'
depends_on:
- gitlab
networks:
- gitlab-network
26 changes: 26 additions & 0 deletions gitlab/testing/docker/docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
pytest-docker fixture overrides.
See https://github.com/avast/pytest-docker#available-fixtures.
"""

import pytest

Check warning on line 6 in gitlab/testing/docker/docker.py

View check run for this annotation

Codecov / codecov/patch

gitlab/testing/docker/docker.py#L6

Added line #L6 was not covered by tests


@pytest.fixture(scope="session")
def docker_compose_project_name():

Check warning on line 10 in gitlab/testing/docker/docker.py

View check run for this annotation

Codecov / codecov/patch

gitlab/testing/docker/docker.py#L9-L10

Added lines #L9 - L10 were not covered by tests
"""Set a consistent project name to enable optional reuse of containers."""
return "pytest-python-gitlab"

Check warning on line 12 in gitlab/testing/docker/docker.py

View check run for this annotation

Codecov / codecov/patch

gitlab/testing/docker/docker.py#L12

Added line #L12 was not covered by tests


@pytest.fixture(scope="session")
def docker_compose_file(docker_assets_dir):
return docker_assets_dir / "docker-compose.yml"

Check warning on line 17 in gitlab/testing/docker/docker.py

View check run for this annotation

Codecov / codecov/patch

gitlab/testing/docker/docker.py#L15-L17

Added lines #L15 - L17 were not covered by tests


@pytest.fixture(scope="session")
def docker_cleanup(request):

Check warning on line 21 in gitlab/testing/docker/docker.py

View check run for this annotation

Codecov / codecov/patch

gitlab/testing/docker/docker.py#L20-L21

Added lines #L20 - L21 were not covered by tests
"""Conditionally keep containers around by overriding the cleanup command."""
if request.config.getoption("--keep-containers"):

Check warning on line 23 in gitlab/testing/docker/docker.py

View check run for this annotation

Codecov / codecov/patch

gitlab/testing/docker/docker.py#L23

Added line #L23 was not covered by tests
# Print version and exit.
return "-v"
return "down -v"

Check warning on line 26 in gitlab/testing/docker/docker.py

View check run for this annotation

Codecov / codecov/patch

gitlab/testing/docker/docker.py#L25-L26

Added lines #L25 - L26 were not covered by tests
11 changes: 11 additions & 0 deletions gitlab/testing/docker/install_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apt-get update
apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
apt-get update
echo \
"deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-compose
usermod -aG docker gitlab-runner
9 changes: 9 additions & 0 deletions gitlab/testing/docker/set_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#programmatically-creating-a-personal-access-token

user = User.find_by_username('root')

token = user.personal_access_tokens.first_or_create(scopes: ['api', 'sudo'], name: 'default', expires_at: 365.days.from_now);
token.set_token('glpat-python-gitlab-token_');
token.save!

puts token.token
Empty file.
Loading