2022-01-18 14:54:14 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import unittest
|
2022-03-30 22:15:01 +00:00
|
|
|
from unittest.mock import patch, MagicMock
|
2023-01-03 14:23:19 +00:00
|
|
|
from pathlib import Path
|
2022-01-18 14:54:14 +00:00
|
|
|
|
2022-03-24 14:40:52 +00:00
|
|
|
from env_helper import GITHUB_RUN_URL
|
2022-01-18 14:54:14 +00:00
|
|
|
from pr_info import PRInfo
|
2023-01-03 14:23:19 +00:00
|
|
|
from report import TestResult
|
2022-01-18 14:54:14 +00:00
|
|
|
import docker_images_check as di
|
|
|
|
|
2023-06-15 13:11:30 +00:00
|
|
|
from version_helper import get_version_from_string
|
|
|
|
import docker_server as ds
|
2022-03-30 22:15:01 +00:00
|
|
|
|
2022-01-18 20:09:39 +00:00
|
|
|
# di.logging.basicConfig(level=di.logging.INFO)
|
|
|
|
|
2022-01-18 14:54:14 +00:00
|
|
|
|
|
|
|
class TestDockerImageCheck(unittest.TestCase):
|
|
|
|
docker_images_path = os.path.join(
|
|
|
|
os.path.dirname(__file__), "tests/docker_images.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_changed_docker_images(self):
|
2022-01-18 20:09:39 +00:00
|
|
|
pr_info = PRInfo(PRInfo.default_event.copy())
|
2022-01-18 14:54:14 +00:00
|
|
|
pr_info.changed_files = {
|
|
|
|
"docker/test/stateless",
|
|
|
|
"docker/test/base",
|
|
|
|
"docker/docs/builder",
|
|
|
|
}
|
2022-01-25 11:09:10 +00:00
|
|
|
images = sorted(
|
2022-02-10 10:27:30 +00:00
|
|
|
list(
|
|
|
|
di.get_changed_docker_images(
|
|
|
|
pr_info, di.get_images_dict("/", self.docker_images_path)
|
|
|
|
)
|
|
|
|
)
|
2022-01-25 11:09:10 +00:00
|
|
|
)
|
|
|
|
self.maxDiff = None
|
|
|
|
expected = sorted(
|
|
|
|
[
|
2022-02-10 16:34:55 +00:00
|
|
|
di.DockerImage("docker/test/base", "clickhouse/test-base", False),
|
|
|
|
di.DockerImage("docker/docs/builder", "clickhouse/docs-builder", True),
|
2022-01-25 11:09:10 +00:00
|
|
|
di.DockerImage(
|
|
|
|
"docker/test/stateless",
|
|
|
|
"clickhouse/stateless-test",
|
2022-02-10 16:34:55 +00:00
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/test-base", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
di.DockerImage(
|
|
|
|
"docker/test/integration/base",
|
|
|
|
"clickhouse/integration-test",
|
2022-02-10 16:34:55 +00:00
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/test-base", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
di.DockerImage(
|
2022-02-10 16:34:55 +00:00
|
|
|
"docker/test/fuzzer",
|
|
|
|
"clickhouse/fuzzer",
|
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/test-base", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
di.DockerImage(
|
|
|
|
"docker/test/keeper-jepsen",
|
|
|
|
"clickhouse/keeper-jepsen-test",
|
2022-02-10 16:34:55 +00:00
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/test-base", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
di.DockerImage(
|
|
|
|
"docker/docs/check",
|
|
|
|
"clickhouse/docs-check",
|
2022-02-10 16:34:55 +00:00
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/docs-builder", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
di.DockerImage(
|
|
|
|
"docker/docs/release",
|
|
|
|
"clickhouse/docs-release",
|
2022-02-10 16:34:55 +00:00
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/docs-builder", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
di.DockerImage(
|
|
|
|
"docker/test/stateful",
|
|
|
|
"clickhouse/stateful-test",
|
2022-02-10 16:34:55 +00:00
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/stateless-test", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
di.DockerImage(
|
|
|
|
"docker/test/unit",
|
|
|
|
"clickhouse/unit-test",
|
2022-02-10 16:34:55 +00:00
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/stateless-test", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
di.DockerImage(
|
|
|
|
"docker/test/stress",
|
|
|
|
"clickhouse/stress-test",
|
2022-02-10 16:34:55 +00:00
|
|
|
False,
|
2022-11-10 15:57:01 +00:00
|
|
|
"clickhouse/stateful-test", # type: ignore
|
2022-01-25 11:09:10 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2022-01-18 14:54:14 +00:00
|
|
|
self.assertEqual(images, expected)
|
|
|
|
|
2022-01-18 20:09:39 +00:00
|
|
|
def test_gen_version(self):
|
|
|
|
pr_info = PRInfo(PRInfo.default_event.copy())
|
2022-09-26 11:58:38 +00:00
|
|
|
pr_info.base_ref = "anything-else"
|
2022-09-20 11:41:13 +00:00
|
|
|
versions, result_version = di.gen_versions(pr_info, None)
|
|
|
|
self.assertEqual(versions, ["0", "0-HEAD"])
|
|
|
|
self.assertEqual(result_version, "0-HEAD")
|
2022-09-26 11:58:38 +00:00
|
|
|
pr_info.base_ref = "master"
|
2022-01-18 20:09:39 +00:00
|
|
|
versions, result_version = di.gen_versions(pr_info, None)
|
|
|
|
self.assertEqual(versions, ["latest", "0", "0-HEAD"])
|
|
|
|
self.assertEqual(result_version, "0-HEAD")
|
|
|
|
versions, result_version = di.gen_versions(pr_info, "suffix")
|
|
|
|
self.assertEqual(versions, ["latest-suffix", "0-suffix", "0-HEAD-suffix"])
|
|
|
|
self.assertEqual(result_version, versions)
|
|
|
|
pr_info.number = 1
|
|
|
|
versions, result_version = di.gen_versions(pr_info, None)
|
|
|
|
self.assertEqual(versions, ["1", "1-HEAD"])
|
|
|
|
self.assertEqual(result_version, "1-HEAD")
|
|
|
|
|
2023-02-09 19:36:39 +00:00
|
|
|
@patch("docker_images_check.TeePopen")
|
2022-02-10 16:34:55 +00:00
|
|
|
@patch("platform.machine")
|
2023-02-09 19:36:39 +00:00
|
|
|
def test_build_and_push_one_image(self, mock_machine, mock_popen):
|
2022-01-18 20:09:39 +00:00
|
|
|
mock_popen.return_value.__enter__.return_value.wait.return_value = 0
|
2022-02-10 16:34:55 +00:00
|
|
|
image = di.DockerImage("path", "name", False, gh_repo_path="")
|
2022-01-18 20:09:39 +00:00
|
|
|
|
2023-02-09 20:18:01 +00:00
|
|
|
result, _ = di.build_and_push_one_image(image, "version", [], True, True)
|
2022-01-18 20:09:39 +00:00
|
|
|
mock_popen.assert_called_once()
|
2022-02-10 16:34:55 +00:00
|
|
|
mock_machine.assert_not_called()
|
2022-01-18 20:09:39 +00:00
|
|
|
self.assertIn(
|
2023-07-21 06:31:45 +00:00
|
|
|
"tar -v --exclude-vcs-ignores --show-transformed-names --transform 's#path#./#' --dereference --create path | "
|
2022-03-24 14:40:52 +00:00
|
|
|
f"docker buildx build --builder default --label build-url={GITHUB_RUN_URL} "
|
|
|
|
"--build-arg FROM_TAG=version "
|
2022-06-08 12:25:41 +00:00
|
|
|
f"--build-arg CACHE_INVALIDATOR={GITHUB_RUN_URL} "
|
2022-04-07 14:28:43 +00:00
|
|
|
"--tag name:version --cache-from type=registry,ref=name:version "
|
|
|
|
"--cache-from type=registry,ref=name:latest "
|
2023-07-21 06:31:45 +00:00
|
|
|
"--cache-to type=inline,mode=max --push --progress plain -",
|
2022-01-18 20:09:39 +00:00
|
|
|
mock_popen.call_args.args,
|
|
|
|
)
|
|
|
|
self.assertTrue(result)
|
2022-02-10 16:34:55 +00:00
|
|
|
mock_popen.reset_mock()
|
|
|
|
mock_machine.reset_mock()
|
2022-01-18 20:09:39 +00:00
|
|
|
|
|
|
|
mock_popen.return_value.__enter__.return_value.wait.return_value = 0
|
2023-02-09 20:18:01 +00:00
|
|
|
result, _ = di.build_and_push_one_image(image, "version2", [], False, True)
|
2022-02-10 16:34:55 +00:00
|
|
|
mock_popen.assert_called_once()
|
|
|
|
mock_machine.assert_not_called()
|
2022-01-18 20:09:39 +00:00
|
|
|
self.assertIn(
|
2023-07-21 06:31:45 +00:00
|
|
|
"tar -v --exclude-vcs-ignores --show-transformed-names --transform 's#path#./#' --dereference --create path | "
|
2022-03-24 14:40:52 +00:00
|
|
|
f"docker buildx build --builder default --label build-url={GITHUB_RUN_URL} "
|
|
|
|
"--build-arg FROM_TAG=version2 "
|
2022-06-08 12:25:41 +00:00
|
|
|
f"--build-arg CACHE_INVALIDATOR={GITHUB_RUN_URL} "
|
2022-04-07 14:28:43 +00:00
|
|
|
"--tag name:version2 --cache-from type=registry,ref=name:version2 "
|
|
|
|
"--cache-from type=registry,ref=name:latest "
|
2023-07-21 06:31:45 +00:00
|
|
|
"--cache-to type=inline,mode=max --progress plain -",
|
2022-01-18 20:09:39 +00:00
|
|
|
mock_popen.call_args.args,
|
|
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
|
|
|
2022-02-10 16:34:55 +00:00
|
|
|
mock_popen.reset_mock()
|
|
|
|
mock_machine.reset_mock()
|
2022-01-18 20:09:39 +00:00
|
|
|
mock_popen.return_value.__enter__.return_value.wait.return_value = 1
|
2023-02-09 20:18:01 +00:00
|
|
|
result, _ = di.build_and_push_one_image(image, "version2", [], False, False)
|
2022-02-10 16:34:55 +00:00
|
|
|
mock_popen.assert_called_once()
|
|
|
|
mock_machine.assert_not_called()
|
2022-01-18 20:09:39 +00:00
|
|
|
self.assertIn(
|
2023-07-21 06:31:45 +00:00
|
|
|
"tar -v --exclude-vcs-ignores --show-transformed-names --transform 's#path#./#' --dereference --create path | "
|
2022-03-24 14:40:52 +00:00
|
|
|
f"docker buildx build --builder default --label build-url={GITHUB_RUN_URL} "
|
2022-06-08 12:25:41 +00:00
|
|
|
f"--build-arg CACHE_INVALIDATOR={GITHUB_RUN_URL} "
|
2022-04-07 14:28:43 +00:00
|
|
|
"--tag name:version2 --cache-from type=registry,ref=name:version2 "
|
|
|
|
"--cache-from type=registry,ref=name:latest "
|
2023-07-21 06:31:45 +00:00
|
|
|
"--cache-to type=inline,mode=max --progress plain -",
|
2022-01-18 20:09:39 +00:00
|
|
|
mock_popen.call_args.args,
|
|
|
|
)
|
|
|
|
self.assertFalse(result)
|
|
|
|
|
2022-07-01 14:45:00 +00:00
|
|
|
mock_popen.reset_mock()
|
|
|
|
mock_machine.reset_mock()
|
|
|
|
mock_popen.return_value.__enter__.return_value.wait.return_value = 1
|
|
|
|
result, _ = di.build_and_push_one_image(
|
2023-02-09 20:18:01 +00:00
|
|
|
image, "version2", ["cached-version", "another-cached"], False, False
|
2022-07-01 14:45:00 +00:00
|
|
|
)
|
|
|
|
mock_popen.assert_called_once()
|
|
|
|
mock_machine.assert_not_called()
|
|
|
|
self.assertIn(
|
2023-07-21 06:31:45 +00:00
|
|
|
"tar -v --exclude-vcs-ignores --show-transformed-names --transform 's#path#./#' --dereference --create path | "
|
2022-07-01 14:45:00 +00:00
|
|
|
f"docker buildx build --builder default --label build-url={GITHUB_RUN_URL} "
|
|
|
|
f"--build-arg CACHE_INVALIDATOR={GITHUB_RUN_URL} "
|
|
|
|
"--tag name:version2 --cache-from type=registry,ref=name:version2 "
|
|
|
|
"--cache-from type=registry,ref=name:latest "
|
|
|
|
"--cache-from type=registry,ref=name:cached-version "
|
2023-02-09 20:18:01 +00:00
|
|
|
"--cache-from type=registry,ref=name:another-cached "
|
2023-07-21 06:31:45 +00:00
|
|
|
"--cache-to type=inline,mode=max --progress plain -",
|
2022-07-01 14:45:00 +00:00
|
|
|
mock_popen.call_args.args,
|
|
|
|
)
|
|
|
|
self.assertFalse(result)
|
|
|
|
|
2022-02-10 16:34:55 +00:00
|
|
|
mock_popen.reset_mock()
|
|
|
|
mock_machine.reset_mock()
|
|
|
|
only_amd64_image = di.DockerImage("path", "name", True)
|
|
|
|
mock_popen.return_value.__enter__.return_value.wait.return_value = 0
|
|
|
|
|
2022-07-01 14:45:00 +00:00
|
|
|
result, _ = di.build_and_push_one_image(
|
2023-02-09 20:18:01 +00:00
|
|
|
only_amd64_image, "version", [], True, True
|
2022-07-01 14:45:00 +00:00
|
|
|
)
|
2022-02-10 16:34:55 +00:00
|
|
|
mock_popen.assert_called_once()
|
|
|
|
mock_machine.assert_called_once()
|
|
|
|
self.assertIn(
|
|
|
|
"docker pull ubuntu:20.04; docker tag ubuntu:20.04 name:version; "
|
|
|
|
"docker push name:version",
|
|
|
|
mock_popen.call_args.args,
|
|
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
|
|
result, _ = di.build_and_push_one_image(
|
2023-02-09 20:18:01 +00:00
|
|
|
only_amd64_image, "version", [], False, True
|
2022-02-10 16:34:55 +00:00
|
|
|
)
|
|
|
|
self.assertIn(
|
|
|
|
"docker pull ubuntu:20.04; docker tag ubuntu:20.04 name:version; ",
|
|
|
|
mock_popen.call_args.args,
|
|
|
|
)
|
2023-02-09 20:18:01 +00:00
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
result, _ = di.build_and_push_one_image(image, "version", [""], False, True)
|
2022-02-10 16:34:55 +00:00
|
|
|
|
2022-01-19 10:26:16 +00:00
|
|
|
@patch("docker_images_check.build_and_push_one_image")
|
|
|
|
def test_process_image_with_parents(self, mock_build):
|
2023-01-03 14:23:19 +00:00
|
|
|
mock_build.side_effect = lambda v, w, x, y, z: (True, Path(f"{v.repo}_{w}.log"))
|
2022-02-10 16:34:55 +00:00
|
|
|
im1 = di.DockerImage("path1", "repo1", False)
|
|
|
|
im2 = di.DockerImage("path2", "repo2", False, im1)
|
|
|
|
im3 = di.DockerImage("path3", "repo3", False, im2)
|
|
|
|
im4 = di.DockerImage("path4", "repo4", False, im1)
|
2022-01-19 10:26:16 +00:00
|
|
|
# We use list to have determined order of image builgings
|
|
|
|
images = [im4, im1, im3, im2, im1]
|
2023-01-03 14:23:19 +00:00
|
|
|
test_results = [
|
2023-02-09 20:18:01 +00:00
|
|
|
di.process_image_with_parents(im, ["v1", "v2", "latest"], [], True)
|
2022-01-19 10:26:16 +00:00
|
|
|
for im in images
|
|
|
|
]
|
2023-01-03 14:23:19 +00:00
|
|
|
# The time is random, so we check it's not None and greater than 0,
|
|
|
|
# and then set to 1
|
|
|
|
for results in test_results:
|
|
|
|
for result in results:
|
|
|
|
self.assertIsNotNone(result.time)
|
|
|
|
self.assertGreater(result.time, 0) # type: ignore
|
|
|
|
result.time = 1
|
2022-01-19 10:26:16 +00:00
|
|
|
|
2023-01-03 14:23:19 +00:00
|
|
|
self.maxDiff = None
|
2022-01-19 10:26:16 +00:00
|
|
|
expected = [
|
|
|
|
[ # repo4 -> repo1
|
2023-01-03 14:23:19 +00:00
|
|
|
TestResult("repo1:v1", "OK", 1, [Path("repo1_v1.log")]),
|
|
|
|
TestResult("repo1:v2", "OK", 1, [Path("repo1_v2.log")]),
|
|
|
|
TestResult("repo1:latest", "OK", 1, [Path("repo1_latest.log")]),
|
|
|
|
TestResult("repo4:v1", "OK", 1, [Path("repo4_v1.log")]),
|
|
|
|
TestResult("repo4:v2", "OK", 1, [Path("repo4_v2.log")]),
|
|
|
|
TestResult("repo4:latest", "OK", 1, [Path("repo4_latest.log")]),
|
2022-01-19 10:26:16 +00:00
|
|
|
],
|
|
|
|
[], # repo1 is built
|
|
|
|
[ # repo3 -> repo2 -> repo1
|
2023-01-03 14:23:19 +00:00
|
|
|
TestResult("repo2:v1", "OK", 1, [Path("repo2_v1.log")]),
|
|
|
|
TestResult("repo2:v2", "OK", 1, [Path("repo2_v2.log")]),
|
|
|
|
TestResult("repo2:latest", "OK", 1, [Path("repo2_latest.log")]),
|
|
|
|
TestResult("repo3:v1", "OK", 1, [Path("repo3_v1.log")]),
|
|
|
|
TestResult("repo3:v2", "OK", 1, [Path("repo3_v2.log")]),
|
|
|
|
TestResult("repo3:latest", "OK", 1, [Path("repo3_latest.log")]),
|
2022-01-19 10:26:16 +00:00
|
|
|
],
|
|
|
|
[], # repo2 -> repo1 are built
|
|
|
|
[], # repo1 is built
|
|
|
|
]
|
2023-01-03 14:23:19 +00:00
|
|
|
self.assertEqual(test_results, expected)
|
2022-01-19 10:26:16 +00:00
|
|
|
|
2022-01-18 14:54:14 +00:00
|
|
|
|
2022-03-30 22:15:01 +00:00
|
|
|
class TestDockerServer(unittest.TestCase):
|
|
|
|
def test_gen_tags(self):
|
2022-04-01 15:53:43 +00:00
|
|
|
version = get_version_from_string("22.2.2.2")
|
2022-03-30 22:15:01 +00:00
|
|
|
cases = (
|
2022-04-01 15:53:43 +00:00
|
|
|
("latest", ["latest", "22", "22.2", "22.2.2", "22.2.2.2"]),
|
|
|
|
("major", ["22", "22.2", "22.2.2", "22.2.2.2"]),
|
|
|
|
("minor", ["22.2", "22.2.2", "22.2.2.2"]),
|
|
|
|
("patch", ["22.2.2", "22.2.2.2"]),
|
|
|
|
("head", ["head"]),
|
2022-03-30 22:15:01 +00:00
|
|
|
)
|
|
|
|
for case in cases:
|
2022-04-01 15:53:43 +00:00
|
|
|
release_type = case[0]
|
2022-03-30 22:15:01 +00:00
|
|
|
self.assertEqual(case[1], ds.gen_tags(version, release_type))
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
2022-04-01 15:53:43 +00:00
|
|
|
ds.gen_tags(version, "auto")
|
2022-03-30 22:15:01 +00:00
|
|
|
|
|
|
|
@patch("docker_server.get_tagged_versions")
|
2022-11-10 15:57:01 +00:00
|
|
|
def test_auto_release_type(self, mock_tagged_versions: MagicMock) -> None:
|
2022-03-30 22:15:01 +00:00
|
|
|
mock_tagged_versions.return_value = [
|
|
|
|
get_version_from_string("1.1.1.1"),
|
|
|
|
get_version_from_string("1.2.1.1"),
|
|
|
|
get_version_from_string("2.1.1.1"),
|
|
|
|
get_version_from_string("2.2.1.1"),
|
|
|
|
get_version_from_string("2.2.2.1"),
|
|
|
|
]
|
2022-04-06 16:41:18 +00:00
|
|
|
|
|
|
|
cases_less = (
|
2022-04-01 15:53:43 +00:00
|
|
|
(get_version_from_string("1.0.1.1"), "minor"),
|
|
|
|
(get_version_from_string("1.1.2.1"), "minor"),
|
|
|
|
(get_version_from_string("1.3.1.1"), "major"),
|
|
|
|
(get_version_from_string("2.1.2.1"), "minor"),
|
|
|
|
(get_version_from_string("2.2.1.3"), "patch"),
|
|
|
|
(get_version_from_string("2.2.3.1"), "latest"),
|
|
|
|
(get_version_from_string("2.3.1.1"), "latest"),
|
2022-03-30 22:15:01 +00:00
|
|
|
)
|
2022-04-06 16:41:18 +00:00
|
|
|
for case in cases_less:
|
|
|
|
release = ds.auto_release_type(case[0], "auto")
|
|
|
|
self.assertEqual(case[1], release)
|
|
|
|
|
|
|
|
cases_equal = (
|
|
|
|
(get_version_from_string("1.1.1.1"), "minor"),
|
|
|
|
(get_version_from_string("1.2.1.1"), "major"),
|
|
|
|
(get_version_from_string("2.1.1.1"), "minor"),
|
|
|
|
(get_version_from_string("2.2.1.1"), "patch"),
|
|
|
|
(get_version_from_string("2.2.2.1"), "latest"),
|
|
|
|
)
|
|
|
|
for case in cases_equal:
|
2022-03-30 22:15:01 +00:00
|
|
|
release = ds.auto_release_type(case[0], "auto")
|
|
|
|
self.assertEqual(case[1], release)
|