Skip to content

Commit

Permalink
Add tests, fallback properly if we can't init the cgroups (#41189)
Browse files Browse the repository at this point in the history
* add tests, fallback properly if we can't init the cgroups

* linter

* fix test
  • Loading branch information
fearful-symmetry authored Oct 10, 2024
1 parent f369a28 commit 648ec22
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func buildDockerMetadataProcessor(log *logp.Logger, cfg *conf.C, watcherConstruc
}

reader, err := initCgroupPaths(resolve.NewTestResolver(config.HostFS), false)
if err != nil {
if err != nil && !errors.Is(err, cgroup.ErrCgroupsMissing) {
return nil, fmt.Errorf("error creating cgroup reader: %w", err)
}

Expand Down Expand Up @@ -284,6 +284,9 @@ func (d *addDockerMetadata) getProcessCgroups(pid int) (cgroup.PathList, error)
return cgroups, nil
}

if d.cgreader == nil {
return cgroups, fs.ErrNotExist
}
cgroups, err := d.cgreader.ProcessCgroupPaths(pid)
if err != nil {
return cgroups, fmt.Errorf("failed to read cgroups for pid=%v: %w", pid, err)
Expand Down
27 changes: 27 additions & 0 deletions libbeat/processors/add_docker_metadata/add_docker_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/processors"
Expand Down Expand Up @@ -70,6 +71,32 @@ func init() {
}
}

func TestDefaultProcessorStartup(t *testing.T) {
// set initCgroupPaths to system non-test defaults
initCgroupPaths = func(rootfsMountpoint resolve.Resolver, ignoreRootCgroups bool) (processors.CGReader, error) {
return cgroup.NewReader(rootfsMountpoint, ignoreRootCgroups)
}

defer func() {
initCgroupPaths = func(_ resolve.Resolver, _ bool) (processors.CGReader, error) {
return testCGReader{}, nil
}
}()

rawCfg := defaultConfig()
cfg, err := config.NewConfigFrom(rawCfg)
require.NoError(t, err)

proc, err := buildDockerMetadataProcessor(logp.L(), cfg, docker.NewWatcher)
require.NoError(t, err)

unwrapped, _ := proc.(*addDockerMetadata)

// make sure pid readers have been initialized properly
_, err = unwrapped.getProcessCgroups(os.Getpid())
require.NoError(t, err)
}

func TestInitializationNoDocker(t *testing.T) {
var testConfig = config.NewConfig()
testConfig.SetString("host", -1, "unix:///var/run42/docker.sock")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func newProcessMetadataProcessorWithProvider(config config, provider processMeta
}

reader, err := initCgroupPaths(resolve.NewTestResolver(config.HostPath), false)
if err != nil {
if err != nil && !errors.Is(err, cgroup.ErrCgroupsMissing) {
return nil, fmt.Errorf("error creating cgroup reader: %w", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"unsafe"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common/capabilities"
Expand All @@ -53,6 +54,22 @@ func newCGHandlerBuilder(handler testCGRsolver) processors.InitCgroupHandler {
}
}

func TestDefaultProcessorStartup(t *testing.T) {
// set initCgroupPaths to system non-test defaults
initCgroupPaths = func(rootfsMountpoint resolve.Resolver, ignoreRootCgroups bool) (processors.CGReader, error) {
return cgroup.NewReader(rootfsMountpoint, ignoreRootCgroups)
}

proc, err := newProcessMetadataProcessorWithProvider(defaultConfig(), &procCache, false)
require.NoError(t, err)

// ensure the underlying provider has been initialized properly
unwrapped, _ := proc.(*addProcessMetadata)
metadata, err := unwrapped.provider.GetProcessMetadata(os.Getpid())
require.NoError(t, err)
require.NotNil(t, metadata)
}

func TestAddProcessMetadata(t *testing.T) {
logp.TestingSetup(logp.WithSelectors(processorName))

Expand Down
13 changes: 4 additions & 9 deletions libbeat/processors/add_process_metadata/gosigar_cid_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
package add_process_metadata

import (
"errors"
"fmt"
"io/fs"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -82,15 +80,12 @@ func newCidProvider(cgroupPrefixes []string, cgroupRegex *regexp.Regexp, process
// getProcessCgroups returns a mapping of cgroup subsystem name to path. It
// returns an error if it failed to retrieve the cgroup info.
func (p gosigarCidProvider) getProcessCgroups(pid int) (cgroup.PathList, error) {
//return nil if we aren't supporting cgroups
if p.processCgroupPaths == nil {
return cgroup.PathList{}, nil
}
pathList, err := p.processCgroupPaths.ProcessCgroupPaths(pid)
if err != nil {
var pathError *fs.PathError
if errors.As(err, &pathError) {
// do no thing when err is nil or when os.PathError happens because the process don't exist,
// or not running in linux system
return cgroup.PathList{}, nil
}
// should never happen
return cgroup.PathList{}, fmt.Errorf("failed to read cgroups for pid=%v: %w", pid, err)
}

Expand Down

0 comments on commit 648ec22

Please sign in to comment.