Files
syft/syft/pkg/cataloger/python/dependency_test.go
T
Alex Goodman b5e85c3ea5 chore: migrate fixtures to testdata (#4651)
* migrate fixtures to testdata

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix: correct broken symlinks after testdata migration

The migration from test-fixtures to testdata broke several symlinks:
- elf-test-fixtures symlinks pointed to old test-fixtures paths
- elf-test-fixtures needed to be renamed to elf-testdata
- image-pkg-coverage symlink pointed to test-fixtures instead of testdata

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix: handle missing classifiers/bin directory in Makefile

The clean-fingerprint target was failing when classifiers/bin doesn't
exist (e.g., on fresh clone without downloaded binaries).

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix: add gitignore negation for jar/zip fixtures in test/cli

The jar and zip files in test/cli/testdata/image-unknowns were being
gitignored by the root .gitignore patterns. This caused them to be
untracked and not included when building docker images in CI, resulting
in Test_Unknowns failures since the test expects errors from corrupt
archive files that weren't present.

Add a .gitignore in test/cli/testdata to negate the exclusions for
these specific test fixture files.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* switch fixture cache to v2

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* test: update expected versions for rebuilt fixtures

Update test expectations for packages that have been updated in
upstream repositories when docker images are rebuilt:
- glibc: 2.42-r4 → 2.43-r1 (wolfi)
- php: 8.2.29 → 8.2.30 (ubuntu/apache)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* upgrade go

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix: add go-shlex dependency for testdata manager tool

The manager tool in syft/pkg/cataloger/binary/testdata/ imports
go-shlex, but since it's in a testdata directory, Go doesn't track
its dependencies. This caused CI failures when go.mod didn't
explicitly list the dependency.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* refactor: move binary classifier manager to internal/

Move the manager tool from testdata/manager to internal/manager so
that Go properly tracks its dependencies. Code in testdata directories
is ignored by Go for dependency tracking, which caused CI failures
when go.mod didn't explicitly list transitive dependencies.

This is a cleaner solution than manually adding dependencies to go.mod
for code that happens to live in testdata.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix: add gitignore negations for test fixtures blocked by root patterns

Multiple test fixtures were being blocked by root-level gitignore patterns
like bin/, *.jar, *.tar, and *.exe. This adds targeted .gitignore files with
negation patterns to allow these specific test fixtures to be tracked:

- syft/linux/testdata/os/busybox/bin/busybox (blocked by bin/)
- syft/pkg/cataloger/java/testdata/corrupt/example.{jar,tar} (blocked by *.jar, *.tar)
- syft/pkg/cataloger/binary/testdata/classifiers/snippets/go-version-hint/**/bin/go (blocked by bin/)
- syft/pkg/cataloger/bitnami/testdata/no-rel/.../bin/redis-server (blocked by bin/)

Also updates the bitnami test expectation to include the newly required
.gitignore files in the test fixture.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* test: update glibc version expectation (2.43-r1 -> 2.43-r2)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* add capability drift check as unit step

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* dont clear test observations before drift detection

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* bump stereoscope commit to main

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
2026-03-06 19:42:04 +00:00

729 lines
17 KiB
Go

package python
import (
"context"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/internal/dependency"
)
func Test_poetryLockDependencySpecifier(t *testing.T) {
tests := []struct {
name string
p pkg.Package
want dependency.Specification
}{
{
name: "no dependencies",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPoetryLockEntry{
Dependencies: []pkg.PythonPoetryLockDependencyEntry{},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
},
},
},
{
name: "with required dependencies",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPoetryLockEntry{
Dependencies: []pkg.PythonPoetryLockDependencyEntry{
{
Name: "bar",
Version: "1.2.3",
},
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
Requires: []string{"bar"},
},
},
},
{
name: "with optional dependencies (explicit)",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPoetryLockEntry{
Dependencies: []pkg.PythonPoetryLockDependencyEntry{
{
Name: "bar",
Version: "1.2.3",
Optional: true,
},
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
Requires: []string{"bar"},
},
},
},
{
name: "without dependencies for non-required extra",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPoetryLockEntry{
Dependencies: []pkg.PythonPoetryLockDependencyEntry{
{
Name: "bar",
Version: "1.2.3",
Optional: true,
Markers: "extra == 'baz'",
},
},
// note: there is no "baz" extra defined
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
Requires: nil, // no requirements for non-required extra
},
},
},
{
name: "package with extra",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPoetryLockEntry{
Dependencies: []pkg.PythonPoetryLockDependencyEntry{
{
Name: "bar", // note: we NEVER reference this, the extras section is the source of truth here
Version: "1.2.3",
Optional: true,
Markers: "extra == 'baz'",
},
},
Extras: []pkg.PythonPoetryLockExtraEntry{
{
Name: "baz",
Dependencies: []string{
"qux",
},
},
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
Requires: nil, // no requirements for non-required extra
},
Variants: []dependency.ProvidesRequires{
{
Provides: []string{"foo[baz]"},
Requires: []string{"qux"},
},
},
},
},
{
name: "package using extra",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPoetryLockEntry{
Dependencies: []pkg.PythonPoetryLockDependencyEntry{
{
Name: "starlette",
Version: ">=0.37.2,<0.38.0",
},
{
Name: "bar",
Version: "1.2.3",
Extras: []string{"standard", "things"}, // note multiple extras needed when installing
},
},
Extras: []pkg.PythonPoetryLockExtraEntry{
{
Name: "baz",
Dependencies: []string{
"qux (>=2.0.0)", // should strip version constraint
},
},
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
Requires: []string{
"starlette",
// note: we break out the package and extra requirements separately
// and extras are never combined
"bar",
"bar[standard]",
"bar[things]",
},
},
Variants: []dependency.ProvidesRequires{
{
Provides: []string{"foo[baz]"},
Requires: []string{"qux"},
},
},
},
},
{
name: "dependency names with mixed case should be normalized",
p: pkg.Package{
Name: "dj-rest-auth",
Metadata: pkg.PythonPoetryLockEntry{
Dependencies: []pkg.PythonPoetryLockDependencyEntry{
{
Name: "Django", // note: capital D
Version: ">=4.2,<6.0",
},
{
Name: "djangorestframework",
Version: ">=3.13.0",
},
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"dj-rest-auth"},
Requires: []string{"django", "djangorestframework"}, // "Django" should be normalized to "django"
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, poetryLockDependencySpecifier(tt.p))
})
}
}
func Test_poetryLockDependencySpecifier_againstPoetryLock(t *testing.T) {
tests := []struct {
name string
fixture string
want []dependency.Specification
}{
{
name: "case-insensitive dependency resolution",
fixture: "testdata/poetry/case-sensitivity/poetry.lock",
want: []dependency.Specification{
// packages are in the order they appear in the lock file
{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"django"},
Requires: []string{"asgiref", "sqlparse"},
},
},
{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"djangorestframework"},
Requires: []string{"django"},
},
},
{
// dj-rest-auth depends on Django (capital D) which should resolve to django
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"dj-rest-auth"},
Requires: []string{"django", "djangorestframework"}, // Django normalized to django
},
Variants: []dependency.ProvidesRequires{
{
Provides: []string{"dj-rest-auth[with-social]"},
Requires: []string{"django-allauth"},
},
},
},
},
},
{
name: "simple dependencies with extras",
fixture: "testdata/poetry/simple-deps/poetry.lock",
want: []dependency.Specification{
{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"certifi"},
},
},
{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"charset-normalizer"},
},
},
{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"idna"},
},
},
{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"requests"},
Requires: []string{"certifi", "charset-normalizer", "idna", "urllib3"},
},
Variants: []dependency.ProvidesRequires{
{
Provides: []string{"requests[socks]"},
Requires: []string{"pysocks"},
},
{
Provides: []string{"requests[use-chardet-on-py3]"},
Requires: []string{"chardet"},
},
},
},
{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"urllib3"},
},
Variants: []dependency.ProvidesRequires{
{
Provides: []string{"urllib3[brotli]"},
Requires: []string{"brotli", "brotlicffi"},
},
{
Provides: []string{"urllib3[h2]"},
Requires: []string{"h2"}},
{
Provides: []string{"urllib3[socks]"},
Requires: []string{"pysocks"},
},
{
Provides: []string{"urllib3[zstd]"},
Requires: []string{"zstandard"},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fh, err := os.Open(tt.fixture)
require.NoError(t, err)
plp := newPoetryLockParser(DefaultCatalogerConfig())
pkgs, err := plp.poetryLockPackages(context.TODO(), file.NewLocationReadCloser(file.NewLocation(tt.fixture), fh))
require.NoError(t, err)
var got []dependency.Specification
for _, p := range pkgs {
got = append(got, poetryLockDependencySpecifier(p))
}
if d := cmp.Diff(tt.want, got); d != "" {
t.Errorf("wrong result (-want +got):\n%s", d)
}
})
}
}
// Test_packageRef verifies that package references are normalized according to
// the Python Packaging specification for names and extras:
// https://packaging.python.org/en/latest/specifications/name-normalization/
func Test_packageRef(t *testing.T) {
tests := []struct {
name string
pkg string
extra string
want string
}{
{
name: "simple package name",
pkg: "requests",
want: "requests",
},
{
name: "package with extra",
pkg: "requests",
extra: "security",
want: "requests[security]",
},
{
name: "package name with mixed case",
pkg: "Django",
want: "django",
},
{
name: "package name with underscores",
pkg: "some_package",
want: "some-package",
},
{
name: "package name with mixed case and extra",
pkg: "Django",
extra: "argon2",
want: "django[argon2]",
},
{
name: "extra with mixed case",
pkg: "package",
extra: "Security",
want: "package[security]",
},
{
name: "both with mixed case and separators",
pkg: "Some_Package",
extra: "Dev_Extra",
want: "some-package[dev-extra]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := packageRef(tt.pkg, tt.extra)
assert.Equal(t, tt.want, got)
})
}
}
func Test_extractPackageName(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "simple package name",
input: "requests",
want: "requests",
},
{
name: "package with version constraint",
input: "requests >= 2.8.1",
want: "requests",
},
{
name: "package with parentheses version constraint",
input: "requests (>= 2.8.1)",
want: "requests",
},
{
name: "package with extras",
input: "requests[security,tests]",
want: "requests",
},
{
name: "package with extras and version",
input: "requests[security] >= 2.8.1",
want: "requests",
},
{
name: "package with environment marker",
input: "requests ; python_version < \"2.7\"",
want: "requests",
},
{
name: "package with everything",
input: "requests[security] >= 2.8.1 ; python_version < \"3\"",
want: "requests",
},
{
name: "package name with capitals (normalization test)",
input: "Werkzeug (>=0.15)",
want: "werkzeug",
},
{
name: "package name with mixed case",
input: "Jinja2 (>=2.10.1)",
want: "jinja2",
},
{
name: "package name with underscores",
input: "some_package >= 1.0",
want: "some-package",
},
{
name: "package name with mixed separators",
input: "Some_Package.Name >= 1.0",
want: "some-package-name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractPackageName(tt.input)
assert.Equal(t, tt.want, got)
})
}
}
func Test_wheelEggDependencySpecifier(t *testing.T) {
tests := []struct {
name string
p pkg.Package
want dependency.Specification
}{
{
name: "no dependencies",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPackage{
RequiresDist: []string{},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
},
},
},
{
name: "simple dependencies",
p: pkg.Package{
Name: "requests",
Metadata: pkg.PythonPackage{
RequiresDist: []string{
"certifi>=2017.4.17",
"urllib3<1.27,>=1.21.1",
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"requests"},
Requires: []string{"certifi", "urllib3"},
},
},
},
{
name: "dependencies with capital letters (Flask-like)",
p: pkg.Package{
Name: "flask",
Metadata: pkg.PythonPackage{
RequiresDist: []string{
"Werkzeug (>=0.15)",
"Jinja2 (>=2.10.1)",
"itsdangerous (>=0.24)",
"click (>=5.1)",
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"flask"},
// Requires are returned in the order they appear in RequiresDist
Requires: []string{"werkzeug", "jinja2", "itsdangerous", "click"},
},
},
},
{
name: "dependencies with extras",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPackage{
RequiresDist: []string{
"bar >= 1.0",
"pytest ; extra == 'dev'",
"sphinx ; extra == 'docs'",
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
Requires: []string{"bar", "pytest", "sphinx"},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, wheelEggDependencySpecifier(tt.p))
})
}
}
func Test_pdmLockDependencySpecifier(t *testing.T) {
tests := []struct {
name string
p pkg.Package
want dependency.Specification
}{
{
name: "no dependencies",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPdmLockEntry{
Dependencies: []string{},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
},
},
},
{
name: "with simple dependencies",
p: pkg.Package{
Name: "requests",
Metadata: pkg.PythonPdmLockEntry{
Dependencies: []string{
"certifi>=2017.4.17",
"urllib3<1.27,>=1.21.1",
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"requests"},
Requires: []string{"certifi", "urllib3"},
},
},
},
{
name: "with dependencies containing environment markers",
p: pkg.Package{
Name: "requests",
Metadata: pkg.PythonPdmLockEntry{
Dependencies: []string{
"certifi>=2017.4.17",
"chardet<5,>=3.0.2; python_version < \"3\"",
"charset-normalizer~=2.0.0; python_version >= \"3\"",
"idna<3,>=2.5; python_version < \"3\"",
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"requests"},
Requires: []string{"certifi", "chardet", "charset-normalizer", "idna"},
},
},
},
{
name: "with dependencies containing extras",
p: pkg.Package{
Name: "pytest-cov",
Metadata: pkg.PythonPdmLockEntry{
Dependencies: []string{
"coverage[toml]>=5.2.1",
"pytest>=4.6",
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"pytest-cov"},
Requires: []string{"coverage", "pytest"},
},
},
},
{
name: "package with single extra variant",
p: pkg.Package{
Name: "coverage",
Metadata: pkg.PythonPdmLockEntry{
Dependencies: []string{}, // base package has no dependencies
Extras: []pkg.PythonPdmLockExtraVariant{
{
Extras: []string{"toml"},
Dependencies: []string{
"coverage==7.4.1", // self-reference, should be excluded
"tomli; python_full_version <= \"3.11.0a6\"",
},
},
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"coverage"},
Requires: nil,
},
Variants: []dependency.ProvidesRequires{
{
Provides: []string{"coverage[toml]"},
Requires: []string{"tomli"}, // coverage self-reference excluded
},
},
},
},
{
name: "package with multiple extras in one variant",
p: pkg.Package{
Name: "foo",
Metadata: pkg.PythonPdmLockEntry{
Dependencies: []string{"bar>=1.0"},
Extras: []pkg.PythonPdmLockExtraVariant{
{
Extras: []string{"dev", "test"},
Dependencies: []string{
"pytest>=6.0",
"black~=22.0",
"foo==1.0.0", // self-reference, should be excluded
},
},
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"foo"},
Requires: []string{"bar"},
},
Variants: []dependency.ProvidesRequires{
{
Provides: []string{"foo[dev]", "foo[test]"},
Requires: []string{"pytest", "black"}, // foo self-reference excluded
},
},
},
},
{
name: "package with multiple separate extra variants",
p: pkg.Package{
Name: "example",
Metadata: pkg.PythonPdmLockEntry{
Dependencies: []string{"requests"},
Extras: []pkg.PythonPdmLockExtraVariant{
{
Extras: []string{"redis"},
Dependencies: []string{"redis>=4.0"},
},
{
Extras: []string{"postgres"},
Dependencies: []string{"psycopg2>=2.9"},
},
},
},
},
want: dependency.Specification{
ProvidesRequires: dependency.ProvidesRequires{
Provides: []string{"example"},
Requires: []string{"requests"},
},
Variants: []dependency.ProvidesRequires{
{
Provides: []string{"example[redis]"},
Requires: []string{"redis"},
},
{
Provides: []string{"example[postgres]"},
Requires: []string{"psycopg2"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, pdmLockDependencySpecifier(tt.p))
})
}
}