@@ -11,7 +11,6 @@ import (
1111 "fmt"
1212 "os"
1313 "path/filepath"
14- "sync/atomic"
1514 "testing"
1615
1716 "github.com/nginx/agent/v3/internal/model"
@@ -28,146 +27,6 @@ import (
2827 "github.com/stretchr/testify/require"
2928)
3029
31- func TestFileManagerService_UpdateOverview (t * testing.T ) {
32- ctx := context .Background ()
33-
34- filePath := filepath .Join (t .TempDir (), "nginx.conf" )
35- fileMeta := protos .FileMeta (filePath , "" )
36-
37- fileContent := []byte ("location /test {\n return 200 \" Test location\\ n\" ;\n }" )
38- fileHash := files .GenerateHash (fileContent )
39-
40- fileWriteErr := os .WriteFile (filePath , fileContent , 0o600 )
41- require .NoError (t , fileWriteErr )
42-
43- overview := protos .FileOverview (filePath , fileHash )
44-
45- fakeFileServiceClient := & v1fakes.FakeFileServiceClient {}
46- fakeFileServiceClient .UpdateOverviewReturnsOnCall (0 , & mpi.UpdateOverviewResponse {
47- Overview : overview ,
48- }, nil )
49-
50- fakeFileServiceClient .UpdateOverviewReturnsOnCall (1 , & mpi.UpdateOverviewResponse {}, nil )
51-
52- fakeFileServiceClient .UpdateFileReturns (& mpi.UpdateFileResponse {}, nil )
53-
54- fileManagerService := NewFileManagerService (fakeFileServiceClient , types .AgentConfig ())
55- fileManagerService .SetIsConnected (true )
56-
57- err := fileManagerService .UpdateOverview (ctx , "123" , []* mpi.File {
58- {
59- FileMeta : fileMeta ,
60- },
61- }, 0 )
62-
63- require .NoError (t , err )
64- assert .Equal (t , 2 , fakeFileServiceClient .UpdateOverviewCallCount ())
65- }
66-
67- func TestFileManagerService_UpdateOverview_MaxIterations (t * testing.T ) {
68- ctx := context .Background ()
69-
70- filePath := filepath .Join (t .TempDir (), "nginx.conf" )
71- fileMeta := protos .FileMeta (filePath , "" )
72-
73- fileContent := []byte ("location /test {\n return 200 \" Test location\\ n\" ;\n }" )
74- fileHash := files .GenerateHash (fileContent )
75-
76- fileWriteErr := os .WriteFile (filePath , fileContent , 0o600 )
77- require .NoError (t , fileWriteErr )
78-
79- overview := protos .FileOverview (filePath , fileHash )
80-
81- fakeFileServiceClient := & v1fakes.FakeFileServiceClient {}
82-
83- // do 5 iterations
84- for i := 0 ; i <= 5 ; i ++ {
85- fakeFileServiceClient .UpdateOverviewReturnsOnCall (i , & mpi.UpdateOverviewResponse {
86- Overview : overview ,
87- }, nil )
88- }
89-
90- fakeFileServiceClient .UpdateFileReturns (& mpi.UpdateFileResponse {}, nil )
91-
92- fileManagerService := NewFileManagerService (fakeFileServiceClient , types .AgentConfig ())
93- fileManagerService .SetIsConnected (true )
94-
95- err := fileManagerService .UpdateOverview (ctx , "123" , []* mpi.File {
96- {
97- FileMeta : fileMeta ,
98- },
99- }, 0 )
100-
101- require .Error (t , err )
102- assert .Equal (t , "too many UpdateOverview attempts" , err .Error ())
103- }
104-
105- func TestFileManagerService_UpdateFile (t * testing.T ) {
106- tests := []struct {
107- name string
108- isCert bool
109- }{
110- {
111- name : "non-cert" ,
112- isCert : false ,
113- },
114- {
115- name : "cert" ,
116- isCert : true ,
117- },
118- }
119-
120- tempDir := os .TempDir ()
121-
122- for _ , test := range tests {
123- ctx := context .Background ()
124-
125- testFile := helpers .CreateFileWithErrorCheck (t , tempDir , "nginx.conf" )
126-
127- var fileMeta * mpi.FileMeta
128- if test .isCert {
129- fileMeta = protos .CertMeta (testFile .Name (), "" )
130- } else {
131- fileMeta = protos .FileMeta (testFile .Name (), "" )
132- }
133-
134- fakeFileServiceClient := & v1fakes.FakeFileServiceClient {}
135- fileManagerService := NewFileManagerService (fakeFileServiceClient , types .AgentConfig ())
136- fileManagerService .SetIsConnected (true )
137-
138- err := fileManagerService .UpdateFile (ctx , "123" , & mpi.File {FileMeta : fileMeta })
139-
140- require .NoError (t , err )
141- assert .Equal (t , 1 , fakeFileServiceClient .UpdateFileCallCount ())
142-
143- helpers .RemoveFileWithErrorCheck (t , testFile .Name ())
144- }
145- }
146-
147- func TestFileManagerService_UpdateFile_LargeFile (t * testing.T ) {
148- ctx := context .Background ()
149- tempDir := os .TempDir ()
150-
151- testFile := helpers .CreateFileWithErrorCheck (t , tempDir , "nginx.conf" )
152- writeFileError := os .WriteFile (testFile .Name (), []byte ("#test content" ), 0o600 )
153- require .NoError (t , writeFileError )
154- fileMeta := protos .FileMetaLargeFile (testFile .Name (), "" )
155-
156- fakeFileServiceClient := & v1fakes.FakeFileServiceClient {}
157- fakeClientStreamingClient := & FakeClientStreamingClient {sendCount : atomic.Int32 {}}
158- fakeFileServiceClient .UpdateFileStreamReturns (fakeClientStreamingClient , nil )
159- fileManagerService := NewFileManagerService (fakeFileServiceClient , types .AgentConfig ())
160- fileManagerService .SetIsConnected (true )
161-
162- err := fileManagerService .UpdateFile (ctx , "123" , & mpi.File {FileMeta : fileMeta })
163-
164- require .NoError (t , err )
165- assert .Equal (t , 0 , fakeFileServiceClient .UpdateFileCallCount ())
166- assert .Equal (t , 14 , int (fakeClientStreamingClient .sendCount .Load ()))
167-
168- helpers .RemoveFileWithErrorCheck (t , testFile .Name ())
169- }
170-
17130func TestFileManagerService_ConfigApply_Add (t * testing.T ) {
17231 ctx := context .Background ()
17332 tempDir := t .TempDir ()
0 commit comments