Skip to content

Commit 547e903

Browse files
committed
adding small fix update
1 parent 6963fd4 commit 547e903

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

internal/mode/static/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
78
"time"
89

910
"github.com/go-logr/logr"
@@ -144,7 +145,7 @@ func StartManager(cfg config.Config) error {
144145
return fmt.Errorf("cannot clear NGINX configuration folders: %w", err)
145146
}
146147

147-
processHandler := &ngxruntime.NewProcessHandlerImpl{}
148+
processHandler := ngxruntime.NewProcessHandlerImpl(os.ReadFile, os.Stat)
148149

149150
// Ensure NGINX is running before registering metrics & starting the manager.
150151
if _, err := processHandler.FindMainProcess(ctx, ngxruntime.PidFileTimeout); err != nil {

internal/mode/static/nginx/runtime/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
const (
2222
// PidFile specifies the location of the PID file for the Nginx process
2323
PidFile = "/var/run/nginx/nginx.pid"
24-
// pidFileTimeout defines the timeout duration for accessing the PID file
24+
// PidFileTimeout defines the timeout duration for accessing the PID file
2525
PidFileTimeout = 10000 * time.Millisecond
26-
/// NginxReloadTimeout sets the timeout duration for reloading the Nginx configuration
26+
// NginxReloadTimeout sets the timeout duration for reloading the Nginx configuration
2727
NginxReloadTimeout = 60000 * time.Millisecond
2828
)
2929

internal/mode/static/nginx/runtime/manager_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package runtime_test
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"io/fs"
78
"testing"
89
"time"
@@ -63,6 +64,52 @@ var _ = Describe("NGINX Runtime Manager", func() {
6364
Expect(metrics.IncReloadErrorsCallCount()).To(Equal(0))
6465
})
6566

67+
It("Fails to find the main process", func() {
68+
process.FindMainProcessReturns(0, fmt.Errorf("failed to find process"))
69+
70+
err := manager.Reload(context.Background(), 1)
71+
72+
Expect(err).To(MatchError("failed to find NGINX main process: failed to find process"))
73+
Expect(process.ReadFileCallCount()).To(Equal(0))
74+
Expect(process.KillCallCount()).To(Equal(0))
75+
Expect(verifyClient.WaitForCorrectVersionCallCount()).To(Equal(0))
76+
})
77+
78+
It("Fails to read file", func() {
79+
process.FindMainProcessReturns(1234, nil)
80+
process.ReadFileReturns(nil, fmt.Errorf("failed to read file"))
81+
82+
err := manager.Reload(context.Background(), 1)
83+
84+
Expect(err).To(MatchError("failed to read file"))
85+
Expect(process.KillCallCount()).To(Equal(0))
86+
Expect(verifyClient.WaitForCorrectVersionCallCount()).To(Equal(0))
87+
})
88+
89+
It("Fails to send kill signal", func() {
90+
process.FindMainProcessReturns(1234, nil)
91+
process.ReadFileReturns([]byte("child1\nchild2"), nil)
92+
process.KillReturns(fmt.Errorf("failed to send kill signal"))
93+
94+
err := manager.Reload(context.Background(), 1)
95+
96+
Expect(err).To(MatchError("failed to send the HUP signal to NGINX main: failed to send kill signal"))
97+
Expect(metrics.IncReloadErrorsCallCount()).To(Equal(1))
98+
Expect(verifyClient.WaitForCorrectVersionCallCount()).To(Equal(0))
99+
})
100+
101+
It("times out waiting for correct version", func() {
102+
process.FindMainProcessReturns(1234, nil)
103+
process.ReadFileReturns([]byte("child1\nchild2"), nil)
104+
process.KillReturns(nil)
105+
verifyClient.WaitForCorrectVersionReturns(fmt.Errorf("timeout waiting for correct version"))
106+
107+
err := manager.Reload(context.Background(), 1)
108+
109+
Expect(err).To(MatchError("timeout waiting for correct version"))
110+
Expect(metrics.IncReloadErrorsCallCount()).To(Equal(1))
111+
})
112+
66113
When("MetricsCollector is nil", func() {
67114
It("panics", func() {
68115
metrics = nil
@@ -110,6 +157,36 @@ var _ = Describe("NGINX Runtime Manager", func() {
110157
Expect(upstreams).To(BeEmpty())
111158
})
112159

160+
It("successfully returns server upstreams", func() {
161+
upstreams := ngxclient.Upstreams{
162+
"upstream1": {
163+
Zone: "zone1",
164+
Peers: []ngxclient.Peer{
165+
{ID: 1, Name: "peer1-name"},
166+
},
167+
Queue: ngxclient.Queue{Size: 10},
168+
Keepalives: 5,
169+
Zombies: 2,
170+
},
171+
"upstream2": {
172+
Zone: "zone2",
173+
Peers: []ngxclient.Peer{
174+
{ID: 2, Name: "peer2-name"},
175+
},
176+
Queue: ngxclient.Queue{Size: 20},
177+
Keepalives: 3,
178+
Zombies: 1,
179+
},
180+
}
181+
182+
ngxPlusClient.GetUpstreamsReturns(&upstreams, nil)
183+
184+
upstreams, err := manager.GetUpstreams()
185+
186+
Expect(err).NotTo(HaveOccurred())
187+
Expect(upstreams).To(Equal(upstreams))
188+
})
189+
113190
It("returns an error when GetUpstreams fails", func() {
114191
ngxPlusClient.GetUpstreamsReturns(nil, errors.New("failed to get upstreams"))
115192

0 commit comments

Comments
 (0)