|
15 | 15 | package ovs |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "reflect" |
19 | 18 | "testing" |
| 19 | + |
| 20 | + "github.com/google/go-cmp/cmp" |
20 | 21 | ) |
21 | 22 |
|
22 | 23 | func TestFlowStatsUnmarshalText(t *testing.T) { |
23 | 24 | var tests = []struct { |
24 | | - desc string |
25 | | - s string |
26 | | - p *FlowStats |
27 | | - err error |
| 25 | + desc string |
| 26 | + s string |
| 27 | + stats *FlowStats |
| 28 | + ok bool |
28 | 29 | }{ |
29 | 30 | { |
30 | 31 | desc: "empty string", |
31 | | - err: ErrInvalidFlowStats, |
32 | 32 | }, |
33 | 33 | { |
34 | | - desc: "incorrect number of fields", |
35 | | - s: "NXST_AGGREGATE reply (xid=0x4): packet_count=642800 byte_count=141379644 flow_count=2, flow_count=3", |
36 | | - err: ErrInvalidFlowStats, |
| 34 | + desc: "too few fields", |
| 35 | + s: "NXST_AGGREGATE reply (xid=0x4): packet_count=642800 byte_count=141379644", |
37 | 36 | }, |
38 | 37 | { |
39 | | - desc: "first field is not NXST_AGGREGATE", |
40 | | - s: "NXST_REPLY reply (xid=0x4): packet_count=642800 byte_count=141379644 flow_count=2", |
41 | | - err: ErrInvalidFlowStats, |
| 38 | + desc: "too many fields", |
| 39 | + s: "NXST_AGGREGATE reply (xid=0x4): packet_count=642800 byte_count=141379644 flow_count=2, flow_count=3", |
42 | 40 | }, |
43 | 41 | { |
44 | | - desc: "packet_count string is missing", |
| 42 | + desc: "packet_count missing", |
45 | 43 | s: "NXST_AGGREGATE reply (xid=0x4): frame_count=642800 byte_count=141379644 flow_count=2", |
46 | | - err: ErrInvalidFlowStats, |
47 | 44 | }, |
48 | 45 | { |
49 | | - desc: "byte_count string is missing", |
| 46 | + desc: "byte_count missing", |
50 | 47 | s: "NXST_AGGREGATE reply (xid=0x4): packet_count=642800 bits*8_count=141379644 flow_count=2", |
51 | | - err: ErrInvalidFlowStats, |
52 | 48 | }, |
53 | 49 | { |
54 | | - desc: "broken packet_count=value pair", |
| 50 | + desc: "bad key=value", |
| 51 | + s: "NXST_AGGREGATE reply (xid=0x4): packet_count=1=foo byte_count=141379644 flow_count=2", |
| 52 | + }, |
| 53 | + { |
| 54 | + desc: "bad packet count", |
55 | 55 | s: "NXST_AGGREGATE reply (xid=0x4): packet_count=toosmall byte_count=141379644 flow_count=2", |
56 | | - err: ErrInvalidFlowStats, |
57 | 56 | }, |
58 | 57 | { |
59 | | - desc: "broken byte_count=value pair", |
| 58 | + desc: "bad byte count", |
60 | 59 | s: "NXST_AGGREGATE reply (xid=0x4): packet_count=642800 byte_count=toolarge flow_count=2", |
61 | | - err: ErrInvalidFlowStats, |
| 60 | + }, |
| 61 | + { |
| 62 | + desc: "bad flow count", |
| 63 | + s: "NXST_AGGREGATE reply (xid=0x4): packet_count=642800 byte_count=1 FLOW_count=2", |
62 | 64 | }, |
63 | 65 | { |
64 | 66 | desc: "OK", |
65 | 67 | s: "NXST_AGGREGATE reply (xid=0x4): packet_count=642800 byte_count=141379644 flow_count=2", |
66 | | - p: &FlowStats{ |
| 68 | + stats: &FlowStats{ |
67 | 69 | PacketCount: 642800, |
68 | 70 | ByteCount: 141379644, |
69 | 71 | }, |
| 72 | + ok: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + desc: "OK, OpenFlow 1.4", |
| 76 | + s: "OFPST_AGGREGATE reply (OF1.4) (xid=0x2): packet_count=1207 byte_count=101673 flow_count=1", |
| 77 | + stats: &FlowStats{ |
| 78 | + PacketCount: 1207, |
| 79 | + ByteCount: 101673, |
| 80 | + }, |
| 81 | + ok: true, |
70 | 82 | }, |
71 | 83 | } |
72 | 84 |
|
73 | 85 | for _, tt := range tests { |
74 | 86 | t.Run(tt.desc, func(t *testing.T) { |
75 | | - p := new(FlowStats) |
76 | | - err := p.UnmarshalText([]byte(tt.s)) |
| 87 | + stats := new(FlowStats) |
| 88 | + err := stats.UnmarshalText([]byte(tt.s)) |
77 | 89 |
|
78 | | - if want, got := errStr(tt.err), errStr(err); want != got { |
79 | | - t.Fatalf("unexpected error:\n- want: %v\n- got: %v", |
80 | | - want, got) |
| 90 | + if err != nil && tt.ok { |
| 91 | + t.Fatalf("unexpected error: %v", err) |
| 92 | + } |
| 93 | + if err == nil && !tt.ok { |
| 94 | + t.Fatal("expected an error, but none occurred") |
81 | 95 | } |
82 | 96 | if err != nil { |
83 | 97 | return |
84 | 98 | } |
85 | 99 |
|
86 | | - if want, got := tt.p, p; !reflect.DeepEqual(want, got) { |
87 | | - t.Fatalf("unexpected PortStats:\n- want: %#v\n- got: %#v", |
88 | | - want, got) |
| 100 | + if diff := cmp.Diff(tt.stats, stats); diff != "" { |
| 101 | + t.Fatalf("unexpected FlowStats (-want +got):\n%s", diff) |
89 | 102 | } |
90 | 103 | }) |
91 | 104 | } |
|
0 commit comments