Merge pull request #87 from anchore/unkown-distro

Add a helper function/constructor for an UnkownDistro
This commit is contained in:
Alfredo Deza 2020-07-17 13:54:14 -04:00 committed by GitHub
commit b5a353349f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 2 deletions

View File

@ -13,7 +13,7 @@ RESET := $(shell tput -T linux sgr0)
TITLE := $(BOLD)$(PURPLE)
SUCCESS := $(BOLD)$(GREEN)
# the quality gate lower threshold for unit test total % coverage (by function statements)
COVERAGE_THRESHOLD := 69
COVERAGE_THRESHOLD := 72
ifndef TEMPDIR
$(error TEMPDIR is not set)

View File

@ -12,6 +12,13 @@ type Distro struct {
RawVersion string
}
// NewUnknownDistro creates a standardized UnkownDistro with a "0.0.0" version
func NewUnknownDistro() Distro {
return Distro{
Type: UnknownDistro,
}
}
func NewDistro(t Type, ver string) (Distro, error) {
verObj, err := hashiVer.NewVersion(ver)
if err != nil {

View File

@ -61,7 +61,8 @@ func Identify(s scope.Scope) *Distro {
}
}
// TODO: is it useful to know partially detected distros? where the ID is known but not the version (and viceversa?)
return nil
distro := NewUnknownDistro()
return &distro
}
func assembleDistro(name, version string) *Distro {

View File

@ -5,8 +5,50 @@ import (
"io/ioutil"
"os"
"testing"
"github.com/anchore/imgbom/imgbom/scope"
)
func TestIdentifyDistro(t *testing.T) {
tests := []struct {
fixture string
name string
RawVersion string
Type Type
}{
{
fixture: "test-fixtures/os/ubuntu-20.04",
name: "ubuntu",
Type: Ubuntu,
},
{
fixture: "test-fixtures/os/empty",
name: "No OS files",
Type: UnknownDistro,
},
{
fixture: "test-fixtures/os/unmatchable",
name: "Unmatchable distro",
Type: UnknownDistro,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
s, err := scope.NewScopeFromDir(test.fixture, scope.AllLayersScope)
if err != nil {
t.Fatalf("unable to produce a new scope for testing: %s", test.fixture)
}
distro := Identify(s)
if distro.Type != test.Type {
t.Errorf("expected distro doesn't match: %v != %v", distro.Type, test.Type)
}
})
}
}
func TestParseOsRelease(t *testing.T) {
tests := []struct {

View File

@ -0,0 +1,12 @@
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

View File

@ -0,0 +1,17 @@
NAME="Scientific Linux"
VERSION="16 (Core)"
ID="scientific"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"