Skip to content

Commit 5cde5c4

Browse files
authored
[CI] Refactor the Agent integration tests (#31801)
1 parent f579490 commit 5cde5c4

File tree

7 files changed

+208
-172
lines changed

7 files changed

+208
-172
lines changed

tasks/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@
8787
tidy,
8888
tidy_all,
8989
)
90+
from tasks.gointegrationtest import integration_tests
9091
from tasks.gotest import (
9192
check_otel_build,
9293
check_otel_module_versions,
9394
e2e_tests,
9495
get_impacted_packages,
9596
get_modified_packages,
96-
integration_tests,
9797
lint_go,
9898
send_unit_tests_stats,
9999
test,

tasks/agent.py

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
from tasks.build_tags import add_fips_tags, filter_incompatible_tags, get_build_tags, get_default_build_tags
1818
from tasks.devcontainer import run_on_devcontainer
1919
from tasks.flavor import AgentFlavor
20+
from tasks.gointegrationtest import (
21+
CORE_AGENT_LINUX_IT_CONF,
22+
CORE_AGENT_WINDOWS_IT_CONF,
23+
containerized_integration_tests,
24+
)
2025
from tasks.libs.common.utils import (
2126
REPO_PATH,
2227
bin_name,
@@ -578,79 +583,18 @@ def integration_tests(ctx, race=False, remote_docker=False, go_mod="readonly", t
578583
"""
579584
Run integration tests for the Agent
580585
"""
581-
582586
if sys.platform == 'win32':
583-
return _windows_integration_tests(ctx, race=race, go_mod=go_mod, timeout=timeout)
584-
else:
585-
# TODO: See if these will function on Windows
586-
return _linux_integration_tests(ctx, race=race, remote_docker=remote_docker, go_mod=go_mod, timeout=timeout)
587-
588-
589-
def _windows_integration_tests(ctx, race=False, go_mod="readonly", timeout=""):
590-
test_args = {
591-
"go_mod": go_mod,
592-
"go_build_tags": " ".join(get_default_build_tags(build="test")),
593-
"race_opt": "-race" if race else "",
594-
"exec_opts": "",
595-
"timeout_opt": f"-timeout {timeout}" if timeout else "",
596-
}
597-
598-
go_cmd = 'go test {timeout_opt} -mod={go_mod} {race_opt} -tags "{go_build_tags}" {exec_opts}'.format(**test_args) # noqa: FS002
599-
600-
tests = [
601-
{
602-
# Run eventlog tests with the Windows API, which depend on the EventLog service
603-
"dir": "./pkg/util/winutil/",
604-
'prefix': './eventlog/...',
605-
'extra_args': '-evtapi Windows',
606-
},
607-
{
608-
# Run eventlog tailer tests with the Windows API, which depend on the EventLog service
609-
"dir": ".",
610-
'prefix': './pkg/logs/tailers/windowsevent/...',
611-
'extra_args': '-evtapi Windows',
612-
},
613-
{
614-
# Run eventlog check tests with the Windows API, which depend on the EventLog service
615-
"dir": ".",
616-
# Don't include submodules, since the `-evtapi` flag is not defined in them
617-
'prefix': './comp/checks/windowseventlog/windowseventlogimpl/check',
618-
'extra_args': '-evtapi Windows',
619-
},
620-
]
621-
622-
for test in tests:
623-
with ctx.cd(f"{test['dir']}"):
624-
ctx.run(f"{go_cmd} {test['prefix']} {test['extra_args']}")
625-
626-
627-
def _linux_integration_tests(ctx, race=False, remote_docker=False, go_mod="readonly", timeout=""):
628-
test_args = {
629-
"go_mod": go_mod,
630-
"go_build_tags": " ".join(get_default_build_tags(build="test")),
631-
"race_opt": "-race" if race else "",
632-
"exec_opts": "",
633-
"timeout_opt": f"-timeout {timeout}" if timeout else "",
634-
}
635-
636-
# since Go 1.13, the -exec flag of go test could add some parameters such as -test.timeout
637-
# to the call, we don't want them because while calling invoke below, invoke
638-
# thinks that the parameters are for it to interpret.
639-
# we're calling an intermediate script which only pass the binary name to the invoke task.
640-
if remote_docker:
641-
test_args["exec_opts"] = f"-exec \"{os.getcwd()}/test/integration/dockerize_tests.sh\""
642-
643-
go_cmd = 'go test {timeout_opt} -mod={go_mod} {race_opt} -tags "{go_build_tags}" {exec_opts}'.format(**test_args) # noqa: FS002
644-
645-
prefixes = [
646-
"./test/integration/config_providers/...",
647-
"./test/integration/corechecks/...",
648-
"./test/integration/listeners/...",
649-
"./test/integration/util/kubelet/...",
650-
]
651-
652-
for prefix in prefixes:
653-
ctx.run(f"{go_cmd} {prefix}")
587+
return containerized_integration_tests(
588+
ctx, CORE_AGENT_WINDOWS_IT_CONF, race=race, go_mod=go_mod, timeout=timeout
589+
)
590+
return containerized_integration_tests(
591+
ctx,
592+
CORE_AGENT_LINUX_IT_CONF,
593+
race=race,
594+
remote_docker=remote_docker,
595+
go_mod=go_mod,
596+
timeout=timeout,
597+
)
654598

655599

656600
def check_supports_python_version(check_dir, python):

tasks/cluster_agent.py

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from invoke import task
1313
from invoke.exceptions import Exit
1414

15-
from tasks.build_tags import get_build_tags, get_default_build_tags
15+
from tasks.build_tags import get_default_build_tags
1616
from tasks.cluster_agent_helpers import build_common, clean_common, refresh_assets_common, version_common
1717
from tasks.cws_instrumentation import BIN_PATH as CWS_INSTRUMENTATION_BIN_PATH
18-
from tasks.libs.common.utils import TestsNotSupportedError
18+
from tasks.gointegrationtest import CLUSTER_AGENT_IT_CONF, containerized_integration_tests
1919
from tasks.libs.releasing.version import load_release_versions
2020

2121
# constants
@@ -92,33 +92,14 @@ def integration_tests(ctx, race=False, remote_docker=False, go_mod="readonly", t
9292
"""
9393
Run integration tests for cluster-agent
9494
"""
95-
if sys.platform == 'win32':
96-
raise TestsNotSupportedError('Cluster Agent integration tests are not supported on Windows')
97-
98-
# We need docker for the kubeapiserver integration tests
99-
tags = get_default_build_tags(build="cluster-agent") + ["docker", "test"]
100-
101-
go_build_tags = " ".join(get_build_tags(tags, []))
102-
race_opt = "-race" if race else ""
103-
exec_opts = ""
104-
timeout_opt = f"-timeout {timeout}" if timeout else ""
105-
106-
# since Go 1.13, the -exec flag of go test could add some parameters such as -test.timeout
107-
# to the call, we don't want them because while calling invoke below, invoke
108-
# thinks that the parameters are for it to interpret.
109-
# we're calling an intermediate script which only pass the binary name to the invoke task.
110-
if remote_docker:
111-
exec_opts = f"-exec \"{os.getcwd()}/test/integration/dockerize_tests.sh\""
112-
113-
go_cmd = f'go test {timeout_opt} -mod={go_mod} {race_opt} -tags "{go_build_tags}" {exec_opts}'
114-
115-
prefixes = [
116-
"./test/integration/util/kube_apiserver",
117-
"./test/integration/util/leaderelection",
118-
]
119-
120-
for prefix in prefixes:
121-
ctx.run(f"{go_cmd} {prefix}")
95+
containerized_integration_tests(
96+
ctx,
97+
CLUSTER_AGENT_IT_CONF,
98+
race=race,
99+
remote_docker=remote_docker,
100+
go_mod=go_mod,
101+
timeout=timeout,
102+
)
122103

123104

124105
@task

tasks/dogstatsd.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
from tasks.build_tags import filter_incompatible_tags, get_build_tags, get_default_build_tags
1313
from tasks.flavor import AgentFlavor
14-
from tasks.libs.common.utils import REPO_PATH, TestsNotSupportedError, bin_name, get_build_flags, get_root
14+
from tasks.gointegrationtest import DOGSTATSD_IT_CONF, containerized_integration_tests
15+
from tasks.libs.common.utils import REPO_PATH, bin_name, get_build_flags, get_root
1516
from tasks.windows_resources import build_messagetable, build_rc, versioninfo_vars
1617

1718
# constants
@@ -174,29 +175,14 @@ def integration_tests(ctx, race=False, remote_docker=False, go_mod="readonly", t
174175
"""
175176
Run integration tests for dogstatsd
176177
"""
177-
if sys.platform == 'win32':
178-
raise TestsNotSupportedError('DogStatsD integration tests are not supported on Windows')
179-
180-
go_build_tags = " ".join(get_default_build_tags(build="test"))
181-
race_opt = "-race" if race else ""
182-
exec_opts = ""
183-
timeout_opt = f"-timeout {timeout}" if timeout else ""
184-
185-
# since Go 1.13, the -exec flag of go test could add some parameters such as -test.timeout
186-
# to the call, we don't want them because while calling invoke below, invoke
187-
# thinks that the parameters are for it to interpret.
188-
# we're calling an intermediate script which only pass the binary name to the invoke task.
189-
if remote_docker:
190-
exec_opts = f"-exec \"{os.getcwd()}/test/integration/dockerize_tests.sh\""
191-
192-
go_cmd = f'go test {timeout_opt} -mod={go_mod} {race_opt} -tags "{go_build_tags}" {exec_opts}'
193-
194-
prefixes = [
195-
"./test/integration/dogstatsd/...",
196-
]
197-
198-
for prefix in prefixes:
199-
ctx.run(f"{go_cmd} {prefix}")
178+
containerized_integration_tests(
179+
ctx,
180+
DOGSTATSD_IT_CONF,
181+
race=race,
182+
remote_docker=remote_docker,
183+
go_mod=go_mod,
184+
timeout=timeout,
185+
)
200186

201187

202188
@task

tasks/gointegrationtest.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)