Skip to content

Commit

Permalink
⭐️ add suse 11 platform detection (#1418)
Browse files Browse the repository at this point in the history
This adds platform detection of suse systems without /etc/os-release
  • Loading branch information
chris-rock authored Aug 2, 2023
1 parent dcec917 commit 24b503b
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 14 deletions.
39 changes: 39 additions & 0 deletions motor/platform/detector/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,45 @@ func TestOpenSuseTumbleweedDetector(t *testing.T) {
assert.Equal(t, []string{"suse", "linux", "unix", "os"}, di.Family)
}

func TestSuse10Detector(t *testing.T) {
detector, err := newDetector("./testdata/detect-suse-sles-10.toml")
assert.Nil(t, err, "was able to create the provider")
di, err := detector.Platform()
require.NoError(t, err)

assert.Equal(t, "sles", di.Name, "os name should be identified")
assert.Equal(t, "SUSE Linux Enterprise Server 10", di.Title, "os title should be identified")
assert.Equal(t, "10.4", di.Version, "os version should be identified")
assert.Equal(t, "x86_64", di.Arch, "os arch should be identified")
assert.Equal(t, []string{"suse", "linux", "unix", "os"}, di.Family)
}

func TestSuse11Detector(t *testing.T) {
detector, err := newDetector("./testdata/detect-suse-sles-11.toml")
assert.Nil(t, err, "was able to create the provider")
di, err := detector.Platform()
require.NoError(t, err)

assert.Equal(t, "sles", di.Name, "os name should be identified")
assert.Equal(t, "SUSE Linux Enterprise Server 11", di.Title, "os title should be identified")
assert.Equal(t, "11.3", di.Version, "os version should be identified")
assert.Equal(t, "x86_64", di.Arch, "os arch should be identified")
assert.Equal(t, []string{"suse", "linux", "unix", "os"}, di.Family)
}

func TestOpenSuse11Detector(t *testing.T) {
detector, err := newDetector("./testdata/detect-opensuse-11.toml")
assert.Nil(t, err, "was able to create the provider")
di, err := detector.Platform()
require.NoError(t, err)

assert.Equal(t, "opensuse", di.Name, "os name should be identified")
assert.Equal(t, "openSUSE 11.4", di.Title, "os title should be identified")
assert.Equal(t, "11.4", di.Version, "os version should be identified")
assert.Equal(t, "x86_64", di.Arch, "os arch should be identified")
assert.Equal(t, []string{"suse", "linux", "unix", "os"}, di.Family)
}

func TestSuse12Detector(t *testing.T) {
detector, err := newDetector("./testdata/detect-suse-sles-12.toml")
assert.Nil(t, err, "was able to create the provider")
Expand Down
30 changes: 30 additions & 0 deletions motor/platform/detector/parser_linux_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package detector
import (
"errors"
"regexp"
"strings"
)

var (
Expand Down Expand Up @@ -46,3 +47,32 @@ func ParseRhelVersion(releaseDescription string) (string, string, error) {

return name, release, nil
}

var (
suseTitle = regexp.MustCompile(`^([\w\d\s\.]+)\(([\w\d\s\.]+)\)`)
suseVersion = regexp.MustCompile(`VERSION\s*=\s*"?([\d\.]+)"?`)
susePatchVersion = regexp.MustCompile(`VERSION = (\d+)\nPATCHLEVEL = (\d+)`)
)

func ParseSuseTitle(v string) (string, string) {
m := suseTitle.FindStringSubmatch(v)
if len(m) == 0 {
return "", ""
}

return strings.TrimSpace(m[1]), strings.TrimSpace(m[2])
}

func ParseSuseVersion(v string) string {
m := susePatchVersion.FindStringSubmatch(v)
if len(m) == 3 {
return m[1] + "." + m[2]
}

m = suseVersion.FindStringSubmatch(v)
if len(m) == 0 {
return ""
}

return m[1]
}
20 changes: 20 additions & 0 deletions motor/platform/detector/parser_linux_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,23 @@ func TestRedhatRelease(t *testing.T) {
assert.Equal(t, "Oracle Linux Server", name, "parse os name")
assert.Equal(t, "7.4", release, "parse release version")
}

func TestSuseParser(t *testing.T) {
data := `openSUSE 11.4 (x86_64)
VERSION = 11.4
CODENAME = Celadon`
title, arch := detector.ParseSuseTitle(data)
assert.Equal(t, "openSUSE 11.4", title)
assert.Equal(t, "x86_64", arch)
v := detector.ParseSuseVersion(data)
assert.Equal(t, "11.4", v)

data = `SUSE Linux Enterprise Server 12 (x86_64)
VERSION = 12
PATCHLEVEL = 0`
title, arch = detector.ParseSuseTitle(data)
assert.Equal(t, "SUSE Linux Enterprise Server 12", title)
assert.Equal(t, "x86_64", arch)
v = detector.ParseSuseVersion(data)
assert.Equal(t, "12.0", v)
}
72 changes: 58 additions & 14 deletions motor/platform/detector/platform_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package detector
import (
"bytes"
"io"
"io/ioutil"
"regexp"
"strconv"
"strings"
Expand All @@ -29,7 +28,7 @@ var macOS = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
return false, nil
}
Expand Down Expand Up @@ -85,7 +84,7 @@ var alpine = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
return false, nil
}
Expand Down Expand Up @@ -129,7 +128,7 @@ var debian = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
return false, nil
}
Expand Down Expand Up @@ -229,7 +228,7 @@ var rhel = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
return false, nil
}
Expand Down Expand Up @@ -315,7 +314,7 @@ var fedora = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
return false, nil
}
Expand Down Expand Up @@ -345,7 +344,7 @@ var oracle = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
return false, nil
}
Expand Down Expand Up @@ -408,6 +407,28 @@ var opensuse = &PlatformResolver{
return true, nil
}

