@@ -59,6 +59,7 @@ func TestFileManagerService_ConfigApply_Add(t *testing.T) {
5959 agentConfig .AllowedDirectories = []string {tempDir }
6060
6161 fileManagerService := NewFileManagerService (fakeFileServiceClient , agentConfig , & sync.RWMutex {})
62+ fileManagerService .configPath = filepath .Dir (filePath )
6263 fileManagerService .agentConfig .LibDir = manifestDirPath
6364 fileManagerService .manifestFilePath = manifestFilePath
6465
@@ -108,6 +109,7 @@ func TestFileManagerService_ConfigApply_Add_LargeFile(t *testing.T) {
108109 agentConfig .AllowedDirectories = []string {tempDir }
109110 fileManagerService := NewFileManagerService (fakeFileServiceClient , agentConfig , & sync.RWMutex {})
110111 fileManagerService .agentConfig .LibDir = manifestDirPath
112+ fileManagerService .configPath = filepath .Dir (filePath )
111113 fileManagerService .manifestFilePath = manifestFilePath
112114
113115 request := protos .CreateConfigApplyRequest (overview )
@@ -168,6 +170,7 @@ func TestFileManagerService_ConfigApply_Update(t *testing.T) {
168170
169171 fileManagerService := NewFileManagerService (fakeFileServiceClient , agentConfig , & sync.RWMutex {})
170172 fileManagerService .agentConfig .LibDir = manifestDirPath
173+ fileManagerService .configPath = filepath .Dir (tempFile .Name ())
171174 fileManagerService .manifestFilePath = manifestFilePath
172175 err := fileManagerService .UpdateCurrentFilesOnDisk (ctx , filesOnDisk , false )
173176 require .NoError (t , err )
@@ -179,7 +182,11 @@ func TestFileManagerService_ConfigApply_Update(t *testing.T) {
179182 data , readErr := os .ReadFile (tempFile .Name ())
180183 require .NoError (t , readErr )
181184 assert .Equal (t , fileContent , data )
182- assert .Equal (t , fileManagerService .rollbackFileContents [tempFile .Name ()], previousFileContent )
185+
186+ content , err := os .ReadFile (fileManagerService .tempRollbackDir + tempFile .Name ())
187+ require .NoError (t , err )
188+ assert .Equal (t , previousFileContent , content )
189+
183190 assert .Equal (t , fileManagerService .fileActions [tempFile .Name ()].File , overview .GetFiles ()[0 ])
184191 assert .True (t , fileManagerService .rollbackManifest )
185192}
@@ -219,6 +226,7 @@ func TestFileManagerService_ConfigApply_Delete(t *testing.T) {
219226 fileManagerService := NewFileManagerService (fakeFileServiceClient , agentConfig , & sync.RWMutex {})
220227 fileManagerService .agentConfig .LibDir = manifestDirPath
221228 fileManagerService .manifestFilePath = manifestFilePath
229+ fileManagerService .configPath = filepath .Dir (tempFile .Name ())
222230 err := fileManagerService .UpdateCurrentFilesOnDisk (ctx , filesOnDisk , false )
223231 require .NoError (t , err )
224232
@@ -236,7 +244,11 @@ func TestFileManagerService_ConfigApply_Delete(t *testing.T) {
236244 writeStatus , err := fileManagerService .ConfigApply (ctx , request )
237245 require .NoError (t , err )
238246 assert .NoFileExists (t , tempFile .Name ())
239- assert .Equal (t , fileManagerService .rollbackFileContents [tempFile .Name ()], fileContent )
247+
248+ content , err := os .ReadFile (fileManagerService .tempRollbackDir + tempFile .Name ())
249+ require .NoError (t , err )
250+ assert .Equal (t , fileContent , content )
251+
240252 assert .Equal (t ,
241253 fileManagerService .fileActions [tempFile .Name ()].File .GetFileMeta ().GetName (),
242254 filesOnDisk [tempFile .Name ()].GetFileMeta ().GetName (),
@@ -278,6 +290,7 @@ func TestFileManagerService_ConfigApply_Failed(t *testing.T) {
278290
279291 fileManagerService := NewFileManagerService (fakeFileServiceClient , agentConfig , & sync.RWMutex {})
280292 fileManagerService .agentConfig .LibDir = manifestDirPath
293+ fileManagerService .configPath = filepath .Dir (filePath )
281294 fileManagerService .manifestFilePath = manifestFilePath
282295
283296 request := protos .CreateConfigApplyRequest (overview )
@@ -322,9 +335,18 @@ func TestFileManagerService_checkAllowedDirectory(t *testing.T) {
322335 require .Error (t , err )
323336}
324337
338+ //nolint:usetesting // need to use MkDirTemp instead of t.tempDir for rollback as t.tempDir does not accept a pattern
325339func TestFileManagerService_ClearCache (t * testing.T ) {
340+ tempDir := t .TempDir ()
341+ rollbackDir , err := os .MkdirTemp (tempDir , "rollback" )
342+ require .NoError (t , err )
343+ configDir , err := os .MkdirTemp (tempDir , "config" )
344+ require .NoError (t , err )
345+
326346 fakeFileServiceClient := & v1fakes.FakeFileServiceClient {}
327347 fileManagerService := NewFileManagerService (fakeFileServiceClient , types .AgentConfig (), & sync.RWMutex {})
348+ fileManagerService .tempConfigDir = configDir
349+ fileManagerService .tempRollbackDir = rollbackDir
328350
329351 filesCache := map [string ]* model.FileCache {
330352 "file/path/test.conf" : {
@@ -340,25 +362,27 @@ func TestFileManagerService_ClearCache(t *testing.T) {
340362 },
341363 }
342364
343- contentsCache := map [string ][]byte {
344- "file/path/test.conf" : []byte ("some test data" ),
345- }
346-
347365 fileManagerService .fileActions = filesCache
348- fileManagerService .rollbackFileContents = contentsCache
349366 assert .NotEmpty (t , fileManagerService .fileActions )
350- assert .NotEmpty (t , fileManagerService .rollbackFileContents )
351367
352368 fileManagerService .ClearCache ()
353369
354370 assert .Empty (t , fileManagerService .fileActions )
355- assert .Empty (t , fileManagerService .rollbackFileContents )
371+
372+ _ , statErr := os .Stat (fileManagerService .tempRollbackDir )
373+ assert .True (t , os .IsNotExist (statErr ))
374+ _ , statConfigErr := os .Stat (fileManagerService .tempConfigDir )
375+ assert .True (t , os .IsNotExist (statConfigErr ))
356376}
357377
378+ //nolint:usetesting // need to use MkDirTemp instead of t.tempDir for rollback as t.tempDir does not accept a pattern
358379func TestFileManagerService_Rollback (t * testing.T ) {
359380 ctx := context .Background ()
360381 tempDir := t .TempDir ()
361382
383+ rollbackDir , mkdirErr := os .MkdirTemp (tempDir , "rollback" )
384+ require .NoError (t , mkdirErr )
385+
362386 deleteFilePath := filepath .Join (tempDir , "nginx_delete.conf" )
363387
364388 newFileContent := []byte ("location /test {\n return 200 \" This config needs to be rolled back\\ n\" ;\n }" )
@@ -373,6 +397,25 @@ func TestFileManagerService_Rollback(t *testing.T) {
373397 _ , writeErr = updateFile .Write (newFileContent )
374398 require .NoError (t , writeErr )
375399
400+ helpers .CreateDirWithErrorCheck (t , rollbackDir + tempDir )
401+
402+ tempAddFile , createErr := os .Create (rollbackDir + addFile .Name ())
403+ require .NoError (t , createErr )
404+ _ , writeErr = tempAddFile .Write (oldFileContent )
405+ require .NoError (t , writeErr )
406+
407+ tempUpdateFile , createErr := os .Create (rollbackDir + updateFile .Name ())
408+ require .NoError (t , createErr )
409+ _ , writeErr = tempUpdateFile .Write (oldFileContent )
410+ require .NoError (t , writeErr )
411+ t .Log (tempUpdateFile .Name ())
412+
413+ tempDeleteFile , createErr := os .Create (rollbackDir + tempDir + "/nginx_delete.conf" )
414+ require .NoError (t , createErr )
415+ _ , writeErr = tempDeleteFile .Write (oldFileContent )
416+ require .NoError (t , writeErr )
417+ t .Log (tempDeleteFile .Name ())
418+
376419 manifestDirPath := tempDir
377420 manifestFilePath := manifestDirPath + "/manifest.json"
378421 helpers .CreateFileWithErrorCheck (t , manifestDirPath , "manifest.json" )
@@ -430,17 +473,14 @@ func TestFileManagerService_Rollback(t *testing.T) {
430473 },
431474 },
432475 }
433- fileContentCache := map [string ][]byte {
434- deleteFilePath : oldFileContent ,
435- updateFile .Name (): oldFileContent ,
436- }
437476
438477 instanceID := protos .NginxOssInstance ([]string {}).GetInstanceMeta ().GetInstanceId ()
439478 fakeFileServiceClient := & v1fakes.FakeFileServiceClient {}
440479 fileManagerService := NewFileManagerService (fakeFileServiceClient , types .AgentConfig (), & sync.RWMutex {})
441- fileManagerService .rollbackFileContents = fileContentCache
442480 fileManagerService .fileActions = filesCache
443481 fileManagerService .agentConfig .LibDir = manifestDirPath
482+ fileManagerService .tempRollbackDir = rollbackDir
483+ fileManagerService .configPath = filepath .Dir (updateFile .Name ())
444484 fileManagerService .manifestFilePath = manifestFilePath
445485
446486 err := fileManagerService .Rollback (ctx , instanceID )
@@ -622,6 +662,7 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
622662 fileManagerService := NewFileManagerService (fakeFileServiceClient , types .AgentConfig (), & sync.RWMutex {})
623663 fileManagerService .agentConfig .LibDir = manifestDirPath
624664 fileManagerService .manifestFilePath = manifestFilePath
665+ fileManagerService .configPath = filepath .Dir (updateTestFile .Name ())
625666
626667 require .NoError (tt , err )
627668
@@ -1013,20 +1054,22 @@ func TestFileManagerService_deleteTempFiles(t *testing.T) {
10131054
10141055func TestFileManagerService_createTempConfigDirectory (t * testing.T ) {
10151056 agentConfig := types .AgentConfig ()
1016- agentConfig .LibDir = t .TempDir ()
1057+ tempDir := t .TempDir ()
1058+ configPath := tempDir
10171059
10181060 fileManagerService := FileManagerService {
10191061 agentConfig : agentConfig ,
1062+ configPath : configPath ,
10201063 }
10211064
1022- dir , err := fileManagerService .createTempConfigDirectory (t . Context (), "config" )
1065+ dir , err := fileManagerService .createTempConfigDirectory ("config" )
10231066 assert .NotEmpty (t , dir )
10241067 require .NoError (t , err )
10251068
10261069 // Test for unknown directory path
1027- agentConfig . LibDir = "/unknown/"
1070+ fileManagerService . configPath = "/unknown/"
10281071
1029- dir , err = fileManagerService .createTempConfigDirectory (t . Context (), "config" )
1072+ dir , err = fileManagerService .createTempConfigDirectory ("config" )
10301073 assert .Empty (t , dir )
10311074 require .Error (t , err )
10321075}
0 commit comments