1 package services
2
3
6 import (
7 "context"
8 "errors"
9 "fmt"
10 "testing"
11
12 "github.com/DATA-DOG/go-sqlmock"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15
16 "edge-infra.dev/pkg/edge/api/graph/model"
17 sqlquery "edge-infra.dev/pkg/edge/api/sql"
18 )
19
20 var (
21 logClassificationLabelTestEdgeIDs = []string{"a6d0489c-56c3-47fb-84cb-0c2d8051b9f0", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f1", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f2", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f3", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f4", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f5", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f6", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f7", "a6d0489c-56c3-47fb-84cb-0c2d8051b9f8"}
22 classificationTestEdgeIDs = []string{"de4f477e-1f68-40f7-9f1f-f9c9dc08c250", "de4f477e-1f68-40f7-9f1f-f9c9dc08c251", "de4f477e-1f68-40f7-9f1f-f9c9dc08c252", "de4f477e-1f68-40f7-9f1f-f9c9dc08c253", "de4f477e-1f68-40f7-9f1f-f9c9dc08c254", "de4f477e-1f68-40f7-9f1f-f9c9dc08c255", "de4f477e-1f68-40f7-9f1f-f9c9dc08c256", "de4f477e-1f68-40f7-9f1f-f9c9dc08c257", "de4f477e-1f68-40f7-9f1f-f9c9dc08c258"}
23 labelTestEdgeIDs = []string{"9c478a9c-b536-4ea9-94c7-144c87094f60", "9c478a9c-b536-4ea9-94c7-144c87094f61", "9c478a9c-b536-4ea9-94c7-144c87094f62", "9c478a9c-b536-4ea9-94c7-144c87094f63", "9c478a9c-b536-4ea9-94c7-144c87094f64", "9c478a9c-b536-4ea9-94c7-144c87094f65", "9c478a9c-b536-4ea9-94c7-144c87094f66", "9c478a9c-b536-4ea9-94c7-144c87094f67", "9c478a9c-b536-4ea9-94c7-144c87094f68"}
24 labelTestKeys = []string{"label-1", "label-2", "label-3", "label-4", "label-5", "label-6", "label-7", "label-8", "label-9"}
25
26 logClassificationLabelRows = []string{"log_classification_label_edge_id", "classification_edge_id", "banner_edge_id", "label_edge_id", "label_key", "classification", "pod", "container", "log_type", "log_class", "pattern"}
27 )
28
29 func TestCreateClassificationLabel(t *testing.T) {
30 db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
31 require.NoError(t, err)
32 defer db.Close()
33
34 testCases := []struct {
35 name string
36 labelID string
37 classificationID string
38 lclID string
39 mocksQuery func() []*sqlmock.ExpectedQuery
40 mocksExec func() []*sqlmock.ExpectedExec
41 expectedError error
42 }{
43 {
44 name: "Test 1 - Classification Already Tied to a label",
45 labelID: labelTestEdgeIDs[3],
46 classificationID: classificationTestEdgeIDs[3],
47 lclID: logClassificationLabelTestEdgeIDs[3],
48 mocksQuery: func() []*sqlmock.ExpectedQuery {
49 return []*sqlmock.ExpectedQuery{
50 mock.ExpectQuery(sqlquery.GetLogClassificationLabelsByLabel).
51 WithArgs(labelTestEdgeIDs[3]).
52 WillReturnRows(sqlmock.NewRows(logClassificationLabelRows).
53 AddRow(logClassificationLabelTestEdgeIDs[3], classificationTestEdgeIDs[3], testClassificationBannerEdgeID, labelTestEdgeIDs[3],
54 labelTestKeys[3], classificationDescriptions[3], classificationPods[3], classificationContainers[3], classificationTypes[3],
55 classificationClasses[0], classificationPatterns[3])),
56 }
57 },
58 expectedError: errors.New("Error Creating Log Classification Label: Classification is already tied to this label"),
59 },
60 {
61 name: "Test 2 - Everything works",
62 labelID: labelTestEdgeIDs[0],
63 classificationID: classificationTestEdgeIDs[0],
64 lclID: logClassificationLabelTestEdgeIDs[0],
65 mocksQuery: func() []*sqlmock.ExpectedQuery {
66 return []*sqlmock.ExpectedQuery{
67 mock.ExpectQuery(sqlquery.GetLogClassificationLabelsByLabel).
68 WithArgs(labelTestEdgeIDs[0]).
69 WillReturnRows(sqlmock.NewRows(logClassificationLabelRows))}
70 },
71 mocksExec: func() []*sqlmock.ExpectedExec {
72 return []*sqlmock.ExpectedExec{
73 mock.ExpectExec(sqlquery.CreateLogClassificationLabel).
74 WithArgs(logClassificationLabelTestEdgeIDs[0], classificationTestEdgeIDs[0], labelTestEdgeIDs[0]).
75 WillReturnResult(sqlmock.NewResult(1, 1))}
76 },
77 expectedError: nil,
78 },
79 }
80
81 service := NewLogClassificationLabelService(db)
82
83 for _, test := range testCases {
84 if test.expectedError == nil {
85 test.mocksQuery()
86 mock.ExpectBegin()
87 test.mocksExec()
88 mock.ExpectCommit()
89
90 t.Run(test.name, func(t *testing.T) {
91 result, err := service.CreateLogClassificationLabel(context.Background(), test.lclID, test.classificationID, test.labelID)
92 assert.NoError(t, err)
93 assert.Contains(t, result, "Entry Created Successfully")
94 })
95 } else {
96 test.mocksQuery()
97 result, err := service.CreateLogClassificationLabel(context.Background(), test.lclID, test.classificationID, test.labelID)
98
99 t.Run(test.name, func(t *testing.T) {
100 assert.Equal(t, result, "")
101 assert.ErrorContains(t, err, test.expectedError.Error())
102 })
103 }
104 }
105 }
106
107 func TestDeleteClassificationLabel(t *testing.T) {
108 db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
109 require.NoError(t, err)
110 defer db.Close()
111
112 mock.ExpectExec(sqlquery.DeleteLogClassificationLabel).
113 WithArgs(logClassificationLabelTestEdgeIDs[0], classificationEdgeIDs[0], labelTestEdgeIDs[0]).
114 WillReturnResult(sqlmock.NewResult(1, 1))
115
116 service := NewLogClassificationLabelService(db)
117
118 if deleted, err := service.DeleteLogClassificationLabel(context.Background(), logClassificationLabelTestEdgeIDs[0], classificationEdgeIDs[0], labelTestEdgeIDs[0]); !deleted && err != nil {
119 t.Errorf("error was not expected while deleting classification: %s", err)
120 }
121 }
122
123 func TestUpdateClassificationLabel(t *testing.T) {
124 db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
125 require.NoError(t, err)
126 defer db.Close()
127
128 mock.ExpectBegin()
129 mock.ExpectExec(sqlquery.UpdateLogClassificationLabel).
130 WithArgs(labelTestEdgeIDs[1], classificationTestEdgeIDs[1]).
131 WillReturnResult(sqlmock.NewResult(1, 1))
132
133 mock.ExpectCommit()
134
135 service := NewLogClassificationLabelService(db)
136
137 returnedString, err := service.UpdateLogClassificationLabel(context.Background(), classificationTestEdgeIDs[1], labelTestEdgeIDs[1])
138
139 assert.NoError(t, err)
140 assert.Contains(t, returnedString, "Updated Successfully.")
141 }
142
143 func TestGetClassificationLabel(t *testing.T) {
144 db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
145 require.NoError(t, err)
146 defer db.Close()
147
148 testCases := []struct {
149 name string
150 labelID string
151 classificationID string
152 lclID string
153 mocksQuery func() []*sqlmock.ExpectedQuery
154 expectedReturn *model.LogClassificationLabel
155 expectedError error
156 }{
157 {
158 name: "Test 1- Simple Get",
159 labelID: labelTestEdgeIDs[8],
160 classificationID: classificationTestEdgeIDs[8],
161 lclID: logClassificationLabelTestEdgeIDs[8],
162 mocksQuery: func() []*sqlmock.ExpectedQuery {
163 return []*sqlmock.ExpectedQuery{
164 mock.ExpectQuery(sqlquery.GetLogClassificationLabel).
165 WithArgs(labelTestEdgeIDs[8], classificationTestEdgeIDs[8], logClassificationLabelTestEdgeIDs[8]).
166 WillReturnRows(sqlmock.NewRows(logClassificationLabelRows).
167 AddRow(logClassificationLabelTestEdgeIDs[8], classificationTestEdgeIDs[8], testClassificationBannerEdgeID, labelTestEdgeIDs[8],
168 labelTestKeys[8], classificationDescriptions[8], classificationPods[8], classificationContainers[8], classificationTypes[8],
169 classificationClasses[1], classificationPatterns[8])),
170 }
171 },
172 expectedReturn: &model.LogClassificationLabel{
173 LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[8],
174 ClassificationEdgeID: classificationTestEdgeIDs[8],
175 BannerEdgeID: testClassificationBannerEdgeID,
176 LabelEdgeID: labelTestEdgeIDs[8],
177 LabelName: labelTestKeys[8],
178 Description: classificationDescriptions[8],
179 Pod: classificationPods[8],
180 Container: classificationContainers[8],
181 Type: classificationTypes[8],
182 Class: classificationClasses[1],
183 Pattern: classificationPatterns[8],
184 },
185 expectedError: nil,
186 },
187 }
188
189 for _, test := range testCases {
190 if test.expectedError == nil {
191 fmt.Printf("ExpectedReturn %+v", test.expectedReturn)
192 test.mocksQuery()
193
194 service := NewLogClassificationLabelService(db)
195 logClassificationLabel, err := service.GetLogClassificationLabel(context.Background(), test.lclID, test.classificationID, test.labelID)
196
197 assert.NoError(t, err)
198 assert.Equal(t, test.expectedReturn.LogClassificationLabelEdgeID, logClassificationLabel.LogClassificationLabelEdgeID)
199 assert.Equal(t, test.expectedReturn.ClassificationEdgeID, logClassificationLabel.ClassificationEdgeID)
200 assert.Equal(t, testClassificationBannerEdgeID, logClassificationLabel.BannerEdgeID)
201 assert.Equal(t, test.expectedReturn.LabelEdgeID, logClassificationLabel.LabelEdgeID)
202 assert.Equal(t, test.expectedReturn.LabelName, logClassificationLabel.LabelName)
203 assert.Equal(t, test.expectedReturn.Description, logClassificationLabel.Description)
204 assert.Equal(t, test.expectedReturn.Pod, logClassificationLabel.Pod)
205 assert.Equal(t, test.expectedReturn.Container, logClassificationLabel.Container)
206 assert.Equal(t, test.expectedReturn.Type, logClassificationLabel.Type)
207 assert.Equal(t, test.expectedReturn.Class, logClassificationLabel.Class)
208 assert.Equal(t, test.expectedReturn.Pattern, logClassificationLabel.Pattern)
209 }
210 }
211 }
212
213 func TestGetClassificationLabelsByLabel(t *testing.T) {
214 db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
215 require.NoError(t, err)
216 defer db.Close()
217 testCases := []struct {
218 name string
219 labelID string
220 mocks func() []*sqlmock.ExpectedQuery
221 expectedError error
222 expectedLabels []*model.LogClassificationLabel
223 }{
224 {
225 name: "Test 1 - Label Edge Id is blank",
226 labelID: "",
227 expectedError: errors.New("Error Getting Log Classification Labels by Label: Label Edge ID not a valid UUID format"),
228 },
229 {
230 name: "Test 2 - Label Edge Id is not a valid UID",
231 labelID: "12345",
232 expectedError: errors.New("Error Getting Log Classification Labels by Label: Label Edge ID not a valid UUID format"),
233 },
234 {
235 name: "Test 3 - Get by Label Edge IDs",
236 labelID: labelTestEdgeIDs[2],
237 mocks: func() []*sqlmock.ExpectedQuery {
238 return []*sqlmock.ExpectedQuery{
239 mock.ExpectQuery(sqlquery.GetLogClassificationLabelsByLabel).
240 WithArgs(labelTestEdgeIDs[2]).
241 WillReturnRows(sqlmock.NewRows(logClassificationLabelRows).
242 AddRow(logClassificationLabelTestEdgeIDs[2], classificationTestEdgeIDs[2], testClassificationBannerEdgeID, labelTestEdgeIDs[2], labelTestKeys[2], classificationDescriptions[2], classificationPods[2], classificationContainers[2], classificationTypes[2], classificationClasses[0], classificationPatterns[2]).
243 AddRow(logClassificationLabelTestEdgeIDs[4], classificationTestEdgeIDs[4], testClassificationBannerEdgeID, labelTestEdgeIDs[2], labelTestKeys[4], classificationDescriptions[4], classificationPods[4], classificationContainers[4], classificationTypes[4], classificationClasses[1], classificationPatterns[4])),
244 }
245 },
246 expectedError: nil,
247
248 expectedLabels: []*model.LogClassificationLabel{
249 {
250 LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[2],
251 ClassificationEdgeID: classificationTestEdgeIDs[2],
252 BannerEdgeID: testClassificationBannerEdgeID,
253 LabelEdgeID: labelTestEdgeIDs[2],
254 LabelName: labelTestKeys[2],
255 Description: classificationDescriptions[2],
256 Pod: classificationPods[2],
257 Container: classificationContainers[2],
258 Type: classificationTypes[2],
259 Class: classificationClasses[0],
260 Pattern: classificationPatterns[2],
261 },
262 {
263 LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[4],
264 ClassificationEdgeID: classificationTestEdgeIDs[4],
265 BannerEdgeID: testClassificationBannerEdgeID,
266 LabelEdgeID: labelTestEdgeIDs[2],
267 LabelName: labelTestKeys[4],
268 Description: classificationDescriptions[4],
269 Pod: classificationPods[4],
270 Container: classificationContainers[4],
271 Type: classificationTypes[4],
272 Class: classificationClasses[1],
273 Pattern: classificationPatterns[4],
274 },
275 },
276 },
277 }
278
279
280 for _, testCase := range testCases {
281 if testCase.expectedError != nil {
282 service := NewLogClassificationLabelService(db)
283
284 t.Run(testCase.name, func(t *testing.T) {
285 logClassificationLabels, err := service.GetLogClassificationLabelsByLabel(context.Background(), testCase.labelID)
286 assert.Nil(t, logClassificationLabels)
287 assert.ErrorContains(t, err, testCase.expectedError.Error())
288 })
289 } else {
290 service := NewLogClassificationLabelService(db)
291
292 testCase.mocks()
293 t.Run(testCase.name, func(t *testing.T) {
294 logClassificationLabels, err := service.GetLogClassificationLabelsByLabel(context.Background(), testCase.labelID)
295 assert.NoError(t, err)
296 logClassificationLabelHelper(t, testCase.expectedLabels, logClassificationLabels)
297 })
298 }
299 }
300 }
301
302 func TestGetClassificationLabelsByBanner(t *testing.T) {
303 db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
304 require.NoError(t, err)
305 defer db.Close()
306 testCases := []struct {
307 name string
308 bannerID string
309 mocks func() []*sqlmock.ExpectedQuery
310 expectedError error
311 expectedLabels []*model.LogClassificationLabel
312 }{
313 {
314 name: "Test 1 - Banner Edge Id is blank",
315 bannerID: "",
316 expectedError: errors.New("Error Getting Log Classification Labels by Banner: Banner Edge ID can not be blank"),
317 },
318 {
319 name: "Test 2 - Banner Edge Id is not a valid UID",
320 bannerID: "12345",
321 expectedError: errors.New("Error Getting Log Classification Labels by Banner: Banner Edge ID not a valid UUID format"),
322 },
323 {
324 name: "Test 3 - Get by Banner Edge IDs",
325
326 bannerID: testClassificationBannerEdgeID,
327 mocks: func() []*sqlmock.ExpectedQuery {
328 return []*sqlmock.ExpectedQuery{
329 mock.ExpectQuery(sqlquery.GetLogClassificationLabelsByBanner).
330 WithArgs(testClassificationBannerEdgeID).
331 WillReturnRows(sqlmock.NewRows(logClassificationLabelRows).
332 AddRow(logClassificationLabelTestEdgeIDs[5], classificationTestEdgeIDs[5], testClassificationBannerEdgeID, labelTestEdgeIDs[5], labelTestKeys[5], classificationDescriptions[5], classificationPods[5], classificationContainers[5], classificationTypes[5], classificationClasses[0], classificationPatterns[5]).
333 AddRow(logClassificationLabelTestEdgeIDs[6], classificationTestEdgeIDs[6], testClassificationBannerEdgeID, labelTestEdgeIDs[6], labelTestKeys[6], classificationDescriptions[6], classificationPods[6], classificationContainers[6], classificationTypes[6], classificationClasses[0], classificationPatterns[6]).
334 AddRow(logClassificationLabelTestEdgeIDs[7], classificationTestEdgeIDs[7], testClassificationBannerEdgeID, labelTestEdgeIDs[7], labelTestKeys[7], classificationDescriptions[7], classificationPods[7], classificationContainers[7], classificationTypes[7], classificationClasses[1], classificationPatterns[7])),
335 }
336 },
337 expectedError: nil,
338
339 expectedLabels: []*model.LogClassificationLabel{
340 {
341 LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[5],
342 ClassificationEdgeID: classificationTestEdgeIDs[5],
343 BannerEdgeID: testClassificationBannerEdgeID,
344 LabelEdgeID: labelTestEdgeIDs[5],
345 LabelName: labelTestKeys[5],
346 Description: classificationDescriptions[5],
347 Pod: classificationPods[5],
348 Container: classificationContainers[5],
349 Type: classificationTypes[5],
350 Class: classificationClasses[0],
351 Pattern: classificationPatterns[5],
352 },
353 {
354 LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[6],
355 ClassificationEdgeID: classificationTestEdgeIDs[6],
356 BannerEdgeID: testClassificationBannerEdgeID,
357 LabelEdgeID: labelTestEdgeIDs[6],
358 LabelName: labelTestKeys[6],
359 Description: classificationDescriptions[6],
360 Pod: classificationPods[6],
361 Container: classificationContainers[6],
362 Type: classificationTypes[6],
363 Class: classificationClasses[0],
364 Pattern: classificationPatterns[6],
365 },
366 {
367 LogClassificationLabelEdgeID: logClassificationLabelTestEdgeIDs[7],
368 ClassificationEdgeID: classificationTestEdgeIDs[7],
369 BannerEdgeID: testClassificationBannerEdgeID,
370 LabelEdgeID: labelTestEdgeIDs[7],
371 LabelName: labelTestKeys[7],
372 Description: classificationDescriptions[7],
373 Pod: classificationPods[7],
374 Container: classificationContainers[7],
375 Type: classificationTypes[7],
376 Class: classificationClasses[1],
377 Pattern: classificationPatterns[7],
378 },
379 },
380 },
381 }
382
383
384 for _, testCase := range testCases {
385 if testCase.expectedError != nil {
386 service := NewLogClassificationLabelService(db)
387
388 t.Run(testCase.name, func(t *testing.T) {
389 logClassificationLabels, err := service.GetLogClassificationLabelsByBanner(context.Background(), testCase.bannerID)
390 assert.Nil(t, logClassificationLabels)
391 assert.ErrorContains(t, err, testCase.expectedError.Error())
392 })
393 } else {
394 service := NewLogClassificationLabelService(db)
395 testCase.mocks()
396
397 t.Run(testCase.name, func(t *testing.T) {
398 logClassificationLabels, err := service.GetLogClassificationLabelsByBanner(context.Background(), testCase.bannerID)
399 assert.NoError(t, err)
400 logClassificationLabelHelper(t, testCase.expectedLabels, logClassificationLabels)
401 })
402 }
403 }
404 }
405
406 func logClassificationLabelHelper(t *testing.T, expectedLabels []*model.LogClassificationLabel, logClassificationLabels []*model.LogClassificationLabel) {
407 for index, logClassificationLabel := range logClassificationLabels {
408 assert.Equal(t, expectedLabels[index].LogClassificationLabelEdgeID, logClassificationLabel.LogClassificationLabelEdgeID)
409 assert.Equal(t, expectedLabels[index].ClassificationEdgeID, logClassificationLabel.ClassificationEdgeID)
410 assert.Equal(t, testClassificationBannerEdgeID, logClassificationLabel.BannerEdgeID)
411 assert.Equal(t, expectedLabels[index].LabelEdgeID, logClassificationLabel.LabelEdgeID)
412 assert.Equal(t, expectedLabels[index].LabelName, logClassificationLabel.LabelName)
413 assert.Equal(t, expectedLabels[index].Description, logClassificationLabel.Description)
414 assert.Equal(t, expectedLabels[index].Pod, logClassificationLabel.Pod)
415 assert.Equal(t, expectedLabels[index].Container, logClassificationLabel.Container)
416 assert.Equal(t, expectedLabels[index].Type, logClassificationLabel.Type)
417 assert.Equal(t, expectedLabels[index].Class, logClassificationLabel.Class)
418 assert.Equal(t, expectedLabels[index].Pattern, logClassificationLabel.Pattern)
419 }
420 }
421
View as plain text