From dd2712982163ddaded7f7e1eb2d7362b0f7d5a70 Mon Sep 17 00:00:00 2001 From: Indira Ramasamy Date: Thu, 6 Oct 2022 14:32:23 -0600 Subject: [PATCH] MFW-2299: Prune the in-memory device data in reportd --- services/discovery/devices_list_test.go | 37 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/services/discovery/devices_list_test.go b/services/discovery/devices_list_test.go index 55e91976..c69b8b4c 100644 --- a/services/discovery/devices_list_test.go +++ b/services/discovery/devices_list_test.go @@ -23,6 +23,7 @@ type DeviceListTestSuite struct { mac2 string mac3 string mac4 string + mac5 string devicesTable map[string]*DeviceEntry now time.Time oneHourAgo time.Time @@ -42,6 +43,7 @@ func (suite *DeviceListTestSuite) SetupTest() { suite.mac2 = "00:11:22:33:44:66" suite.mac3 = "00:11:33:44:55:66" suite.mac4 = "00:aa:bb:cc:dd:ee" + suite.mac5 = "00:aa:bb:cc:dd:ef" suite.devicesTable = map[string]*DeviceEntry{ suite.mac1: {DiscoveryEntry: disco.DiscoveryEntry{ MacAddress: suite.mac1, @@ -62,6 +64,11 @@ func (suite *DeviceListTestSuite) SetupTest() { MacAddress: suite.mac4, LastUpdate: suite.aDayago.Unix(), }}, + suite.mac5: {DiscoveryEntry: disco.DiscoveryEntry{ + MacAddress: suite.mac5, + LastUpdate: suite.aDayago.Unix(), + IPv4Address: "192.168.56.4", + }}, } suite.deviceList = NewDevicesList() for _, entry := range suite.devicesTable { @@ -89,7 +96,7 @@ func (suite *DeviceListTestSuite) TestListing() { tests := []testSpec{ { predicates: []ListPredicate{}, - expectedListMacs: []string{suite.mac1, suite.mac2, suite.mac3, suite.mac4}, + expectedListMacs: []string{suite.mac1, suite.mac2, suite.mac3, suite.mac4, suite.mac5}, description: "Empty predicate list should return all MACs.", }, { @@ -108,7 +115,7 @@ func (suite *DeviceListTestSuite) TestListing() { predicates: []ListPredicate{ LastUpdateOlderThanDuration(time.Hour * 24), }, - expectedListMacs: []string{suite.mac4}, + expectedListMacs: []string{suite.mac4, suite.mac5}, description: "List of macs with an update time older than 24 hours should be mac4", }, } @@ -152,6 +159,7 @@ func (suite *DeviceListTestSuite) TestListing() { } wg.Wait() + } // TestMerge tests the merge and add funcionality. We test that @@ -264,26 +272,28 @@ func (suite *DeviceListTestSuite) TestBroadcastInsertion() { func (suite *DeviceListTestSuite) TestCleanDeviceEntry() { var deviceList DevicesList - var count uint32 + var device_count, device_ip_count uint32 deviceList.Devices = map[string]*DeviceEntry{} + deviceList.devicesByIP = map[string]*DeviceEntry{} for _, entry := range suite.devicesTable { - deviceList.Devices[entry.MacAddress] = &DeviceEntry{ - DiscoveryEntry: disco.DiscoveryEntry{ - IPv4Address: entry.IPv4Address, - MacAddress: entry.MacAddress, - LastUpdate: entry.LastUpdate, - }, - } - count++ + deviceList.PutDevice(entry) + + device_count++ + } + //device_ip_count = device_count - 1, because one device entry didn't have IPV4address. + //So it should not be added to devicesByIP map + device_ip_count = device_count - 1 + //Clean entries which are 48 hours older predicates1 := []ListPredicate{ LastUpdateOlderThanDuration(time.Hour * 48), } deviceList.CleanOldDeviceEntry(predicates1...) //No entries should be deleted from the list, because no entries with LastUpdate older than 48 hours - suite.EqualValues(count, len(deviceList.Devices), "Cleaned 48 hours older entry") + suite.EqualValues(device_count, len(deviceList.Devices), "Cleaned 48 hours older entry") + suite.EqualValues(device_ip_count, len(deviceList.devicesByIP), "Cleaned 48 hours older entry") //Clean entries which are 24 hours older predicates2 := []ListPredicate{ @@ -291,7 +301,8 @@ func (suite *DeviceListTestSuite) TestCleanDeviceEntry() { } deviceList.CleanOldDeviceEntry(predicates2...) //One device entry should be deleted from the device list which entry has LastUpdate with >24 hours - suite.EqualValues(count-1, len(deviceList.Devices), "Cleaned 24 hours older entry") + suite.EqualValues(device_count-2, len(deviceList.Devices), "Cleaned 24 hours older entry") + suite.EqualValues(device_ip_count-1, len(deviceList.devicesByIP), "Cleaned 24 hours older entry") } // TestMarshallingList tests that we can marshal a list of devices