Skip to content

Commit b50260e

Browse files
committed
fix tests
1 parent 63eb2d1 commit b50260e

File tree

3 files changed

+106
-103
lines changed

3 files changed

+106
-103
lines changed

internal/model/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package model
77

88
import (
9+
"log/slog"
910
"reflect"
1011

1112
"github.com/nginx/agent/v3/api/grpc/mpi/v1"
@@ -81,35 +82,44 @@ func (ncc *NginxConfigContext) Equal(otherNginxConfigContext *NginxConfigContext
8182
if ncc.StubStatus.URL != otherNginxConfigContext.StubStatus.URL || ncc.StubStatus.Listen !=
8283
otherNginxConfigContext.StubStatus.Listen || ncc.StubStatus.Location !=
8384
otherNginxConfigContext.StubStatus.Location {
85+
slog.Info("stub status not equal")
8486
return false
8587
}
8688

8789
if ncc.PlusAPI.URL != otherNginxConfigContext.PlusAPI.URL || ncc.PlusAPI.Listen !=
8890
otherNginxConfigContext.PlusAPI.Listen || ncc.PlusAPI.Location !=
8991
otherNginxConfigContext.PlusAPI.Location {
92+
slog.Info("plus api not equal")
9093
return false
9194
}
9295

9396
if ncc.InstanceID != otherNginxConfigContext.InstanceID {
97+
slog.Info("id not equal")
9498
return false
9599
}
96100

97101
if !ncc.areFileEqual(otherNginxConfigContext.Files) {
102+
slog.Info("files not equal")
98103
return false
99104
}
100105

101106
if !reflect.DeepEqual(ncc.AccessLogs, otherNginxConfigContext.AccessLogs) {
107+
slog.Info("access logs not equal")
102108
return false
103109
}
104110

105111
if !reflect.DeepEqual(ncc.ErrorLogs, otherNginxConfigContext.ErrorLogs) {
112+
slog.Info("error logs not equal")
106113
return false
107114
}
108115

109116
if !reflect.DeepEqual(ncc.NAPSysLogServers, otherNginxConfigContext.NAPSysLogServers) {
117+
slog.Info("nap syslog servers not equal")
110118
return false
111119
}
112120

121+
slog.Info("config is equal")
122+
113123
return true
114124
}
115125

internal/watcher/instance/instance_watcher_service.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (iw *InstanceWatcherService) ReparseConfig(ctx context.Context, instanceID
170170

171171
slog.Info("Send Config Context Update - Reparse config", "instance_id", instanceID)
172172
iw.sendNginxConfigContextUpdate(ctx, nginxConfigContext)
173-
iw.updateCache(nginxConfigContext)
173+
iw.nginxConfigCache[nginxConfigContext.InstanceID] = nginxConfigContext
174174
updatesRequired = proto.UpdateNginxInstanceRuntime(instance, nginxConfigContext)
175175
}
176176

@@ -185,13 +185,6 @@ func (iw *InstanceWatcherService) ReparseConfig(ctx context.Context, instanceID
185185
}
186186
}
187187

188-
func (iw *InstanceWatcherService) updateCache(nginxConfigContext *model.NginxConfigContext) {
189-
slog.Info("Updating Cache")
190-
iw.nginxConfigCache[nginxConfigContext.InstanceID] = nginxConfigContext
191-
slog.Info("Updated Cache for instance", "", nginxConfigContext.InstanceID)
192-
slog.Info("Current Cache", "", iw.nginxConfigCache[nginxConfigContext.InstanceID])
193-
}
194-
195188
func (iw *InstanceWatcherService) checkForUpdates(
196189
ctx context.Context,
197190
) {

test/integration/grpc_management_plane_api_test.go

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
)
3232

