Skip to content

Commit 4f65425

Browse files
committed
fix unit tests
1 parent ad599b0 commit 4f65425

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

internal/file/file_manager_service_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,9 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
609609

610610
for _, test := range tests {
611611
t.Run(test.name, func(tt *testing.T) {
612-
helpers.CreateManifestFileWithErrorCheck(t, manifestDirPath, "manifest.json")
612+
// Delete manifest file if it already exists
613+
dir := helpers.CreateManifestDirWithErrorCheck(t, manifestDirPath, "manifest.json")
614+
manifestFilePath = dir + "manifest.json"
613615
fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
614616
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig())
615617
err = fileManagerService.UpdateManifestFile(test.currentFiles, true)

internal/file/file_plugin_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,44 @@ func TestFilePlugin_Process_ConfigApplyRollbackCompleteTopic(t *testing.T) {
473473

474474
assert.Equal(t, expectedResponse.GetInstanceId(), response.GetInstanceId())
475475
}
476+
477+
func TestFilePlugin_Process_ConfigApplyCompleteTopic(t *testing.T) {
478+
ctx := context.Background()
479+
instance := protos.GetNginxOssInstance([]string{})
480+
mockFileManager := &filefakes.FakeFileManagerServiceInterface{}
481+
482+
messagePipe := busfakes.NewFakeMessagePipe()
483+
agentConfig := types.AgentConfig()
484+
fakeGrpcConnection := &grpcfakes.FakeGrpcConnectionInterface{}
485+
filePlugin := NewFilePlugin(agentConfig, fakeGrpcConnection)
486+
487+
err := filePlugin.Init(ctx, messagePipe)
488+
require.NoError(t, err)
489+
filePlugin.fileManagerService = mockFileManager
490+
expectedResponse := &mpi.DataPlaneResponse{
491+
MessageMeta: &mpi.MessageMeta{
492+
MessageId: id.GenerateMessageID(),
493+
CorrelationId: "dfsbhj6-bc92-30c1-a9c9-85591422068e",
494+
Timestamp: timestamppb.Now(),
495+
},
496+
CommandResponse: &mpi.CommandResponse{
497+
Status: mpi.CommandResponse_COMMAND_STATUS_OK,
498+
Message: "Config apply successful",
499+
Error: "",
500+
},
501+
InstanceId: instance.GetInstanceMeta().GetInstanceId(),
502+
}
503+
504+
filePlugin.Process(ctx, &bus.Message{Topic: bus.ConfigApplyCompleteTopic, Data: expectedResponse})
505+
506+
messages := messagePipe.GetMessages()
507+
response, ok := messages[0].Data.(*mpi.DataPlaneResponse)
508+
assert.True(t, ok)
509+
510+
assert.Equal(t, expectedResponse.GetCommandResponse().GetStatus(), response.GetCommandResponse().GetStatus())
511+
assert.Equal(t, expectedResponse.GetCommandResponse().GetMessage(), response.GetCommandResponse().GetMessage())
512+
assert.Equal(t, expectedResponse.GetCommandResponse().GetError(), response.GetCommandResponse().GetError())
513+
assert.Equal(t, expectedResponse.GetMessageMeta().GetCorrelationId(), response.GetMessageMeta().GetCorrelationId())
514+
515+
assert.Equal(t, expectedResponse.GetInstanceId(), response.GetInstanceId())
516+
}

test/helpers/os_utils.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package helpers
77

88
import (
99
"errors"
10+
"log/slog"
1011
"os"
1112
"regexp"
1213
"strings"
@@ -37,18 +38,16 @@ func CreateFileWithErrorCheck(t testing.TB, dir, fileName string) *os.File {
3738
return testConf
3839
}
3940

40-
func CreateManifestFileWithErrorCheck(t testing.TB, dir, manifestFileName string) *os.File {
41+
func CreateManifestDirWithErrorCheck(t testing.TB, dir, manifestFileName string) string {
4142
t.Helper()
4243

4344
if _, err := os.Stat(dir + manifestFileName); !errors.Is(err, os.ErrNotExist) {
45+
slog.Info("Manifest file exists, deleting", "", dir+manifestFileName)
4446
removeErr := os.Remove(dir + manifestFileName)
4547
require.NoError(t, removeErr)
4648
}
4749

48-
testConf, err := os.CreateTemp(dir, manifestFileName)
49-
require.NoError(t, err)
50-
51-
return testConf
50+
return os.TempDir()
5251
}
5352

5453
func RemoveFileWithErrorCheck(t testing.TB, fileName string) {

0 commit comments

Comments
 (0)