@@ -259,3 +259,108 @@ func TestStorageRemoveAllButCurrent(t *testing.T) {
259259 }
260260 })
261261}
262+
263+ func TestStorageCopyFromPath (t * testing.T ) {
264+ type File struct {
265+ Name string
266+ Content []byte
267+ }
268+
269+ dir , err := createStoragePath ()
270+ if err != nil {
271+ t .Fatal (err )
272+ }
273+ t .Cleanup (cleanupStoragePath (dir ))
274+
275+ storage , err := NewStorage (dir , "hostname" , time .Minute )
276+ if err != nil {
277+ t .Fatalf ("error while bootstrapping storage: %v" , err )
278+ }
279+
280+ createFile := func (file * File ) (absPath string , err error ) {
281+ defer func () {
282+ if err != nil && dir != "" {
283+ os .RemoveAll (dir )
284+ }
285+ }()
286+ dir , err = os .MkdirTemp ("" , "test-files-" )
287+ if err != nil {
288+ return
289+ }
290+ absPath = filepath .Join (dir , file .Name )
291+ if err = os .MkdirAll (filepath .Dir (absPath ), 0755 ); err != nil {
292+ return
293+ }
294+ f , err := os .Create (absPath )
295+ if err != nil {
296+ return "" , fmt .Errorf ("could not create file %q: %w" , absPath , err )
297+ }
298+ if n , err := f .Write (file .Content ); err != nil {
299+ f .Close ()
300+ return "" , fmt .Errorf ("could not write %d bytes to file %q: %w" , n , f .Name (), err )
301+ }
302+ f .Close ()
303+ return
304+ }
305+
306+ matchFile := func (t * testing.T , storage * Storage , artifact sourcev1.Artifact , file * File , wantErr bool ) {
307+ c , err := os .ReadFile (storage .LocalPath (artifact ))
308+ if err != nil {
309+ t .Fatalf ("failed reading file: %v" , err )
310+ }
311+ if (string (c ) != string (file .Content )) != wantErr {
312+ t .Errorf ("artifact content does not match, got: %q, want: %q" , string (c ), string (file .Content ))
313+ }
314+ }
315+
316+ tests := []struct {
317+ name string
318+ file * File
319+ want * File
320+ wantErr bool
321+ }{
322+ {
323+ name : "content match" ,
324+ file : & File {
325+ Name : "manifest.yaml" ,
326+ Content : []byte (`contents` ),
327+ },
328+ want : & File {
329+ Name : "manifest.yaml" ,
330+ Content : []byte (`contents` ),
331+ },
332+ },
333+ {
334+ name : "content not match" ,
335+ file : & File {
336+ Name : "manifest.yaml" ,
337+ Content : []byte (`contents` ),
338+ },
339+ want : & File {
340+ Name : "manifest.yaml" ,
341+ Content : []byte (`bad contents` ),
342+ },
343+ wantErr : true ,
344+ },
345+ }
346+ for _ , tt := range tests {
347+ t .Run (tt .name , func (t * testing.T ) {
348+ absPath , err := createFile (tt .file )
349+ if err != nil {
350+ t .Error (err )
351+ return
352+ }
353+ defer os .RemoveAll (absPath )
354+ artifact := sourcev1.Artifact {
355+ Path : filepath .Join (randStringRunes (10 ), randStringRunes (10 ), randStringRunes (10 )),
356+ }
357+ if err := storage .MkdirAll (artifact ); err != nil {
358+ t .Fatalf ("artifact directory creation failed: %v" , err )
359+ }
360+ if err := storage .CopyFromPath (& artifact , absPath ); err != nil {
361+ t .Errorf ("CopyFromPath() error = %v" , err )
362+ }
363+ matchFile (t , storage , artifact , tt .want , tt .wantErr )
364+ })
365+ }
366+ }
0 commit comments