@@ -12,8 +12,11 @@ import (
12
12
"net/http"
13
13
"net/http/httptest"
14
14
"os"
15
+ "sort"
15
16
"testing"
16
17
18
+ "google.golang.org/protobuf/proto"
19
+
17
20
"google.golang.org/protobuf/types/known/timestamppb"
18
21
19
22
"github.com/nginx/agent/v3/internal/model"
@@ -260,6 +263,7 @@ var (
260
263
`
261
264
)
262
265
266
+ // nolint: maintidx
263
267
func TestNginxConfigParser_Parse (t * testing.T ) {
264
268
ctx := context .Background ()
265
269
dir := t .TempDir ()
@@ -287,6 +291,23 @@ func TestNginxConfigParser_Parse(t *testing.T) {
287
291
defer helpers .RemoveFileWithErrorCheck (t , allowedFile .Name ())
288
292
fileMetaAllowedFiles , err := files .FileMeta (allowedFile .Name ())
289
293
require .NoError (t , err )
294
+ allowedFileWithMetas := mpi.File {FileMeta : fileMetaAllowedFiles }
295
+
296
+ _ , cert := helpers .GenerateSelfSignedCert (t )
297
+ certContents := helpers.Cert {Name : "nginx.cert" , Type : "CERTIFICATE" , Contents : cert }
298
+ certFile := helpers .WriteCertFiles (t , dir , certContents )
299
+ require .NotNil (t , certFile )
300
+ fileMetaCertFiles , err := files .FileMetaWithCertificate (certFile )
301
+ require .NoError (t , err )
302
+ certFileWithMetas := mpi.File {FileMeta : fileMetaCertFiles }
303
+
304
+ _ , diffCert := helpers .GenerateSelfSignedCert (t )
305
+ diffCertContents := helpers.Cert {Name : "nginx1.cert" , Type : "CERTIFICATE" , Contents : diffCert }
306
+ diffCertFile := helpers .WriteCertFiles (t , dir , diffCertContents )
307
+ require .NotNil (t , diffCertFile )
308
+ diffFileMetaCertFiles , err := files .FileMetaWithCertificate (diffCertFile )
309
+ require .NoError (t , err )
310
+ diffCertFileWithMetas := mpi.File {FileMeta : diffFileMetaCertFiles }
290
311
291
312
tests := []struct {
292
313
instance * mpi.Instance
@@ -341,34 +362,13 @@ func TestNginxConfigParser_Parse(t *testing.T) {
341
362
instance : protos .GetNginxPlusInstance ([]string {}),
342
363
content : testconfig .GetNginxConfigWithNotAllowedDir (errorLog .Name (), allowedFile .Name (),
343
364
notAllowedFile .Name (), accessLog .Name ()),
344
- expectedConfigContext : & model.NginxConfigContext {
345
- StubStatus : & model.APIDetails {},
346
- PlusAPI : & model.APIDetails {},
347
- InstanceID : protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
348
- Files : []* mpi.File {
349
- {
350
- FileMeta : fileMetaAllowedFiles ,
351
- },
352
- },
353
- AccessLogs : []* model.AccessLog {
354
- {
355
- Name : accessLog .Name (),
356
- Format : "$remote_addr - $remote_user [$time_local] \" $request\" $status $body_bytes_sent " +
357
- "\" $http_referer\" \" $http_user_agent\" \" $http_x_forwarded_for\" \" $bytes_sent\" " +
358
- "\" $request_length\" \" $request_time\" \" $gzip_ratio\" $server_protocol " ,
359
- Permissions : "0600" ,
360
- Readable : true ,
361
- },
362
- },
363
- ErrorLogs : []* model.ErrorLog {
364
- {
365
- Name : errorLog .Name (),
366
- Permissions : "0600" ,
367
- Readable : true ,
368
- },
369
- },
370
- NAPSysLogServers : nil ,
371
- },
365
+ expectedConfigContext : modelHelpers .GetConfigContextWithFiles (
366
+ accessLog .Name (),
367
+ errorLog .Name (),
368
+ []* mpi.File {& allowedFileWithMetas },
369
+ protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
370
+ nil ,
371
+ ),
372
372
expectedLog : "" ,
373
373
allowedDirectories : []string {dir },
374
374
},
@@ -427,6 +427,59 @@ func TestNginxConfigParser_Parse(t *testing.T) {
427
427
"config; log errors to file to enable error monitoring" ,
428
428
allowedDirectories : []string {dir },
429
429
},
430
+ {
431
+ name : "Test 7: Check Parser for SSL Certs" ,
432
+ instance : protos .GetNginxPlusInstance ([]string {}),
433
+ content : testconfig .GetNginxConfigWithSSLCerts (
434
+ errorLog .Name (),
435
+ accessLog .Name (),
436
+ certFile ,
437
+ ),
438
+ expectedConfigContext : modelHelpers .GetConfigContextWithFiles (
439
+ accessLog .Name (),
440
+ errorLog .Name (),
441
+ []* mpi.File {& certFileWithMetas },
442
+ protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
443
+ nil ,
444
+ ),
445
+ allowedDirectories : []string {dir },
446
+ },
447
+ {
448
+ name : "Test 8: Check for multiple different SSL Certs" ,
449
+ instance : protos .GetNginxPlusInstance ([]string {}),
450
+ content : testconfig .GetNginxConfigWithMultipleSSLCerts (
451
+ errorLog .Name (),
452
+ accessLog .Name (),
453
+ certFile ,
454
+ diffCertFile ,
455
+ ),
456
+ expectedConfigContext : modelHelpers .GetConfigContextWithFiles (
457
+ accessLog .Name (),
458
+ errorLog .Name (),
459
+ []* mpi.File {& diffCertFileWithMetas , & certFileWithMetas },
460
+ protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
461
+ nil ,
462
+ ),
463
+ allowedDirectories : []string {dir },
464
+ },
465
+ {
466
+ name : "Test 9: Check for multiple same SSL Certs" ,
467
+ instance : protos .GetNginxPlusInstance ([]string {}),
468
+ content : testconfig .GetNginxConfigWithMultipleSSLCerts (
469
+ errorLog .Name (),
470
+ accessLog .Name (),
471
+ certFile ,
472
+ certFile ,
473
+ ),
474
+ expectedConfigContext : modelHelpers .GetConfigContextWithFiles (
475
+ accessLog .Name (),
476
+ errorLog .Name (),
477
+ []* mpi.File {& certFileWithMetas },
478
+ protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
479
+ nil ,
480
+ ),
481
+ allowedDirectories : []string {dir },
482
+ },
430
483
}
431
484
432
485
for _ , test := range tests {
@@ -455,16 +508,28 @@ func TestNginxConfigParser_Parse(t *testing.T) {
455
508
require .NoError (t , parseError )
456
509
457
510
helpers .ValidateLog (t , test .expectedLog , logBuf )
458
-
459
511
logBuf .Reset ()
460
512
461
- assert .ElementsMatch (t , test .expectedConfigContext .Files , result .Files )
513
+ sort .Slice (test .expectedConfigContext .Files , func (i , j int ) bool {
514
+ return test .expectedConfigContext .Files [i ].GetFileMeta ().GetName () >
515
+ test .expectedConfigContext .Files [j ].GetFileMeta ().GetName ()
516
+ })
517
+
518
+ sort .Slice (result .Files , func (i , j int ) bool {
519
+ return result .Files [i ].GetFileMeta ().GetName () >
520
+ result .Files [j ].GetFileMeta ().GetName ()
521
+ })
522
+
523
+ assert .Truef (t ,
524
+ protoListEqual (test .expectedConfigContext .Files , result .Files ),
525
+ "Expect %s Got %s" , test .expectedConfigContext .Files , result .Files )
462
526
assert .Equal (t , test .expectedConfigContext .NAPSysLogServers , result .NAPSysLogServers )
463
527
assert .Equal (t , test .expectedConfigContext .PlusAPI , result .PlusAPI )
464
528
assert .ElementsMatch (t , test .expectedConfigContext .AccessLogs , result .AccessLogs )
465
529
assert .ElementsMatch (t , test .expectedConfigContext .ErrorLogs , result .ErrorLogs )
466
530
assert .Equal (t , test .expectedConfigContext .StubStatus , result .StubStatus )
467
531
assert .Equal (t , test .expectedConfigContext .InstanceID , result .InstanceID )
532
+ assert .Equal (t , len (test .expectedConfigContext .Files ), len (result .Files ))
468
533
})
469
534
}
470
535
}
@@ -1160,3 +1225,14 @@ func TestNginxConfigParser_checkDuplicate(t *testing.T) {
1160
1225
})
1161
1226
}
1162
1227
}
1228
+
1229
+ func protoListEqual (protoListA , protoListB []* mpi.File ) bool {
1230
+ for i := 0 ; i < len (protoListA ); i ++ {
1231
+ res := proto .Equal (protoListA [i ], protoListB [i ])
1232
+ if ! res {
1233
+ return false
1234
+ }
1235
+ }
1236
+
1237
+ return true
1238
+ }
0 commit comments