Skip to content

Commit b8b5106

Browse files
authored
chore: Migrate tests to cmp package (#760)
Migrate the majority of diff tests from `reflect.DeepEqual()` to `cmp.Diff()` to improve the test failure readability. * Fixup Copyright lines Signed-off-by: SuperQ <[email protected]>
1 parent d4dc49b commit b8b5106

32 files changed

+176
-173
lines changed

blockdevice/stats_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ package blockdevice
1616
import (
1717
"errors"
1818
"os"
19-
"reflect"
2019
"testing"
20+
21+
"github.com/google/go-cmp/cmp"
2122
)
2223

2324
const (
@@ -150,8 +151,8 @@ func TestBlockDevice(t *testing.T) {
150151
if err != nil {
151152
t.Fatal(err)
152153
}
153-
if !reflect.DeepEqual(blockQueueStat, blockQueueStatExpected) {
154-
t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", blockQueueStatExpected, blockQueueStat)
154+
if diff := cmp.Diff(blockQueueStat, blockQueueStatExpected); diff != "" {
155+
t.Fatalf("unexpected BlockQueueStat (-want +got):\n%s", diff)
155156
}
156157
}
157158

@@ -176,8 +177,8 @@ func TestBlockDmInfo(t *testing.T) {
176177
UseBlkMQ: 0,
177178
UUID: "LVM-3zSHSR5Nbf4j7g6auAAefWY2CMaX01theZYEvQyecVsm2WtX3iY5q51qq5dWWOq7",
178179
}
179-
if !reflect.DeepEqual(dm0Info, dm0InfoExpected) {
180-
t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", dm0InfoExpected, dm0Info)
180+
if diff := cmp.Diff(dm0Info, dm0InfoExpected); diff != "" {
181+
t.Fatalf("unexpected BlockQueueStat (-want +got):\n%s", diff)
181182
}
182183

183184
dm1Info, err := blockdevice.SysBlockDeviceMapperInfo(devices[1])
@@ -195,8 +196,8 @@ func TestBlockDmInfo(t *testing.T) {
195196
t.Fatal("SysBlockDeviceMapperInfo on sda was supposed to fail.")
196197
}
197198
dm1InfoExpected := DeviceMapperInfo{}
198-
if !reflect.DeepEqual(dm1Info, dm1InfoExpected) {
199-
t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", dm0InfoExpected, dm0Info)
199+
if diff := cmp.Diff(dm1Info, dm1InfoExpected); diff != "" {
200+
t.Fatalf("unexpected BlockQueueStat (-want +got):\n%s", diff)
200201
}
201202
}
202203

@@ -217,8 +218,8 @@ func TestSysBlockDeviceUnderlyingDevices(t *testing.T) {
217218
underlying0Expected := UnderlyingDeviceInfo{
218219
DeviceNames: []string{"sda"},
219220
}
220-
if !reflect.DeepEqual(underlying0, underlying0Expected) {
221-
t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", underlying0Expected, underlying0)
221+
if diff := cmp.Diff(underlying0, underlying0Expected); diff != "" {
222+
t.Fatalf("unexpected BlockQueueStat (-want +got):\n%s", diff)
222223
}
223224
}
224225

fscache_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
package procfs
1515

1616
import (
17-
"reflect"
1817
"testing"
18+
19+
"github.com/google/go-cmp/cmp"
1920
)
2021

2122
func TestFscacheinfo(t *testing.T) {
@@ -128,9 +129,7 @@ func TestFscacheinfo(t *testing.T) {
128129
t.Fatal(err)
129130
}
130131

131-
if !reflect.DeepEqual(have, expected) {
132-
t.Logf("have: %+v", have)
133-
t.Logf("expected: %+v", expected)
134-
t.Errorf("structs are not equal")
132+
if diff := cmp.Diff(have, expected); diff != "" {
133+
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
135134
}
136135
}

iscsi/get_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
package iscsi_test
1515

1616
import (
17-
"reflect"
1817
"testing"
1918

19+
"github.com/google/go-cmp/cmp"
20+
2021
"github.com/prometheus/procfs/iscsi"
2122
)
2223

@@ -140,8 +141,8 @@ func TestGetStats(t *testing.T) {
140141

141142
for i, stat := range sysfsStat {
142143
want, have := tests[i].stat, stat
143-
if !reflect.DeepEqual(want, have) {
144-
t.Errorf("unexpected iSCSI stats:\nwant:\n%v\nhave:\n%v", want, have)
144+
if diff := cmp.Diff(want, have); diff != "" {
145+
t.Fatalf("unexpected iSCSI stats (-want +got):\n%s", diff)
145146
} else {
146147
readMB, writeMB, iops, err := iscsi.ReadWriteOPS(stat.RootPath+"/"+stat.Name,
147148
stat.Tpgt[0].Name, stat.Tpgt[0].Luns[0].Name)
@@ -150,14 +151,14 @@ func TestGetStats(t *testing.T) {
150151
stat.Name, stat.Tpgt[0].Name, stat.Tpgt[0].Luns[0].Name)
151152
t.Errorf("%v", err)
152153
}
153-
if !reflect.DeepEqual(readTests[i].read, readMB) {
154-
t.Errorf("unexpected iSCSI read data :\nwant:\n%v\nhave:\n%v", readTests[i].read, readMB)
154+
if diff := cmp.Diff(readTests[i].read, readMB); diff != "" {
155+
t.Fatalf("unexpected iSCSI read data (-want +got):\n%s", diff)
155156
}
156-
if !reflect.DeepEqual(readTests[i].write, writeMB) {
157-
t.Errorf("unexpected iSCSI write data :\nwant:\n%v\nhave:\n%v", readTests[i].write, writeMB)
157+
if diff := cmp.Diff(readTests[i].write, writeMB); diff != "" {
158+
t.Fatalf("unexpected iSCSI write data (-want +got):\n%s", diff)
158159
}
159-
if !reflect.DeepEqual(readTests[i].iops, iops) {
160-
t.Errorf("unexpected iSCSI iops data :\nwant:\n%v\nhave:\n%v", readTests[i].iops, iops)
160+
if diff := cmp.Diff(readTests[i].iops, iops); diff != "" {
161+
t.Fatalf("unexpected iSCSI iops data (-want +got):\n%s", diff)
161162
}
162163
switch stat.Tpgt[0].Luns[0].Backstore {
163164
case "rd_mcp":
@@ -168,8 +169,8 @@ func TestGetStats(t *testing.T) {
168169
// Name ObjectName
169170
wantRdmcp := &iscsi.RDMCP{"rd_mcp_" + stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].ObjectName}
170171

171-
if !reflect.DeepEqual(wantRdmcp, haveRdmcp) {
172-
t.Errorf("unexpected rdmcp data :\nwant:\n%v\nhave:\n%v", wantRdmcp, haveRdmcp)
172+
if diff := cmp.Diff(wantRdmcp, haveRdmcp); diff != "" {
173+
t.Fatalf("unexpected rdmcp data (-want +got):\n%s", diff)
173174
}
174175
case "iblock":
175176
haveIblock, err := sysconfigfs.GetIblockUdev("0", "block_lio_rbd1")
@@ -178,8 +179,8 @@ func TestGetStats(t *testing.T) {
178179
}
179180
// Name Bnumber ObjectName Iblock
180181
wantIblock := &iscsi.IBLOCK{"iblock_" + stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].ObjectName, "/dev/rbd1"}
181-
if !reflect.DeepEqual(wantIblock, haveIblock) {
182-
t.Errorf("unexpected iblock data :\nwant:\n%v\nhave:\n%v", wantIblock, haveIblock)
182+
if diff := cmp.Diff(wantIblock, haveIblock); diff != "" {
183+
t.Fatalf("unexpected iblock data (-want +got):\n%s", diff)
183184
}
184185
case "fileio":
185186
haveFileIO, err := sysconfigfs.GetFileioUdev("1", "file_lio_1G")
@@ -188,8 +189,8 @@ func TestGetStats(t *testing.T) {
188189
}
189190
// Name, Fnumber, ObjectName, Filename
190191
wantFileIO := &iscsi.FILEIO{"fileio_" + stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].TypeNumber, "file_lio_1G", "/home/iscsi/file_back_1G"}
191-
if !reflect.DeepEqual(wantFileIO, haveFileIO) {
192-
t.Errorf("unexpected fileio data :\nwant:\n%v\nhave:\n%v", wantFileIO, haveFileIO)
192+
if diff := cmp.Diff(wantFileIO, haveFileIO); diff != "" {
193+
t.Fatalf("unexpected fileio data (-want +got):\n%s", diff)
193194
}
194195
case "rbd":
195196
haveRBD, err := sysconfigfs.GetRBDMatch("0", "iscsi-images-demo")
@@ -198,8 +199,8 @@ func TestGetStats(t *testing.T) {
198199
}
199200
// Name, Rnumber, Pool, Image
200201
wantRBD := &iscsi.RBD{"rbd_" + stat.Tpgt[0].Luns[0].TypeNumber, stat.Tpgt[0].Luns[0].TypeNumber, "iscsi-images", "demo"}
201-
if !reflect.DeepEqual(wantRBD, haveRBD) {
202-
t.Errorf("unexpected fileio data :\nwant:\n%v\nhave:\n%v", wantRBD, haveRBD)
202+
if diff := cmp.Diff(wantRBD, haveRBD); diff != "" {
203+
t.Fatalf("unexpected fileio data (-want +got):\n%s", diff)
203204
}
204205
}
205206
}

mountinfo_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package procfs
1414

1515
import (
16-
"reflect"
1716
"testing"
1817

1918
"github.com/google/go-cmp/cmp"
@@ -179,8 +178,8 @@ func TestMountInfo(t *testing.T) {
179178
t.Errorf("unexpected error: %v", err)
180179
}
181180

182-
if want, have := test.mount, mount; !reflect.DeepEqual(want, have) {
183-
t.Errorf("mounts:\nwant:\n%+v\nhave:\n%+v", want, have)
181+
if diff := cmp.Diff(test.mount, mount); diff != "" {
182+
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
184183
}
185184
}
186185
}

mountstats_test.go

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
package procfs
1515

1616
import (
17-
"fmt"
18-
"reflect"
1917
"strings"
2018
"testing"
2119
"time"
20+
21+
"github.com/google/go-cmp/cmp"
2222
)
2323

2424
func TestMountStats(t *testing.T) {
@@ -503,40 +503,12 @@ func TestMountStats(t *testing.T) {
503503
t.Errorf("unexpected error: %v", err)
504504
}
505505

506-
if want, have := tt.mounts, mounts; !reflect.DeepEqual(want, have) {
507-
t.Errorf("mounts:\nwant:\n%v\nhave:\n%v", mountsStr(want), mountsStr(have))
506+
if diff := cmp.Diff(tt.mounts, mounts); diff != "" {
507+
t.Fatalf("unexpected mounts (-want +got):\n%s", diff)
508508
}
509509
}
510510
}
511511

512-
func mountsStr(mounts []*Mount) string {
513-
var out string
514-
for i, m := range mounts {
515-
out += fmt.Sprintf("[%d] %q on %q (%q)", i, m.Device, m.Mount, m.Type)
516-
517-
stats, ok := m.Stats.(*MountStatsNFS)
518-
if !ok {
519-
out += "\n"
520-
continue
521-
}
522-
523-
out += fmt.Sprintf("\n\t- opts: %s", stats.Opts)
524-
out += fmt.Sprintf("\n\t- v%s, age: %s", stats.StatVersion, stats.Age)
525-
out += fmt.Sprintf("\n\t- bytes: %v", stats.Bytes)
526-
out += fmt.Sprintf("\n\t- events: %v", stats.Events)
527-
out += fmt.Sprintf("\n\t- transport: %v", stats.Transport)
528-
out += "\n\t- per-operation stats:"
529-
530-
for _, o := range stats.Operations {
531-
out += fmt.Sprintf("\n\t\t- %v", o)
532-
}
533-
534-
out += "\n"
535-
}
536-
537-
return out
538-
}
539-
540512
func TestMountStatsExtendedOperationStats(t *testing.T) {
541513
r := strings.NewReader(extendedOpsExampleMountstats)
542514
_, err := parseMountStats(r)

net_conntrackstat_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ package procfs
1515

1616
import (
1717
"bytes"
18-
"reflect"
1918
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
2021
)
2122

2223
func TestParseConntrackStat(t *testing.T) {
@@ -79,8 +80,8 @@ func TestParseConntrackStat(t *testing.T) {
7980
SearchRestart: 4,
8081
},
8182
}
82-
if !reflect.DeepEqual(want, have) {
83-
t.Errorf("want %v, have %v", want, have)
83+
if diff := cmp.Diff(want, have); diff != "" {
84+
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
8485
}
8586
}
8687

@@ -112,7 +113,7 @@ func TestParseOldConntrackStat(t *testing.T) {
112113
SearchRestart: 0,
113114
},
114115
}
115-
if !reflect.DeepEqual(want, have) {
116-
t.Errorf("want %v, have %v", want, have)
116+
if diff := cmp.Diff(want, have); diff != "" {
117+
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
117118
}
118119
}

net_ip_socket_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ package procfs
1515

1616
import (
1717
"net"
18-
"reflect"
1918
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
2021
)
2122

2223
func Test_parseNetIPSocketLine(t *testing.T) {
@@ -115,8 +116,8 @@ func Test_parseNetIPSocketLine(t *testing.T) {
115116
if tt.want == nil && got != nil {
116117
t.Errorf("parseNetIPSocketLine() = %v, want %v", got, tt.want)
117118
}
118-
if !reflect.DeepEqual(got, tt.want) {
119-
t.Errorf("parseNetIPSocketLine() = %#v, want %#v", got, tt.want)
119+
if diff := cmp.Diff(got, tt.want); diff != "" {
120+
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
120121
}
121122
})
122123
}

net_route_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ package procfs
1515

1616
import (
1717
"bytes"
18-
"reflect"
1918
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
2021
)
2122

2223
func TestParseNetRoute(t *testing.T) {
@@ -54,7 +55,7 @@ eno16780032 0000A8C0 00000000 0001 0 0 100 0000FFFF 0
5455
IRTT: 0,
5556
},
5657
}
57-
if !reflect.DeepEqual(want, parsed) {
58-
t.Errorf("want %v, parsed %v", want, parsed)
58+
if diff := cmp.Diff(want, parsed); diff != "" {
59+
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
5960
}
6061
}

net_tcp_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ package procfs
1515

1616
import (
1717
"net"
18-
"reflect"
1918
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
2021
)
2122

2223
func Test_newNetTCP(t *testing.T) {
@@ -120,8 +121,8 @@ func Test_newNetTCP(t *testing.T) {
120121
t.Errorf("newNetTCP() error = %v, wantErr %v", err, tt.wantErr)
121122
return
122123
}
123-
if !reflect.DeepEqual(got, tt.want) {
124-
t.Errorf("newNetTCP() = %v, want %v", got, tt.want)
124+
if diff := cmp.Diff(tt.want, got); diff != "" {
125+
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
125126
}
126127
})
127128
}
@@ -166,8 +167,8 @@ func Test_newNetTCPSummary(t *testing.T) {
166167
t.Errorf("newNetTCPSummary() error = %v, wantErr %v", err, tt.wantErr)
167168
return
168169
}
169-
if !reflect.DeepEqual(got, tt.want) {
170-
t.Errorf("newNetTCPSummary() = %v, want %v", got, tt.want)
170+
if diff := cmp.Diff(tt.want, got); diff != "" {
171+
t.Fatalf("unexpected diff (-want +got):\n%s", diff)
171172
}
172173
})
173174
}

net_udp_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ package procfs
1515

1616
import (
1717
"net"
18-
"reflect"
1918
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
2021
)
2122

2223
func Test_newNetUDP(t *testing.T) {
@@ -125,8 +126,8 @@ func Test_newNetUDP(t *testing.T) {
125126
t.Errorf("newNetUDP() error = %v, wantErr %v", err, tt.wantErr)
126127
return
127128
}
128-
if !reflect.DeepEqual(got, tt.want) {
129-
t.Errorf("newNetUDP() = %v, want %v", got, tt.want)
129+
if diff := cmp.Diff(got, tt.want); diff != "" {
130+
t.Fatalf("unexpected newNetUDP() (-want +got):\n%s", diff)
130131
}
131132
})
132133
}
@@ -171,8 +172,8 @@ func Test_newNetUDPSummary(t *testing.T) {
171172
t.Errorf("newNetUDPSummary() error = %v, wantErr %v", err, tt.wantErr)
172173
return
173174
}
174-
if !reflect.DeepEqual(got, tt.want) {
175-
t.Errorf("newNetUDPSummary() = %v, want %v", got, tt.want)
175+
if diff := cmp.Diff(got, tt.want); diff != "" {
176+
t.Fatalf("unexpected newNetUDPSummary() (-want +got):\n%s", diff)
176177
}
177178
})
178179
}

0 commit comments

Comments
 (0)