@@ -17,181 +17,16 @@ limitations under the License.
1717package notifications
1818
1919import (
20- "sync"
2120 "testing"
2221
2322 "github.com/stretchr/testify/assert"
2423
25- istioclientv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
2624 networkingv1 "k8s.io/api/networking/v1"
2725 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2826 "sigs.k8s.io/controller-runtime/pkg/client"
2927 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3028)
3129
32- func TestCreateNotificationsTables (t * testing.T ) {
33- testCases := []struct {
34- name string
35- notifications map [string ][]Notification
36- wantedTables map [string ]string
37- }{
38- {
39- name : "no notifications" ,
40- notifications : map [string ][]Notification {},
41- wantedTables : map [string ]string {},
42- },
43- {
44- name : "single provider with one notification" ,
45- notifications : map [string ][]Notification {
46- "provider1" : {
47- {
48- Type : WarningNotification ,
49- Message : "warning message" ,
50- },
51- },
52- },
53- wantedTables : map [string ]string {
54- "provider1" : `Notifications from PROVIDER1:
55- +--------------+-----------------+----------------+
56- | MESSAGE TYPE | NOTIFICATION | CALLING OBJECT |
57- +--------------+-----------------+----------------+
58- | WARNING | warning message | |
59- +--------------+-----------------+----------------+
60- ` ,
61- },
62- },
63- {
64- name : "two providers each with two notifications" ,
65- notifications : map [string ][]Notification {
66- "istio" : {
67- {
68- Type : WarningNotification ,
69- Message : "a very long warning notification generated by VirtualService ns/test from the provider istio" ,
70- CallingObjects : []client.Object {
71- & istioclientv1beta1.VirtualService {
72- TypeMeta : metav1.TypeMeta {
73- Kind : "VirtualService" ,
74- },
75- ObjectMeta : metav1.ObjectMeta {
76- Name : "test" ,
77- Namespace : "ns" ,
78- },
79- },
80- },
81- },
82- {
83- Type : InfoNotification ,
84- Message : "successfully converted VirtualService ns/test to HTTPRoute" ,
85- CallingObjects : []client.Object {
86- & istioclientv1beta1.VirtualService {
87- TypeMeta : metav1.TypeMeta {
88- Kind : "VirtualService" ,
89- },
90- ObjectMeta : metav1.ObjectMeta {
91- Name : "test" ,
92- Namespace : "ns" ,
93- },
94- },
95- },
96- },
97- },
98- "kong" : {
99- {
100- Type : InfoNotification ,
101- Message : "informational notification from kong provider" ,
102- CallingObjects : []client.Object {
103- & networkingv1.Ingress {
104- TypeMeta : metav1.TypeMeta {
105- Kind : "Ingress" ,
106- },
107- ObjectMeta : metav1.ObjectMeta {
108- Name : "ingress-kong" ,
109- Namespace : "default" ,
110- },
111- },
112- },
113- },
114- {
115- Type : ErrorNotification ,
116- Message : "error notification genereated by kong" ,
117- },
118- },
119- },
120- wantedTables : map [string ]string {
121- "istio" : `Notifications from ISTIO:
122- +--------------+----------------------------------------------------------------------------------------------+-------------------------+
123- | MESSAGE TYPE | NOTIFICATION | CALLING OBJECT |
124- +--------------+----------------------------------------------------------------------------------------------+-------------------------+
125- | WARNING | a very long warning notification generated by VirtualService ns/test from the provider istio | VirtualService: ns/test |
126- +--------------+----------------------------------------------------------------------------------------------+-------------------------+
127- | INFO | successfully converted VirtualService ns/test to HTTPRoute | VirtualService: ns/test |
128- +--------------+----------------------------------------------------------------------------------------------+-------------------------+
129- ` ,
130- "kong" : `Notifications from KONG:
131- +--------------+-----------------------------------------------+-------------------------------+
132- | MESSAGE TYPE | NOTIFICATION | CALLING OBJECT |
133- +--------------+-----------------------------------------------+-------------------------------+
134- | INFO | informational notification from kong provider | Ingress: default/ingress-kong |
135- +--------------+-----------------------------------------------+-------------------------------+
136- | ERROR | error notification genereated by kong | |
137- +--------------+-----------------------------------------------+-------------------------------+
138- ` ,
139- },
140- },
141- }
142-
143- for _ , tc := range testCases {
144- tc := tc
145- t .Run (tc .name , func (t * testing.T ) {
146- na := NotificationAggregator {
147- Notifications : tc .notifications ,
148- }
149- result := na .CreateNotificationTables ()
150- for provider , table := range result {
151- assert .Equal (t , tc .wantedTables [provider ], table )
152- }
153- })
154- }
155- }
156-
157- // TestNotificationAggregatorRace checks for data races in NotificationAggregator when accessed
158- // concurrently.
159- func TestNotificationAggregatorRace (_ * testing.T ) {
160- na := NotificationAggregator {Notifications : map [string ][]Notification {}}
161-
162- providers := []string {"provider1" , "provider2" , "provider3" , "provider4" }
163- var wg sync.WaitGroup
164-
165- // Start multiple goroutines that dispatch notifications concurrently.
166- for _ , provider := range providers {
167- wg .Add (1 )
168- go func (p string ) {
169- for range 100 {
170- na .DispatchNotification (
171- Notification {
172- Type : WarningNotification ,
173- Message : "concurrent warning" ,
174- },
175- p ,
176- )
177- }
178- wg .Done ()
179- }(provider )
180- }
181-
182- // Concurrently read from the aggregator while writes are happening.
183- wg .Add (1 )
184- go func () {
185- for range 100 {
186- _ = na .CreateNotificationTables ()
187- }
188- wg .Done ()
189- }()
190-
191- // Wait for all goroutines to complete.
192- wg .Wait ()
193- }
194-
19530func TestConvertObjectsToStr (t * testing.T ) {
19631 testCases := []struct {
19732 name string
0 commit comments