-
Notifications
You must be signed in to change notification settings - Fork 2
/
disks_test.go
72 lines (56 loc) · 1.58 KB
/
disks_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
72
package unused_test
import (
"testing"
"time"
"github.com/grafana/unused"
"github.com/grafana/unused/unusedtest"
)
func TestDisksSort(t *testing.T) {
var (
now = time.Now()
foo = unusedtest.NewProvider("foo", nil)
baz = unusedtest.NewProvider("baz", nil)
bar = unusedtest.NewProvider("bar", nil)
gcp = unusedtest.NewDisk("ghi", foo, now.Add(-10*time.Minute))
aws = unusedtest.NewDisk("abc", baz, now.Add(-5*time.Minute))
az = unusedtest.NewDisk("def", bar, now.Add(-2*time.Minute))
disks = unused.Disks{gcp, aws, az}
)
tests := map[string]struct {
exp []unused.Disk
by unused.ByFunc
}{
"ByProvider": {[]unused.Disk{az, aws, gcp}, unused.ByProvider},
"ByName": {[]unused.Disk{aws, az, gcp}, unused.ByName},
"ByCreatedAt": {[]unused.Disk{gcp, aws, az}, unused.ByCreatedAt},
}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
disks.Sort(tt.by)
for i, got := range disks {
assertEqualDisks(t, tt.exp[i], got)
}
})
}
}
func assertEqualDisks(t *testing.T, p, q unused.Disk) {
t.Helper()
if e, g := p.Name(), q.Name(); e != g {
t.Errorf("expecting name %q, got %q", e, g)
}
if e, g := p.Provider(), q.Provider(); e != g {
t.Errorf("expecting provider %v, got %v", e, g)
}
if e, g := p.CreatedAt(), q.CreatedAt(); !e.Equal(g) {
t.Errorf("expecting created at %v, got %v", e, g)
}
mp, mq := p.Meta(), q.Meta()
if e, g := len(mp), len(mq); e != g {
t.Fatalf("expecting %d metadata items, got %d", e, g)
}
for k, v := range mp {
if mq[k] != v {
t.Errorf("expecting metadata %q with value %q, got %q", k, v, mq[k])
}
}
}