Skip to content

Commit fb3b613

Browse files
committed
removed duplicate rename function
1 parent 38913b5 commit fb3b613

File tree

7 files changed

+116
-292
lines changed

7 files changed

+116
-292
lines changed

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

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/file/file_manager_service.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ type (
8585
fileToUpdate *mpi.File,
8686
) error
8787
SetIsConnected(isConnected bool)
88-
RenameFile(ctx context.Context, hash, fileName, tempDir string) error
89-
RenameExternalFile(ctx context.Context, fileName, tempDir string) error
88+
RenameFile(ctx context.Context, fileName, tempDir string) error
89+
ValidateFileHash(ctx context.Context, fileName, expectedHash string) error
9090
UpdateClient(ctx context.Context, fileServiceClient mpi.FileServiceClient)
9191
}
9292

@@ -646,6 +646,7 @@ func (fms *FileManagerService) downloadUpdatedFilesToTempLocation(ctx context.Co
646646
return errGroup.Wait()
647647
}
648648

649+
//nolint:revive // cognitive-complexity of 14 max is 12, loop is needed cant be broken up
649650
func (fms *FileManagerService) moveOrDeleteFiles(ctx context.Context, actionError error) error {
650651
actionsLoop:
651652
for _, fileAction := range fms.fileActions {
@@ -664,9 +665,14 @@ actionsLoop:
664665

665666
continue
666667
case model.Add, model.Update:
667-
err = fms.fileServiceOperator.RenameFile(ctx, fileMeta.GetHash(), tempFilePath, fileMeta.GetName())
668+
err = fms.fileServiceOperator.RenameFile(ctx, tempFilePath, fileMeta.GetName())
669+
if err != nil {
670+
actionError = err
671+
break actionsLoop
672+
}
673+
err = fms.fileServiceOperator.ValidateFileHash(ctx, fileMeta.GetName(), fileMeta.GetHash())
668674
case model.ExternalFile:
669-
err = fms.fileServiceOperator.RenameExternalFile(ctx, tempFilePath, fileMeta.GetName())
675+
err = fms.fileServiceOperator.RenameFile(ctx, tempFilePath, fileMeta.GetName())
670676
case model.Unchanged:
671677
slog.DebugContext(ctx, "File unchanged")
672678
}

internal/file/file_manager_service_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"time"
2222

2323
"github.com/nginx/agent/v3/internal/config"
24-
"github.com/nginx/agent/v3/internal/file/filefakes"
2524
"github.com/nginx/agent/v3/internal/model"
2625

2726
"github.com/nginx/agent/v3/pkg/files"
@@ -1402,42 +1401,6 @@ func TestFileManagerService_downloadExternalFiles(t *testing.T) {
14021401
}
14031402
}
14041403

1405-
func TestFileManagerService_ExternalFileRenameCalled(t *testing.T) {
1406-
ctx := context.Background()
1407-
fms := NewFileManagerService(nil, types.AgentConfig(), &sync.RWMutex{})
1408-
1409-
fakeFSO := &filefakes.FakeFileServiceOperatorInterface{}
1410-
1411-
fakeFSO.RenameExternalFileStub = func(ctx context.Context, fileName, tempDir string) error {
1412-
return nil
1413-
}
1414-
1415-
fms.fileServiceOperator = fakeFSO
1416-
1417-
fileName := filepath.Join(t.TempDir(), "ext.conf")
1418-
fms.fileActions = map[string]*model.FileCache{
1419-
fileName: {
1420-
File: &mpi.File{FileMeta: &mpi.FileMeta{Name: fileName}},
1421-
Action: model.ExternalFile,
1422-
},
1423-
}
1424-
1425-
tempPath := tempFilePath(fileName)
1426-
reqDir := filepath.Dir(tempPath)
1427-
require.NoError(t, os.MkdirAll(reqDir, 0o755))
1428-
require.NoError(t, os.WriteFile(tempPath, []byte("data"), 0o600))
1429-
1430-
err := fms.moveOrDeleteFiles(ctx, nil)
1431-
require.NoError(t, err)
1432-
1433-
assert.Equal(t, 1, fakeFSO.RenameExternalFileCallCount(), "RenameExternalFile should be called once")
1434-
1435-
_, srcArg, dstArg := fakeFSO.RenameExternalFileArgsForCall(0)
1436-
1437-
assert.Equal(t, tempPath, srcArg, "RenameExternalFile source argument mismatch")
1438-
assert.Equal(t, fileName, dstArg, "RenameExternalFile destination argument mismatch")
1439-
}
1440-
14411404
func TestFileManagerService_DownloadFileContent_MaxBytesLimit(t *testing.T) {
14421405
ctx := context.Background()
14431406
fms := NewFileManagerService(nil, types.AgentConfig(), &sync.RWMutex{})

internal/file/file_service_operator.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (fso *FileServiceOperator) File(
110110
return writeErr
111111
}
112112

113-
return fso.validateFileHash(tempFilePath, expectedHash)
113+
return fso.ValidateFileHash(ctx, tempFilePath, expectedHash)
114114
}
115115

