-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Change `package` dataset on macOS to logging an error when Homebrew is not installed instead of failing to start altogether. It will keep checking and start sending events as usual in case Homebrew is installed later. (cherry picked from commit bbe4991)
- Loading branch information
Christoph Wurm
authored
Apr 30, 2019
1 parent
2441249
commit d67ffc3
Showing
6 changed files
with
125 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
x-pack/auditbeat/module/system/package/package_homebrew_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
// +build !windows | ||
|
||
package pkg | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/auditbeat/core" | ||
abtest "github.com/elastic/beats/auditbeat/testing" | ||
"github.com/elastic/beats/libbeat/beat" | ||
mbtest "github.com/elastic/beats/metricbeat/mb/testing" | ||
) | ||
|
||
func TestHomebrew(t *testing.T) { | ||
defer abtest.SetupDataDir(t)() | ||
|
||
oldPath := homebrewCellarPath | ||
defer func() { | ||
homebrewCellarPath = oldPath | ||
}() | ||
homebrewCellarPath = "testdata/homebrew/" | ||
|
||
// Test just listBrewPackages() | ||
packages, err := listBrewPackages() | ||
assert.NoError(t, err) | ||
if assert.Len(t, packages, 1) { | ||
pkg := packages[0] | ||
assert.Equal(t, "test-package", pkg.Name) | ||
assert.Equal(t, "Test package", pkg.Summary) | ||
assert.Equal(t, "https://www.elastic.co/", pkg.URL) | ||
assert.Equal(t, "1.0.0", pkg.Version) | ||
} | ||
|
||
// Test whole dataset if on Darwin | ||
if runtime.GOOS == "darwin" { | ||
f := mbtest.NewReportingMetricSetV2(t, getConfig()) | ||
defer f.(*MetricSet).bucket.DeleteBucket() | ||
|
||
events, errs := mbtest.ReportingFetchV2(f) | ||
if len(errs) > 0 { | ||
t.Fatalf("received error: %+v", errs[0]) | ||
} | ||
|
||
if assert.Len(t, events, 1) { | ||
event := mbtest.StandardizeEvent(f, events[0], core.AddDatasetToEvent) | ||
checkFieldValue(t, event, "system.audit.package.name", "test-package") | ||
checkFieldValue(t, event, "system.audit.package.summary", "Test package") | ||
checkFieldValue(t, event, "system.audit.package.url", "https://www.elastic.co/") | ||
checkFieldValue(t, event, "system.audit.package.version", "1.0.0") | ||
checkFieldValue(t, event, "system.audit.package.entity_id", "Krm421rtYM4wgq1S") | ||
} | ||
} | ||
} | ||
|
||
func checkFieldValue(t *testing.T, event beat.Event, fieldName string, fieldValue interface{}) { | ||
value, err := event.GetValue(fieldName) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, fieldValue, value) | ||
} | ||
} | ||
|
||
func TestHomebrewNotExist(t *testing.T) { | ||
defer abtest.SetupDataDir(t)() | ||
|
||
oldPath := homebrewCellarPath | ||
defer func() { | ||
homebrewCellarPath = oldPath | ||
}() | ||
homebrewCellarPath = "/does/not/exist" | ||
|
||
// Test just listBrewPackages() | ||
packages, err := listBrewPackages() | ||
if assert.Error(t, err) { | ||
assert.True(t, os.IsNotExist(err), "Unexpected error %v", err) | ||
} | ||
assert.Empty(t, packages) | ||
|
||
// Test whole dataset if on Darwin | ||
if runtime.GOOS == "darwin" { | ||
f := mbtest.NewReportingMetricSetV2(t, getConfig()) | ||
defer f.(*MetricSet).bucket.DeleteBucket() | ||
|
||
events, errs := mbtest.ReportingFetchV2(f) | ||
if len(errs) > 0 { | ||
t.Fatalf("received error: %+v", errs[0]) | ||
} | ||
assert.Empty(t, events) | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...uditbeat/module/system/package/testdata/homebrew/test-package/1.0.0/.brew/test-package.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Test < Formula | ||
desc "Test package" | ||
homepage "https://www.elastic.co/" | ||
end |
1 change: 1 addition & 0 deletions
1
...auditbeat/module/system/package/testdata/homebrew/test-package/1.0.0/INSTALL_RECEIPT.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"homebrew_version":"1.6.7-44-g1bfec50","used_options":[],"unused_options":["--without-cgo","--without-race"],"built_as_bottle":true,"poured_from_bottle":true,"installed_as_dependency":false,"installed_on_request":true,"changed_files":["INSTALL_RECEIPT.json","libexec/pkg/bootstrap/src/bootstrap/cmd/cgo/doc.go","libexec/src/cmd/cgo/doc.go","libexec/src/cmd/vendor/golang.org/x/arch/arm/armasm/objdumpext_test.go","libexec/src/crypto/x509/root_bsd.go","libexec/src/crypto/x509/root_unix.go","libexec/src/mime/type_dragonfly.go","libexec/src/mime/type_freebsd.go","libexec/src/net/http/cgi/host.go","libexec/src/path/filepath/example_unix_test.go"],"time":1533834877,"source_modified_time":1528329050,"HEAD":null,"stdlib":null,"compiler":"clang","aliases":["google-go","golang","go@1.10"],"runtime_dependencies":[],"source":{"path":"","tap":"homebrew/core","spec":"stable","versions":{"stable":"1.10.3","devel":"","head":"HEAD","version_scheme":0}}} |