|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import traceback |
| 4 | +from dataclasses import dataclass |
| 5 | + |
| 6 | +from invoke import Context, task |
| 7 | +from invoke.exceptions import Exit |
| 8 | + |
| 9 | +from tasks.build_tags import get_default_build_tags |
| 10 | +from tasks.libs.common.utils import TestsNotSupportedError, gitlab_section |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class IntegrationTest: |
| 15 | + """ |
| 16 | + Integration tests |
| 17 | + """ |
| 18 | + |
| 19 | + prefix: str |
| 20 | + dir: str = None |
| 21 | + extra_args: str = None |
| 22 | + |
| 23 | + |
| 24 | +@dataclass |
| 25 | +class IntegrationTestsConfig: |
| 26 | + """ |
| 27 | + Integration tests configuration |
| 28 | + """ |
| 29 | + |
| 30 | + name: str |
| 31 | + go_build_tags: list[str] |
| 32 | + tests: list[IntegrationTest] |
| 33 | + env: dict[str, str] = None |
| 34 | + is_windows_supported: bool = True |
| 35 | + |
| 36 | + |
| 37 | +CORE_AGENT_LINUX_IT_CONF = IntegrationTestsConfig( |
| 38 | + name="Core Agent Linux", |
| 39 | + go_build_tags=get_default_build_tags(build="test"), |
| 40 | + tests=[ |
| 41 | + IntegrationTest(prefix="./test/integration/config_providers/..."), |
| 42 | + IntegrationTest(prefix="./test/integration/corechecks/..."), |
| 43 | + IntegrationTest(prefix="./test/integration/listeners/..."), |
| 44 | + IntegrationTest(prefix="./test/integration/util/kubelet/..."), |
| 45 | + ], |
| 46 | + is_windows_supported=False, |
| 47 | +) |
| 48 | + |
| 49 | +CORE_AGENT_WINDOWS_IT_CONF = IntegrationTestsConfig( |
| 50 | + name="Core Agent Windows", |
| 51 | + go_build_tags=get_default_build_tags(build="test"), |
| 52 | + tests=[ |
| 53 | + # Run eventlog tests with the Windows API, which depend on the EventLog service |
| 54 | + IntegrationTest(dir="./pkg/util/winutil/", prefix="./eventlog/...", extra_args="-evtapi Windows"), |
| 55 | + # Run eventlog tailer tests with the Windows API, which depend on the EventLog service |
| 56 | + IntegrationTest(dir=".", prefix="./pkg/logs/tailers/windowsevent/...", extra_args="-evtapi Windows"), |
| 57 | + # Run eventlog check tests with the Windows API, which depend on the EventLog service |
| 58 | + # Don't include submodules, since the `-evtapi` flag is not defined in them |
| 59 | + IntegrationTest( |
| 60 | + dir=".", prefix="./comp/checks/windowseventlog/windowseventlogimpl/check", extra_args="-evtapi Windows" |
| 61 | + ), |
| 62 | + ], |
| 63 | +) |
| 64 | + |
| 65 | +DOGSTATSD_IT_CONF = IntegrationTestsConfig( |
| 66 | + name="DogStatsD", |
| 67 | + go_build_tags=get_default_build_tags(build="test"), |
| 68 | + tests=[IntegrationTest(prefix="./test/integration/dogstatsd/...")], |
| 69 | + is_windows_supported=False, |
| 70 | +) |
| 71 | + |
| 72 | +CLUSTER_AGENT_IT_CONF = IntegrationTestsConfig( |
| 73 | + name="Cluster Agent", |
| 74 | + go_build_tags=get_default_build_tags(build="cluster-agent") + ["docker", "test"], |
| 75 | + tests=[ |
| 76 | + IntegrationTest(prefix="./test/integration/util/kube_apiserver"), |
| 77 | + IntegrationTest(prefix="./test/integration/util/leaderelection"), |
| 78 | + ], |
| 79 | + is_windows_supported=False, |
| 80 | +) |
| 81 | + |
| 82 | +TRACE_AGENT_IT_CONF = IntegrationTestsConfig( |
| 83 | + name="Trace Agent", |
| 84 | + go_build_tags=get_default_build_tags(build="test"), |
| 85 | + tests=[IntegrationTest(prefix="./cmd/trace-agent/test/testsuite/...")], |
| 86 | + env={"INTEGRATION": "yes"}, |
| 87 | + is_windows_supported=False, |
| 88 | +) |
| 89 | + |
| 90 | + |
| 91 | +def containerized_integration_tests( |
| 92 | + ctx: Context, |
| 93 | + integration_tests_config: IntegrationTestsConfig, |
| 94 | + race=False, |
| 95 | + remote_docker=False, |
| 96 | + go_mod="readonly", |
| 97 | + timeout="", |
| 98 | +): |
| 99 | + if sys.platform == 'win32' and not integration_tests_config.is_windows_supported: |
| 100 | + raise TestsNotSupportedError(f'{integration_tests_config.name} integration tests are not supported on Windows') |
| 101 | + test_args = { |
| 102 | + "go_mod": go_mod, |
| 103 | + "go_build_tags": " ".join(integration_tests_config.go_build_tags), |
| 104 | + "race_opt": "-race" if race else "", |
| 105 | + "exec_opts": "", |
| 106 | + "timeout_opt": f"-timeout {timeout}" if timeout else "", |
| 107 | + } |
| 108 | + |
| 109 | + # since Go 1.13, the -exec flag of go test could add some parameters such as -test.timeout |
| 110 | + # to the call, we don't want them because while calling invoke below, invoke |
| 111 | + # thinks that the parameters are for it to interpret. |
| 112 | + # we're calling an intermediate script which only pass the binary name to the invoke task. |
| 113 | + if remote_docker: |
| 114 | + test_args["exec_opts"] = f"-exec \"{os.getcwd()}/test/integration/dockerize_tests.sh\"" |
| 115 | + |
| 116 | + go_cmd = 'go test {timeout_opt} -mod={go_mod} {race_opt} -tags "{go_build_tags}" {exec_opts}'.format(**test_args) # noqa: FS002 |
| 117 | + |
| 118 | + for it in integration_tests_config.tests: |
| 119 | + if it.dir: |
| 120 | + with ctx.cd(f"{it.dir}"): |
| 121 | + ctx.run(f"{go_cmd} {it.prefix}", env=integration_tests_config.env) |
| 122 | + else: |
| 123 | + ctx.run(f"{go_cmd} {it.prefix}", env=integration_tests_config.env) |
| 124 | + |
| 125 | + |
| 126 | +@task |
| 127 | +def integration_tests(ctx, race=False, remote_docker=False, timeout=""): |
| 128 | + """ |
| 129 | + Run all the available integration tests |
| 130 | + """ |
| 131 | + core_agent_conf = CORE_AGENT_WINDOWS_IT_CONF if sys.platform == 'win32' else CORE_AGENT_LINUX_IT_CONF |
| 132 | + tests = { |
| 133 | + "Agent Core": lambda: containerized_integration_tests( |
| 134 | + ctx, core_agent_conf, race=race, remote_docker=remote_docker, timeout=timeout |
| 135 | + ), |
| 136 | + "DogStatsD": lambda: containerized_integration_tests( |
| 137 | + ctx, DOGSTATSD_IT_CONF, race=race, remote_docker=remote_docker, timeout=timeout |
| 138 | + ), |
| 139 | + "Cluster Agent": lambda: containerized_integration_tests( |
| 140 | + ctx, CLUSTER_AGENT_IT_CONF, race=race, remote_docker=remote_docker, timeout=timeout |
| 141 | + ), |
| 142 | + "Trace Agent": lambda: containerized_integration_tests(ctx, TRACE_AGENT_IT_CONF, race=race, timeout=timeout), |
| 143 | + } |
| 144 | + tests_failures = {} |
| 145 | + for t_name, t in tests.items(): |
| 146 | + with gitlab_section(f"Running the {t_name} integration tests", collapsed=True, echo=True): |
| 147 | + try: |
| 148 | + t() |
| 149 | + except TestsNotSupportedError as e: |
| 150 | + print(f"Skipping {t_name}: {e}") |
| 151 | + except Exception: |
| 152 | + # Keep printing the traceback not to have to wait until all tests are done to see what failed |
| 153 | + traceback.print_exc() |
| 154 | + # Storing the traceback to print it at the end without directly raising the exception |
| 155 | + tests_failures[t_name] = traceback.format_exc() |
| 156 | + if tests_failures: |
| 157 | + print("The following integration tests failed:") |
| 158 | + for t_name in tests_failures: |
| 159 | + print(f"- {t_name}") |
| 160 | + print("See the above logs to get the full traceback.") |
| 161 | + raise Exit(code=1) |
0 commit comments