3333
const (
34-
// configApplyErrorMessage = "failed to parse config invalid " +
35-
// "number of arguments in \"worker_processes\" directive in /etc/nginx/nginx.conf:1"
34+
configApplyErrorMessage = "failed to parse config invalid " +
35+
"number of arguments in \"worker_processes\" directive in /etc/nginx/nginx.conf:1"
3636

3737
retryCount = 5
3838
retryWaitTime = 4 * time.Second
@@ -200,15 +200,15 @@ func TestGrpc_ConfigApply(t *testing.T) {
200200
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
201201
assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
202202

203-
// t.Run("Test 1: No config changes", func(t *testing.T) {
204-
// clearManagementPlaneResponses(t)
205-
// performConfigApply(t, nginxInstanceID)
206-
// responses = getManagementPlaneResponses(t, 1)
207-
// t.Logf("Config apply responses: %v", responses)
208-
//
209-
// assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
210-
// assert.Equal(t, "Config apply successful, no files to change", responses[0].GetCommandResponse().GetMessage())
211-
// })
203+
t.Run("Test 1: No config changes", func(t *testing.T) {
204+
clearManagementPlaneResponses(t)
205+
performConfigApply(t, nginxInstanceID)
206+
responses = getManagementPlaneResponses(t, 1)
207+
t.Logf("Config apply responses: %v", responses)
208+
209+
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
210+
assert.Equal(t, "Config apply successful, no files to change", responses[0].GetCommandResponse().GetMessage())
211+
})
212212

213213
t.Run("Test 2: Valid config", func(t *testing.T) {
214214
clearManagementPlaneResponses(t)
@@ -235,44 +235,44 @@ func TestGrpc_ConfigApply(t *testing.T) {
235235
assert.Equal(t, "Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
236236
})
237237

238-
// t.Run("Test 3: Invalid config", func(t *testing.T) {
239-
// clearManagementPlaneResponses(t)
240-
// err := mockManagementPlaneGrpcContainer.CopyFileToContainer(
241-
// ctx,
242-
// "../config/nginx/invalid-nginx.conf",
243-
// fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID),
244-
// 0o666,
245-
// )
246-
// require.NoError(t, err)
247-
//
248-
// performConfigApply(t, nginxInstanceID)
249-
//
250-
// responses = getManagementPlaneResponses(t, 2)
251-
// t.Logf("Config apply responses: %v", responses)
252-
//
253-
// assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_ERROR, responses[0].GetCommandResponse().GetStatus())
254-
// assert.Equal(t, "Config apply failed, rolling back config", responses[0].GetCommandResponse().GetMessage())
255-
// assert.Equal(t, configApplyErrorMessage, responses[0].GetCommandResponse().GetError())
256-
// assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[1].GetCommandResponse().GetStatus())
257-
// assert.Equal(t, "Config apply failed, rollback successful", responses[1].GetCommandResponse().GetMessage())
258-
// assert.Equal(t, configApplyErrorMessage, responses[1].GetCommandResponse().GetError())
259-
// })
260-
//
261-
// t.Run("Test 4: File not in allowed directory", func(t *testing.T) {
262-
// clearManagementPlaneResponses(t)
263-
// performInvalidConfigApply(t, nginxInstanceID)
264-
//
265-
// responses = getManagementPlaneResponses(t, 1)
266-
// t.Logf("Config apply responses: %v", responses)
267-
//
268-
// assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[0].GetCommandResponse().GetStatus())
269-
// assert.Equal(t, "Config apply failed", responses[0].GetCommandResponse().GetMessage())
270-
// assert.Equal(
271-
// t,
272-
// "file not in allowed directories /unknown/nginx.conf",
273-
// responses[0].GetCommandResponse().GetError(),
274-
// )
275-
// })
238+
t.Run("Test 3: Invalid config", func(t *testing.T) {
239+
clearManagementPlaneResponses(t)
240+
err := mockManagementPlaneGrpcContainer.CopyFileToContainer(
241+
ctx,
242+
"../config/nginx/invalid-nginx.conf",
243+
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID),
244+
0o666,
245+
)
246+
require.NoError(t, err)
247+
248+
performConfigApply(t, nginxInstanceID)
249+
250+
responses = getManagementPlaneResponses(t, 2)
251+
t.Logf("Config apply responses: %v", responses)
252+
253+
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_ERROR, responses[0].GetCommandResponse().GetStatus())
254+
assert.Equal(t, "Config apply failed, rolling back config", responses[0].GetCommandResponse().GetMessage())
255+
assert.Equal(t, configApplyErrorMessage, responses[0].GetCommandResponse().GetError())
256+
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[1].GetCommandResponse().GetStatus())
257+
assert.Equal(t, "Config apply failed, rollback successful", responses[1].GetCommandResponse().GetMessage())
258+
assert.Equal(t, configApplyErrorMessage, responses[1].GetCommandResponse().GetError())
259+
})
260+
261+
t.Run("Test 4: File not in allowed directory", func(t *testing.T) {
262+
clearManagementPlaneResponses(t)
263+
performInvalidConfigApply(t, nginxInstanceID)
264+
265+
responses = getManagementPlaneResponses(t, 1)
266+
t.Logf("Config apply responses: %v", responses)
267+
268+
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[0].GetCommandResponse().GetStatus())
269+
assert.Equal(t, "Config apply failed", responses[0].GetCommandResponse().GetMessage())
270+
assert.Equal(
271+
t,
272+
"file not in allowed directories /unknown/nginx.conf",
273+
responses[0].GetCommandResponse().GetError(),
274+
)
275+
})
276276
}
277277

