Merge branch 'master' of github.com:shirou/gopsutil

This commit is contained in:
WAKAYAMA shirou 2014-06-02 23:49:15 +09:00
commit 799a4495e9
3 changed files with 93 additions and 13 deletions

View File

@ -3,7 +3,6 @@ package gopsutil
import (
"fmt"
"testing"
)
func TestCpu_times(t *testing.T) {

View File

@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"syscall"
@ -155,19 +156,22 @@ func GetPlatformInformation() (string, string, string, error) {
family := ""
version := ""
lsb, _ := getLSB()
lsb, err := getLSB()
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" {
@ -190,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
@ -250,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) {

61
host_linux_test.go Normal file
View File

@ -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)
}
}