Skip to content

Commit d4957c6

Browse files
committed
Refactor tests
1 parent 5861ff3 commit d4957c6

2 files changed

Lines changed: 141 additions & 182 deletions

File tree

testing/integration/journald_test.go

Lines changed: 141 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/elastic/elastic-agent-libs/testing/estools"
2121
"github.com/elastic/elastic-agent/pkg/testing/define"
22+
"github.com/elastic/go-elasticsearch/v8"
2223
)
2324

2425
func TestKubernetesJournaldInput(t *testing.T) {
@@ -46,106 +47,73 @@ func TestKubernetesJournaldInput(t *testing.T) {
4647
namespace := kCtx.getNamespace(t)
4748
hostPathType := corev1.HostPathDirectory
4849

49-
testCases := []struct {
50-
name string
51-
skipReason string
52-
steps []k8sTestStep
53-
}{
54-
{
55-
name: "happy path",
56-
steps: []k8sTestStep{
57-
k8sStepCreateNamespace(),
58-
k8sStepDeployKustomize(
59-
agentK8SKustomize,
60-
"elastic-agent-standalone",
61-
k8sKustomizeOverrides{
62-
agentContainerExtraEnv: []corev1.EnvVar{
63-
{
64-
Name: "ELASTICSEARCH_USERNAME",
65-
Value: os.Getenv("ELASTICSEARCH_USERNAME"),
66-
},
67-
{
68-
Name: "ELASTICSEARCH_PASSWORD",
69-
Value: os.Getenv("ELASTICSEARCH_PASSWORD"),
70-
},
71-
{
72-
Name: "EA_POLICY_NAMESPACE",
73-
Value: namespace,
74-
},
75-
},
76-
agentContainerVolumeMounts: []corev1.VolumeMount{
77-
{
78-
Name: "journald-mount",
79-
MountPath: "/opt/journald",
80-
ReadOnly: true,
81-
},
82-
},
83-
agentPodVolumes: []corev1.Volume{
84-
{
85-
Name: "journald-mount",
86-
VolumeSource: corev1.VolumeSource{
87-
HostPath: &corev1.HostPathVolumeSource{
88-
Path: "/run/log/journal",
89-
Type: &hostPathType,
90-
},
91-
},
50+
steps := []k8sTestStep{
51+
k8sStepCreateNamespace(),
52+
k8sStepDeployKustomize(
53+
agentK8SKustomize,
54+
"elastic-agent-standalone",
55+
k8sKustomizeOverrides{
56+
agentContainerExtraEnv: []corev1.EnvVar{
57+
{
58+
Name: "ELASTICSEARCH_USERNAME",
59+
Value: os.Getenv("ELASTICSEARCH_USERNAME"),
60+
},
61+
{
62+
Name: "ELASTICSEARCH_PASSWORD",
63+
Value: os.Getenv("ELASTICSEARCH_PASSWORD"),
64+
},
65+
{
66+
Name: "EA_POLICY_NAMESPACE",
67+
Value: namespace,
68+
},
69+
},
70+
agentContainerVolumeMounts: []corev1.VolumeMount{
71+
{
72+
Name: "journald-mount",
73+
MountPath: "/opt/journald",
74+
ReadOnly: true,
75+
},
76+
},
77+
agentPodVolumes: []corev1.Volume{
78+
{
79+
Name: "journald-mount",
80+
VolumeSource: corev1.VolumeSource{
81+
HostPath: &corev1.HostPathVolumeSource{
82+
Path: "/run/log/journal",
83+
Type: &hostPathType,
9284
},
9385
},
9486
},
95-
func(obj k8s.Object) {
96-
// update the configmap to use the journald input
97-
switch objWithType := obj.(type) {
98-
case *corev1.ConfigMap:
99-
_, ok := objWithType.Data["agent.yml"]
100-
if ok {
101-
objWithType.Data["agent.yml"] = string(agentConfigYAML)
102-
}
103-
}
104-
105-
}),
106-
k8sStepCheckAgentStatus(
107-
"app=elastic-agent-standalone",
108-
schedulableNodeCount,
109-
"elastic-agent-standalone",
110-
map[string]bool{
111-
"journald": true,
112-
}),
87+
},
11388
},
114-
},
89+
func(obj k8s.Object) {
90+
// update the configmap to use the journald input
91+
switch objWithType := obj.(type) {
92+
case *corev1.ConfigMap:
93+
_, ok := objWithType.Data["agent.yml"]
94+
if ok {
95+
objWithType.Data["agent.yml"] = string(agentConfigYAML)
96+
}
97+
}
98+
99+
}),
100+
k8sStepCheckAgentStatus(
101+
"app=elastic-agent-standalone",
102+
schedulableNodeCount,
103+
"elastic-agent-standalone",
104+
map[string]bool{
105+
"journald": true,
106+
}),
115107
}
116108

117-
for _, tc := range testCases {
118-
t.Run(tc.name, func(t *testing.T) {
119-
if tc.skipReason != "" {
120-
t.Skip(tc.skipReason)
121-
}
122-
123-
ctx := context.Background()
124-
testNamespace := kCtx.getNamespace(t)
125-
126-
for _, step := range tc.steps {
127-
step(t, ctx, kCtx, testNamespace)
128-
}
129-
130-
// Check if the context was cancelled or timed out
131-
if ctx.Err() != nil {
132-
t.Errorf("context error: %v", ctx.Err())
133-
}
134-
135-
// Query the index and filter by the input type
136-
docs := findESDocs(t, func() (estools.Documents, error) {
137-
return estools.GetLogsForIndexWithContext(
138-
ctx,
139-
info.ESClient, fmt.Sprintf("logs-%s-default", namespace),
140-
map[string]any{
141-
"input.type": "journald",
142-
},
143-
)
144-
})
145-
146-
require.NotEmpty(t, docs, "expected logs to be found in Elasticsearch")
147-
})
148-
}
109+
journaldTest(
110+
t,
111+
info.ESClient,
112+
kCtx,
113+
steps,
114+
fmt.Sprintf("logs-%s-default", namespace),
115+
"input.type",
116+
"journald")
149117
}
150118

151119
func TestKubernetesJournaldInputOtel(t *testing.T) {
@@ -162,104 +130,98 @@ func TestKubernetesJournaldInputOtel(t *testing.T) {
162130
otelConfigYAML, err := os.ReadFile(filepath.Join("testdata", "journald-otel.yml"))
163131
require.NoError(t, err, "failed to read journald input template")
164132

165-
ctx := context.Background()
166133
kCtx := k8sGetContext(t, info)
167-
168-
schedulableNodeCount, err := k8sSchedulableNodeCount(ctx, kCtx)
169-
require.NoError(t, err, "error at getting schedulable node count")
170-
require.NotZero(t, schedulableNodeCount, "no schedulable Kubernetes nodes found")
171-
172134
namespace := kCtx.getNamespace(t)
173135
hostPathType := corev1.HostPathDirectory
174136

175-
testCases := []struct {
176-
name string
177-
skipReason string
178-
steps []k8sTestStep
179-
}{
180-
{
181-
name: "happy path",
182-
steps: []k8sTestStep{
183-
k8sStepCreateNamespace(),
184-
k8sStepDeployKustomize(
185-
agentK8SKustomize,
186-
"elastic-agent-standalone",
187-
k8sKustomizeOverrides{
188-
agentContainerArgs: []string{"--config", "/etc/elastic-agent/agent.yml"},
189-
agentContainerExtraEnv: []corev1.EnvVar{
190-
{
191-
Name: "EA_POLICY_NAMESPACE",
192-
Value: namespace,
193-
},
194-
{
195-
Name: "ES_API_KEY_ENCODED",
196-
Value: kCtx.esEncodedAPIKey,
197-
},
198-
},
199-
agentContainerVolumeMounts: []corev1.VolumeMount{
200-
{
201-
Name: "journald-mount",
202-
MountPath: "/opt/journal",
203-
ReadOnly: true,
204-
},
205-
},
206-
agentPodVolumes: []corev1.Volume{
207-
{
208-
Name: "journald-mount",
209-
VolumeSource: corev1.VolumeSource{
210-
HostPath: &corev1.HostPathVolumeSource{
211-
Path: "/run/log/journal",
212-
Type: &hostPathType,
213-
},
214-
},
137+
steps := []k8sTestStep{
138+
k8sStepCreateNamespace(),
139+
k8sStepDeployKustomize(
140+
agentK8SKustomize,
141+
"elastic-agent-standalone",
142+
k8sKustomizeOverrides{
143+
agentContainerArgs: []string{"--config", "/etc/elastic-agent/agent.yml"},
144+
agentContainerExtraEnv: []corev1.EnvVar{
145+
{
146+
Name: "EA_POLICY_NAMESPACE",
147+
Value: namespace,
148+
},
149+
{
150+
Name: "ES_API_KEY_ENCODED",
151+
Value: kCtx.esEncodedAPIKey,
152+
},
153+
},
154+
agentContainerVolumeMounts: []corev1.VolumeMount{
155+
{
156+
Name: "journald-mount",
157+
MountPath: "/opt/journal",
158+
ReadOnly: true,
159+
},
160+
},
161+
agentPodVolumes: []corev1.Volume{
162+
{
163+
Name: "journald-mount",
164+
VolumeSource: corev1.VolumeSource{
165+
HostPath: &corev1.HostPathVolumeSource{
166+
Path: "/run/log/journal",
167+
Type: &hostPathType,
215168
},
216169
},
217170
},
218-
func(obj k8s.Object) {
219-
// update the configmap to use the journald input
220-
switch objWithType := obj.(type) {
221-
case *corev1.ConfigMap:
222-
_, ok := objWithType.Data["agent.yml"]
223-
if ok {
224-
objWithType.Data["agent.yml"] = string(otelConfigYAML)
225-
}
226-
}
227-
}),
171+
},
228172
},
229-
},
173+
func(obj k8s.Object) {
174+
// update the configmap to use the journald input
175+
switch objWithType := obj.(type) {
176+
case *corev1.ConfigMap:
177+
_, ok := objWithType.Data["agent.yml"]
178+
if ok {
179+
objWithType.Data["agent.yml"] = string(otelConfigYAML)
180+
}
181+
}
182+
}),
230183
}
231184

232-
for _, tc := range testCases {
233-
t.Run(tc.name, func(t *testing.T) {
234-
if tc.skipReason != "" {
235-
t.Skip(tc.skipReason)
236-
}
237-
238-
ctx := context.Background()
239-
testNamespace := kCtx.getNamespace(t)
185+
journaldTest(
186+
t,
187+
info.ESClient,
188+
kCtx,
189+
steps,
190+
fmt.Sprintf("logs-generic.otel-%s", namespace),
191+
"body.structured.input.type",
192+
"journald")
193+
}
240194

241-
for _, step := range tc.steps {
242-
step(t, ctx, kCtx, testNamespace)
243-
}
195+
func journaldTest(
196+
t *testing.T,
197+
esClient *elasticsearch.Client,
198+
kCtx k8sContext,
199+
steps []k8sTestStep,
200+
index, field, value string) {
201+
t.Helper()
244202

245-
// Check if the context was cancelled or timed out
246-
if ctx.Err() != nil {
247-
t.Errorf("context error: %v", ctx.Err())
248-
}
203+
ctx := context.Background()
204+
testNamespace := kCtx.getNamespace(t)
249205

250-
// Query the index and filter by the input type
251-
docs := findESDocs(t, func() (estools.Documents, error) {
252-
return estools.GetLogsForIndexWithContext(
253-
ctx,
254-
info.ESClient,
255-
fmt.Sprintf("logs-generic.otel-%s", namespace),
256-
map[string]any{
257-
"event.dataset": "generic.otel",
258-
},
259-
)
260-
})
206+
for _, step := range steps {
207+
step(t, ctx, kCtx, testNamespace)
208+
}
261209

262-
require.NotEmpty(t, docs, "expected logs to be found in Elasticsearch")
263-
})
210+
// Check if the context was cancelled or timed out
211+
if ctx.Err() != nil {
212+
t.Errorf("context error: %v", ctx.Err())
264213
}
214+
215+
// Query the index and filter by the input type
216+
docs := findESDocs(t, func() (estools.Documents, error) {
217+
return estools.GetLogsForIndexWithContext(
218+
ctx,
219+
esClient,
220+
index,
221+
map[string]any{
222+
field: value,
223+
},
224+
)
225+
})
226+
require.NotEmpty(t, docs, "expected logs to be found in Elasticsearch")
265227
}

testing/integration/testdata/journald-otel.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ receivers:
1616
processors:
1717
resource:
1818
attributes:
19-
- key: journald-test
20-
action: insert
21-
value: true
2219
- key: data_stream.namespace
2320
action: insert
2421
value: "${EA_POLICY_NAMESPACE}"

0 commit comments

Comments
 (0)