Skip to content

Commit df02594

Browse files
committed
test coverage
1 parent fee476c commit df02594

File tree

3 files changed

+189
-71
lines changed

3 files changed

+189
-71
lines changed

internal/resource/nginx_instance_operator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type NginxInstanceOperator struct {
2525
agentConfig *config.Config
2626
executer exec.ExecInterface
2727
logTailer logTailerOperator
28-
nginxProccessOperator processOperator
28+
nginxProcessOperator processOperator
2929
treatWarningsAsErrors bool
3030
}
3131

@@ -35,7 +35,7 @@ func NewInstanceOperator(agentConfig *config.Config) *NginxInstanceOperator {
3535
return &NginxInstanceOperator{
3636
executer: &exec.Exec{},
3737
logTailer: NewLogTailerOperator(agentConfig),
38-
nginxProccessOperator: NewNginxInstanceProcessOperator(),
38+
nginxProcessOperator: NewNginxInstanceProcessOperator(),
3939
treatWarningsAsErrors: agentConfig.DataPlaneConfig.Nginx.TreatWarningsAsErrors,
4040
agentConfig: agentConfig,
4141
}
@@ -67,7 +67,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
6767
instance.GetInstanceRuntime().GetProcessId())
6868

6969
pid := instance.GetInstanceRuntime().GetProcessId()
70-
workers := i.nginxProccessOperator.NginxWorkerProcesses(ctx, pid)
70+
workers := i.nginxProcessOperator.NginxWorkerProcesses(ctx, pid)
7171

7272
if len(workers) > 0 {
7373
reloadTime = workers[0].Created
@@ -85,7 +85,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
8585
return err
8686
}
8787

