From 76866feb0329f56535f575de55d0076a5835bf8e Mon Sep 17 00:00:00 2001 From: WAKAYAMA Shirou Date: Tue, 27 May 2014 22:37:23 +0900 Subject: [PATCH] implement redhatishVersion and platform on Linux. --- cpu_test.go | 1 - host_linux.go | 43 ++++++++++++++++++++++---------- host_linux_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 host_linux_test.go diff --git a/cpu_test.go b/cpu_test.go index 963e3e8..15b6705 100644 --- a/cpu_test.go +++ b/cpu_test.go @@ -3,7 +3,6 @@ package gopsutil import ( "fmt" "testing" - ) func TestCpu_times(t *testing.T) { diff --git a/host_linux.go b/host_linux.go index 6bd7f49..cf83d74 100644 --- a/host_linux.go +++ b/host_linux.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "os/exec" + "regexp" "runtime" "strings" "syscall" @@ -156,21 +157,21 @@ func GetPlatformInformation() (string, string, string, error) { version := "" lsb, err := getLSB() - if err != nil{ - lsb = LSB{} + if err != nil { + lsb = &LSB{} } if pathExists("/etc/oracle-release") { platform = "oracle" contents, err := readLines("/etc/oracle-release") if err == nil { - version, _ = getRedhatishVersion(contents) + version = getRedhatishVersion(contents) } } else if pathExists("/etc/enterprise-release") { platform = "oracle" contents, err := readLines("/etc/enterprise-release") if err == nil { - version, _ = getRedhatishVersion(contents) + version = getRedhatishVersion(contents) } } else if pathExists("/etc/debian_version") { if lsb.ID == "Ubuntu" { @@ -193,20 +194,20 @@ func GetPlatformInformation() (string, string, string, error) { } else if pathExists("/etc/redhat-release") { contents, err := readLines("/etc/redhat-release") if err == nil { - version, _ = getRedhatishVersion(contents) - platform, _ = getRedhatishPlatform(contents) + version = getRedhatishVersion(contents) + platform = getRedhatishPlatform(contents) } } else if pathExists("/etc/system-release") { contents, err := readLines("/etc/system-release") if err == nil { - version, _ = getRedhatishVersion(contents) - platform, _ = getRedhatishPlatform(contents) + version = getRedhatishVersion(contents) + platform = getRedhatishPlatform(contents) } } else if pathExists("/etc/gentoo-release") { platform = "gentoo" contents, err := readLines("/etc/gentoo-release") if err == nil { - version, _ = getRedhatishVersion(contents) + version = getRedhatishVersion(contents) } // TODO: suse detection // TODO: slackware detecion @@ -253,12 +254,28 @@ func GetPlatformInformation() (string, string, string, error) { } -func getRedhatishVersion(contents []string) (string, error) { - return "", nil +func getRedhatishVersion(contents []string) string { + c := strings.ToLower(strings.Join(contents, "")) + + if strings.Contains(c, "rawhide") { + return "rawhide" + } + if matches := regexp.MustCompile(`release (\d[\d.]*)`).FindStringSubmatch(c); matches != nil { + return matches[1] + } else { + return "" + } } -func getRedhatishPlatform(contents []string) (string, error) { - return "", nil +func getRedhatishPlatform(contents []string) string { + c := strings.ToLower(strings.Join(contents, "")) + + if strings.Contains(c, "red hat") { + return "redhat" + } + f := strings.Split(c, " ") + + return f[0] } func GetVirtualization() (string, string, error) { diff --git a/host_linux_test.go b/host_linux_test.go new file mode 100644 index 0000000..56343aa --- /dev/null +++ b/host_linux_test.go @@ -0,0 +1,61 @@ +// +build linux + +package gopsutil + +import ( + "testing" +) + +func TestGetRedhatishVersion(t *testing.T) { + var ret string + c := []string{"Rawhide"} + ret = getRedhatishVersion(c) + if ret != "rawhide" { + t.Errorf("Could not get version rawhide: %v", ret) + } + + c = []string{"Fedora release 15 (Lovelock)"} + ret = getRedhatishVersion(c) + if ret != "15" { + t.Errorf("Could not get version fedora: %v", ret) + } + + c = []string{"Enterprise Linux Server release 5.5 (Carthage)"} + ret = getRedhatishVersion(c) + if ret != "5.5" { + t.Errorf("Could not get version redhat enterprise: %v", ret) + } + + c = []string{""} + ret = getRedhatishVersion(c) + if ret != "" { + t.Errorf("Could not get version with no value: %v", ret) + } +} + +func TestGetRedhatishPlatform(t *testing.T) { + var ret string + c := []string{"red hat"} + ret = getRedhatishPlatform(c) + if ret != "redhat" { + t.Errorf("Could not get platform redhat: %v", ret) + } + + c = []string{"Fedora release 15 (Lovelock)"} + ret = getRedhatishPlatform(c) + if ret != "fedora" { + t.Errorf("Could not get platform fedora: %v", ret) + } + + c = []string{"Enterprise Linux Server release 5.5 (Carthage)"} + ret = getRedhatishPlatform(c) + if ret != "enterprise" { + t.Errorf("Could not get platform redhat enterprise: %v", ret) + } + + c = []string{""} + ret = getRedhatishPlatform(c) + if ret != "" { + t.Errorf("Could not get platform with no value: %v", ret) + } +}