77package integration
88
99import (
10- "bufio"
11- "bytes"
1210 "context"
13- "fmt"
1411 "os"
1512 "path/filepath"
1613 "testing"
1714
1815 "github.com/stretchr/testify/require"
1916 corev1 "k8s.io/api/core/v1"
20- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2117 "sigs.k8s.io/e2e-framework/klient/k8s"
2218
2319 "github.com/elastic/elastic-agent/pkg/testing/define"
@@ -35,73 +31,38 @@ func TestKubernetesAgentService(t *testing.T) {
3531 Group : define .Kubernetes ,
3632 })
3733
34+ // read the service agent config
35+ serviceAgentYAML , err := os .ReadFile (filepath .Join ("testdata" , "connectors.agent.yml" ))
36+ require .NoError (t , err , "failed to read service agent config" )
37+
3838 ctx := context .Background ()
3939 kCtx := k8sGetContext (t , info )
40- testNamespace := kCtx .getNamespace (t )
41-
42- renderedManifest , err := renderKustomize (agentK8SKustomize )
43- require .NoError (t , err , "failed to render kustomize" )
44-
45- k8sObjects , err := k8sYAMLToObjects (bufio .NewReader (bytes .NewReader (renderedManifest )))
46- require .NoError (t , err , "failed to convert yaml to k8s objects" )
4740
48- // add the testNamespace in the k8sObjects
49- k8sObjects = append ([]k8s.Object {& corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : testNamespace }}}, k8sObjects ... )
41+ schedulableNodeCount , err := k8sSchedulableNodeCount (ctx , kCtx )
42+ require .NoError (t , err , "error at getting schedulable node count" )
43+ require .NotZero (t , schedulableNodeCount , "no schedulable Kubernetes nodes found" )
5044
51- t .Cleanup (func () {
52- err = k8sDeleteObjects (ctx , kCtx .client , k8sDeleteOpts {wait : true }, k8sObjects ... )
53- require .NoError (t , err , "failed to delete k8s namespace" )
54- })
55-
56- k8sKustomizeAdjustObjects (k8sObjects , testNamespace , "elastic-agent-standalone" ,
57- func (container * corev1.Container ) {
58- // set agent image
59- container .Image = kCtx .agentImage
60- // set ImagePullPolicy to "Never" to avoid pulling the image
61- // as the image is already loaded by the kubernetes provisioner
62- container .ImagePullPolicy = "Never"
63-
64- // set Elasticsearch host and API key
65- for idx , env := range container .Env {
66- if env .Name == "ES_HOST" {
67- container .Env [idx ].Value = kCtx .esHost
68- container .Env [idx ].ValueFrom = nil
69- }
70- if env .Name == "API_KEY" {
71- container .Env [idx ].Value = kCtx .esAPIKey
72- container .Env [idx ].ValueFrom = nil
45+ testSteps := []k8sTestStep {
46+ k8sStepCreateNamespace (),
47+ k8sStepDeployKustomize (agentK8SKustomize , "elastic-agent-standalone" , k8sKustomizeOverrides {
48+ agentContainerMemoryLimit : "800Mi" ,
49+ }, func (obj k8s.Object ) {
50+ // update the configmap to only run the connectors input
51+ switch objWithType := obj .(type ) {
52+ case * corev1.ConfigMap :
53+ _ , ok := objWithType .Data ["agent.yml" ]
54+ if ok {
55+ objWithType .Data ["agent.yml" ] = string (serviceAgentYAML )
7356 }
7457 }
75- },
76- func (pod * corev1.PodSpec ) {
77- for volumeIdx , volume := range pod .Volumes {
78- // need to update the volume path of the state directory
79- // to match the test namespace
80- if volume .Name == "elastic-agent-state" {
81- hostPathType := corev1 .HostPathDirectoryOrCreate
82- pod .Volumes [volumeIdx ].VolumeSource .HostPath = & corev1.HostPathVolumeSource {
83- Type : & hostPathType ,
84- Path : fmt .Sprintf ("/var/lib/elastic-agent-standalone/%s/state" , testNamespace ),
85- }
86- }
87- }
88- })
89-
90- // update the configmap to only run the connectors input
91- serviceAgentYAML , err := os .ReadFile (filepath .Join ("testdata" , "connectors.agent.yml" ))
92- require .NoError (t , err )
93- for _ , obj := range k8sObjects {
94- switch objWithType := obj .(type ) {
95- case * corev1.ConfigMap :
96- _ , ok := objWithType .Data ["agent.yml" ]
97- if ok {
98- objWithType .Data ["agent.yml" ] = string (serviceAgentYAML )
99- }
100- }
58+ }),
59+ k8sStepCheckAgentStatus ("app=elastic-agent-standalone" , schedulableNodeCount , "elastic-agent-standalone" , map [string ]bool {
60+ "connectors-py" : true ,
61+ }),
10162 }
10263
103- k8sKustomizeDeployAgent ( t , ctx , kCtx . client , k8sObjects , testNamespace , false , kCtx .logsBasePath ,
104- true , map [ string ] bool {
105- "connectors-py" : true ,
106- })
64+ testNamespace := kCtx .getNamespace ( t )
65+ for _ , step := range testSteps {
66+ step ( t , ctx , kCtx , testNamespace )
67+ }
10768}
0 commit comments