forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Auditbeat] Cherry-pick elastic#11667 to 6.8: Log error when Homebrew…
… not installed (elastic#11949) * [Auditbeat] Log error when Homebrew not installed (elastic#11667) 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
May 3, 2019
1 parent
9b42cbb
commit 4f3b628
Showing
8 changed files
with
165 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package testing | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/elastic/beats/libbeat/paths" | ||
) | ||
|
||
// SetupDataDir sets up a temporary data directory to use for testing. | ||
func SetupDataDir(t testing.TB) func() { | ||
// path.data should be set so that the DB is written to a predictable location. | ||
var err error | ||
paths.Paths.Data, err = ioutil.TempDir("", "beat-data-dir") | ||
if err != nil { | ||
t.Fatal() | ||
} | ||
return func() { os.RemoveAll(paths.Paths.Data) } | ||
} |
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) | ||
} | ||
} |
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
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","[email protected]"],"runtime_dependencies":[],"source":{"path":"","tap":"homebrew/core","spec":"stable","versions":{"stable":"1.10.3","devel":"","head":"HEAD","version_scheme":0}}} |