diff --git a/Makefile b/Makefile index 9a46a38ef..df61cf166 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/imgbom/distro/distro.go b/imgbom/distro/distro.go index 6ad0b2cae..ac7b518a1 100644 --- a/imgbom/distro/distro.go +++ b/imgbom/distro/distro.go @@ -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 { diff --git a/imgbom/distro/identify.go b/imgbom/distro/identify.go index 7070ce4ca..1af85d6cc 100644 --- a/imgbom/distro/identify.go +++ b/imgbom/distro/identify.go @@ -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 { diff --git a/imgbom/distro/identify_test.go b/imgbom/distro/identify_test.go index 671f012a3..aa745ae4a 100644 --- a/imgbom/distro/identify_test.go +++ b/imgbom/distro/identify_test.go @@ -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 { diff --git a/imgbom/distro/test-fixtures/os/empty/etc/os-release b/imgbom/distro/test-fixtures/os/empty/etc/os-release new file mode 100644 index 000000000..e69de29bb diff --git a/imgbom/distro/test-fixtures/os/ubuntu-20.04/etc/os-release b/imgbom/distro/test-fixtures/os/ubuntu-20.04/etc/os-release new file mode 100644 index 000000000..e07dd980d --- /dev/null +++ b/imgbom/distro/test-fixtures/os/ubuntu-20.04/etc/os-release @@ -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 diff --git a/imgbom/distro/test-fixtures/os/unmatchable/etc/os-release b/imgbom/distro/test-fixtures/os/unmatchable/etc/os-release new file mode 100644 index 000000000..97edcdbf2 --- /dev/null +++ b/imgbom/distro/test-fixtures/os/unmatchable/etc/os-release @@ -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" +