Skip to content

Commit 40ae35b

Browse files
committed
merge main
2 parents c13774a + 4c52f40 commit 40ae35b

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

.github/workflows/upload-release-assets.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Upload Release Assets
1+
name: Publish Release packages
22

33
on:
44
workflow_dispatch:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/nginx/agent/v3
22

33
go 1.23.7
44

5-
toolchain go1.23.8
5+
toolchain go1.23.10
66

77
require (
88
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.4-20250130201111-63bb56e20495.1

internal/file/file_manager_service.go

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

6969
fileManagerServiceInterface interface {
7070
UpdateOverview(ctx context.Context, instanceID string, filesToUpdate []*mpi.File, iteration int) error
71-
ConfigApply(ctx context.Context, configApplyRequest *mpi.ConfigApplyRequest) (writeStatus model.WriteStatus,
72-
err error)
71+
ConfigApply(
72+
ctx context.Context,
73+
configApplyRequest *mpi.ConfigApplyRequest,
74+
) (writeStatus model.WriteStatus, err error)
7375
Rollback(ctx context.Context, instanceID string) error
7476
UpdateFile(ctx context.Context, instanceID string, fileToUpdate *mpi.File) error
7577
ClearCache()
7678
UpdateCurrentFilesOnDisk(ctx context.Context, updateFiles map[string]*mpi.File, referenced bool) error
77-
DetermineFileActions(currentFiles map[string]*mpi.File, modifiedFiles map[string]*model.FileCache) (
78-
map[string]*model.FileCache, map[string][]byte, error)
79+
DetermineFileActions(
80+
ctx context.Context,
81+
currentFiles map[string]*mpi.File,
82+
modifiedFiles map[string]*model.FileCache,
83+
) (map[string]*model.FileCache, map[string][]byte, error)
7984
IsConnected() bool
8085
SetIsConnected(isConnected bool)
8186
}
@@ -505,8 +510,11 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
505510
return model.Error, allowedErr
506511
}
507512

508-
diffFiles, fileContent, compareErr := fms.DetermineFileActions(fms.currentFilesOnDisk,
509-
ConvertToMapOfFileCache(fileOverview.GetFiles()))
513+
diffFiles, fileContent, compareErr := fms.DetermineFileActions(
514+
ctx,
515+
fms.currentFilesOnDisk,
516+
ConvertToMapOfFileCache(fileOverview.GetFiles()),
517+
)
510518

511519
if compareErr != nil {
512520
return model.Error, compareErr
@@ -541,7 +549,7 @@ func (fms *FileManagerService) ClearCache() {
541549

542550
// nolint:revive,cyclop
543551
func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string) error {
544-
slog.InfoContext(ctx, "Rolling back config for instance", "instanceid", instanceID)
552+
slog.InfoContext(ctx, "Rolling back config for instance", "instance_id", instanceID)
545553

546554
fms.filesMutex.Lock()
547555
defer fms.filesMutex.Unlock()
@@ -710,6 +718,7 @@ func (fms *FileManagerService) checkAllowedDirectory(checkFiles []*mpi.File) err
710718
// that have changed and a map of the contents for each updated and deleted file. Key to both maps is file path
711719
// nolint: revive,cyclop,gocognit
712720
func (fms *FileManagerService) DetermineFileActions(
721+
ctx context.Context,
713722
currentFiles map[string]*mpi.File,
714723
modifiedFiles map[string]*model.FileCache,
715724
) (
@@ -732,6 +741,7 @@ func (fms *FileManagerService) DetermineFileActions(
732741
return nil, nil, manifestFileErr
733742
}
734743
}
744+
735745
// if file is in manifestFiles but not in modified files, file has been deleted
736746
// copy contents, set file action
737747
for fileName, manifestFile := range filesMap {
@@ -741,7 +751,12 @@ func (fms *FileManagerService) DetermineFileActions(
741751
// Read file contents before marking it deleted
742752
fileContent, readErr := os.ReadFile(fileName)
743753
if readErr != nil {
744-
return nil, nil, fmt.Errorf("error reading file %s: %w", fileName, readErr)
754+
if errors.Is(readErr, os.ErrNotExist) {
755+
slog.DebugContext(ctx, "Unable to backup file contents since file does not exist", "file", fileName)
756+
continue
757+
} else {
758+
return nil, nil, fmt.Errorf("error reading file %s: %w", fileName, readErr)
759+
}
745760
}
746761
fileContents[fileName] = fileContent
747762

internal/file/file_manager_service_test.go

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

561561
func TestFileManagerService_DetermineFileActions(t *testing.T) {
562+
ctx := context.Background()
562563
tempDir := os.TempDir()
563564

564565
deleteTestFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx_delete.conf")
@@ -584,7 +585,6 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
584585

585586
addTestFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx_add.conf")
586587
defer helpers.RemoveFileWithErrorCheck(t, addTestFile.Name())
587-
t.Logf("Adding file: %s", addTestFile.Name())
588588
addFileContent := []byte("test add file")
589589
addErr := os.WriteFile(addTestFile.Name(), addFileContent, 0o600)
590590
require.NoError(t, addErr)
@@ -694,6 +694,18 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
694694
expectedContent: make(map[string][]byte),
695695
expectedError: nil,
696696
},
697+
{
698+
name: "Test 3: File being deleted already doesn't exist",
699+
modifiedFiles: make(map[string]*model.FileCache),
700+
currentFiles: map[string]*mpi.File{
701+
"/unknown/file.conf": {
702+
FileMeta: protos.FileMeta("/unknown/file.conf", files.GenerateHash(fileContent)),
703+
},
704+
},
705+
expectedCache: make(map[string]*model.FileCache),
706+
expectedContent: make(map[string][]byte),
707+
expectedError: nil,
708+
},
697709
}
698710

699711
for _, test := range tests {
@@ -711,8 +723,11 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
711723

712724
require.NoError(tt, err)
713725

714-
diff, contents, fileActionErr := fileManagerService.DetermineFileActions(test.currentFiles,
715-
test.modifiedFiles)
726+
diff, contents, fileActionErr := fileManagerService.DetermineFileActions(
727+
ctx,
728+
test.currentFiles,
729+
test.modifiedFiles,
730+
)
716731
require.NoError(tt, fileActionErr)
717732
assert.Equal(tt, test.expectedContent, contents)
718733
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)