From 0534a68ec6e7a2f54b3cf7939d199d2fc342f718 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Fri, 17 Jul 2020 13:07:14 -0400 Subject: [PATCH] tests for new Identify distro func Signed-off-by: Alfredo Deza --- imgbom/distro/identify_test.go | 42 +++++++++++++++++++ .../test-fixtures/os/empty/etc/os-release | 0 .../os/ubuntu-20.04/etc/os-release | 12 ++++++ .../os/unmatchable/etc/os-release | 17 ++++++++ 4 files changed, 71 insertions(+) create mode 100644 imgbom/distro/test-fixtures/os/empty/etc/os-release create mode 100644 imgbom/distro/test-fixtures/os/ubuntu-20.04/etc/os-release create mode 100644 imgbom/distro/test-fixtures/os/unmatchable/etc/os-release 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" +