| Giorgi Lekveishvili | 285ab62 | 2023-11-22 13:50:45 +0400 | [diff] [blame] | 1 | # Copyright (C) 2018 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os.path |
| 16 | import socket |
| 17 | |
| 18 | import pytest |
| 19 | |
| 20 | |
| 21 | class GerritContainer: |
| 22 | def __init__(self, docker_client, docker_network, tmp_dir, image, configs, port): |
| 23 | self.docker_client = docker_client |
| 24 | self.docker_network = docker_network |
| 25 | self.tmp_dir = tmp_dir |
| 26 | self.image = image |
| 27 | self.configs = configs |
| 28 | self.port = port |
| 29 | |
| 30 | self.container = None |
| 31 | |
| 32 | def _create_config_files(self): |
| 33 | tmp_config_dir = os.path.join(self.tmp_dir, "configs") |
| 34 | if not os.path.isdir(tmp_config_dir): |
| 35 | os.mkdir(tmp_config_dir) |
| 36 | config_paths = {} |
| 37 | for filename, content in self.configs.items(): |
| 38 | gerrit_config_file = os.path.join(tmp_config_dir, filename) |
| 39 | with open(gerrit_config_file, "w", encoding="utf-8") as config_file: |
| 40 | config_file.write(content) |
| 41 | config_paths[filename] = gerrit_config_file |
| 42 | return config_paths |
| 43 | |
| 44 | def _define_volume_mounts(self): |
| 45 | volumes = { |
| 46 | v: {"bind": f"/var/gerrit/etc/{k}", "mode": "rw"} |
| 47 | for (k, v) in self._create_config_files().items() |
| 48 | } |
| 49 | volumes[os.path.join(self.tmp_dir, "lib")] = { |
| 50 | "bind": "/var/gerrit/lib", |
| 51 | "mode": "rw", |
| 52 | } |
| 53 | return volumes |
| 54 | |
| 55 | def start(self): |
| 56 | self.container = self.docker_client.containers.run( |
| 57 | image=self.image.id, |
| 58 | user="gerrit", |
| 59 | volumes=self._define_volume_mounts(), |
| 60 | ports={8080: str(self.port)}, |
| 61 | network=self.docker_network.name, |
| 62 | detach=True, |
| 63 | auto_remove=True, |
| 64 | platform="linux/amd64", |
| 65 | ) |
| 66 | |
| 67 | def stop(self): |
| 68 | self.container.stop(timeout=1) |
| 69 | |
| 70 | |
| 71 | @pytest.fixture(scope="session") |
| 72 | def gerrit_container_factory(): |
| 73 | def get_gerrit_container( |
| 74 | docker_client, docker_network, tmp_dir, image, gerrit_config, port |
| 75 | ): |
| 76 | return GerritContainer( |
| 77 | docker_client, docker_network, tmp_dir, image, gerrit_config, port |
| 78 | ) |
| 79 | |
| 80 | return get_gerrit_container |
| 81 | |
| 82 | |
| 83 | @pytest.fixture(scope="session") |
| 84 | def container_endless_run_factory(): |
| 85 | def get_container(docker_client, image): |
| 86 | return docker_client.containers.run( |
| 87 | image=image.id, |
| 88 | entrypoint="/bin/ash", |
| 89 | command=["-c", "tail -f /dev/null"], |
| 90 | user="gerrit", |
| 91 | detach=True, |
| 92 | auto_remove=True, |
| 93 | platform="linux/amd64", |
| 94 | ) |
| 95 | |
| 96 | return get_container |
| 97 | |
| 98 | |
| 99 | @pytest.fixture(scope="session") |
| 100 | def free_port(): |
| 101 | skt = socket.socket() |
| 102 | skt.bind(("", 0)) |
| 103 | port = skt.getsockname()[1] |
| 104 | skt.close() |
| 105 | return port |