1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
package system
import (
"reflect"
"runtime"
"testing"
)
type noInputs func() string
// test that a function with no inputs returns one of the expected strings
func testOutputs(f noInputs, validOutputs []string, t *testing.T) {
output := f()
// use reflect to get the name of the function
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
failed := true
for _, valid := range validOutputs {
if output == valid {
failed = false
}
}
if failed {
t.Errorf("Function %v returned %v, which is not one of %v", name, output, validOutputs)
}
}
func TestPackageManager(t *testing.T) {
t.Parallel()
testOutputs(
DetectPackageManager,
[]string{"dpkg", "rpm", "apk", "pacman", ""},
t,
)
}
func TestDetectService(t *testing.T) {
t.Parallel()
testOutputs(
DetectService,
[]string{"systemd", "init", "alpineinit", "upstart", ""},
t,
)
}
func TestDetectDistro(t *testing.T) {
t.Parallel()
testOutputs(
DetectDistro,
[]string{"ubuntu", "redhat", "alpine", "arch", "debian", ""},
t,
)
}
func TestHasCommand(t *testing.T) {
t.Parallel()
if !HasCommand("sh") {
t.Error("System didn't have sh!")
}
}
|