Skip to content

Commit

Permalink
[k8s]: transition integration tests to adapter pattern (#6277) (#6359)
Browse files Browse the repository at this point in the history
* feat: transition kubernetes integration tests to adapter pattern

* fix: count nodes without NoSchedule taints

(cherry picked from commit 67c744f)

Co-authored-by: Panos Koutsovasilis <[email protected]>
  • Loading branch information
mergify[bot] and pkoutsovasilis authored Dec 19, 2024
1 parent 8cdef42 commit 8640228
Show file tree
Hide file tree
Showing 2 changed files with 485 additions and 478 deletions.
91 changes: 26 additions & 65 deletions testing/integration/kubernetes_agent_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
package integration

import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/e2e-framework/klient/k8s"

"github.com/elastic/elastic-agent/pkg/testing/define"
Expand All @@ -35,73 +31,38 @@ func TestKubernetesAgentService(t *testing.T) {
Group: define.Kubernetes,
})

// read the service agent config
serviceAgentYAML, err := os.ReadFile(filepath.Join("testdata", "connectors.agent.yml"))
require.NoError(t, err, "failed to read service agent config")

ctx := context.Background()
kCtx := k8sGetContext(t, info)
testNamespace := kCtx.getNamespace(t)

renderedManifest, err := renderKustomize(agentK8SKustomize)
require.NoError(t, err, "failed to render kustomize")

k8sObjects, err := k8sYAMLToObjects(bufio.NewReader(bytes.NewReader(renderedManifest)))
require.NoError(t, err, "failed to convert yaml to k8s objects")

// add the testNamespace in the k8sObjects
k8sObjects = append([]k8s.Object{&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}}}, k8sObjects...)
schedulableNodeCount, err := k8sSchedulableNodeCount(ctx, kCtx)
require.NoError(t, err, "error at getting schedulable node count")
require.NotZero(t, schedulableNodeCount, "no schedulable Kubernetes nodes found")

t.Cleanup(func() {
err = k8sDeleteObjects(ctx, kCtx.client, k8sDeleteOpts{wait: true}, k8sObjects...)
require.NoError(t, err, "failed to delete k8s namespace")
})

k8sKustomizeAdjustObjects(k8sObjects, testNamespace, "elastic-agent-standalone",
func(container *corev1.Container) {
// set agent image
container.Image = kCtx.agentImage
// set ImagePullPolicy to "Never" to avoid pulling the image
// as the image is already loaded by the kubernetes provisioner
container.ImagePullPolicy = "Never"

// set Elasticsearch host and API key
for idx, env := range container.Env {
if env.Name == "ES_HOST" {
container.Env[idx].Value = kCtx.esHost
container.Env[idx].ValueFrom = nil
}
if env.Name == "API_KEY" {
container.Env[idx].Value = kCtx.esAPIKey
container.Env[idx].ValueFrom = nil
testSteps := []k8sTestStep{
k8sStepCreateNamespace(),
k8sStepDeployKustomize(agentK8SKustomize, "elastic-agent-standalone", k8sKustomizeOverrides{
agentContainerMemoryLimit: "800Mi",
}, func(obj k8s.Object) {
// update the configmap to only run the connectors input
switch objWithType := obj.(type) {
case *corev1.ConfigMap:
_, ok := objWithType.Data["agent.yml"]
if ok {
objWithType.Data["agent.yml"] = string(serviceAgentYAML)
}
}
},
func(pod *corev1.PodSpec) {
for volumeIdx, volume := range pod.Volumes {
// need to update the volume path of the state directory
// to match the test namespace
if volume.Name == "elastic-agent-state" {
hostPathType := corev1.HostPathDirectoryOrCreate
pod.Volumes[volumeIdx].VolumeSource.HostPath = &corev1.HostPathVolumeSource{
Type: &hostPathType,
Path: fmt.Sprintf("/var/lib/elastic-agent-standalone/%s/state", testNamespace),
}
}
}
})

// update the configmap to only run the connectors input
serviceAgentYAML, err := os.ReadFile(filepath.Join("testdata", "connectors.agent.yml"))
require.NoError(t, err)
for _, obj := range k8sObjects {
switch objWithType := obj.(type) {
case *corev1.ConfigMap:
_, ok := objWithType.Data["agent.yml"]
if ok {
objWithType.Data["agent.yml"] = string(serviceAgentYAML)
}
}
}),
k8sStepCheckAgentStatus("app=elastic-agent-standalone", schedulableNodeCount, "elastic-agent-standalone", map[string]bool{
"connectors-py": true,
}),
}

k8sKustomizeDeployAgent(t, ctx, kCtx.client, k8sObjects, testNamespace, false, kCtx.logsBasePath,
true, map[string]bool{
"connectors-py": true,
})
testNamespace := kCtx.getNamespace(t)
for _, step := range testSteps {
step(t, ctx, kCtx, testNamespace)
}
}
Loading

0 comments on commit 8640228

Please sign in to comment.