// fallback to /etc/etc/SuSE-release file
f, err := p.FS().Open("/etc/SuSE-release")
if err != nil {
return false, nil
}
defer f.Close()

c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
log.Debug().Err(err)
return false, nil
}

if strings.Contains(strings.ToLower(string(c)), "opensuse") {
pf.Name = "opensuse"
title, arch := ParseSuseTitle(string(c))
pf.Title = title
pf.Arch = arch
pf.Version = ParseSuseVersion(string(c))
return true, nil
}

return false, nil
},
}
Expand All @@ -419,6 +440,29 @@ var sles = &PlatformResolver{
if pf.Name == "sles" {
return true, nil
}

// fallback to /etc/etc/SuSE-release file
f, err := p.FS().Open("/etc/SuSE-release")
if err != nil {
return false, nil
}
defer f.Close()

c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
log.Debug().Err(err)
return false, nil
}

if strings.Contains(strings.ToLower(string(c)), "suse linux enterprise server") {
pf.Name = "sles"
title, arch := ParseSuseTitle(string(c))
pf.Title = title
pf.Arch = arch
pf.Version = ParseSuseVersion(string(c))
return true, nil
}

return false, nil
},
}
Expand All @@ -444,7 +488,7 @@ var gentoo = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
log.Debug().Err(err)
return false, nil
Expand Down Expand Up @@ -495,7 +539,7 @@ var busybox = &PlatformResolver{
}
defer f.Close()

content, err := ioutil.ReadAll(f)
content, err := io.ReadAll(f)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -549,7 +593,7 @@ var openwrt = &PlatformResolver{
}
defer f.Close()

content, err := ioutil.ReadAll(f)
content, err := io.ReadAll(f)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -807,7 +851,7 @@ var redhatFamily = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil || len(c) == 0 {
log.Debug().Err(err)
return false, nil
Expand Down Expand Up @@ -866,7 +910,7 @@ var archFamily = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil {
return false, nil
}
Expand Down Expand Up @@ -1038,7 +1082,7 @@ var solaris = &PlatformResolver{
}
defer f.Close()

c, err := ioutil.ReadAll(f)
c, err := io.ReadAll(f)
if err != nil {
return false, nil
}
Expand All @@ -1065,7 +1109,7 @@ var esxi = &PlatformResolver{
log.Debug().Err(err).Msg("could not run command")
return false, nil
}
vmware_info, err := ioutil.ReadAll(cmd.Stdout)
vmware_info, err := io.ReadAll(cmd.Stdout)
if err != nil {
log.Debug().Err(err).Msg("could not run command")
return false, err
Expand Down
15 changes: 15 additions & 0 deletions motor/platform/detector/testdata/detect-opensuse-11.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[commands."uname -s"]
stdout = "Linux"

[commands."uname -m"]
stdout = "x86_64"

[commands."uname -r"]
stdout = "2.6.37.6-24-desktop"

[files."/etc/SuSE-release"]
content = """
openSUSE 11.4 (x86_64)
VERSION = 11.4
CODENAME = Celadon
"""
15 changes: 15 additions & 0 deletions motor/platform/detector/testdata/detect-suse-sles-10.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[commands."uname -s"]
stdout = "Linux"

[commands."uname -m"]
stdout = "x86_64"

[commands."uname -r"]
stdout = "2.6.37.6-24-desktop"

[files."/etc/SuSE-release"]
content = """
SUSE Linux Enterprise Server 10 (x86_64)
VERSION = 10
PATCHLEVEL = 4
"""
15 changes: 15 additions & 0 deletions motor/platform/detector/testdata/detect-suse-sles-11.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[commands."uname -s"]
stdout = "Linux"

[commands."uname -m"]
stdout = "x86_64"

[commands."uname -r"]
stdout = "2.6.37.6-24-desktop"

[files."/etc/SuSE-release"]
content = """
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 3
"""

0 comments on commit 24b503b

Please sign in to comment.