Skip to content

Commit

Permalink
findESDocs ensures non-zero documents
Browse files Browse the repository at this point in the history
findESDocs now ensures the function returns at least one document.

Debug logs are also removed.
  • Loading branch information
belimawr committed Nov 27, 2023
1 parent a081090 commit c1e4f96
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
14 changes: 0 additions & 14 deletions pkg/testing/tools/estools/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ func CheckForErrorsInLogsWithContext(ctx context.Context, client elastictranspor
}

return performQueryForRawQuery(ctx, queryRaw, "logs-elastic_agent*", client)

}

// GetLogsForDataset returns any logs associated with the datastream
Expand Down Expand Up @@ -535,19 +534,6 @@ func performQueryForRawQuery(ctx context.Context, queryRaw map[string]interface{

docs, err := handleDocsResponse(res)

fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
fmt.Println("Query:")
query, debugErr := json.MarshalIndent(queryRaw, "|", " ")
if debugErr != nil {
fmt.Println("Error marshalling 'queryRaw':", debugErr)
return docs, err
}
fmt.Println("Raw Query")
fmt.Println(string(query))
fmt.Println("Documents docs.Hits.Total.Value: ", docs.Hits.Total.Value)
fmt.Println("Documents len(docs.Hits.Hits): ", len(docs.Hits.Hits))
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")

return docs, err
}

Expand Down
32 changes: 29 additions & 3 deletions testing/integration/logs_ingestion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func testMonitoringLogsAreShipped(

// Stage 3: Make sure there are no errors in logs
t.Log("Making sure there are no error logs")
docs = findESDocs(t, func() (estools.Documents, error) {
docs = queryESDocs(t, func() (estools.Documents, error) {
return estools.CheckForErrorsInLogs(info.ESClient, info.Namespace, []string{
// acceptable error messages (include reason)
"Error dialing dial tcp 127.0.0.1:9200: connect: connection refused", // beat is running default config before its config gets updated
Expand Down Expand Up @@ -164,7 +164,6 @@ func testMonitoringLogsAreShipped(
// this field is not mapped. There is an issue for that:
// https://github.com/elastic/integrations/issues/6545
// TODO: use runtime fields while the above issue is not resolved.

docs = findESDocs(t, func() (estools.Documents, error) {
return estools.GetLogsForAgentID(info.ESClient, agentID)
})
Expand Down Expand Up @@ -194,13 +193,18 @@ func testMonitoringLogsAreShipped(
}
}

func findESDocs(t *testing.T, findFn func() (estools.Documents, error)) estools.Documents {
// queryESDocs runs `findFn` until it returns no error. Zero documents returned
// is considered a success.
func queryESDocs(t *testing.T, findFn func() (estools.Documents, error)) estools.Documents {
var docs estools.Documents
require.Eventually(
t,
func() bool {
var err error
docs, err = findFn()
if err != nil {
t.Logf("got an error querying ES, retrying. Error: %s", err)
}
return err == nil
},
3*time.Minute,
Expand All @@ -210,6 +214,28 @@ func findESDocs(t *testing.T, findFn func() (estools.Documents, error)) estools.
return docs
}

// findESDocs runs `findFn` until at least one document is returned and there is no error
func findESDocs(t *testing.T, findFn func() (estools.Documents, error)) estools.Documents {
var docs estools.Documents
require.Eventually(
t,
func() bool {
var err error
docs, err = findFn()
if err != nil {
t.Logf("got an error querying ES, retrying. Error: %s", err)
return false
}

return docs.Hits.Total.Value != 0
},
3*time.Minute,
15*time.Second,
)

return docs
}

func testFlattenedDatastreamFleetPolicy(
t *testing.T,
ctx context.Context,
Expand Down

0 comments on commit c1e4f96

Please sign in to comment.