Skip to content

Commit 59784e2

Browse files
authored
Handle scenario where agent tries to delete a file that does not exist during a config apply (#1123)
1 parent a45c50b commit 59784e2

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

internal/file/file_manager_service.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,19 @@ type (
7373

7474
fileManagerServiceInterface interface {
7575
UpdateOverview(ctx context.Context, instanceID string, filesToUpdate []*mpi.File, iteration int) error
76-
ConfigApply(ctx context.Context, configApplyRequest *mpi.ConfigApplyRequest) (writeStatus model.WriteStatus,
77-
err error)
76+
ConfigApply(
77+
ctx context.Context,
78+
configApplyRequest *mpi.ConfigApplyRequest,
79+
) (writeStatus model.WriteStatus, err error)
7880
Rollback(ctx context.Context, instanceID string) error
7981
UpdateFile(ctx context.Context, instanceID string, fileToUpdate *mpi.File) error
8082
ClearCache()
8183
UpdateCurrentFilesOnDisk(ctx context.Context, updateFiles map[string]*mpi.File, referenced bool) error
82-
DetermineFileActions(currentFiles map[string]*mpi.File, modifiedFiles map[string]*model.FileCache) (
83-
map[string]*model.FileCache, map[string][]byte, error)
84+
DetermineFileActions(
85+
ctx context.Context,
86+
currentFiles map[string]*mpi.File,
87+
modifiedFiles map[string]*model.FileCache,
88+
) (map[string]*model.FileCache, map[string][]byte, error)
8489
IsConnected() bool
8590
SetIsConnected(isConnected bool)
8691
}
@@ -510,8 +515,11 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
510515
return model.Error, allowedErr
511516
}
512517

513-
diffFiles, fileContent, compareErr := fms.DetermineFileActions(fms.currentFilesOnDisk,
514-
ConvertToMapOfFileCache(fileOverview.GetFiles()))
518+
diffFiles, fileContent, compareErr := fms.DetermineFileActions(
519+
ctx,
520+
fms.currentFilesOnDisk,
521+
ConvertToMapOfFileCache(fileOverview.GetFiles()),
522+
)
515523

516524
if compareErr != nil {
517525
return model.Error, compareErr
@@ -546,7 +554,7 @@ func (fms *FileManagerService) ClearCache() {
546554

547555
// nolint:revive,cyclop
548556
func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string) error {
549-
slog.InfoContext(ctx, "Rolling back config for instance", "instanceid", instanceID)
557+
slog.InfoContext(ctx, "Rolling back config for instance", "instance_id", instanceID)
550558

551559
fms.filesMutex.Lock()
552560
defer fms.filesMutex.Unlock()
@@ -714,6 +722,7 @@ func (fms *FileManagerService) checkAllowedDirectory(checkFiles []*mpi.File) err
714722
// that have changed and a map of the contents for each updated and deleted file. Key to both maps is file path
715723
// nolint: revive,cyclop,gocognit
716724
func (fms *FileManagerService) DetermineFileActions(
725+
ctx context.Context,
717726
currentFiles map[string]*mpi.File,
718727
modifiedFiles map[string]*model.FileCache,
719728
) (
@@ -736,6 +745,7 @@ func (fms *FileManagerService) DetermineFileActions(
736745
return nil, nil, manifestFileErr
737746
}
738747
}
748+
739749
// if file is in manifestFiles but not in modified files, file has been deleted
740750
// copy contents, set file action
741751
for fileName, manifestFile := range filesMap {
@@ -745,7 +755,12 @@ func (fms *FileManagerService) DetermineFileActions(
745755
// Read file contents before marking it deleted
746756
fileContent, readErr := os.ReadFile(fileName)
747757
if readErr != nil {
748-
return nil, nil, fmt.Errorf("error reading file %s: %w", fileName, readErr)
758+
if errors.Is(readErr, os.ErrNotExist) {
759+
slog.DebugContext(ctx, "Unable to backup file contents since file does not exist", "file", fileName)
760+
continue
761+
} else {
762+
return nil, nil, fmt.Errorf("error reading file %s: %w", fileName, readErr)
763+
}
749764
}
750765
fileContents[fileName] = fileContent
751766

internal/file/file_manager_service_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ func TestFileManagerService_Rollback(t *testing.T) {
548548
}
549549

550550
func TestFileManagerService_DetermineFileActions(t *testing.T) {
551+
ctx := context.Background()
551552
tempDir := os.TempDir()
552553

553554
deleteTestFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx_delete.conf")
@@ -573,7 +574,6 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
573574

574575
addTestFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx_add.conf")
575576
defer helpers.RemoveFileWithErrorCheck(t, addTestFile.Name())
576-
t.Logf("Adding file: %s", addTestFile.Name())
577577
addFileContent := []byte("test add file")
578578
addErr := os.WriteFile(addTestFile.Name(), addFileContent, 0o600)
579579
require.NoError(t, addErr)
@@ -683,6 +683,18 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
683683
expectedContent: make(map[string][]byte),
684684
expectedError: nil,
685685
},
686+
{
687+
name: "Test 3: File being deleted already doesn't exist",
688+
modifiedFiles: make(map[string]*model.FileCache),
689+
currentFiles: map[string]*mpi.File{
690+
"/unknown/file.conf": {
691+
FileMeta: protos.FileMeta("/unknown/file.conf", files.GenerateHash(fileContent)),
692+
},
693+
},
694+
expectedCache: make(map[string]*model.FileCache),
695+
expectedContent: make(map[string][]byte),
696+
expectedError: nil,
697+
},
686698
}
687699

688700
for _, test := range tests {
@@ -697,8 +709,11 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
697709
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig())
698710
require.NoError(tt, err)
699711

700-
diff, contents, fileActionErr := fileManagerService.DetermineFileActions(test.currentFiles,
701-
test.modifiedFiles)
712+
diff, contents, fileActionErr := fileManagerService.DetermineFileActions(
713+
ctx,
714+
test.currentFiles,
715+
test.modifiedFiles,
716+
)
702717
require.NoError(tt, fileActionErr)
703718
assert.Equal(tt, test.expectedContent, contents)
704719
assert.Equal(tt, test.expectedCache, diff)

internal/file/filefakes/fake_file_manager_service_interface.go

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

0 commit comments

Comments
 (0)