Skip to content

Commit d13a27c

Browse files
authored
Add unit test for stderr/stdout error log (#1054)
* add unit test to check expected error log output for stderr/stdout * Update order of tests and labels
1 parent 015eb68 commit d13a27c

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

internal/watcher/instance/nginx_config_parser_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
293293
name string
294294
content string
295295
expectedConfigContext *model.NginxConfigContext
296+
expectedLog string
296297
allowedDirectories []string
297298
}{
298299
{
@@ -312,6 +313,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
312313
protos.GetNginxOssInstance([]string{}).GetInstanceMeta().GetInstanceId(),
313314
[]string{"127.0.0.1:1515"},
314315
),
316+
expectedLog: "",
315317
allowedDirectories: []string{dir},
316318
},
317319
{
@@ -331,6 +333,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
331333
protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
332334
[]string{"127.0.0.1:1515"},
333335
),
336+
expectedLog: "",
334337
allowedDirectories: []string{dir},
335338
},
336339
{
@@ -366,6 +369,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
366369
},
367370
NAPSysLogServers: nil,
368371
},
372+
expectedLog: "",
369373
allowedDirectories: []string{dir},
370374
},
371375
{
@@ -383,6 +387,46 @@ func TestNginxConfigParser_Parse(t *testing.T) {
383387
},
384388
allowedDirectories: []string{dir},
385389
},
390+
{
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+
},
386430
}
387431

388432
for _, test := range tests {
@@ -403,9 +447,17 @@ func TestNginxConfigParser_Parse(t *testing.T) {
403447
agentConfig.AllowedDirectories = test.allowedDirectories
404448

405449
nginxConfig := NewNginxConfigParser(agentConfig)
450+
451+
logBuf := &bytes.Buffer{}
452+
stub.StubLoggerWith(logBuf)
453+
406454
result, parseError := nginxConfig.Parse(ctx, test.instance)
407455
require.NoError(t, parseError)
408456

457+
helpers.ValidateLog(t, test.expectedLog, logBuf)
458+
459+
logBuf.Reset()
460+
409461
assert.ElementsMatch(t, test.expectedConfigContext.Files, result.Files)
410462
assert.Equal(t, test.expectedConfigContext.NAPSysLogServers, result.NAPSysLogServers)
411463
assert.Equal(t, test.expectedConfigContext.PlusAPI, result.PlusAPI)

test/model/config.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,47 @@ func GetConfigContextWithNames(
7272
NAPSysLogServers: syslogServers,
7373
}
7474
}
75+
76+
func GetConfigContextWithoutErrorLog(
77+
accessLogName,
78+
combinedAccessLogName,
79+
ltsvAccessLogName,
80+
instanceID string,
81+
syslogServers []string,
82+
) *model.NginxConfigContext {
83+
return &model.NginxConfigContext{
84+
StubStatus: &model.APIDetails{
85+
URL: "",
86+
Listen: "",
87+
Location: "",
88+
},
89+
PlusAPI: &model.APIDetails{
90+
URL: "",
91+
Listen: "",
92+
Location: "",
93+
},
94+
AccessLogs: []*model.AccessLog{
95+
{
96+
Name: accessLogName,
97+
Format: "$remote_addr - $remote_user [$time_local]",
98+
Readable: true,
99+
Permissions: "0600",
100+
},
101+
{
102+
Name: combinedAccessLogName,
103+
Format: "$remote_addr - $remote_user [$time_local] " +
104+
"\"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\"",
105+
Readable: true,
106+
Permissions: "0600",
107+
},
108+
{
109+
Name: ltsvAccessLogName,
110+
Format: "ltsv",
111+
Readable: true,
112+
Permissions: "0600",
113+
},
114+
},
115+
InstanceID: instanceID,
116+
NAPSysLogServers: syslogServers,
117+
}
118+
}

0 commit comments

Comments
 (0)