|
| 1 | +package flow |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + // the same flow from 2 different interfaces |
| 12 | + oneIf1 = &Record{RawRecord: RawRecord{RecordKey: RecordKey{ |
| 13 | + EthProtocol: 1, Direction: 1, Transport: Transport{SrcPort: 123, DstPort: 456}, |
| 14 | + DataLink: DataLink{DstMac: MacAddr{0x1}, SrcMac: MacAddr{0x1}}, IFIndex: 1, |
| 15 | + }, RecordMetrics: RecordMetrics{ |
| 16 | + Packets: 2, Bytes: 456, |
| 17 | + }}, Interface: "eth0"} |
| 18 | + oneIf2 = &Record{RawRecord: RawRecord{RecordKey: RecordKey{ |
| 19 | + EthProtocol: 1, Direction: 1, Transport: Transport{SrcPort: 123, DstPort: 456}, |
| 20 | + DataLink: DataLink{DstMac: MacAddr{0x2}, SrcMac: MacAddr{0x2}}, IFIndex: 2, |
| 21 | + }, RecordMetrics: RecordMetrics{ |
| 22 | + Packets: 2, Bytes: 456, |
| 23 | + }}, Interface: "123456789"} |
| 24 | + // another fow from 2 different interfaces |
| 25 | + twoIf1 = &Record{RawRecord: RawRecord{RecordKey: RecordKey{ |
| 26 | + EthProtocol: 1, Direction: 1, Transport: Transport{SrcPort: 333, DstPort: 456}, |
| 27 | + DataLink: DataLink{DstMac: MacAddr{0x1}, SrcMac: MacAddr{0x1}}, IFIndex: 1, |
| 28 | + }, RecordMetrics: RecordMetrics{ |
| 29 | + Packets: 2, Bytes: 456, |
| 30 | + }}, Interface: "eth0"} |
| 31 | + twoIf2 = &Record{RawRecord: RawRecord{RecordKey: RecordKey{ |
| 32 | + EthProtocol: 1, Direction: 1, Transport: Transport{SrcPort: 333, DstPort: 456}, |
| 33 | + DataLink: DataLink{DstMac: MacAddr{0x2}, SrcMac: MacAddr{0x2}}, IFIndex: 2, |
| 34 | + }, RecordMetrics: RecordMetrics{ |
| 35 | + Packets: 2, Bytes: 456, |
| 36 | + }}, Interface: "123456789"} |
| 37 | +) |
| 38 | + |
| 39 | +func TestDedupe(t *testing.T) { |
| 40 | + input := make(chan []*Record, 100) |
| 41 | + output := make(chan []*Record, 100) |
| 42 | + |
| 43 | + go Dedupe(time.Minute)(input, output) |
| 44 | + |
| 45 | + input <- []*Record{ |
| 46 | + oneIf2, // record 1 at interface 2: should be accepted |
| 47 | + twoIf1, // record 2 at interface 1: should be accepted |
| 48 | + oneIf1, // record 1 duplicate at interface 1: should NOT be accepted |
| 49 | + oneIf1, // (same record key, different interface) |
| 50 | + twoIf2, // record 2 duplicate at interface 2: should NOT be accepted |
| 51 | + oneIf2, // record 1 at interface 1: should be accepted (same record key, same interface) |
| 52 | + } |
| 53 | + deduped := receiveTimeout(t, output) |
| 54 | + assert.Equal(t, []*Record{oneIf2, twoIf1, oneIf2}, deduped) |
| 55 | + |
| 56 | + // should still accept records with same key, same interface, |
| 57 | + // and discard these with same key, different interface |
| 58 | + input <- []*Record{oneIf1, oneIf2} |
| 59 | + deduped = receiveTimeout(t, output) |
| 60 | + assert.Equal(t, []*Record{oneIf2}, deduped) |
| 61 | +} |
| 62 | + |
| 63 | +func TestDedupe_EvictFlows(t *testing.T) { |
| 64 | + tm := &timerMock{now: time.Now()} |
| 65 | + timeNow = tm.Now |
| 66 | + input := make(chan []*Record, 100) |
| 67 | + output := make(chan []*Record, 100) |
| 68 | + |
| 69 | + go Dedupe(15*time.Second)(input, output) |
| 70 | + |
| 71 | + // Should only accept records 1 and 2, at interface 1 |
| 72 | + input <- []*Record{oneIf1, twoIf1, oneIf2} |
| 73 | + assert.Equal(t, []*Record{oneIf1, twoIf1}, |
| 74 | + receiveTimeout(t, output)) |
| 75 | + |
| 76 | + tm.now = tm.now.Add(10 * time.Second) |
| 77 | + |
| 78 | + // After 10 seconds, it still filters existing flows from different interfaces |
| 79 | + input <- []*Record{oneIf2} |
| 80 | + time.Sleep(100 * time.Millisecond) |
| 81 | + requireNoEviction(t, output) |
| 82 | + |
| 83 | + tm.now = tm.now.Add(10 * time.Second) |
| 84 | + |
| 85 | + // Record 2 hasn't been accounted for >expiryTime, so it will accept the it again |
| 86 | + // whatever the interface. |
| 87 | + // Since record 1 was accessed 10 seconds ago (<expiry time) it will filter it |
| 88 | + input <- []*Record{oneIf2, twoIf2, twoIf1} |
| 89 | + assert.Equal(t, []*Record{twoIf2}, |
| 90 | + receiveTimeout(t, output)) |
| 91 | + |
| 92 | + tm.now = tm.now.Add(20 * time.Second) |
| 93 | + |
| 94 | + // when all the records expire, the deduper is reset for that flow |
| 95 | + input <- []*Record{oneIf2, twoIf2} |
| 96 | + assert.Equal(t, []*Record{oneIf2, twoIf2}, |
| 97 | + receiveTimeout(t, output)) |
| 98 | +} |
| 99 | + |
| 100 | +type timerMock struct { |
| 101 | + now time.Time |
| 102 | +} |
| 103 | + |
| 104 | +func (tm *timerMock) Now() time.Time { |
| 105 | + return tm.now |
| 106 | +} |
0 commit comments