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

Add tests, fallback properly if we can't init the cgroups #41189

Merged
Merged
Show file tree
Hide file tree
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
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) {
Kavindu-Dodan marked this conversation as resolved.
Show resolved Hide resolved
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
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
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
Loading