From e460c031b6f288d69213a8c5558691369a0373e2 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Fri, 12 Jun 2020 10:47:07 -0400 Subject: [PATCH] distro: add tests and fixtures for identification Signed-off-by: Alfredo Deza --- imgbom/distro/identify_test.go | 108 +++++++++++++++++++++++ imgbom/distro/test-fixtures/bad-id | 15 ++++ imgbom/distro/test-fixtures/bad-version | 15 ++++ imgbom/distro/test-fixtures/centos-8 | 17 ++++ imgbom/distro/test-fixtures/debian-8 | 8 ++ imgbom/distro/test-fixtures/rhel-8 | 16 ++++ imgbom/distro/test-fixtures/ubuntu-20.04 | 12 +++ 7 files changed, 191 insertions(+) create mode 100644 imgbom/distro/identify_test.go create mode 100644 imgbom/distro/test-fixtures/bad-id create mode 100644 imgbom/distro/test-fixtures/bad-version create mode 100644 imgbom/distro/test-fixtures/centos-8 create mode 100644 imgbom/distro/test-fixtures/debian-8 create mode 100644 imgbom/distro/test-fixtures/rhel-8 create mode 100644 imgbom/distro/test-fixtures/ubuntu-20.04 diff --git a/imgbom/distro/identify_test.go b/imgbom/distro/identify_test.go new file mode 100644 index 000000000..b21899dac --- /dev/null +++ b/imgbom/distro/identify_test.go @@ -0,0 +1,108 @@ +package distro + +import ( + "fmt" + "io/ioutil" + "os" + "testing" +) + +func TestParseOsRelease(t *testing.T) { + + tests := []struct { + fixture string + name string + RawVersion string + }{ + { + fixture: "test-fixtures/ubuntu-20.04", + name: "ubuntu", + RawVersion: "20.04", + }, + { + fixture: "test-fixtures/debian-8", + name: "debian", + RawVersion: "8", + }, + { + fixture: "test-fixtures/centos-8", + name: "centos", + RawVersion: "8", + }, + { + fixture: "test-fixtures/rhel-8", + name: "redhat", + RawVersion: "8.1", + }, + } + + for _, test := range tests { + name := fmt.Sprintf("%s:%s", test.name, test.RawVersion) + fixture, err := os.Open(test.fixture) + defer fixture.Close() + + if err != nil { + t.Fatalf("failed to open fixture: %+v", err) + } + + b, err := ioutil.ReadAll(fixture) + if err != nil { + t.Fatalf("unable to read fixture file: %+v", err) + } + + contents := string(b) + + t.Run(name, func(t *testing.T) { + distro := parseOsRelease(contents) + if distro.Name() != test.name { + t.Errorf("mismatched name in distro: '%s' != '%s'", distro.Name(), test.name) + } + if distro.RawVersion != test.RawVersion { + t.Errorf("mismatched distro version: '%s' != '%s'", distro.RawVersion, test.RawVersion) + } + }) + } + +} + +func TestParseOsReleaseFailures(t *testing.T) { + + tests := []struct { + fixture string + name string + }{ + { + fixture: "test-fixtures/bad-version", + name: "No version", + }, + { + fixture: "test-fixtures/bad-id", + name: "No name ID", + }, + } + + for _, test := range tests { + name := fmt.Sprintf("%s:%s", test.name, test.fixture) + fixture, err := os.Open(test.fixture) + defer fixture.Close() + + if err != nil { + t.Fatalf("failed to open fixture: %+v", err) + } + + b, err := ioutil.ReadAll(fixture) + if err != nil { + t.Fatalf("unable to read fixture file: %+v", err) + } + + contents := string(b) + + t.Run(name, func(t *testing.T) { + distro := parseOsRelease(contents) + if distro != nil { + t.Errorf("unexpected non-nil distro: '%s' != nil", distro) + } + }) + } + +} diff --git a/imgbom/distro/test-fixtures/bad-id b/imgbom/distro/test-fixtures/bad-id new file mode 100644 index 000000000..871e65561 --- /dev/null +++ b/imgbom/distro/test-fixtures/bad-id @@ -0,0 +1,15 @@ +NAME="Red Hat Enterprise Linux" +VERSION="8.1 (Ootpa)" +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" + diff --git a/imgbom/distro/test-fixtures/bad-version b/imgbom/distro/test-fixtures/bad-version new file mode 100644 index 000000000..fa940db6e --- /dev/null +++ b/imgbom/distro/test-fixtures/bad-version @@ -0,0 +1,15 @@ +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" diff --git a/imgbom/distro/test-fixtures/centos-8 b/imgbom/distro/test-fixtures/centos-8 new file mode 100644 index 000000000..7f10b7745 --- /dev/null +++ b/imgbom/distro/test-fixtures/centos-8 @@ -0,0 +1,17 @@ +NAME="CentOS Linux" +VERSION="8 (Core)" +ID="centos" +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" + diff --git a/imgbom/distro/test-fixtures/debian-8 b/imgbom/distro/test-fixtures/debian-8 new file mode 100644 index 000000000..120c51b08 --- /dev/null +++ b/imgbom/distro/test-fixtures/debian-8 @@ -0,0 +1,8 @@ +PRETTY_NAME="Debian GNU/Linux 8 (jessie)" +NAME="Debian GNU/Linux" +VERSION_ID="8" +VERSION="8 (jessie)" +ID=debian +HOME_URL="http://www.debian.org/" +SUPPORT_URL="http://www.debian.org/support" +BUG_REPORT_URL="https://bugs.debian.org/" diff --git a/imgbom/distro/test-fixtures/rhel-8 b/imgbom/distro/test-fixtures/rhel-8 new file mode 100644 index 000000000..216c1d226 --- /dev/null +++ b/imgbom/distro/test-fixtures/rhel-8 @@ -0,0 +1,16 @@ +NAME="Red Hat Enterprise Linux" +VERSION="8.1 (Ootpa)" +ID="rhel" +ID_LIKE="fedora" +VERSION_ID="8.1" +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" diff --git a/imgbom/distro/test-fixtures/ubuntu-20.04 b/imgbom/distro/test-fixtures/ubuntu-20.04 new file mode 100644 index 000000000..e07dd980d --- /dev/null +++ b/imgbom/distro/test-fixtures/ubuntu-20.04 @@ -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