116116
func (fso *FileServiceOperator) UpdateOverview(
@@ -260,7 +260,7 @@ func (fso *FileServiceOperator) ChunkedFile(
260260
return writeChunkedFileError
261261
}
262262

263-
return fso.validateFileHash(tempFilePath, expectedHash)
263+
return fso.ValidateFileHash(ctx, tempFilePath, expectedHash)
264264
}
265265

266266
func (fso *FileServiceOperator) UpdateFile(
@@ -282,26 +282,9 @@ func (fso *FileServiceOperator) UpdateFile(
282282
return fso.sendUpdateFileStream(ctx, fileToUpdate, fso.agentConfig.Client.Grpc.FileChunkSize)
283283
}
284284

285-
func (fso *FileServiceOperator) RenameExternalFile(
286-
ctx context.Context, source, desination string,
287-
) error {
288-
slog.DebugContext(ctx, fmt.Sprintf("Moving file %s to %s (no hash validation)", source, desination))
289-
290-
if err := os.MkdirAll(filepath.Dir(desination), dirPerm); err != nil {
291-
return fmt.Errorf("failed to create directories for %s: %w", desination, err)
292-
}
293-
294-
moveErr := os.Rename(source, desination)
295-
if moveErr != nil {
296-
return fmt.Errorf("failed to move file: %w", moveErr)
297-
}
298-
299-
return nil
300-
}
301-
302285
// renameFile, renames (moves) file from tempDir to new location to update file.
303286
func (fso *FileServiceOperator) RenameFile(
304-
ctx context.Context, hash, source, desination string,
287+
ctx context.Context, source, desination string,
305288
) error {
306289
slog.DebugContext(ctx, fmt.Sprintf("Renaming file %s to %s", source, desination))
307290

@@ -315,10 +298,11 @@ func (fso *FileServiceOperator) RenameFile(
315298
return fmt.Errorf("failed to rename file: %w", moveErr)
316299
}
317300

318-
return fso.validateFileHash(desination, hash)
301+
return nil
319302
}
320303

321-
func (fso *FileServiceOperator) validateFileHash(filePath, expectedHash string) error {
304+
func (fso *FileServiceOperator) ValidateFileHash(ctx context.Context, filePath, expectedHash string) error {
305+
slog.DebugContext(ctx, "Validating file hash for file ", "file_path", filePath)
322306
content, err := os.ReadFile(filePath)
323307
if err != nil {
324308
return err

internal/file/file_service_operator_test.go

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -187,130 +187,3 @@ func TestFileManagerService_UpdateFile_LargeFile(t *testing.T) {
187187

188188
helpers.RemoveFileWithErrorCheck(t, testFile.Name())
189189
}
190-
191-
//nolint:revive // complexity is 21.
192-
func TestFileServiceOperator_RenameExternalFile(t *testing.T) {
193-
type testCase struct {
194-
name string
195-
wantErrMsg string
196-
setupFailurePath string
197-
destinationPath string
198-
expectedDstContent []byte
199-
srcContent []byte
200-
wantErr bool
201-
}
202-
203-
tests := []testCase{
204-
{
205-
name: "Test 1: success",
206-
srcContent: []byte("hello world"),
207-
setupFailurePath: "",
208-
destinationPath: "subdir/dest.txt",
209-
wantErr: false,
210-
expectedDstContent: []byte("hello world"),
211-
},
212-
{
213-
name: "Test 2: mkdirall_fail",
214-
srcContent: []byte("content"),
215-
setupFailurePath: "not_a_dir",
216-
destinationPath: "not_a_dir/dest.txt",
217-
wantErr: true,
218-
wantErrMsg: "failed to create directories for",
219-
expectedDstContent: nil,
220-
},
221-
{
222-
name: "Test 3: rename_fail (src does not exist)",
223-
srcContent: nil,
224-
setupFailurePath: "",
225-
destinationPath: "subdir/dest.txt",
226-
wantErr: true,
227-
wantErrMsg: "failed to move file",
228-
expectedDstContent: nil,
229-
},
230-
{
231-
name: "Test 4: No destination specified (empty dst path)",
232-
srcContent: []byte("source content"),
233-
setupFailurePath: "",
234-
destinationPath: "",
235-
wantErr: true,
236-
wantErrMsg: "failed to move file:",
237-
expectedDstContent: nil,
238-
},
239-
{
240-
name: "Test 5: Restricted directory (simulated permission fail)",
241-
srcContent: []byte("source content"),
242-
setupFailurePath: "",
243-
destinationPath: "restricted_dir/dest.txt",
244-
wantErr: true,
245-
wantErrMsg: "permission denied",
246-
expectedDstContent: nil,
247-
},
248-
{
249-
name: "Test 6: Two files to the same destination",
250-
srcContent: []byte("source content 1"),
251-
setupFailurePath: "",
252-
destinationPath: "collision_dir/file.txt",
253-
wantErr: false,
254-
expectedDstContent: []byte("source content 2"),
255-
},
256-
}
257-
258-
for _, tc := range tests {
259-
t.Run(tc.name, func(t *testing.T) {
260-
t.Helper()
261-
ctx := context.Background()
262-
fso := NewFileServiceOperator(types.AgentConfig(), nil, &sync.RWMutex{})
263-
tmp := t.TempDir()
264-
265-
src := filepath.Join(tmp, "src.txt")
266-
dst := filepath.Join(tmp, tc.destinationPath)
267-
268-
if tc.setupFailurePath != "" {
269-
parentFile := filepath.Join(tmp, tc.setupFailurePath)
270-
require.NoError(t, os.WriteFile(parentFile, []byte("block"), 0o600))
271-
}
272-
273-
if tc.srcContent != nil {
274-
require.NoError(t, os.WriteFile(src, tc.srcContent, 0o600))
275-
}
276-
277-
if tc.name == "Test 6: Two files to the same destination" {
278-
src1 := src
279-
dstCollision := dst
280-
require.NoError(t, fso.RenameExternalFile(ctx, src1, dstCollision), "initial rename must succeed")
281-
282-
src2 := filepath.Join(tmp, "src2.txt")
283-
content2 := []byte("source content 2")
284-
require.NoError(t, os.WriteFile(src2, content2, 0o600))
285-
286-
src = src2
287-
dst = dstCollision
288-
}
289-
290-
if tc.name == "Test 5: Restricted directory (simulated permission fail)" {
291-
parentDir := filepath.Dir(dst)
292-
require.NoError(t, os.MkdirAll(parentDir, 0o500))
293-
}
294-
295-
err := fso.RenameExternalFile(ctx, src, dst)
296-
297-
if tc.wantErr {
298-
require.Error(t, err)
299-
if tc.wantErrMsg != "" {
300-
require.Contains(t, err.Error(), tc.wantErrMsg)
301-
}
302-
303-
return
304-
}
305-
306-
require.NoError(t, err)
307-
308-
dstContent, readErr := os.ReadFile(dst)
309-
require.NoError(t, readErr)
310-
require.Equal(t, tc.expectedDstContent, dstContent)
311-
312-
_, statErr := os.Stat(src)
313-
require.True(t, os.IsNotExist(statErr), "Source file should not exist after successful rename")
314-
})
315-
}
316-
}

0 commit comments

Comments
 (0)