@@ -29,6 +29,7 @@ import (
2929"github.com/stretchr/testify/require"
3030
3131"github.com/elastic/elastic-agent-libs/kibana"
32+ "github.com/elastic/elastic-agent-libs/testing/estools"
3233"github.com/elastic/go-elasticsearch/v8"
3334
3435appsv1 "k8s.io/api/apps/v1"
@@ -864,7 +865,8 @@ func k8sCheckAgentStatus(ctx context.Context, client klient.Client, stdout *byte
864865
865866// k8sGetAgentID returns the agent ID for the given agent pod
866867func k8sGetAgentID (ctx context.Context , client klient.Client , stdout * bytes.Buffer , stderr * bytes.Buffer ,
867- namespace string , agentPodName string , containerName string ) (string , error ) {
868+ namespace string , agentPodName string , containerName string ,
869+ ) (string , error ) {
868870command := []string {"elastic-agent" , "status" , "--output=json" }
869871
870872status := atesting.AgentStatusOutput {} // clear status output
@@ -997,7 +999,7 @@ func k8sDumpPods(t *testing.T, ctx context.Context, client klient.Client, testNa
997999header := & tar.Header {
9981000Name : logFileName ,
9991001Size : int64 (len (b )),
1000- Mode : 0600 ,
1002+ Mode : 0o600 ,
10011003ModTime : time .Now (),
10021004AccessTime : time .Now (),
10031005ChangeTime : time .Now (),
@@ -1024,7 +1026,7 @@ func k8sDumpPods(t *testing.T, ctx context.Context, client klient.Client, testNa
10241026header := & tar.Header {
10251027Name : statesDumpFile ,
10261028Size : int64 (len (b )),
1027- Mode : 0600 ,
1029+ Mode : 0o600 ,
10281030ModTime : time .Now (),
10291031AccessTime : time .Now (),
10301032ChangeTime : time .Now (),
@@ -1102,35 +1104,8 @@ func k8sRenderKustomize(kustomizePath string) ([]byte, error) {
11021104}
11031105
11041106// generateESAPIKey generates an API key for the given Elasticsearch.
1105- func generateESAPIKey (esClient * elasticsearch.Client , keyName string ) (string , error ) {
1106- apiKeyReqBody := fmt .Sprintf (`{
1107- "name": "%s",
1108- "expiration": "1d"
1109- }` , keyName )
1110-
1111- resp , err := esClient .Security .CreateAPIKey (strings .NewReader (apiKeyReqBody ))
1112- if err != nil {
1113- return "" , err
1114- }
1115- defer resp .Body .Close ()
1116-
1117- response := make (map [string ]interface {})
1118- err = json .NewDecoder (resp .Body ).Decode (& response )
1119- if err != nil {
1120- return "" , err
1121- }
1122-
1123- keyToken := response ["api_key" ].(string )
1124- if keyToken == "" {
1125- return "" , fmt .Errorf ("key token is empty" )
1126- }
1127-
1128- keyID := response ["id" ].(string )
1129- if keyID == "" {
1130- return "" , fmt .Errorf ("key ID is empty" )
1131- }
1132-
1133- return fmt .Sprintf ("%s:%s" , keyID , keyToken ), nil
1107+ func generateESAPIKey (esClient * elasticsearch.Client , keyName string ) (estools.APIKeyResponse , error ) {
1108+ return estools .CreateAPIKey (context .Background (), esClient , estools.APIKeyRequest {Name : keyName , Expiration : "1d" })
11341109}
11351110
11361111// k8sDeleteOpts contains options for deleting k8s objects
@@ -1330,6 +1305,8 @@ type k8sContext struct {
13301305esHost string
13311306// esAPIKey is the API key of the elasticsearch to use in the test
13321307esAPIKey string
1308+ // esEncodedAPIKey is the encoded API key of the elasticsearch to use in the test
1309+ esEncodedAPIKey string
13331310// enrollParams contains the information needed to enroll an agent with Fleet in the test
13341311enrollParams * fleettools.EnrollParams
13351312// createdAt is the time when the k8sContext was created
@@ -1415,16 +1392,17 @@ func k8sGetContext(t *testing.T, info *define.Info) k8sContext {
14151392require .NoError (t , err , "failed to create fleet enroll params" )
14161393
14171394return k8sContext {
1418- client : client ,
1419- clientSet : clientSet ,
1420- agentImage : agentImage ,
1421- agentImageRepo : agentImageRepo ,
1422- agentImageTag : agentImageTag ,
1423- logsBasePath : testLogsBasePath ,
1424- esHost : esHost ,
1425- esAPIKey : esAPIKey ,
1426- enrollParams : enrollParams ,
1427- createdAt : time .Now (),
1395+ client : client ,
1396+ clientSet : clientSet ,
1397+ agentImage : agentImage ,
1398+ agentImageRepo : agentImageRepo ,
1399+ agentImageTag : agentImageTag ,
1400+ logsBasePath : testLogsBasePath ,
1401+ esHost : esHost ,
1402+ esAPIKey : esAPIKey .APIKey ,
1403+ esEncodedAPIKey : esAPIKey .Encoded ,
1404+ enrollParams : enrollParams ,
1405+ createdAt : time .Now (),
14281406}
14291407}
14301408
0 commit comments