Skip to content

Commit 30b4da7

Browse files
committed
fix tests
1 parent 7b51833 commit 30b4da7

File tree

6 files changed

+52
-23
lines changed

6 files changed

+52
-23
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ TAG ?= ""
3737

3838
BUILD_DIR := build
3939
TEST_BUILD_DIR := build/test
40-
CERTS_DIR := /Users/a.griffin/dev/certs
40+
CERTS_DIR := build/certs
4141

4242
DOCS_DIR := docs
4343
PROTO_DIR := proto

api/grpc/mpi/v1/command.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/common.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/mpi/v1/files.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/resource/nginx_instance_operator.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
"context"
1111
"errors"
1212
"fmt"
13+
"log/slog"
14+
"time"
15+
1316
"github.com/nginx/agent/v3/internal/backoff"
1417
"github.com/nginx/agent/v3/pkg/nginxprocess"
1518
"github.com/shirou/gopsutil/v4/process"
16-
"log/slog"
17-
"time"
1819

1920
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
2021
"github.com/nginx/agent/v3/internal/config"
@@ -24,6 +25,7 @@ import (
2425
type NginxInstanceOperator struct {
2526
executer exec.ExecInterface
2627
logTailer logTailerOperator
28+
agentConfig *config.Config
2729
treatWarningsAsErrors bool
2830
}
2931

@@ -34,6 +36,7 @@ func NewInstanceOperator(agentConfig *config.Config) *NginxInstanceOperator {
3436
executer: &exec.Exec{},
3537
logTailer: NewLogTailerOperator(agentConfig),
3638
treatWarningsAsErrors: agentConfig.DataPlaneConfig.Nginx.TreatWarningsAsErrors,
39+
agentConfig: agentConfig,
3740
}
3841
}
3942

@@ -56,6 +59,7 @@ func (i *NginxInstanceOperator) Validate(ctx context.Context, instance *mpi.Inst
5659
return nil
5760
}
5861

