Skip to content

Commit fe8d2fc

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

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package runtime_test
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"io/fs"
78
"testing"
89
"time"
910

11+
"github.com/nginxinc/nginx-plus-go-client/client"
1012
ngxclient "github.com/nginxinc/nginx-plus-go-client/client"
1113
. "github.com/onsi/ginkgo/v2"
1214
. "github.com/onsi/gomega"
@@ -63,6 +65,52 @@ var _ = Describe("NGINX Runtime Manager", func() {
6365
Expect(metrics.IncReloadErrorsCallCount()).To(Equal(0))
6466
})
6567

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

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

0 commit comments

Comments
 (0)