@@ -179,3 +179,94 @@ streams:
179179 errs := ValidateIntegrationPolicyTemplates (fspath .DirFS (d ))
180180 require .Empty (t , errs )
181181}
182+ func TestFindPathWithPattern (t * testing.T ) {
183+ d := t .TempDir ()
184+
185+ dsDir := filepath .ToSlash (path .Join ("data_stream" , "logs" ))
186+ err := os .MkdirAll (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" ), 0o755 )
187+ require .NoError (t , err )
188+
189+ t .Run ("exact match" , func (t * testing.T ) {
190+ templatePath := "exact.yml.hbs"
191+ err = os .WriteFile (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , templatePath ), []byte ("content" ), 0o644 )
192+ require .NoError (t , err )
193+ defer os .Remove (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , templatePath ))
194+
195+ foundFile , err := findPathWithPattern (fspath .DirFS (d ), dsDir , templatePath )
196+ require .NoError (t , err )
197+ require .NotEmpty (t , foundFile )
198+ require .Equal (t , filepath .ToSlash (path .Join (dsDir , "agent" , "stream" , templatePath )), foundFile )
199+ })
200+
201+ t .Run ("match with .link extension" , func (t * testing.T ) {
202+ templatePath := "linked.yml.hbs"
203+ err = os .WriteFile (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , templatePath + ".link" ), []byte ("content" ), 0o644 )
204+ require .NoError (t , err )
205+ defer os .Remove (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , templatePath + ".link" ))
206+
207+ foundFile , err := findPathWithPattern (fspath .DirFS (d ), dsDir , templatePath )
208+ require .NoError (t , err )
209+ require .NotEmpty (t , foundFile )
210+ require .Equal (t , filepath .ToSlash (path .Join (dsDir , "agent" , "stream" , templatePath + ".link" )), foundFile )
211+ })
212+
213+ t .Run ("match with prefix" , func (t * testing.T ) {
214+ templatePath := "stream.yml.hbs"
215+ prefixedFile := "prefixstream.yml.hbs"
216+ err = os .WriteFile (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , prefixedFile ), []byte ("content" ), 0o644 )
217+ require .NoError (t , err )
218+ defer os .Remove (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , prefixedFile ))
219+
220+ foundFile , err := findPathWithPattern (fspath .DirFS (d ), dsDir , templatePath )
221+ require .NoError (t , err )
222+ require .NotEmpty (t , foundFile )
223+ require .Equal (t , filepath .ToSlash (path .Join (dsDir , "agent" , "stream" , prefixedFile )), foundFile )
224+ })
225+
226+ t .Run ("no match found" , func (t * testing.T ) {
227+ templatePath := "nonexistent.yml.hbs"
228+
229+ foundFile , err := findPathWithPattern (fspath .DirFS (d ), dsDir , templatePath )
230+ require .NoError (t , err )
231+ require .Empty (t , foundFile )
232+ })
233+
234+ t .Run ("multiple matches - exact match takes precedence" , func (t * testing.T ) {
235+ templatePath := "multi.yml.hbs"
236+ exactFile := "multi.yml.hbs"
237+ prefixedFile := "prefixmulti.yml.hbs"
238+
239+ err = os .WriteFile (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , exactFile ), []byte ("exact" ), 0o644 )
240+ require .NoError (t , err )
241+ defer os .Remove (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , exactFile ))
242+
243+ err = os .WriteFile (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , prefixedFile ), []byte ("prefixed" ), 0o644 )
244+ require .NoError (t , err )
245+ defer os .Remove (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , prefixedFile ))
246+
247+ foundFile , err := findPathWithPattern (fspath .DirFS (d ), dsDir , templatePath )
248+ require .NoError (t , err )
249+ require .NotEmpty (t , foundFile )
250+ require .Equal (t , filepath .ToSlash (path .Join (dsDir , "agent" , "stream" , exactFile )), foundFile )
251+ })
252+
253+ t .Run ("link file takes precedence over suffix match" , func (t * testing.T ) {
254+ templatePath := "link.yml.hbs"
255+ linkFile := "link.yml.hbs.link"
256+ suffixFile := "prefixlink.yml.hbs"
257+
258+ err = os .WriteFile (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , linkFile ), []byte ("link" ), 0o644 )
259+ require .NoError (t , err )
260+ defer os .Remove (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , linkFile ))
261+
262+ err = os .WriteFile (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , suffixFile ), []byte ("suffix" ), 0o644 )
263+ require .NoError (t , err )
264+ defer os .Remove (filepath .Join (d , "data_stream" , "logs" , "agent" , "stream" , suffixFile ))
265+
266+ foundFile , err := findPathWithPattern (fspath .DirFS (d ), dsDir , templatePath )
267+ require .NoError (t , err )
268+ require .NotEmpty (t , foundFile )
269+ require .Equal (t , filepath .ToSlash (path .Join (dsDir , "agent" , "stream" , linkFile )), foundFile )
270+ })
271+ }
272+
0 commit comments