Skip to content

Commit 19d623a

Browse files
pchilardner
andauthored
Detect endpoint component IDs from agent state in TestEndpointLogsAreCollectedInDiagnostics (#4453)
This is needed after a change in default output name in the policy returned by Fleet --------- Co-authored-by: Denis <denis.rechkunov@elastic.co>
1 parent 7f83ddd commit 19d623a

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

testing/integration/endpoint_security_test.go

+41-15
Original file line numberDiff line numberDiff line change
@@ -721,16 +721,40 @@ func TestEndpointLogsAreCollectedInDiagnostics(t *testing.T) {
721721
"Endpoint component or units are not healthy.",
722722
)
723723

724+
// get endpoint component name
725+
endpointComponents := getEndpointComponents(ctx, t, fixture.Client())
726+
require.NotEmpty(t, endpointComponents, "there should be at least one endpoint component")
727+
728+
t.Logf("endpoint components: %v", endpointComponents)
729+
724730
outDir := t.TempDir()
725731
diagFile := t.Name() + ".zip"
726732
diagAbsPath := filepath.Join(outDir, diagFile)
727733
_, err = fixture.Exec(ctx, []string{"diagnostics", "-f", diagAbsPath})
728734
require.NoError(t, err, "diagnostics command failed")
729735
require.FileExists(t, diagAbsPath, "diagnostic archive should have been created")
730-
checkDiagnosticsForEndpointFiles(t, diagAbsPath)
736+
checkDiagnosticsForEndpointFiles(t, diagAbsPath, endpointComponents)
731737
}
732738

733-
func checkDiagnosticsForEndpointFiles(t *testing.T, diagsPath string) {
739+
func getEndpointComponents(ctx context.Context, t *testing.T, c client.Client) []string {
740+
741+
err := c.Connect(ctx)
742+
require.NoError(t, err, "connecting to agent to retrieve endpoint components")
743+
defer c.Disconnect()
744+
745+
agentState, err := c.State(ctx)
746+
require.NoError(t, err, "retrieving agent state")
747+
748+
var endpointComponents []string
749+
for _, componentState := range agentState.Components {
750+
if strings.Contains(componentState.Name, "endpoint") {
751+
endpointComponents = append(endpointComponents, componentState.ID)
752+
}
753+
}
754+
return endpointComponents
755+
}
756+
757+
func checkDiagnosticsForEndpointFiles(t *testing.T, diagsPath string, endpointComponents []string) {
734758
zipReader, err := zip.OpenReader(diagsPath)
735759
require.NoError(t, err, "error opening diagnostics archive")
736760

@@ -745,20 +769,22 @@ func checkDiagnosticsForEndpointFiles(t *testing.T, diagsPath string) {
745769
}
746770
t.Logf("---- End contents of diagnostics archive")
747771
// check there are files under the components/ directory
748-
endpointComponentDirName := "components/endpoint-default"
749-
endpointComponentDir, err := zipReader.Open(endpointComponentDirName)
750-
if assert.NoErrorf(t, err, "error looking up directory %q in diagnostic archive: %v", endpointComponentDirName, err) {
751-
defer func(endpointComponentDir fs.File) {
752-
err := endpointComponentDir.Close()
753-
if err != nil {
754-
assert.NoError(t, err, "error closing endpoint component directory")
772+
for _, componentName := range endpointComponents {
773+
endpointComponentDirName := fmt.Sprintf("components/%s", componentName)
774+
endpointComponentDir, err := zipReader.Open(endpointComponentDirName)
775+
if assert.NoErrorf(t, err, "error looking up directory %q for endpoint component %q in diagnostic archive: %v", endpointComponentDirName, componentName, err) {
776+
defer func(endpointComponentDir fs.File) {
777+
err := endpointComponentDir.Close()
778+
if err != nil {
779+
assert.NoError(t, err, "error closing endpoint component directory")
780+
}
781+
}(endpointComponentDir)
782+
if assert.Implementsf(t, (*fs.ReadDirFile)(nil), endpointComponentDir, "endpoint component %q should have a directory in the diagnostic archive under %s", componentName, endpointComponentDirName) {
783+
dirFile := endpointComponentDir.(fs.ReadDirFile)
784+
endpointFiles, err := dirFile.ReadDir(-1)
785+
assert.NoErrorf(t, err, "error reading endpoint component %q directory %q in diagnostic archive", componentName, endpointComponentDirName)
786+
assert.NotEmptyf(t, endpointFiles, "endpoint component %q directory should not be empty", componentName)
755787
}
756-
}(endpointComponentDir)
757-
if assert.Implementsf(t, (*fs.ReadDirFile)(nil), endpointComponentDir, "endpoint should have a directory in the diagnostic archive under %s", endpointComponentDirName) {
758-
dirFile := endpointComponentDir.(fs.ReadDirFile)
759-
endpointFiles, err := dirFile.ReadDir(-1)
760-
assert.NoError(t, err, "error reading endpoint component directory %q in diagnostic archive", endpointComponentDirName)
761-
assert.NotEmpty(t, endpointFiles, "endpoint component directory should not be empty")
762788
}
763789
}
764790

0 commit comments

Comments
 (0)