88-
processes, procErr := i.nginxProccessOperator.FindNginxProcesses(ctx)
88+
processes, procErr := i.nginxProcessOperator.FindNginxProcesses(ctx)
8989
if procErr != nil {
9090
slog.WarnContext(ctx, "Error finding parent process ID, unable to check if NGINX worker "+
9191
"processes have reloaded", "error", procErr)
@@ -128,7 +128,7 @@ func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID str
128128

129129
slog.DebugContext(ctx, "Waiting for NGINX to finish reloading")
130130

131-
newPid, findErr := i.nginxProccessOperator.FindParentProcessID(ctx, instanceID, processes, i.executer)
131+
newPid, findErr := i.nginxProcessOperator.FindParentProcessID(ctx, instanceID, processes, i.executer)
132132
if findErr != nil {
133133
slog.WarnContext(ctx, "Error finding parent process ID, unable to check if NGINX worker "+
134134
"processes have reloaded", "error", findErr)
@@ -139,7 +139,7 @@ func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID str
139139
slog.DebugContext(ctx, "Found parent process ID", "process_id", newPid)
140140

141141
err := backoff.WaitUntil(ctx, backoffSettings, func() error {
142-
currentWorkers := i.nginxProccessOperator.NginxWorkerProcesses(ctx, newPid)
142+
currentWorkers := i.nginxProcessOperator.NginxWorkerProcesses(ctx, newPid)
143143
if len(currentWorkers) == 0 {
144144
return errors.New("waiting for NGINX worker processes")
145145
}

internal/resource/nginx_instance_operator_test.go

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -311,65 +311,7 @@ func TestInstanceOperator_checkWorkers(t *testing.T) {
311311
},
312312
},
313313
{
314-
name: "Test 2: Successful reload with 2 NGINX processes",
315-
expectedLog: "Found parent process ID\" process_id=1234",
316-
reloadTime: time.Date(2025, 8, 13, 8, 0, 0, 0, time.Local),
317-
instanceID: "e1374cb1-462d-3b6c-9f3b-f28332b5f10c",
318-
masterProcess: []*nginxprocess.Process{
319-
{
320-
PID: 1234,
321-
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
322-
PPID: 1,
323-
Name: "nginx",
324-
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
325-
Exe: exePath,
326-
},
327-
{
328-
PID: 5678,
329-
Created: time.Date(2025, 8, 13, 5, 1, 0, 0, time.Local),
330-
PPID: 1,
331-
Name: "nginx",
332-
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
333-
Exe: exePath2,
334-
},
335-
},
336-
workers: []*nginxprocess.Process{
337-
{
338-
PID: 567,
339-
Created: time.Date(2025, 8, 13, 5, 1, 0, 0, time.Local),
340-
PPID: 5678,
341-
Name: "nginx",
342-
Cmd: "nginx: worker process",
343-
Exe: exePath2,
344-
},
345-
{
346-
PID: 789,
347-
PPID: 5678,
348-
Created: time.Date(2025, 8, 13, 5, 1, 0, 0, time.Local),
349-
Name: "nginx",
350-
Cmd: "nginx: worker process",
351-
Exe: exePath2,
352-
},
353-
{
354-
PID: 567,
355-
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
356-
PPID: 1234,
357-
Name: "nginx",
358-
Cmd: "nginx: worker process",
359-
Exe: exePath,
360-
},
361-
{
362-
PID: 789,
363-
PPID: 1234,
364-
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
365-
Name: "nginx",
366-
Cmd: "nginx: worker process",
367-
Exe: exePath,
368-
},
369-
},
370-
},
371-
{
372-
name: "Test 3: Successful reload",
314+
name: "Test 2: Unsuccessful reload",
373315
expectedLog: "\"Failed to check if NGINX worker processes have successfully reloaded, timed out " +
374316
"waiting\" error=\"waiting for NGINX worker processes\"",
375317
reloadTime: time.Date(2025, 8, 13, 8, 0, 0, 0, time.Local),
@@ -416,12 +358,8 @@ func TestInstanceOperator_checkWorkers(t *testing.T) {
416358
mockProcessOp := &resourcefakes.FakeProcessOperator{}
417359
allProcesses := slices.Concat(test.workers, test.masterProcess)
418360
mockProcessOp.FindNginxProcessesReturnsOnCall(0, allProcesses, nil)
419-
mockProcessOp.NginxWorkerProcessesReturnsOnCall(0, test.workers[0:2])
361+
mockProcessOp.NginxWorkerProcessesReturnsOnCall(0, test.workers)
420362
mockProcessOp.FindParentProcessIDReturnsOnCall(0, test.masterProcess[0].PID, nil)
421-
if len(test.masterProcess) > 1 {
422-
t.Logf("")
423-
mockProcessOp.FindParentProcessIDReturnsOnCall(1, test.masterProcess[1].PID, nil)
424-
}
425363

426364
logBuf := &bytes.Buffer{}
427365
stub.StubLoggerWith(logBuf)
@@ -437,7 +375,7 @@ func TestInstanceOperator_checkWorkers(t *testing.T) {
437375
}
438376
operator := NewInstanceOperator(agentConfig)
439377
operator.executer = mockExec
440-
operator.nginxProccessOperator = mockProcessOp
378+
operator.nginxProcessOperator = mockProcessOp
441379

442380
operator.checkWorkers(ctx, test.instanceID, test.reloadTime, allProcesses)
443381

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Copyright (c) F5, Inc.
2+
//
3+
// This source code is licensed under the Apache License, Version 2.0 license found in the
4+
// LICENSE file in the root directory of this source tree.
5+
6+
package resource
7+
8+
import (
9+
"bytes"
10+
"context"
11+
"errors"
12+
"fmt"
13+
"github.com/nginx/agent/v3/internal/datasource/host/exec/execfakes"
14+
"github.com/nginx/agent/v3/pkg/nginxprocess"
15+
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
17+
"testing"
18+
"time"
19+
)
20+
21+
func TestProcessOperator_FindParentProcessID(t *testing.T) {
22+
ctx := context.Background()
23+
24+
modulePath := t.TempDir() + "/usr/lib/nginx/modules"
25+
26+
configArgs := fmt.Sprintf(ossConfigArgs, modulePath)
27+
nginxVersionCommandOutput := `nginx version: nginx/1.25.3
28+
built by clang 14.0.0 (clang-1400.0.29.202)
29+
built with OpenSSL 1.1.1s 1 Nov 2022 (running with OpenSSL 1.1.1t 7 Feb 2023)
30+
TLS SNI support enabled
31+
configure arguments: ` + configArgs
32+
33+
tests := []struct {
34+
expectedLog string
35+
name string
36+
instanceID string
37+
expectErr error
38+
expectedPPID int32
39+
nginxProcesses []*nginxprocess.Process
40+
}{
41+
{
42+
name: "Test 1: Found parent process",
43+
instanceID: "e1374cb1-462d-3b6c-9f3b-f28332b5f10c",
44+
expectErr: nil,
45+
expectedPPID: 1234,
46+
nginxProcesses: []*nginxprocess.Process{
47+
{
48+
PID: 567,
49+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
50+
PPID: 1234,
51+
Name: "nginx",
52+
Cmd: "nginx: worker process",
53+
Exe: exePath,
54+
},
55+
{
56+
PID: 789,
57+
PPID: 1234,
58+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
59+
Name: "nginx",
60+
Cmd: "nginx: worker process",
61+
Exe: exePath,
62+
},
63+
{
64+
PID: 1234,
65+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
66+
PPID: 1,
67+
Name: "nginx",
68+
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
69+
Exe: exePath,
70+
},
71+
},
72+
},
73+
{
74+
name: "Test 2: unable to find parent process",
75+
instanceID: "e1374cb1-462d-3b6c-9f3b-f28332b5f10c",
76+
expectErr: errors.New("unable to find parent process"),
77+
expectedPPID: 0,
78+
nginxProcesses: []*nginxprocess.Process{
79+
{
80+
PID: 567,
81+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
82+
PPID: 1234,
83+
Name: "nginx",
84+
Cmd: "nginx: worker process",
85+
Exe: exePath,
86+
},
87+
{
88+
PID: 789,
89+
PPID: 1234,
90+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
91+
Name: "nginx",
92+
Cmd: "nginx: worker process",
93+
Exe: exePath,
94+
},
95+
{
96+
PID: 4567,
97+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
98+
PPID: 1,
99+
Name: "nginx",
100+
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
101+
Exe: exePath2,
102+
},
103+
},
104+
},
105+
{
106+
name: "Test 3: Found parent process, multiple NGINX processes",
107+
instanceID: "e1374cb1-462d-3b6c-9f3b-f28332b5f10c",
108+
expectErr: nil,
109+
expectedPPID: 1234,
110+
nginxProcesses: []*nginxprocess.Process{
111+
{
112+
PID: 567,
113+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
114+
PPID: 1234,
115+
Name: "nginx",
116+
Cmd: "nginx: worker process",
117+
Exe: exePath,
118+
},
119+
{
120+
PID: 789,
121+
PPID: 1234,
122+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
123+
Name: "nginx",
124+
Cmd: "nginx: worker process",
125+
Exe: exePath,
126+
},
127+
{
128+
PID: 1234,
129+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
130+
PPID: 1,
131+
Name: "nginx",
132+
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
133+
Exe: exePath,
134+
},
135+
{
136+
PID: 5678,
137+
Created: time.Date(2025, 8, 13, 5, 1, 0, 0, time.Local),
138+
PPID: 1,
139+
Name: "nginx",
140+
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
141+
Exe: exePath2,
142+
},
143+
{
144+
PID: 567,
145+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
146+
PPID: 1234,
147+
Name: "nginx",
148+
Cmd: "nginx: worker process",
149+
Exe: exePath,
150+
},
151+
{
152+
PID: 789,
153+
PPID: 1234,
154+
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
155+
Name: "nginx",
156+
Cmd: "nginx: worker process",
157+
Exe: exePath,
158+
},
159+
},
160+
},
161+
}
162+
163+
for _, test := range tests {
164+
t.Run(test.name, func(tt *testing.T) {
165+
processOperator := NewNginxInstanceProcessOperator()
166+
mockExec := &execfakes.FakeExecInterface{}
167+
mockExec.RunCmdReturnsOnCall(0, bytes.NewBufferString(nginxVersionCommandOutput), nil)
168+
ppid, err := processOperator.FindParentProcessID(ctx, test.instanceID, test.nginxProcesses, mockExec)
169+
170+
if test.expectErr != nil {
171+
assert.Error(tt, err)
172+
} else {
173+
require.NoError(tt, err)
174+
}
175+
176+
assert.Equal(tt, test.expectedPPID, ppid)
177+
178+
})
179+
}
180+
}

0 commit comments

Comments
 (0)