Skip to content

Commit

Permalink
[skip ci] commit a8dddfe
Browse files Browse the repository at this point in the history
  • Loading branch information
dd-mergequeue[bot] authored Nov 28, 2024
2 parents 19733fb + a8dddfe commit beb110a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
13 changes: 11 additions & 2 deletions pkg/collector/corechecks/servicediscovery/usm/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,23 @@ func (p pythonDetector) detect(args []string) (ServiceMetadata, bool) {
if value, ok := p.deducePackageName(path.Clean(stripped), filename); ok {
return NewServiceMetadata(value), true
}
return NewServiceMetadata(p.findNearestTopLevel(stripped)), true

name := p.findNearestTopLevel(stripped)
// If we have generic/useless directory names, fallback to the filename.
if name == "." || name == "/" || name == "bin" || name == "sbin" {
name = p.findNearestTopLevel(filename)
}

return NewServiceMetadata(name), true
}

if hasFlagPrefix && a == "-m" {
moduleFlag = true
}

prevArgIsFlag = hasFlagPrefix
// The -u (unbuffered) option doesn't take an argument so we should
// consider the next arg even though this one is a flag.
prevArgIsFlag = hasFlagPrefix && a != "-u"
}

return ServiceMetadata{}, false
Expand Down
32 changes: 29 additions & 3 deletions pkg/collector/corechecks/servicediscovery/usm/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

//go:build !windows

package usm

import (
Expand All @@ -16,8 +18,9 @@ import (
)

func TestPythonDetect(t *testing.T) {
//prepare the in mem fs
memFs := fstest.MapFS{
// Wrap the MapFS in a SubDirFS since we want to test absolute paths and the
// former doesn't allow them in calls to Open().
memFs := SubDirFS{FS: fstest.MapFS{
"modules/m1/first/nice/package/__init__.py": &fstest.MapFile{},
"modules/m1/first/nice/__init__.py": &fstest.MapFile{},
"modules/m1/first/nice/something.py": &fstest.MapFile{},
Expand All @@ -28,7 +31,9 @@ func TestPythonDetect(t *testing.T) {
"apps/app2/cmd/run.py": &fstest.MapFile{},
"apps/app2/setup.py": &fstest.MapFile{},
"example.py": &fstest.MapFile{},
}
"usr/bin/pytest": &fstest.MapFile{},
"bin/WALinuxAgent.egg": &fstest.MapFile{},
}}
tests := []struct {
name string
cmd string
Expand Down Expand Up @@ -79,6 +84,27 @@ func TestPythonDetect(t *testing.T) {
cmd: "python example.py",
expected: "example",
},
{
name: "root level script",
cmd: "python /example.py",
expected: "example",
},
{
name: "root level script with ..",
// This results in a path of "." after findNearestTopLevel is called on the split path.
cmd: "python /../example.py",
expected: "example",
},
{
name: "script in bin",
cmd: "python /usr/bin/pytest",
expected: "pytest",
},
{
name: "script in bin with -u",
cmd: "python3 -u bin/WALinuxAgent.egg",
expected: "WALinuxAgent",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit beb110a

Please sign in to comment.