From 39e133391916ef46602c6ae67ef1e5e2ef7caad0 Mon Sep 17 00:00:00 2001 From: Srijeet Chatterjee Date: Wed, 29 Jul 2020 12:32:03 -0600 Subject: [PATCH 1/3] Fixing GetMonitoringJson test --- .../monitoring/monitoring_test.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go b/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go index c51278eeb9..9c39641eab 100644 --- a/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go +++ b/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go @@ -392,6 +392,20 @@ func TestGetConfig(t *testing.T) { } } +func sortByInterfaceName(servers []Cache) { + for _, cache := range servers { + sort.SliceStable(cache.Interfaces, func(i, j int) bool { + interface1, interface2 := cache.Interfaces[i], cache.Interfaces[j] + switch { + case interface1.Name != interface2.Name: + return interface1.Name < interface2.Name + default: + return interface1.Name < interface2.Name + } + }) + } +} + func TestGetMonitoringJSON(t *testing.T) { resp := MonitoringResponse{} mockDB, mock, err := sqlmock.New() @@ -528,9 +542,10 @@ func TestGetMonitoringJSON(t *testing.T) { if err != nil { t.Errorf("GetMonitoringJSON expected: nil error, actual: %v", err) } - resp.Response.TrafficServers = sortCaches(resp.Response.TrafficServers) sqlResp.TrafficServers = sortCaches(sqlResp.TrafficServers) + sortByInterfaceName(resp.Response.TrafficServers) + sortByInterfaceName(sqlResp.TrafficServers) resp.Response.TrafficMonitors = sortMonitors(resp.Response.TrafficMonitors) sqlResp.TrafficMonitors = sortMonitors(sqlResp.TrafficMonitors) resp.Response.Cachegroups = sortCachegroups(resp.Response.Cachegroups) From 930dab504b087c7a0f205767b1083540724e0621 Mon Sep 17 00:00:00 2001 From: Srijeet Chatterjee Date: Wed, 29 Jul 2020 19:15:03 -0600 Subject: [PATCH 2/3] Code review fixes --- traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go b/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go index 9c39641eab..f99ee05d96 100644 --- a/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go +++ b/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go @@ -544,8 +544,6 @@ func TestGetMonitoringJSON(t *testing.T) { } resp.Response.TrafficServers = sortCaches(resp.Response.TrafficServers) sqlResp.TrafficServers = sortCaches(sqlResp.TrafficServers) - sortByInterfaceName(resp.Response.TrafficServers) - sortByInterfaceName(sqlResp.TrafficServers) resp.Response.TrafficMonitors = sortMonitors(resp.Response.TrafficMonitors) sqlResp.TrafficMonitors = sortMonitors(sqlResp.TrafficMonitors) resp.Response.Cachegroups = sortCachegroups(resp.Response.Cachegroups) @@ -623,6 +621,7 @@ func (s SortableCaches) Less(i, j int) bool { func sortCaches(p []Cache) []Cache { sort.Sort(SortableCaches(p)) + sortByInterfaceName(p) return p } From 1f5457501d2085be274489ab7d29c952c1d67082 Mon Sep 17 00:00:00 2001 From: Srijeet Chatterjee Date: Wed, 29 Jul 2020 20:51:30 -0600 Subject: [PATCH 3/3] Making interfaces into a sortable type --- .../monitoring/monitoring_test.go | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go b/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go index f99ee05d96..3f5ecb78f7 100644 --- a/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go +++ b/traffic_ops/traffic_ops_golang/monitoring/monitoring_test.go @@ -392,20 +392,6 @@ func TestGetConfig(t *testing.T) { } } -func sortByInterfaceName(servers []Cache) { - for _, cache := range servers { - sort.SliceStable(cache.Interfaces, func(i, j int) bool { - interface1, interface2 := cache.Interfaces[i], cache.Interfaces[j] - switch { - case interface1.Name != interface2.Name: - return interface1.Name < interface2.Name - default: - return interface1.Name < interface2.Name - } - }) - } -} - func TestGetMonitoringJSON(t *testing.T) { resp := MonitoringResponse{} mockDB, mock, err := sqlmock.New() @@ -542,6 +528,12 @@ func TestGetMonitoringJSON(t *testing.T) { if err != nil { t.Errorf("GetMonitoringJSON expected: nil error, actual: %v", err) } + for _, cache := range resp.Response.TrafficServers { + cache.Interfaces = sortInterfaces(cache.Interfaces) + } + for _, cache := range sqlResp.TrafficServers { + cache.Interfaces = sortInterfaces(cache.Interfaces) + } resp.Response.TrafficServers = sortCaches(resp.Response.TrafficServers) sqlResp.TrafficServers = sortCaches(sqlResp.TrafficServers) resp.Response.TrafficMonitors = sortMonitors(resp.Response.TrafficMonitors) @@ -619,9 +611,25 @@ func (s SortableCaches) Less(i, j int) bool { return s[i].HostName < s[j].HostName } +type SortableInterfaces []tc.ServerInterfaceInfo + +func (s SortableInterfaces) Len() int { + return len(s) +} +func (s SortableInterfaces) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} +func (s SortableInterfaces) Less(i, j int) bool { + return s[i].Name < s[j].Name +} + +func sortInterfaces(i []tc.ServerInterfaceInfo) []tc.ServerInterfaceInfo { + sort.Sort(SortableInterfaces(i)) + return i +} + func sortCaches(p []Cache) []Cache { sort.Sort(SortableCaches(p)) - sortByInterfaceName(p) return p }