From e6dda34cc6aad9d0ce45170789ec0221ad925f15 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Tue, 18 Jul 2023 16:08:37 -0700 Subject: [PATCH 1/3] Pass namespace to FQDN ES query --- testing/integration/fqdn_test.go | 43 ++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/testing/integration/fqdn_test.go b/testing/integration/fqdn_test.go index 758bab6e521..e5c7a60e007 100644 --- a/testing/integration/fqdn_test.go +++ b/testing/integration/fqdn_test.go @@ -6,6 +6,7 @@ package integration import ( + "bytes" "context" "encoding/json" "fmt" @@ -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{}{ @@ -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{}{ @@ -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( @@ -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()) @@ -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, From 59495fb28c924be265f242dcd8212d3030a64b0b Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Wed, 19 Jul 2023 16:49:26 -0700 Subject: [PATCH 2/3] Add documentation on namespace usage --- docs/test-framework-dev-guide.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/test-framework-dev-guide.md b/docs/test-framework-dev-guide.md index 6e9da26c886..ab788ae0b32 100644 --- a/docs/test-framework-dev-guide.md +++ b/docs/test-framework-dev-guide.md @@ -63,6 +63,22 @@ the `github.com/elastic/elastic-agent/pkg/testing/define` package for the test framework's API and the `github.com/elastic/elastic-agent/pkg/testing/tools` package for helper utilities. +### Test namespaces + +Every test has access to its own unique namespace (a string value). This namespace can +be accessed from the `info.Namespace` field, where `info` is the struct value returned +from the `define.Require(...)` call made at the start of the test. + +Namespaces should be used whenever test data is being written to or read from a store that's +shared across all tests. Most commonly, this store will be the Elasticsearch cluster that Agent +components may index their data into. All tests share a single stack deployment and, therefore, +a single Elasticsearch cluster as well. + +Some examples of where namespaces should be used: +* When creating a policy in Fleet. The Create Policy and Update Policy APIs takes a namespace parameter. +* When searching for documents in `logs-*` or `metrics-*` data streams. Every document in these + data streams has a `data_stream.namespace` field. + ## Troubleshooting Tips ### Error: GCE service token missing; run 'mage integration:auth' @@ -99,4 +115,4 @@ If we need to use a different version between agent and stack we can specify the using a separate env variable `AGENT_STACK_VERSION` like in this example (we used a custom package version for the agent): -```AGENT_VERSION="8.10.0-testpkgversion.1-SNAPSHOT" AGENT_STACK_VERSION="8.10.0-SNAPSHOT" mage integration:test``` \ No newline at end of file +```AGENT_VERSION="8.10.0-testpkgversion.1-SNAPSHOT" AGENT_STACK_VERSION="8.10.0-SNAPSHOT" mage integration:test``` From e30301562d4b1972c2497f9a262adc948383a324 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Wed, 19 Jul 2023 16:50:53 -0700 Subject: [PATCH 3/3] Add warning --- docs/test-framework-dev-guide.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/test-framework-dev-guide.md b/docs/test-framework-dev-guide.md index ab788ae0b32..aceecb5c15f 100644 --- a/docs/test-framework-dev-guide.md +++ b/docs/test-framework-dev-guide.md @@ -69,7 +69,7 @@ Every test has access to its own unique namespace (a string value). This namespa be accessed from the `info.Namespace` field, where `info` is the struct value returned from the `define.Require(...)` call made at the start of the test. -Namespaces should be used whenever test data is being written to or read from a store that's +Namespaces should be used whenever test data is being written to or read from a persistent store that's shared across all tests. Most commonly, this store will be the Elasticsearch cluster that Agent components may index their data into. All tests share a single stack deployment and, therefore, a single Elasticsearch cluster as well. @@ -79,6 +79,9 @@ Some examples of where namespaces should be used: * When searching for documents in `logs-*` or `metrics-*` data streams. Every document in these data streams has a `data_stream.namespace` field. +:warning: Not using namespaces when accessing data in a shared persistent store can cause tests to +be flaky. + ## Troubleshooting Tips ### Error: GCE service token missing; run 'mage integration:auth'