Skip to content

Commit

Permalink
Re-add the test for Uname on GOOS=linux
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon committed Jun 29, 2015
1 parent 0e2d558 commit 7d8660a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion probe/host/system_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"time"
)

// Uname is swappable for mocking in tests.
var Uname = syscall.Uname

func charsToString(ca [65]int8) string {
s := make([]byte, len(ca))
var lens int
Expand All @@ -24,7 +27,7 @@ func charsToString(ca [65]int8) string {
// GetKernelVersion returns the kernel version as reported by uname.
var GetKernelVersion = func() (string, error) {
var utsname syscall.Utsname
if err := syscall.Uname(&utsname); err != nil {
if err := Uname(&utsname); err != nil {
return "unknown", err
}
return fmt.Sprintf("%s %s", charsToString(utsname.Release), charsToString(utsname.Version)), nil
Expand Down
40 changes: 40 additions & 0 deletions probe/host/system_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package host_test

import (
"fmt"
"syscall"
"testing"

"github.com/weaveworks/scope/probe/host"
)

func TestUname(t *testing.T) {
oldUname := host.Uname
defer func() { host.Uname = oldUname }()

const (
release = "rls"
version = "ver"
)
host.Uname = func(uts *syscall.Utsname) error {
uts.Release = string2c(release)
uts.Version = string2c(version)
return nil
}

have, err := host.GetKernelVersion()
if err != nil {
t.Fatal(err)
}
if want := fmt.Sprintf("%s %s", release, version); want != have {
t.Errorf("want %q, have %q", want, have)
}
}

func string2c(s string) [65]int8 {
var result [65]int8
for i, c := range s {
result[i] = int8(c)
}
return result
}

0 comments on commit 7d8660a

Please sign in to comment.