Skip to content

Commit

Permalink
Pass namespace to FQDN ES query
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Jul 18, 2023
1 parent 7e6b4f1 commit e6dda34
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions testing/integration/fqdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package integration

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -98,8 +99,8 @@ func TestFQDN(t *testing.T) {
agent := verifyAgentName(t, shortName, info.KibanaClient)

t.Log("Verify that hostname in `logs-*` and `metrics-*` is short hostname")
verifyHostNameInIndices(t, "logs-*", shortName, info.ESClient)
verifyHostNameInIndices(t, "metrics-*", shortName, info.ESClient)
verifyHostNameInIndices(t, "logs-*", shortName, info.Namespace, info.ESClient)
verifyHostNameInIndices(t, "metrics-*", shortName, info.Namespace, info.ESClient)

t.Log("Update Agent policy to enable FQDN")
policy.AgentFeatures = []map[string]interface{}{
Expand Down Expand Up @@ -129,8 +130,8 @@ func TestFQDN(t *testing.T) {
verifyAgentName(t, fqdn, info.KibanaClient)

t.Log("Verify that hostname in `logs-*` and `metrics-*` is FQDN")
verifyHostNameInIndices(t, "logs-*", fqdn, info.ESClient)
verifyHostNameInIndices(t, "metrics-*", fqdn, info.ESClient)
verifyHostNameInIndices(t, "logs-*", fqdn, info.Namespace, info.ESClient)
verifyHostNameInIndices(t, "metrics-*", fqdn, info.Namespace, info.ESClient)

t.Log("Update Agent policy to disable FQDN")
policy.AgentFeatures = []map[string]interface{}{
Expand Down Expand Up @@ -185,7 +186,34 @@ func verifyAgentName(t *testing.T, hostname string, kibClient *kibana.Client) *k
return agent
}

func verifyHostNameInIndices(t *testing.T, indices, hostname string, esClient *elasticsearch.Client) {
func verifyHostNameInIndices(t *testing.T, indices, hostname, namespace string, esClient *elasticsearch.Client) {
queryRaw := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{
{
"term": map[string]interface{}{
"host.name": map[string]interface{}{
"value": hostname,
},
},
},
{
"term": map[string]interface{}{
"data_stream.namespace": map[string]interface{}{
"value": namespace,
},
},
},
},
},
},
}

var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(queryRaw)
require.NoError(t, err)

search := esClient.Search

require.Eventually(
Expand All @@ -196,6 +224,7 @@ func verifyHostNameInIndices(t *testing.T, indices, hostname string, esClient *e
search.WithSort("@timestamp:desc"),
search.WithFilterPath("hits.hits"),
search.WithSize(1),
search.WithBody(&buf),
)
require.NoError(t, err)
require.False(t, resp.IsError())
Expand All @@ -216,9 +245,7 @@ func verifyHostNameInIndices(t *testing.T, indices, hostname string, esClient *e
err = decoder.Decode(&body)
require.NoError(t, err)

require.Len(t, body.Hits.Hits, 1)
hit := body.Hits.Hits[0]
return hostname == hit.Source.Host.Name
return len(body.Hits.Hits) == 1
},
2*time.Minute,
5*time.Second,
Expand Down

0 comments on commit e6dda34

Please sign in to comment.