Skip to content

Commit 2d3eec1

Browse files
miledxzMileTildeloop
authored andcommitted
adding tests for runtime manager
1 parent 22a0f9d commit 2d3eec1

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import (
1919
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
2020

2121
const (
22-
PidFile = "/var/run/nginx/nginx.pid"
23-
pidFileTimeout = 10000 * time.Millisecond
22+
// PidFile specifies the location of the PID file for the Nginx process
23+
PidFile = "/var/run/nginx/nginx.pid"
24+
// pidFileTimeout defines the timeout duration for accessing the PID file
25+
pidFileTimeout = 10000 * time.Millisecond
26+
/// NginxReloadTimeout sets the timeout duration for reloading the Nginx configuration
2427
NginxReloadTimeout = 60000 * time.Millisecond
2528
)
2629

@@ -60,8 +63,6 @@ type ProcessHandler interface {
6063
EnsureNginxRunning(ctx context.Context) error
6164
}
6265

63-
type ProcessHandlerImpl struct{}
64-
6566
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Manager
6667

6768
// Manager manages the runtime of NGINX.
@@ -91,7 +92,7 @@ type MetricsCollector interface {
9192
type ManagerImpl struct {
9293
processHandler ProcessHandler
9394
metricsCollector MetricsCollector
94-
verifyClient verifyClient
95+
verifyClient nginxConfigVerifier
9596
ngxPlusClient nginxPlusClient
9697
logger logr.Logger
9798
}
@@ -102,7 +103,7 @@ func NewManagerImpl(
102103
collector MetricsCollector,
103104
logger logr.Logger,
104105
processHandler ProcessHandler,
105-
verifyClient verifyClient,
106+
verifyClient nginxConfigVerifier,
106107
) *ManagerImpl {
107108
return &ManagerImpl{
108109
processHandler: processHandler,
@@ -190,6 +191,8 @@ func (m *ManagerImpl) GetUpstreams() (ngxclient.Upstreams, error) {
190191
return *upstreams, nil
191192
}
192193

194+
type ProcessHandlerImpl struct{}
195+
193196
// EnsureNginxRunning ensures NGINX is running by locating the main process.
194197
func (p *ProcessHandlerImpl) EnsureNginxRunning(ctx context.Context) error {
195198
if _, err := p.FindMainProcess(ctx, os.Stat, os.ReadFile, pidFileTimeout); err != nil {

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ var _ = Describe("NGINX Runtime Manager", func() {
5151
manager = runtime.NewManagerImpl(ngxPlusClient, metrics, zap.New(), process, verifyClient)
5252
})
5353

54-
It("NGINX configuration reload is successful", func() {
55-
Expect(manager.Reload(context.Background(), 1)).To(Succeed())
56-
57-
Expect(process.FindMainProcessCallCount()).To(Equal(1))
58-
Expect(process.ReadFileCallCount()).To(Equal(1))
59-
Expect(process.KillCallCount()).To(Equal(1))
60-
Expect(metrics.IncReloadCountCallCount()).To(Equal(1))
61-
Expect(verifyClient.WaitForCorrectVersionCallCount()).To(Equal(1))
62-
Expect(metrics.ObserveLastReloadTimeCallCount()).To(Equal(1))
54+
When("MetricsCollector is nil", func() {
55+
It("NGINX configuration reload is successful", func() {
56+
Expect(manager.Reload(context.Background(), 1)).To(Succeed())
57+
58+
Expect(process.FindMainProcessCallCount()).To(Equal(1))
59+
Expect(process.ReadFileCallCount()).To(Equal(1))
60+
Expect(process.KillCallCount()).To(Equal(1))
61+
Expect(metrics.IncReloadCountCallCount()).To(Equal(1))
62+
Expect(verifyClient.WaitForCorrectVersionCallCount()).To(Equal(1))
63+
Expect(metrics.ObserveLastReloadTimeCallCount()).To(Equal(1))
64+
Expect(metrics.IncReloadErrorsCallCount()).To(Equal(0))
65+
})
6366
})
6467

6568
When("NGINX configuration reload is not successful", func() {
@@ -100,12 +103,22 @@ var _ = Describe("NGINX Runtime Manager", func() {
100103
Expect(manager.UpdateHTTPServers("test", upstreamServers)).To(Succeed())
101104
})
102105

103-
It("returns no upstreams from NGINX Plus API", func() {
106+
It("returns no upstreams from NGINX Plus API when upstreams are nil", func() {
104107
upstreams, err := manager.GetUpstreams()
105108

106109
Expect(err).To(HaveOccurred())
107110
Expect(upstreams).To(BeEmpty())
108111
})
112+
113+
It("returns an error when GetUpstreams fails", func() {
114+
ngxPlusClient.GetUpstreamsReturns(nil, errors.New("failed to get upstreams"))
115+
116+
upstreams, err := manager.GetUpstreams()
117+
118+
Expect(err).To(HaveOccurred())
119+
Expect(err).To(MatchError("failed to get upstreams"))
120+
Expect(upstreams).To(BeNil())
121+
})
109122
})
110123

111124
When("not running NGINX plus", func() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var noNewWorkersErrFmt = "reload unsuccessful: no new NGINX worker processes sta
2121

2222
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . verifyClient
2323

24-
type verifyClient interface {
24+
type nginxConfigVerifier interface {
2525
GetConfigVersion() (int, error)
2626
WaitForCorrectVersion(
2727
ctx context.Context,

0 commit comments

Comments
 (0)