go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/detector/parser_linux_version_test.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package detector_test 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 "go.mondoo.com/cnquery/providers/os/detector" 12 ) 13 14 func TestOSReleaseParser(t *testing.T) { 15 osRelease := `NAME="Ubuntu" 16 VERSION="16.04.3 LTS (Xenial Xerus)" 17 ID=ubuntu 18 ID_LIKE=debian 19 PRETTY_NAME="Ubuntu 16.04.3 LTS" 20 VERSION_ID="16.04" 21 HOME_URL="http://www.ubuntu.com/" 22 SUPPORT_URL="http://help.ubuntu.com/" 23 BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" 24 VERSION_CODENAME=xenial 25 UBUNTU_CODENAME=xenial` 26 27 m, err := detector.ParseOsRelease(osRelease) 28 assert.Nil(t, err) 29 30 assert.Equal(t, "Ubuntu", m["NAME"], "NAME should be parsed properly") 31 assert.Equal(t, "16.04.3 LTS (Xenial Xerus)", m["VERSION"], "VERSION should be parsed properly") 32 assert.Equal(t, "ubuntu", m["ID"], "ID should be parsed properly") 33 assert.Equal(t, "debian", m["ID_LIKE"], "ID_LIKE should be parsed properly") 34 assert.Equal(t, "Ubuntu 16.04.3 LTS", m["PRETTY_NAME"], "PRETTY_NAME should be parsed properly") 35 assert.Equal(t, "16.04", m["VERSION_ID"], "VERSION_ID should be parsed properly") 36 assert.Equal(t, "http://www.ubuntu.com/", m["HOME_URL"], "HOME_URL should be parsed properly") 37 assert.Equal(t, "http://help.ubuntu.com/", m["SUPPORT_URL"], "SUPPORT_URL should be parsed properly") 38 assert.Equal(t, "http://bugs.launchpad.net/ubuntu/", m["BUG_REPORT_URL"], "BUG_REPORT_URL should be parsed properly") 39 assert.Equal(t, "xenial", m["VERSION_CODENAME"], "VERSION_CODENAME should be parsed properly") 40 assert.Equal(t, "xenial", m["UBUNTU_CODENAME"], "UBUNTU_CODENAME should be parsed properly") 41 42 osRelease = `NAME="Oracle Linux Server" 43 VERSION="6.9" 44 ID="ol" 45 VERSION_ID="6.9" 46 PRETTY_NAME="Oracle Linux Server 6.9" 47 ANSI_COLOR="0;31" 48 CPE_NAME="cpe:/o:oracle:linux:6:9:server" 49 HOME_URL="https://linux.oracle.com/" 50 BUG_REPORT_URL="https://bugzilla.oracle.com/" 51 52 ORACLE_BUGZILLA_PRODUCT="Oracle Linux 6" 53 ORACLE_BUGZILLA_PRODUCT_VERSION=6.9 54 ORACLE_SUPPORT_PRODUCT="Oracle Linux" 55 ORACLE_SUPPORT_PRODUCT_VERSION=6.9` 56 57 m, err = detector.ParseOsRelease(osRelease) 58 require.NoError(t, err) 59 assert.Equal(t, "Oracle Linux Server", m["NAME"], "NAME should be parsed properly") 60 assert.Equal(t, "ol", m["ID"], "ID should be parsed properly") 61 assert.Equal(t, "6.9", m["VERSION"], "VERSION should be parsed properly") 62 } 63 64 func TestEtcLsbReleaseParser(t *testing.T) { 65 lsbRelease := `DISTRIB_ID=Ubuntu 66 DISTRIB_RELEASE=16.04 67 DISTRIB_CODENAME=xenial 68 DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"` 69 70 m, err := detector.ParseLsbRelease(lsbRelease) 71 require.NoError(t, err) 72 73 assert.Equal(t, "Ubuntu", m["DISTRIB_ID"], "DISTRIB_ID should be parsed properly") 74 assert.Equal(t, "16.04", m["DISTRIB_RELEASE"], "DISTRIB_RELEASE should be parsed properly") 75 assert.Equal(t, "xenial", m["DISTRIB_CODENAME"], "DISTRIB_CODENAME should be parsed properly") 76 assert.Equal(t, "Ubuntu 16.04.3 LTS", m["DISTRIB_DESCRIPTION"], "DISTRIB_DESCRIPTION should be parsed properly") 77 } 78 79 func TestRedhatRelease(t *testing.T) { 80 rhRelease := "CentOS Linux release 7.4.1708 (Core)" 81 name, release, err := detector.ParseRhelVersion(rhRelease) 82 require.NoError(t, err) 83 assert.Equal(t, "CentOS Linux", name, "parse os name") 84 assert.Equal(t, "7.4.1708", release, "parse release version") 85 86 rhRelease = "CentOS release 6.9 (Final)" 87 name, release, err = detector.ParseRhelVersion(rhRelease) 88 require.NoError(t, err) 89 assert.Equal(t, "CentOS", name, "parse os name") 90 assert.Equal(t, "6.9", release, "parse release version") 91 92 rhRelease = "Red Hat Enterprise Linux Server release 7.4 (Maipo)" 93 name, release, err = detector.ParseRhelVersion(rhRelease) 94 assert.Nil(t, err) 95 assert.Equal(t, "Red Hat Enterprise Linux Server", name, "parse os name") 96 assert.Equal(t, "7.4", release, "parse release version") 97 98 rhRelease = "Oracle Linux Server release 7.4 (Maipo)" 99 name, release, err = detector.ParseRhelVersion(rhRelease) 100 require.NoError(t, err) 101 assert.Equal(t, "Oracle Linux Server", name, "parse os name") 102 assert.Equal(t, "7.4", release, "parse release version") 103 }