Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect endpoint component IDs from agent state in TestEndpointLogsAreCollectedInDiagnostics #4453

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions testing/integration/endpoint_security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,40 @@ func TestEndpointLogsAreCollectedInDiagnostics(t *testing.T) {
"Endpoint component or units are not healthy.",
)

// get endpoint component name
endpointComponents := getEndpointComponents(ctx, t, fixture.Client())
require.NotEmpty(t, endpointComponents, "there should be at least one endpoint component")

t.Logf("endpoint components: %v", endpointComponents)

outDir := t.TempDir()
diagFile := t.Name() + ".zip"
diagAbsPath := filepath.Join(outDir, diagFile)
_, err = fixture.Exec(ctx, []string{"diagnostics", "-f", diagAbsPath})
require.NoError(t, err, "diagnostics command failed")
require.FileExists(t, diagAbsPath, "diagnostic archive should have been created")
checkDiagnosticsForEndpointFiles(t, diagAbsPath)
checkDiagnosticsForEndpointFiles(t, diagAbsPath, endpointComponents)
}

func checkDiagnosticsForEndpointFiles(t *testing.T, diagsPath string) {
func getEndpointComponents(ctx context.Context, t *testing.T, c client.Client) []string {

err := c.Connect(ctx)
require.NoError(t, err, "connecting to agent to retrieve endpoint components")
defer c.Disconnect()

agentState, err := c.State(ctx)
require.NoError(t, err, "retrieving agent state")

var endpointComponents []string
for _, componentState := range agentState.Components {
if strings.Contains(componentState.Name, "endpoint") {
endpointComponents = append(endpointComponents, componentState.ID)
}
}
return endpointComponents
}

func checkDiagnosticsForEndpointFiles(t *testing.T, diagsPath string, endpointComponents []string) {
zipReader, err := zip.OpenReader(diagsPath)
require.NoError(t, err, "error opening diagnostics archive")

Expand All @@ -745,20 +769,22 @@ func checkDiagnosticsForEndpointFiles(t *testing.T, diagsPath string) {
}
t.Logf("---- End contents of diagnostics archive")
// check there are files under the components/ directory
endpointComponentDirName := "components/endpoint-default"
endpointComponentDir, err := zipReader.Open(endpointComponentDirName)
if assert.NoErrorf(t, err, "error looking up directory %q in diagnostic archive: %v", endpointComponentDirName, err) {
defer func(endpointComponentDir fs.File) {
err := endpointComponentDir.Close()
if err != nil {
assert.NoError(t, err, "error closing endpoint component directory")
for _, componentName := range endpointComponents {
endpointComponentDirName := fmt.Sprintf("components/%s", componentName)
endpointComponentDir, err := zipReader.Open(endpointComponentDirName)
if assert.NoErrorf(t, err, "error looking up directory %q for endpoint component %q in diagnostic archive: %v", endpointComponentDirName, componentName, err) {
defer func(endpointComponentDir fs.File) {
err := endpointComponentDir.Close()
if err != nil {
assert.NoError(t, err, "error closing endpoint component directory")
}
}(endpointComponentDir)
if assert.Implementsf(t, (*fs.ReadDirFile)(nil), endpointComponentDir, "endpoint component %q should have a directory in the diagnostic archive under %s", componentName, endpointComponentDirName) {
dirFile := endpointComponentDir.(fs.ReadDirFile)
endpointFiles, err := dirFile.ReadDir(-1)
assert.NoErrorf(t, err, "error reading endpoint component %q directory %q in diagnostic archive", componentName, endpointComponentDirName)
assert.NotEmptyf(t, endpointFiles, "endpoint component %q directory should not be empty", componentName)
}
}(endpointComponentDir)
if assert.Implementsf(t, (*fs.ReadDirFile)(nil), endpointComponentDir, "endpoint should have a directory in the diagnostic archive under %s", endpointComponentDirName) {
dirFile := endpointComponentDir.(fs.ReadDirFile)
endpointFiles, err := dirFile.ReadDir(-1)
assert.NoError(t, err, "error reading endpoint component directory %q in diagnostic archive", endpointComponentDirName)
assert.NotEmpty(t, endpointFiles, "endpoint component directory should not be empty")
}
}

Expand Down
Loading