Merge pull request #170 from anchore/add-distro-mappings-arch-and-opensuseleap

Add support for Arch (which doesnt return a version) and Open Suse Leap
This commit is contained in:
Sam Dacanay 2020-09-14 09:02:28 -07:00 committed by GitHub
commit 22dfcb2d0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 34 deletions

View File

@ -20,6 +20,9 @@ func NewUnknownDistro() Distro {
} }
func NewDistro(t Type, ver string) (Distro, error) { func NewDistro(t Type, ver string) (Distro, error) {
if ver == "" {
return Distro{Type: t}, nil
}
verObj, err := hashiVer.NewVersion(ver) verObj, err := hashiVer.NewVersion(ver)
if err != nil { if err != nil {
return Distro{}, fmt.Errorf("could not create distro version: %w", err) return Distro{}, fmt.Errorf("could not create distro version: %w", err)
@ -32,6 +35,9 @@ func NewDistro(t Type, ver string) (Distro, error) {
} }
func (d Distro) MajorVersion() string { func (d Distro) MajorVersion() string {
if d.Version == nil {
return fmt.Sprint("(version unknown)")
}
return fmt.Sprintf("%d", d.Version.Segments()[0]) return fmt.Sprintf("%d", d.Version.Segments()[0])
} }
@ -40,7 +46,11 @@ func (d Distro) FullVersion() string {
} }
func (d Distro) String() string { func (d Distro) String() string {
return fmt.Sprintf("%s %s", d.Type, d.RawVersion) versionStr := "(version unknown)"
if d.RawVersion != "" {
versionStr = d.RawVersion
}
return fmt.Sprintf("%s %s", d.Type, versionStr)
} }
// Name provides a string repr of the distro // Name provides a string repr of the distro

View File

@ -48,6 +48,7 @@ identifyLoop:
} }
if len(refs) == 0 { if len(refs) == 0 {
log.Debugf("No Refs found from path: %s", entry.path)
continue continue
} }
@ -84,7 +85,7 @@ func assemble(name, version string) *Distro {
distroType, ok := IDMapping[name] distroType, ok := IDMapping[name]
// Both distro and version must be present // Both distro and version must be present
if len(name) == 0 || len(version) == 0 { if len(name) == 0 {
return nil return nil
} }

View File

@ -65,6 +65,15 @@ func TestIdentifyDistro(t *testing.T) {
fixture: "test-fixtures/os/unmatchable", fixture: "test-fixtures/os/unmatchable",
Type: UnknownDistroType, Type: UnknownDistroType,
}, },
{
fixture: "test-fixtures/os/opensuse-leap",
Type: OpenSuseLeap,
Version: "15.2.0",
},
{
fixture: "test-fixtures/os/arch",
Type: ArchLinux,
},
} }
observedDistros := internal.NewStringSet() observedDistros := internal.NewStringSet()
@ -94,6 +103,11 @@ func TestIdentifyDistro(t *testing.T) {
return return
} }
if d.Version == nil {
t.Log("Distro doesn't have a Version")
return
}
if d.Version.String() != test.Version { if d.Version.String() != test.Version {
t.Errorf("expected distro version doesn't match: %v != %v", d.Version.String(), test.Version) t.Errorf("expected distro version doesn't match: %v != %v", d.Version.String(), test.Version)
} }
@ -175,10 +189,6 @@ func TestParseOsReleaseFailures(t *testing.T) {
fixture string fixture string
name string name string
}{ }{
{
fixture: "test-fixtures/bad-version",
name: "No version",
},
{ {
fixture: "test-fixtures/bad-id", fixture: "test-fixtures/bad-id",
name: "No name ID", name: "No name ID",

View File

@ -1,15 +0,0 @@
NAME="Red Hat Enterprise Linux"
VERSION="8.1 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.1 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8.1:GA"
HOME_URL="https://www.redhat.com/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.1
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.1"

View File

@ -0,0 +1,10 @@
NAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
BUILD_ID=rolling
ANSI_COLOR="38;2;23;147;209"
HOME_URL="https://www.archlinux.org/"
DOCUMENTATION_URL="https://wiki.archlinux.org/"
SUPPORT_URL="https://bbs.archlinux.org/"
BUG_REPORT_URL="https://bugs.archlinux.org/"
LOGO=archlinux

View File

@ -0,0 +1,10 @@
NAME="openSUSE Leap"
VERSION="15.2"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.2"
PRETTY_NAME="openSUSE Leap 15.2"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.2"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"

View File

@ -11,7 +11,8 @@ const (
Busybox Busybox
AmazonLinux AmazonLinux
OracleLinux OracleLinux
//ArchLinux ArchLinux
OpenSuseLeap
) )
type Type int type Type int
@ -27,7 +28,8 @@ var distroStr = []string{
"busybox", "busybox",
"amazn", "amazn",
"oraclelinux", "oraclelinux",
//"archlinux", "archlinux",
"opensuse-leap",
} }
var All = []Type{ var All = []Type{
@ -40,7 +42,8 @@ var All = []Type{
Busybox, Busybox,
AmazonLinux, AmazonLinux,
OracleLinux, OracleLinux,
//ArchLinux, ArchLinux,
OpenSuseLeap,
} }
func (t Type) String() string { func (t Type) String() string {
@ -53,14 +56,15 @@ func (t Type) String() string {
// IDMapping connects a distro ID like "ubuntu" to a Distro type // IDMapping connects a distro ID like "ubuntu" to a Distro type
var IDMapping = map[string]Type{ var IDMapping = map[string]Type{
"debian": Debian, "debian": Debian,
"ubuntu": Ubuntu, "ubuntu": Ubuntu,
"rhel": RedHat, "rhel": RedHat,
"centos": CentOS, "centos": CentOS,
"fedora": Fedora, "fedora": Fedora,
"alpine": Alpine, "alpine": Alpine,
"busybox": Busybox, "busybox": Busybox,
"amzn": AmazonLinux, "amzn": AmazonLinux,
"ol": OracleLinux, "ol": OracleLinux,
//"arch": ArchLinux, "arch": ArchLinux,
"opensuse-leap": OpenSuseLeap,
} }