62+
// nolint: revive
5963
func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instance) error {
6064
var reloadTime time.Time
6165
var errorsFound error
@@ -64,7 +68,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
6468

6569
workers := nginxWorkerProcesses(ctx)
6670

67-
if workers != nil && len(workers) > 0 {
71+
if len(workers) > 0 {
6872
reloadTime = workers[0].Created
6973
}
7074

@@ -81,18 +85,18 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
8185
}
8286

8387
backoffSettings := &config.BackOff{
84-
InitialInterval: config.DefBackoffInitialInterval,
85-
MaxInterval: config.DefBackoffMaxInterval,
86-
MaxElapsedTime: config.DefBackoffMaxElapsedTime,
87-
RandomizationFactor: config.DefBackoffRandomizationFactor,
88-
Multiplier: config.DefBackoffMultiplier,
88+
InitialInterval: i.agentConfig.Client.Backoff.InitialInterval,
89+
MaxInterval: i.agentConfig.Client.Backoff.MaxInterval,
90+
MaxElapsedTime: i.agentConfig.Client.Backoff.MaxElapsedTime,
91+
RandomizationFactor: i.agentConfig.Client.Backoff.RandomizationFactor,
92+
Multiplier: i.agentConfig.Client.Backoff.Multiplier,
8993
}
9094

91-
slog.Info("Waiting for NGINX to finish reloading")
95+
slog.DebugContext(ctx, "Waiting for NGINX to finish reloading")
9296
err = backoff.WaitUntil(ctx, backoffSettings, func() error {
9397
currentWorkers := nginxWorkerProcesses(ctx)
94-
if currentWorkers == nil || len(currentWorkers) == 0 {
95-
return fmt.Errorf("waiting for NGINX worker processes")
98+
if len(currentWorkers) == 0 {
99+
return errors.New("waiting for NGINX worker processes")
96100
}
97101

98102
for _, worker := range currentWorkers {
@@ -103,8 +107,13 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
103107
}
104108

105109
slog.InfoContext(ctx, "All NGINX workers have been reloaded", "worker_count", len(currentWorkers))
110+
106111
return nil
107112
})
113+
if err != nil {
114+
slog.WarnContext(ctx, "Failed to check if NGINX worker processes have successfully reloaded",
115+
"error", err)
116+
}
108117

109118
slog.InfoContext(ctx, "NGINX reloaded", "processid", instance.GetInstanceRuntime().GetProcessId())
110119

@@ -129,17 +138,16 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
129138
}
130139

131140
func nginxWorkerProcesses(ctx context.Context) []*nginxprocess.Process {
132-
slog.Debug("Getting NGINX worker processes for NGINX reload")
141+
slog.DebugContext(ctx, "Getting NGINX worker processes for NGINX reload")
133142
var workers []*nginxprocess.Process
134143
processes, err := process.ProcessesWithContext(ctx)
135144
if err != nil {
136-
slog.Warn("Failed to get processes", "error", err)
145+
slog.WarnContext(ctx, "Failed to get processes", "error", err)
137146
}
138147

139148
nginxProcesses, err := nginxprocess.ListWithProcesses(ctx, processes)
140-
141149
if err != nil {
142-
slog.Warn("Failed to get NGINX processes", "error", err)
150+
slog.WarnContext(ctx, "Failed to get NGINX processes", "error", err)
143151
}
144152

145153
for _, nginxProcess := range nginxProcesses {

internal/resource/nginx_instance_operator_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"testing"
1515
"time"
1616

17+
"github.com/nginx/agent/v3/internal/config"
18+
1719
"github.com/nginx/agent/v3/internal/datasource/host/exec/execfakes"
1820
"github.com/nginx/agent/v3/test/helpers"
1921
"github.com/nginx/agent/v3/test/protos"
@@ -38,12 +40,17 @@ func TestInstanceOperator_ValidateConfigCheckResponse(t *testing.T) {
3840
out: "nginx [emerg]",
3941
expected: errors.New("error running nginx -t -c:\nnginx [emerg]"),
4042
},
43+
{
44+
name: "Test 3: Warn response",
45+
out: "nginx [warn]",
46+
expected: errors.New("error running nginx -t -c:\nnginx [warn]"),
47+
},
4148
}
4249

4350
for _, test := range tests {
4451
t.Run(test.name, func(t *testing.T) {
4552
operator := NewInstanceOperator(types.AgentConfig())
46-
53+
operator.treatWarningsAsErrors = true
4754
err := operator.validateConfigCheckResponse([]byte(test.out))
4855
assert.Equal(t, test.expected, err)
4956
})
@@ -126,7 +133,15 @@ func TestInstanceOperator_Reload(t *testing.T) {
126133

127134
instance := protos.NginxOssInstance([]string{})
128135

129-
operator := NewInstanceOperator(types.AgentConfig())
136+
agentConfig := types.AgentConfig()
137+
agentConfig.Client.Backoff = &config.BackOff{
138+
InitialInterval: 1 * time.Millisecond,
139+
MaxInterval: 1 * time.Millisecond,
140+
MaxElapsedTime: 1 * time.Second,
141+
RandomizationFactor: 1,
142+
Multiplier: 1,
143+
}
144+
operator := NewInstanceOperator(agentConfig)
130145
operator.executer = mockExec
131146

132147
err := operator.Reload(ctx, instance)
@@ -179,7 +194,14 @@ func TestInstanceOperator_ReloadAndMonitor(t *testing.T) {
179194

180195
agentConfig := types.AgentConfig()
181196
agentConfig.DataPlaneConfig.Nginx.ReloadMonitoringPeriod = 10 * time.Second
182-
operator := NewInstanceOperator(types.AgentConfig())
197+
agentConfig.Client.Backoff = &config.BackOff{
198+
InitialInterval: 1 * time.Millisecond,
199+
MaxInterval: 1 * time.Millisecond,
200+
MaxElapsedTime: 1 * time.Second,
201+
RandomizationFactor: 1,
202+
Multiplier: 1,
203+
}
204+
operator := NewInstanceOperator(agentConfig)
183205
operator.executer = mockExec
184206

185207
var wg sync.WaitGroup
@@ -201,4 +223,3 @@ func TestInstanceOperator_ReloadAndMonitor(t *testing.T) {
201223
})
202224
}
203225
}
204-

0 commit comments

Comments
 (0)