|
| 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