-
Notifications
You must be signed in to change notification settings - Fork 66
/
utils_test.go
71 lines (67 loc) · 1.53 KB
/
utils_test.go
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
60
61
62
63
64
65
66
67
68
69
70
71
package zfs
import (
"errors"
"os/exec"
"reflect"
"testing"
)
func TestParseLine(t *testing.T) {
for name, test := range map[string]struct {
prop string
value string
want Zpool
}{
"some fragmentation": {
prop: "fragmentation",
value: "15%",
want: Zpool{Fragmentation: 15},
},
"no fragmentation": {
prop: "fragmentation",
value: "0",
want: Zpool{Fragmentation: 0},
},
"untracked fragmentation": {
prop: "fragmentation",
value: "-",
want: Zpool{Fragmentation: 0},
},
} {
t.Run(name, func(t *testing.T) {
got := Zpool{}
got.parseLine([]string{"", test.prop, test.value})
if !reflect.DeepEqual(test.want, got) {
t.Fatalf("parse failure: wanted: %v, got: %v", test.want, got)
}
})
}
}
func TestCommandError(t *testing.T) {
cmd := &command{Command: "false"}
expectedPath, err := exec.LookPath(cmd.Command)
if err != nil {
t.Fatal(err)
}
for _, tt := range []struct {
name string
args []string
expectedDebug string
}{
{name: "NoArgs", expectedDebug: expectedPath},
{name: "WithArgs", args: []string{"foo"}, expectedDebug: expectedPath + " foo"},
} {
t.Run(tt.name, func(t *testing.T) {
_, err := cmd.Run(tt.args...)
if err == nil {
t.Fatal("command.Run: wanted error, got nil")
}
var e *Error
if !errors.As(err, &e) {
t.Fatalf("command.Run (error): wanted *Error, got %T (%[1]v)", err)
}
if e.Debug != tt.expectedDebug {
t.Fatalf("command.Run (error): wanted Debug %q, got %q", tt.expectedDebug, e.Debug)
}
})
}
}