Skip to content

Commit 1c0c05a

Browse files
authored
[chore] Tests refactoring: move helm operations to a common package (#1724)
1 parent 8dc6f53 commit 1c0c05a

File tree

7 files changed

+207
-420
lines changed

7 files changed

+207
-420
lines changed

functional_tests/configuration_switching/configuration_switching_test.go

Lines changed: 33 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@
44
package configuration_switching
55

66
import (
7-
"bytes"
87
"context"
98
"fmt"
109
"os"
1110
"path/filepath"
1211
"strings"
1312
"testing"
14-
"text/template"
15-
"time"
1613

1714
"github.com/stretchr/testify/assert"
1815
"github.com/stretchr/testify/require"
1916
"go.opentelemetry.io/collector/consumer/consumertest"
2017
"go.opentelemetry.io/collector/pdata/plog"
2118
"go.opentelemetry.io/collector/pdata/pmetric"
22-
"gopkg.in/yaml.v3"
23-
"helm.sh/helm/v3/pkg/action"
24-
"helm.sh/helm/v3/pkg/kube"
2519
corev1 "k8s.io/api/core/v1"
2620
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2721
"k8s.io/client-go/kubernetes"
@@ -52,12 +46,6 @@ func deployChartsAndApps(t *testing.T, valuesFileName string, repl map[string]in
5246
client, err := kubernetes.NewForConfig(kubeConfig)
5347
require.NoError(t, err)
5448

55-
chart := internal.LoadCollectorChart(t)
56-
57-
var valuesBytes []byte
58-
valuesBytes, err = os.ReadFile(filepath.Join(testDir, valuesDir, valuesFileName))
59-
require.NoError(t, err)
60-
6149
hostEp := internal.HostEndpoint(t)
6250
if len(hostEp) == 0 {
6351
require.Fail(t, "Host endpoint not found")
@@ -70,36 +58,10 @@ func deployChartsAndApps(t *testing.T, valuesFileName string, repl map[string]in
7058
replacements[k] = v
7159
}
7260

73-
tmpl, err := template.New("").Parse(string(valuesBytes))
61+
valuesFile, err := filepath.Abs(filepath.Join(testDir, valuesDir, valuesFileName))
7462
require.NoError(t, err)
75-
var buf bytes.Buffer
76-
err = tmpl.Execute(&buf, replacements)
77-
require.NoError(t, err)
78-
var values map[string]interface{}
79-
err = yaml.Unmarshal(buf.Bytes(), &values)
80-
require.NoError(t, err)
81-
82-
actionConfig := new(action.Configuration)
83-
if err := actionConfig.Init(kube.GetConfig(testKubeConfig, "", "default"), "default", os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
84-
t.Logf(format+"\n", v...)
85-
}); err != nil {
86-
require.NoError(t, err)
87-
}
88-
install := action.NewInstall(actionConfig)
89-
install.Namespace = "default"
90-
install.ReleaseName = "sock"
91-
_, err = install.Run(chart, values)
92-
if err != nil {
93-
t.Logf("error reported during helm install: %v\n", err)
94-
retryUpgrade := action.NewUpgrade(actionConfig)
95-
retryUpgrade.Namespace = "default"
96-
retryUpgrade.Install = true
97-
_, err = retryUpgrade.Run("sock", chart, values)
98-
require.NoError(t, err)
99-
}
100-
101-
waitForAllDeploymentsToStart(t, client)
102-
t.Log("Deployments started")
63+
internal.ChartInstallOrUpgrade(t, testKubeConfig, valuesFile, replacements)
64+
internal.WaitForAllDeploymentsToStart(t, client)
10365

10466
t.Cleanup(func() {
10567
if os.Getenv("SKIP_TEARDOWN") == "true" {
@@ -114,27 +76,9 @@ func deployChartsAndApps(t *testing.T, valuesFileName string, repl map[string]in
11476
}
11577
func teardown(t *testing.T) {
11678
t.Log("Running teardown")
117-
uninstallDeployment(t)
118-
}
119-
120-
func waitForAllDeploymentsToStart(t *testing.T, client *kubernetes.Clientset) {
121-
require.Eventually(t, func() bool {
122-
di, err := client.AppsV1().Deployments("default").List(context.Background(), metav1.ListOptions{})
123-
require.NoError(t, err)
124-
for _, d := range di.Items {
125-
if d.Status.ReadyReplicas != d.Status.Replicas {
126-
var messages string
127-
for _, c := range d.Status.Conditions {
128-
messages += c.Message
129-
messages += "\n"
130-
}
131-
132-
t.Logf("Deployment not ready: %s, %s", d.Name, messages)
133-
return false
134-
}
135-
}
136-
return true
137-
}, 5*time.Minute, 10*time.Second)
79+
testKubeConfig, setKubeConfig := os.LookupEnv("KUBECONFIG")
80+
require.True(t, setKubeConfig, "the environment variable KUBECONFIG must be set")
81+
internal.ChartUninstall(t, testKubeConfig)
13882
}
13983

14084
func Test_Functions(t *testing.T) {
@@ -152,14 +96,10 @@ func Test_Functions(t *testing.T) {
15296
return
15397
}
15498

155-
t.Setenv("KUBECONFIG", "/tmp/kube-config-splunk-otel-collector-chart-functional-testing")
156-
t.Setenv("KUBE_TEST_ENV", "kind")
157-
15899
t.Run("agent logs and metrics enabled or disabled", testAgentLogsAndMetrics)
159100
t.Run("logs and metrics index switch", testIndexSwitch)
160101
t.Run("cluster receiver enabled or disabled", testClusterReceiverEnabledOrDisabled)
161102
t.Run("logs and metrics attributes verification", testVerifyLogsAndMetricsAttributes)
162-
163103
}
164104

165105
func testAgentLogsAndMetrics(t *testing.T) {
@@ -183,7 +123,6 @@ func testAgentLogsAndMetrics(t *testing.T) {
183123

184124
internal.WaitForMetrics(t, 5, hecMetricsConsumer)
185125
internal.WaitForLogs(t, 5, agentLogsConsumer)
186-
uninstallDeployment(t)
187126
})
188127

189128
t.Run("check metrics only enabled", func(t *testing.T) {
@@ -201,7 +140,6 @@ func testAgentLogsAndMetrics(t *testing.T) {
201140

202141
internal.WaitForMetrics(t, 5, hecMetricsConsumer)
203142
internal.CheckNoEventsReceived(t, agentLogsConsumer)
204-
uninstallDeployment(t)
205143
})
206144

207145
t.Run("check logs only enabled", func(t *testing.T) {
@@ -215,26 +153,25 @@ func testAgentLogsAndMetrics(t *testing.T) {
215153
deployChartsAndApps(t, valuesFileName, replacements)
216154

217155
internal.WaitForLogs(t, 5, agentLogsConsumer)
218-
uninstallDeployment(t)
219-
internal.ResetLogsSink(t, agentLogsConsumer)
220-
internal.ResetMetricsSink(t, hecMetricsConsumer)
221156
})
222157
}
223158

224159
func testIndexSwitch(t *testing.T) {
225-
var metricsIndex string = "metricsIndex"
226-
var newMetricsIndex string = "newMetricsIndex"
227-
var logsIndex string = "main"
228-
var newLogsIndex string = "newLogsIndex"
160+
var metricsIndex = "metricsIndex"
161+
var newMetricsIndex = "newMetricsIndex"
162+
var logsIndex = "main"
163+
var newLogsIndex = "newLogsIndex"
229164
var nonDefaultSourcetype = "my-sourcetype"
230165

231166
valuesFileName := "values_indexes_switching.yaml.tmpl"
232167
hecMetricsConsumer := globalSinks.hecMetricsConsumer
168+
internal.ResetMetricsSink(t, hecMetricsConsumer)
233169
internal.CheckNoMetricsReceived(t, hecMetricsConsumer)
234170
agentLogsConsumer := globalSinks.logsConsumer
171+
internal.ResetLogsSink(t, agentLogsConsumer)
235172
internal.CheckNoEventsReceived(t, agentLogsConsumer)
236173

237-
t.Run("check logs and metrics index switching", func(t *testing.T) {
174+
t.Run("default_source_type", func(t *testing.T) {
238175
replacements := map[string]interface{}{
239176
"MetricsIndex": metricsIndex,
240177
"LogsIndex": logsIndex,
@@ -257,82 +194,73 @@ func testIndexSwitch(t *testing.T) {
257194
}
258195
}
259196
assert.NotContains(t, sourcetypes, nonDefaultSourcetype)
260-
assert.True(t, len(indices) == 1)
261-
assert.True(t, indices[0] == logsIndex)
197+
assert.Len(t, indices, 1)
198+
assert.Equal(t, logsIndex, indices[0])
262199

263200
var mIndices []string
264201
mIndices = getMetricsIndex(hecMetricsConsumer.AllMetrics())
265-
assert.True(t, len(mIndices) == 1)
266-
assert.True(t, mIndices[0] == metricsIndex)
202+
assert.Len(t, mIndices, 1)
203+
assert.Equal(t, metricsIndex, mIndices[0])
204+
})
267205

268-
replacements = map[string]interface{}{
206+
t.Run("non_default_source_type", func(t *testing.T) {
207+
replacements := map[string]interface{}{
269208
"MetricsIndex": newMetricsIndex,
270209
"LogsIndex": newLogsIndex,
271210
"NonDefaultSourcetype": true,
272211
"Sourcetype": nonDefaultSourcetype,
273212
}
274213
deployChartsAndApps(t, valuesFileName, replacements)
275-
internal.ResetLogsSink(t, agentLogsConsumer)
276-
internal.ResetMetricsSink(t, hecMetricsConsumer)
277214

278215
internal.WaitForLogs(t, 3, agentLogsConsumer)
279-
logs = agentLogsConsumer.AllLogs()
280-
sourcetypes, indices = getLogsIndexAndSourceType(logs)
216+
logs := agentLogsConsumer.AllLogs()
217+
sourcetypes, indices := getLogsIndexAndSourceType(logs)
281218
t.Logf("Indices: %v", indices)
282219
assert.Contains(t, indices, newLogsIndex)
283220
assert.Contains(t, sourcetypes, nonDefaultSourcetype)
284-
assert.True(t, len(indices) == 1)
285-
assert.True(t, len(sourcetypes) == 1)
286221

287222
internal.WaitForMetrics(t, 3, hecMetricsConsumer)
288-
mIndices = getMetricsIndex(hecMetricsConsumer.AllMetrics())
289-
assert.True(t, len(mIndices) == 1)
290-
assert.True(t, mIndices[0] == newMetricsIndex)
223+
mIndices := getMetricsIndex(hecMetricsConsumer.AllMetrics())
224+
assert.Contains(t, mIndices, newMetricsIndex)
291225
})
292-
uninstallDeployment(t)
293-
internal.ResetLogsSink(t, agentLogsConsumer)
294-
internal.ResetMetricsSink(t, hecMetricsConsumer)
295226
}
296227

297228
func testClusterReceiverEnabledOrDisabled(t *testing.T) {
298229
valuesFileName := "values_cluster_receiver_switching.yaml.tmpl"
299-
namespace := "default"
300230
logsObjectsConsumer := globalSinks.logsObjectsConsumer
301231
hostEp := internal.HostEndpoint(t)
302232
if len(hostEp) == 0 {
303233
require.Fail(t, "Host endpoint not found")
304234
}
305235
logsObjectsHecEndpoint := fmt.Sprintf("http://%s:%d/services/collector", hostEp, internal.HECObjectsReceiverPort)
306236

307-
t.Run("check cluster receiver enabled", func(t *testing.T) {
237+
t.Run("check cluster receiver disabled", func(t *testing.T) {
308238
internal.ResetLogsSink(t, logsObjectsConsumer)
309239
replacements := map[string]interface{}{
310240
"ClusterReceiverEnabled": false,
311241
"LogObjectsHecEndpoint": logsObjectsHecEndpoint,
312242
}
313243
deployChartsAndApps(t, valuesFileName, replacements)
314-
var pods *corev1.PodList
315-
pods = listPodsInNamespace(t, namespace)
316-
assert.True(t, len(pods.Items) == 1)
244+
pods := listPodsInNamespace(t, internal.Namespace)
245+
assert.Len(t, pods.Items, 1)
317246
assert.True(t, strings.HasPrefix(pods.Items[0].Name, "sock-splunk-otel-collector-agent"))
318247
internal.CheckNoEventsReceived(t, logsObjectsConsumer)
248+
})
319249

320-
t.Log("cluster receiver enabled")
321-
replacements = map[string]interface{}{
250+
t.Run("check cluster receiver enabled", func(t *testing.T) {
251+
internal.ResetLogsSink(t, logsObjectsConsumer)
252+
replacements := map[string]interface{}{
322253
"ClusterReceiverEnabled": true,
323254
"LogObjectsHecEndpoint": logsObjectsHecEndpoint,
324255
}
325256
deployChartsAndApps(t, valuesFileName, replacements)
326257
internal.ResetLogsSink(t, logsObjectsConsumer)
327-
328-
pods = listPodsInNamespace(t, namespace)
329-
assert.True(t, len(pods.Items) == 2)
258+
pods := listPodsInNamespace(t, internal.Namespace)
259+
assert.Len(t, pods.Items, 2)
330260
assert.True(t, checkPodExists(pods, "sock-splunk-otel-collector-agent"))
331261
assert.True(t, checkPodExists(pods, "sock-splunk-otel-collector-k8s-cluster-receiver"))
332262
internal.WaitForLogs(t, 5, logsObjectsConsumer)
333263
})
334-
uninstallDeployment(t)
335-
internal.ResetLogsSink(t, logsObjectsConsumer)
336264
}
337265

338266
func testVerifyLogsAndMetricsAttributes(t *testing.T) {
@@ -456,13 +384,6 @@ func listPodsInNamespace(t *testing.T, namespace string) *corev1.PodList {
456384
return pods
457385
}
458386

459-
func waitForAllPodsToBeRemoved(t *testing.T, namespace string) {
460-
timeoutMinutes := 2
461-
require.Eventuallyf(t, func() bool {
462-
return len(listPodsInNamespace(t, namespace).Items) == 0
463-
}, time.Duration(timeoutMinutes)*time.Minute, 5*time.Second, "There are still %d pods in the namespace", len(listPodsInNamespace(t, namespace).Items))
464-
}
465-
466387
func getLogsIndexAndSourceType(logs []plog.Logs) ([]string, []string) {
467388
var sourcetypes []string
468389
var indices []string
@@ -595,22 +516,3 @@ func getMetricsAttributes(metrics []pmetric.Metrics, attributeName string) ([]st
595516
fmt.Printf("Counters: Found: %d | Skipped: %d | Not Found: %d\n", foundCounter, skippedCounter, notFoundCounter)
596517
return resourceAttributes, notFoundCounter
597518
}
598-
599-
func uninstallDeployment(t *testing.T) {
600-
testKubeConfig, setKubeConfig := os.LookupEnv("KUBECONFIG")
601-
require.True(t, setKubeConfig, "the environment variable KUBECONFIG must be set")
602-
actionConfig := new(action.Configuration)
603-
if err := actionConfig.Init(kube.GetConfig(testKubeConfig, "", "default"), "default", os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
604-
t.Logf(format+"\n", v...)
605-
}); err != nil {
606-
require.NoError(t, err)
607-
}
608-
609-
uninstall := action.NewUninstall(actionConfig)
610-
uninstallResponse, err := uninstall.Run("sock")
611-
if err != nil {
612-
t.Logf("Failed to uninstall release: %v", err)
613-
}
614-
t.Logf("Uninstalled release: %v", uninstallResponse)
615-
waitForAllPodsToBeRemoved(t, "default")
616-
}

0 commit comments

Comments
 (0)