| Giorgi Lekveishvili | 285ab62 | 2023-11-22 13:50:45 +0400 | [diff] [blame^] | 1 | # pylint: disable=W0613 |
| 2 | |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import random |
| 18 | import string |
| 19 | import time |
| 20 | |
| 21 | import pytest |
| 22 | |
| 23 | |
| 24 | class GitBackendContainer: |
| 25 | def __init__(self, docker_client, image, port, credentials_dir): |
| 26 | self.docker_client = docker_client |
| 27 | self.image = image |
| 28 | self.port = port |
| 29 | self.apache_credentials_dir = credentials_dir |
| 30 | |
| 31 | self.container = None |
| 32 | |
| 33 | def start(self): |
| 34 | self.container = self.docker_client.containers.run( |
| 35 | image=self.image.id, |
| 36 | ports={"80": self.port}, |
| 37 | volumes={ |
| 38 | self.apache_credentials_dir: { |
| 39 | "bind": "/var/apache/credentials", |
| 40 | "mode": "ro", |
| 41 | } |
| 42 | }, |
| 43 | detach=True, |
| 44 | auto_remove=True, |
| 45 | platform="linux/amd64", |
| 46 | ) |
| 47 | |
| 48 | def stop(self): |
| 49 | self.container.stop(timeout=1) |
| 50 | |
| 51 | |
| 52 | @pytest.fixture(scope="module") |
| 53 | def container_run_factory( |
| 54 | docker_client, apache_git_http_backend_image, htpasswd, credentials_dir |
| 55 | ): |
| 56 | def run_container(port): |
| 57 | return GitBackendContainer( |
| 58 | docker_client, |
| 59 | apache_git_http_backend_image, |
| 60 | port, |
| 61 | str(credentials_dir), |
| 62 | ) |
| 63 | |
| 64 | return run_container |
| 65 | |
| 66 | |
| 67 | @pytest.fixture(scope="module") |
| 68 | def container_run(container_run_factory, free_port): |
| 69 | test_setup = container_run_factory(free_port) |
| 70 | test_setup.start() |
| 71 | time.sleep(3) |
| 72 | |
| 73 | yield test_setup |
| 74 | |
| 75 | test_setup.stop() |
| 76 | |
| 77 | |
| 78 | @pytest.fixture(scope="module") |
| 79 | def base_url(container_run): |
| 80 | return f"http://localhost:{container_run.port}" |
| 81 | |
| 82 | |
| 83 | @pytest.fixture(scope="function") |
| 84 | def random_repo_name(): |
| 85 | return "".join( |
| 86 | [random.choice(string.ascii_letters + string.digits) for n in range(8)] |
| 87 | ) |
| 88 | |
| 89 | |
| 90 | @pytest.fixture(scope="function") |
| 91 | def repo_creation_url(base_url, random_repo_name): |
| 92 | return f"{base_url}/a/projects/{random_repo_name}" |