278278
func performConfigApply(t *testing.T, nginxInstanceID string) {
@@ -288,52 +288,52 @@ func performConfigApply(t *testing.T, nginxInstanceID string) {
288288
assert.Equal(t, http.StatusOK, resp.StatusCode())
289289
}
290290

291-
// func performInvalidConfigApply(t *testing.T, nginxInstanceID string) {
292-
// t.Helper()
293-
//
294-
// client := resty.New()
295-
// client.SetRetryCount(retryCount).SetRetryWaitTime(retryWaitTime).SetRetryMaxWaitTime(retryMaxWaitTime)
296-
//
297-
// body := fmt.Sprintf(`{
298-
// "message_meta": {
299-
// "message_id": "e2254df9-8edd-4900-91ce-88782473bcb9",
300-
// "correlation_id": "9673f3b4-bf33-4d98-ade1-ded9266f6818",
301-
// "timestamp": "2023-01-15T01:30:15.01Z"
302-
// },
303-
// "config_apply_request": {
304-
// "overview": {
305-
// "files": [{
306-
// "file_meta": {
307-
// "name": "/etc/nginx/nginx.conf",
308-
// "hash": "ea57e443-e968-3a50-b842-f37112acde71",
309-
// "modifiedTime": "2023-01-15T01:30:15.01Z",
310-
// "permissions": "0644",
311-
// "size": 0
312-
// },
313-
// "action": "FILE_ACTION_UPDATE"
314-
// },
315-
// {
316-
// "file_meta": {
317-
// "name": "/unknown/nginx.conf",
318-
// "hash": "bd1f337d-6874-35ea-9d4d-2b543efd42cf",
319-
// "modifiedTime": "2023-01-15T01:30:15.01Z",
320-
// "permissions": "0644",
321-
// "size": 0
322-
// },
323-
// "action": "FILE_ACTION_ADD"
324-
// }],
325-
// "config_version": {
326-
// "instance_id": "%s",
327-
// "version": "6f343257-55e3-309e-a2eb-bb13af5f80f4"
328-
// }
329-
// }
330-
// }
331-
// }`, nginxInstanceID)
332-
// url := fmt.Sprintf("http://%s/api/v1/requests", mockManagementPlaneAPIAddress)
333-
// resp, err := client.R().EnableTrace().SetBody(body).Post(url)
334-
// require.NoError(t, err)
335-
// assert.Equal(t, http.StatusOK, resp.StatusCode())
336-
// }
291+
func performInvalidConfigApply(t *testing.T, nginxInstanceID string) {
292+
t.Helper()
293+
294+
client := resty.New()
295+
client.SetRetryCount(retryCount).SetRetryWaitTime(retryWaitTime).SetRetryMaxWaitTime(retryMaxWaitTime)
296+
297+
body := fmt.Sprintf(`{
298+
"message_meta": {
299+
"message_id": "e2254df9-8edd-4900-91ce-88782473bcb9",
300+
"correlation_id": "9673f3b4-bf33-4d98-ade1-ded9266f6818",
301+
"timestamp": "2023-01-15T01:30:15.01Z"
302+
},
303+
"config_apply_request": {
304+
"overview": {
305+
"files": [{
306+
"file_meta": {
307+
"name": "/etc/nginx/nginx.conf",
308+
"hash": "ea57e443-e968-3a50-b842-f37112acde71",
309+
"modifiedTime": "2023-01-15T01:30:15.01Z",
310+
"permissions": "0644",
311+
"size": 0
312+
},
313+
"action": "FILE_ACTION_UPDATE"
314+
},
315+
{
316+
"file_meta": {
317+
"name": "/unknown/nginx.conf",
318+
"hash": "bd1f337d-6874-35ea-9d4d-2b543efd42cf",
319+
"modifiedTime": "2023-01-15T01:30:15.01Z",
320+
"permissions": "0644",
321+
"size": 0
322+
},
323+
"action": "FILE_ACTION_ADD"
324+
}],
325+
"config_version": {
326+
"instance_id": "%s",
327+
"version": "6f343257-55e3-309e-a2eb-bb13af5f80f4"
328+
}
329+
}
330+
}
331+
}`, nginxInstanceID)
332+
url := fmt.Sprintf("http://%s/api/v1/requests", mockManagementPlaneAPIAddress)
333+
resp, err := client.R().EnableTrace().SetBody(body).Post(url)
334+
require.NoError(t, err)
335+
assert.Equal(t, http.StatusOK, resp.StatusCode())
336+
}
337337

338338
func getManagementPlaneResponses(t *testing.T, numberOfExpectedResponses int) []*mpi.DataPlaneResponse {
339339
t.Helper()

0 commit comments

Comments
 (0)