Skip to content

Commit f0d1302

Browse files
[utils] Add alert utils
Signed-off-by: Mykola <mykola_kobets@epam.com>
1 parent 1ffb37b commit f0d1302

File tree

2 files changed

+124
-32
lines changed

2 files changed

+124
-32
lines changed

resourcemonitor/resourcemonitor_internal_test.go

+3-32
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/aosedge/aos_common/aoserrors"
3030
"github.com/aosedge/aos_common/aostypes"
3131
"github.com/aosedge/aos_common/api/cloudprotocol"
32+
"github.com/aosedge/aos_common/utils/alertutils"
3233
"github.com/shirou/gopsutil/disk"
3334
"github.com/shirou/gopsutil/mem"
3435
log "github.com/sirupsen/logrus"
@@ -766,7 +767,7 @@ func TestInstances(t *testing.T) {
766767
alertLoop:
767768
for _, expectedAlert := range item.alerts {
768769
for _, receivedAlert := range alertSender.alerts {
769-
if AlertsEqual(expectedAlert, receivedAlert) {
770+
if alertutils.AlertsPayloadEqual(expectedAlert, receivedAlert) {
770771
continue alertLoop
771772
}
772773
}
@@ -1233,43 +1234,13 @@ func (host *testInstancesUsage) FillSystemInfo(instanceID string, instance *inst
12331234
return nil
12341235
}
12351236

1236-
func AlertsEqual(alert1, alert2 interface{}) bool {
1237-
switch alert1casted := alert1.(type) {
1238-
case cloudprotocol.SystemQuotaAlert:
1239-
alert2casted, ok := alert2.(cloudprotocol.SystemQuotaAlert)
1240-
1241-
if !ok {
1242-
return false
1243-
}
1244-
1245-
alert1casted.Timestamp = time.Time{}
1246-
alert2casted.Timestamp = time.Time{}
1247-
1248-
return reflect.DeepEqual(alert1casted, alert2casted)
1249-
1250-
case cloudprotocol.InstanceQuotaAlert:
1251-
alert2casted, ok := alert2.(cloudprotocol.InstanceQuotaAlert)
1252-
1253-
if !ok {
1254-
return false
1255-
}
1256-
1257-
alert1casted.Timestamp = time.Time{}
1258-
alert2casted.Timestamp = time.Time{}
1259-
1260-
return reflect.DeepEqual(alert1casted, alert2casted)
1261-
}
1262-
1263-
return false
1264-
}
1265-
12661237
func AlertSlicesEqual(alerts1, alerts2 []interface{}) bool {
12671238
if len(alerts1) != len(alerts2) {
12681239
return false
12691240
}
12701241

12711242
for i := range alerts1 {
1272-
if !AlertsEqual(alerts1[i], alerts2[i]) {
1243+
if !alertutils.AlertsPayloadEqual(alerts1[i], alerts2[i]) {
12731244
return false
12741245
}
12751246
}

utils/alertutils/alertutils.go

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright (C) 2024 Renesas Electronics Corporation.
4+
// Copyright (C) 2024 EPAM Systems, Inc.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
package alertutils
19+
20+
import (
21+
"reflect"
22+
23+
"github.com/aosedge/aos_common/api/cloudprotocol"
24+
)
25+
26+
// AlertsPayloadEqual compares alerts ignoring timestamp.
27+
//
28+
//nolint:funlen
29+
func AlertsPayloadEqual(alert1, alert2 interface{}) bool {
30+
switch alert1casted := alert1.(type) {
31+
case cloudprotocol.SystemAlert:
32+
alert2casted, ok := alert2.(cloudprotocol.SystemAlert)
33+
if !ok {
34+
return false
35+
}
36+
37+
newAlert1 := alert1casted
38+
newAlert1.Timestamp = alert2casted.Timestamp
39+
40+
return reflect.DeepEqual(newAlert1, alert2casted)
41+
42+
case cloudprotocol.CoreAlert:
43+
alert2casted, ok := alert2.(cloudprotocol.CoreAlert)
44+
if !ok {
45+
return false
46+
}
47+
48+
newAlert1 := alert1casted
49+
newAlert1.Timestamp = alert2casted.Timestamp
50+
51+
return reflect.DeepEqual(newAlert1, alert2casted)
52+
53+
case cloudprotocol.DownloadAlert:
54+
alert2casted, ok := alert2.(cloudprotocol.DownloadAlert)
55+
if !ok {
56+
return false
57+
}
58+
59+
newAlert1 := alert1casted
60+
newAlert1.Timestamp = alert2casted.Timestamp
61+
62+
return reflect.DeepEqual(newAlert1, alert2casted)
63+
64+
case cloudprotocol.SystemQuotaAlert:
65+
alert2casted, ok := alert2.(cloudprotocol.SystemQuotaAlert)
66+
if !ok {
67+
return false
68+
}
69+
70+
newAlert1 := alert1casted
71+
newAlert1.Timestamp = alert2casted.Timestamp
72+
73+
return reflect.DeepEqual(newAlert1, alert2casted)
74+
75+
case cloudprotocol.InstanceQuotaAlert:
76+
alert2casted, ok := alert2.(cloudprotocol.InstanceQuotaAlert)
77+
if !ok {
78+
return false
79+
}
80+
81+
newAlert1 := alert1casted
82+
newAlert1.Timestamp = alert2casted.Timestamp
83+
84+
return reflect.DeepEqual(newAlert1, alert2casted)
85+
86+
case cloudprotocol.DeviceAllocateAlert:
87+
alert2casted, ok := alert2.(cloudprotocol.DeviceAllocateAlert)
88+
if !ok {
89+
return false
90+
}
91+
92+
newAlert1 := alert1casted
93+
newAlert1.Timestamp = alert2casted.Timestamp
94+
95+
return reflect.DeepEqual(newAlert1, alert2casted)
96+
97+
case cloudprotocol.ResourceValidateAlert:
98+
alert2casted, ok := alert2.(cloudprotocol.ResourceValidateAlert)
99+
if !ok {
100+
return false
101+
}
102+
103+
newAlert1 := alert1casted
104+
newAlert1.Timestamp = alert2casted.Timestamp
105+
106+
return reflect.DeepEqual(newAlert1, alert2casted)
107+
108+
case cloudprotocol.ServiceInstanceAlert:
109+
alert2casted, ok := alert2.(cloudprotocol.ServiceInstanceAlert)
110+
if !ok {
111+
return false
112+
}
113+
114+
newAlert1 := alert1casted
115+
newAlert1.Timestamp = alert2casted.Timestamp
116+
117+
return reflect.DeepEqual(newAlert1, alert2casted)
118+
}
119+
120+
return false
121+
}

0 commit comments

Comments
 (0)