Skip to content

Commit 92586df

Browse files
committed
resolve merge conflicts
2 parents eec0457 + d13a27c commit 92586df

File tree

2 files changed

+120
-5
lines changed

2 files changed

+120
-5
lines changed

internal/watcher/instance/nginx_config_parser_test.go

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"sort"
1616
"testing"
1717

18+
"google.golang.org/protobuf/proto"
19+
1820
"google.golang.org/protobuf/types/known/timestamppb"
1921

2022
"github.com/nginx/agent/v3/internal/model"
@@ -312,6 +314,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
312314
name string
313315
content string
314316
expectedConfigContext *model.NginxConfigContext
317+
expectedLog string
315318
allowedDirectories []string
316319
}{
317320
{
@@ -331,6 +334,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
331334
protos.GetNginxOssInstance([]string{}).GetInstanceMeta().GetInstanceId(),
332335
[]string{"127.0.0.1:1515"},
333336
),
337+
expectedLog: "",
334338
allowedDirectories: []string{dir},
335339
},
336340
{
@@ -350,6 +354,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
350354
protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
351355
[]string{"127.0.0.1:1515"},
352356
),
357+
expectedLog: "",
353358
allowedDirectories: []string{dir},
354359
},
355360
{
@@ -364,6 +369,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
364369
protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
365370
nil,
366371
),
372+
expectedLog: "",
367373
allowedDirectories: []string{dir},
368374
},
369375
{
@@ -382,7 +388,47 @@ func TestNginxConfigParser_Parse(t *testing.T) {
382388
allowedDirectories: []string{dir},
383389
},
384390
{
385-
name: "Test 5: Check Parser for SSL Certs",
391+
name: "Test 5: Error Log outputting to stderr",
392+
instance: protos.GetNginxPlusInstance([]string{}),
393+
content: testconfig.GetNginxConfigWithMultipleAccessLogs(
394+
"stderr",
395+
accessLog.Name(),
396+
combinedAccessLog.Name(),
397+
ltsvAccessLog.Name(),
398+
),
399+
expectedConfigContext: modelHelpers.GetConfigContextWithoutErrorLog(
400+
accessLog.Name(),
401+
combinedAccessLog.Name(),
402+
ltsvAccessLog.Name(),
403+
protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
404+
[]string{"127.0.0.1:1515"},
405+
),
406+
expectedLog: "Currently error log outputs to stderr. Log monitoring is disabled while applying a " +
407+
"config; log errors to file to enable error monitoring",
408+
allowedDirectories: []string{dir},
409+
},
410+
{
411+
name: "Test 6: Error Log outputting to stdout",
412+
instance: protos.GetNginxPlusInstance([]string{}),
413+
content: testconfig.GetNginxConfigWithMultipleAccessLogs(
414+
"stdout",
415+
accessLog.Name(),
416+
combinedAccessLog.Name(),
417+
ltsvAccessLog.Name(),
418+
),
419+
expectedConfigContext: modelHelpers.GetConfigContextWithoutErrorLog(
420+
accessLog.Name(),
421+
combinedAccessLog.Name(),
422+
ltsvAccessLog.Name(),
423+
protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
424+
[]string{"127.0.0.1:1515"},
425+
),
426+
expectedLog: "Currently error log outputs to stdout. Log monitoring is disabled while applying a " +
427+
"config; log errors to file to enable error monitoring",
428+
allowedDirectories: []string{dir},
429+
},
430+
{
431+
name: "Test 7: Check Parser for SSL Certs",
386432
instance: protos.GetNginxPlusInstance([]string{}),
387433
content: testconfig.GetNginxConfigWithSSLCerts(
388434
errorLog.Name(),
@@ -399,7 +445,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
399445
allowedDirectories: []string{dir},
400446
},
401447
{
402-
name: "Test 6: Check for multiple different SSL Certs",
448+
name: "Test 8: Check for multiple different SSL Certs",
403449
instance: protos.GetNginxPlusInstance([]string{}),
404450
content: testconfig.GetNginxConfigWithMultipleSSLCerts(
405451
errorLog.Name(),
@@ -410,14 +456,14 @@ func TestNginxConfigParser_Parse(t *testing.T) {
410456
expectedConfigContext: modelHelpers.GetConfigContextWithFiles(
411457
accessLog.Name(),
412458
errorLog.Name(),
413-
[]*mpi.File{&certFileWithMetas, &diffCertFileWithMetas},
459+
[]*mpi.File{&diffCertFileWithMetas, &certFileWithMetas},
414460
protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
415461
nil,
416462
),
417463
allowedDirectories: []string{dir},
418464
},
419465
{
420-
name: "Test 7: Check for multiple same SSL Certs",
466+
name: "Test 9: Check for multiple same SSL Certs",
421467
instance: protos.GetNginxPlusInstance([]string{}),
422468
content: testconfig.GetNginxConfigWithMultipleSSLCerts(
423469
errorLog.Name(),
@@ -454,15 +500,29 @@ func TestNginxConfigParser_Parse(t *testing.T) {
454500
agentConfig.AllowedDirectories = test.allowedDirectories
455501

456502
nginxConfig := NewNginxConfigParser(agentConfig)
503+
504+
logBuf := &bytes.Buffer{}
505+
stub.StubLoggerWith(logBuf)
506+
457507
result, parseError := nginxConfig.Parse(ctx, test.instance)
458508
require.NoError(t, parseError)
459509

510+
helpers.ValidateLog(t, test.expectedLog, logBuf)
511+
logBuf.Reset()
512+
460513
sort.Slice(test.expectedConfigContext.Files, func(i, j int) bool {
461514
return test.expectedConfigContext.Files[i].GetFileMeta().GetName() >
462515
test.expectedConfigContext.Files[j].GetFileMeta().GetName()
463516
})
464517

465-
assert.ElementsMatch(t, test.expectedConfigContext.Files, result.Files)
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)
466526
assert.Equal(t, test.expectedConfigContext.NAPSysLogServers, result.NAPSysLogServers)
467527
assert.Equal(t, test.expectedConfigContext.PlusAPI, result.PlusAPI)
468528
assert.ElementsMatch(t, test.expectedConfigContext.AccessLogs, result.AccessLogs)
@@ -1165,3 +1225,14 @@ func TestNginxConfigParser_checkDuplicate(t *testing.T) {
11651225
})
11661226
}
11671227
}
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+
}

test/model/config.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,50 @@ func GetConfigContextWithNames(
7676
}
7777
}
7878

79+
func GetConfigContextWithoutErrorLog(
80+
accessLogName,
81+
combinedAccessLogName,
82+
ltsvAccessLogName,
83+
instanceID string,
84+
syslogServers []string,
85+
) *model.NginxConfigContext {
86+
return &model.NginxConfigContext{
87+
StubStatus: &model.APIDetails{
88+
URL: "",
89+
Listen: "",
90+
Location: "",
91+
},
92+
PlusAPI: &model.APIDetails{
93+
URL: "",
94+
Listen: "",
95+
Location: "",
96+
},
97+
AccessLogs: []*model.AccessLog{
98+
{
99+
Name: accessLogName,
100+
Format: "$remote_addr - $remote_user [$time_local]",
101+
Readable: true,
102+
Permissions: "0600",
103+
},
104+
{
105+
Name: combinedAccessLogName,
106+
Format: "$remote_addr - $remote_user [$time_local] " +
107+
"\"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\"",
108+
Readable: true,
109+
Permissions: "0600",
110+
},
111+
{
112+
Name: ltsvAccessLogName,
113+
Format: "ltsv",
114+
Readable: true,
115+
Permissions: "0600",
116+
},
117+
},
118+
InstanceID: instanceID,
119+
NAPSysLogServers: syslogServers,
120+
}
121+
}
122+
79123
func GetConfigContextWithFiles(
80124
accessLogName,
81125
errorLogName string,

0 commit comments

Comments
 (0)