Skip to content

Commit

Permalink
debian:distroless dist parsing support and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kieran Muller <[email protected]>
  • Loading branch information
Kieran-Muller authored and crozzy committed Sep 1, 2023
1 parent 942834f commit 161259a
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 21 deletions.
8 changes: 6 additions & 2 deletions debian/distributionscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io/fs"
"regexp"
"runtime/trace"
"strconv"
"strings"
Expand All @@ -31,7 +32,7 @@ type DistributionScanner struct{}
func (*DistributionScanner) Name() string { return "debian" }

// Version implements [indexer.VersionedScanner].
func (*DistributionScanner) Version() string { return "2" }
func (*DistributionScanner) Version() string { return "3" }

// Kind implements [indexer.VersionedScanner].
func (*DistributionScanner) Kind() string { return "distribution" }
Expand Down Expand Up @@ -85,10 +86,13 @@ func findDist(ctx context.Context, sys fs.FS) (*claircore.Distribution, error) {
return nil, nil
}

// Regex pattern matches item within string that appear as so: (bookworm), (buster), (bullseye)
ver := regexp.MustCompile(`\(\w+\)$`)

name, nameok := kv[`VERSION_CODENAME`]
idstr := kv[`VERSION_ID`]
if !nameok {
name = strings.TrimFunc(kv[`VERSION`], func(r rune) bool { return !unicode.IsLetter(r) })
name = strings.TrimFunc(ver.FindString(kv[`VERSION`]), func(r rune) bool { return !unicode.IsLetter(r) })
}
if name == "" || idstr == "" {
zlog.Info(ctx).
Expand Down
51 changes: 32 additions & 19 deletions debian/distributionscanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package debian

import (
"context"
"io/fs"
"os"
"path/filepath"
"regexp"
Expand All @@ -17,24 +18,36 @@ func TestDistributionScanner(t *testing.T) {
if err != nil {
t.Fatal(err)
}
for _, e := range ents {
t.Run(e.Name(), func(t *testing.T) {
sys := os.DirFS(filepath.Join(`testdata/dist`, e.Name()))
d, err := findDist(ctx, sys)
if err != nil {
t.Error(err)
}
if d == nil {
t.Fatalf("%s does not represent a Debian dist", e.Name())
}
got, want := d.VersionID, e.Name()
t.Logf("got: %q, want: %q", got, want)
if got != want {
t.Fail()
}
if !ver.MatchString(d.Version) {
t.Fatalf("weird version: %q", d.Version)
}
})
dEnts, err := os.ReadDir(`testdata/distroless_dist`)
if err != nil {
t.Fatal(err)
}

testCase := map[string][]fs.DirEntry{
"testdata/dist": ents,
"testdata/distroless_dist": dEnts,
}

for tcDir, tcEnts := range testCase {
for _, e := range tcEnts {
t.Run(e.Name(), func(t *testing.T) {
sys := os.DirFS(filepath.Join(tcDir, e.Name()))
d, err := findDist(ctx, sys)
if err != nil {
t.Error(err)
}
if d == nil {
t.Fatalf("tc: %v | %s does not represent a Debian dist", tcDir, e.Name())
}
got, want := d.VersionID, e.Name()
t.Logf("tc: %v | got: %q, want: %q", tcDir, got, want)
if got != want {
t.Fail()
}
if !ver.MatchString(d.Version) {
t.Fatalf("tc: %v | weird version: %q", tcDir, d.Version)
}
})
}
}
}
8 changes: 8 additions & 0 deletions debian/testdata/distroless_dist/10/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PRETTY_NAME="Distroless"
NAME="Debian GNU/Linux"
ID="debian"
VERSION_ID="10"
VERSION="Debian GNU/Linux 10 (buster)"
HOME_URL="https://github.com/GoogleContainerTools/distroless"
SUPPORT_URL="https://github.com/GoogleContainerTools/distroless/blob/master/README.md"
BUG_REPORT_URL="https://github.com/GoogleContainerTools/distroless/issues/new"
8 changes: 8 additions & 0 deletions debian/testdata/distroless_dist/11/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PRETTY_NAME="Distroless"
NAME="Debian GNU/Linux"
ID="debian"
VERSION_ID="11"
VERSION="Debian GNU/Linux 11 (bullseye)"
HOME_URL="https://github.com/GoogleContainerTools/distroless"
SUPPORT_URL="https://github.com/GoogleContainerTools/distroless/blob/master/README.md"
BUG_REPORT_URL="https://github.com/GoogleContainerTools/distroless/issues/new"
8 changes: 8 additions & 0 deletions debian/testdata/distroless_dist/9/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PRETTY_NAME="Distroless"
NAME="Debian GNU/Linux"
ID="debian"
VERSION_ID="9"
VERSION="Debian GNU/Linux 9 (stretch)"
HOME_URL="https://github.com/GoogleContainerTools/distroless"
SUPPORT_URL="https://github.com/GoogleContainerTools/distroless/blob/master/README.md"
BUG_REPORT_URL="https://github.com/GoogleContainerTools/distroless/issues/new"
22 changes: 22 additions & 0 deletions osrelease/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ func TestParse(t *testing.T) {
PrettyName: "Red Hat Enterprise Linux 8",
},
},
{
File: "distroless_corrupt_layer",
Want: claircore.Distribution{
DID: "debian",
Name: "Debian GNU/Linux",
Version: "Debian GNU/Linux 12 (bookworm)",
VersionCodeName: "",
VersionID: "12",
PrettyName: "Distroless",
},
},
{
File: "distroless_valid_layer",
Want: claircore.Distribution{
DID: "debian",
Name: "Debian GNU/Linux",
Version: "12 (bookworm)",
VersionCodeName: "bookworm",
VersionID: "12",
PrettyName: "Debian GNU/Linux 12 (bookworm)",
},
},
}
for _, tc := range tt {
t.Run(tc.File, tc.Test)
Expand Down
8 changes: 8 additions & 0 deletions osrelease/testdata/distroless_corrupt_layer
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PRETTY_NAME="Distroless"
NAME="Debian GNU/Linux"
ID="debian"
VERSION_ID="12"
VERSION="Debian GNU/Linux 12 (bookworm)"
HOME_URL="https://github.com/GoogleContainerTools/distroless"
SUPPORT_URL="https://github.com/GoogleContainerTools/distroless/blob/master/README.md"
BUG_REPORT_URL="https://github.com/GoogleContainerTools/distroless/issues/new"
9 changes: 9 additions & 0 deletions osrelease/testdata/distroless_valid_layer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

0 comments on commit 161259a

Please sign in to comment.