@@ -38,7 +38,7 @@ import (
3838func TestStorageConstructor (t * testing.T ) {
3939 dir := t .TempDir ()
4040
41- if _ , err := NewStorage ("/nonexistent" , "hostname" , time .Minute , 2 ); err == nil {
41+ if _ , err := NewStorage ("/nonexistent" , "hostname" , time .Minute , 2 , 0 ); err == nil {
4242 t .Fatal ("nonexistent path was allowable in storage constructor" )
4343 }
4444
@@ -48,13 +48,13 @@ func TestStorageConstructor(t *testing.T) {
4848 }
4949 f .Close ()
5050
51- if _ , err := NewStorage (f .Name (), "hostname" , time .Minute , 2 ); err == nil {
51+ if _ , err := NewStorage (f .Name (), "hostname" , time .Minute , 2 , 0 ); err == nil {
5252 os .Remove (f .Name ())
5353 t .Fatal ("file path was accepted as basedir" )
5454 }
5555 os .Remove (f .Name ())
5656
57- if _ , err := NewStorage (dir , "hostname" , time .Minute , 2 ); err != nil {
57+ if _ , err := NewStorage (dir , "hostname" , time .Minute , 2 , 0 ); err != nil {
5858 t .Fatalf ("Valid path did not successfully return: %v" , err )
5959 }
6060}
@@ -103,7 +103,7 @@ func walkTar(tarFile string, match string, dir bool) (int64, bool, error) {
103103func TestStorage_Archive (t * testing.T ) {
104104 dir := t .TempDir ()
105105
106- storage , err := NewStorage (dir , "hostname" , time .Minute , 2 )
106+ storage , err := NewStorage (dir , "hostname" , time .Minute , 2 , 0 )
107107 if err != nil {
108108 t .Fatalf ("error while bootstrapping storage: %v" , err )
109109 }
@@ -263,7 +263,7 @@ func TestStorageRemoveAllButCurrent(t *testing.T) {
263263 t .Run ("bad directory in archive" , func (t * testing.T ) {
264264 dir := t .TempDir ()
265265
266- s , err := NewStorage (dir , "hostname" , time .Minute , 2 )
266+ s , err := NewStorage (dir , "hostname" , time .Minute , 2 , 0 )
267267 if err != nil {
268268 t .Fatalf ("Valid path did not successfully return: %v" , err )
269269 }
@@ -277,7 +277,7 @@ func TestStorageRemoveAllButCurrent(t *testing.T) {
277277 g := NewWithT (t )
278278 dir := t .TempDir ()
279279
280- s , err := NewStorage (dir , "hostname" , time .Minute , 2 )
280+ s , err := NewStorage (dir , "hostname" , time .Minute , 2 , 0 )
281281 g .Expect (err ).ToNot (HaveOccurred (), "failed to create new storage" )
282282
283283 artifact := sourcev1.Artifact {
@@ -338,7 +338,7 @@ func TestStorageRemoveAll(t *testing.T) {
338338 g := NewWithT (t )
339339 dir := t .TempDir ()
340340
341- s , err := NewStorage (dir , "hostname" , time .Minute , 2 )
341+ s , err := NewStorage (dir , "hostname" , time .Minute , 2 , 0 )
342342 g .Expect (err ).ToNot (HaveOccurred (), "failed to create new storage" )
343343
344344 artifact := sourcev1.Artifact {
@@ -364,7 +364,7 @@ func TestStorageCopyFromPath(t *testing.T) {
364364
365365 dir := t .TempDir ()
366366
367- storage , err := NewStorage (dir , "hostname" , time .Minute , 2 )
367+ storage , err := NewStorage (dir , "hostname" , time .Minute , 2 , 0 )
368368 if err != nil {
369369 t .Fatalf ("error while bootstrapping storage: %v" , err )
370370 }
@@ -542,7 +542,7 @@ func TestStorage_getGarbageFiles(t *testing.T) {
542542 g := NewWithT (t )
543543 dir := t .TempDir ()
544544
545- s , err := NewStorage (dir , "hostname" , tt .ttl , tt .maxItemsToBeRetained )
545+ s , err := NewStorage (dir , "hostname" , tt .ttl , tt .maxItemsToBeRetained , 0 )
546546 g .Expect (err ).ToNot (HaveOccurred (), "failed to create new storage" )
547547
548548 artifact := sourcev1.Artifact {
@@ -616,7 +616,7 @@ func TestStorage_GarbageCollect(t *testing.T) {
616616 g := NewWithT (t )
617617 dir := t .TempDir ()
618618
619- s , err := NewStorage (dir , "hostname" , time .Second * 2 , 2 )
619+ s , err := NewStorage (dir , "hostname" , time .Second * 2 , 2 , 0 )
620620 g .Expect (err ).ToNot (HaveOccurred (), "failed to create new storage" )
621621
622622 artifact := sourcev1.Artifact {
@@ -658,3 +658,90 @@ func TestStorage_GarbageCollect(t *testing.T) {
658658 })
659659 }
660660}
661+
662+ func TestStorage_MaxSize (t * testing.T ) {
663+ createFiles := func (files map [string ][]byte ) (dir string , err error ) {
664+ dir = t .TempDir ()
665+ for name , b := range files {
666+ absPath := filepath .Join (dir , name )
667+ if err = os .MkdirAll (filepath .Dir (absPath ), 0o750 ); err != nil {
668+ return
669+ }
670+ f , err := os .Create (absPath )
671+ if err != nil {
672+ return "" , fmt .Errorf ("could not create file %q: %w" , absPath , err )
673+ }
674+ if n , err := f .Write (b ); err != nil {
675+ f .Close ()
676+ return "" , fmt .Errorf ("could not write %d bytes to file %q: %w" , n , f .Name (), err )
677+ }
678+ f .Close ()
679+ }
680+ return
681+ }
682+
683+ tests := []struct {
684+ name string
685+ files map [string ][]byte
686+ maxSize int64
687+ wantErrMatch string
688+ }{
689+ {
690+ name : "creates artifact without size limit" ,
691+ files : map [string ][]byte {
692+ "test.txt" : []byte (`contents` ),
693+ "test.yaml" : []byte (`a: b` ),
694+ },
695+ maxSize : - 1 ,
696+ wantErrMatch : "" ,
697+ },
698+ {
699+ name : "fails to create artifact due to size limit" ,
700+ files : map [string ][]byte {
701+ "test.txt" : []byte (`contents` ),
702+ "test.yaml" : []byte (`a: b` ),
703+ },
704+ maxSize : 200 ,
705+ wantErrMatch : "exceeds the max limit" ,
706+ },
707+ {
708+ name : "creates artifact in the size limit range" ,
709+ files : map [string ][]byte {
710+ "test.txt" : []byte (`contents` ),
711+ "test.yaml" : []byte (`a: b` ),
712+ },
713+ maxSize : 300 ,
714+ },
715+ }
716+
717+ for _ , tt := range tests {
718+ t .Run (tt .name , func (t * testing.T ) {
719+ g := NewWithT (t )
720+
721+ dir , err := createFiles (tt .files )
722+ if err != nil {
723+ t .Error (err )
724+ return
725+ }
726+ defer os .RemoveAll (dir )
727+
728+ artifact := sourcev1.Artifact {
729+ Path : filepath .Join (randStringRunes (10 ), randStringRunes (10 ), randStringRunes (10 )+ ".tar.gz" ),
730+ }
731+
732+ s , err := NewStorage (dir , "hostname" , time .Second * 2 , 2 , tt .maxSize )
733+ g .Expect (err ).ToNot (HaveOccurred (), "failed to create new storage" )
734+
735+ if err := s .MkdirAll (artifact ); err != nil {
736+ t .Fatalf ("artifact directory creation failed: %v" , err )
737+ }
738+
739+ err = s .Archive (& artifact , dir , SourceIgnoreFilter (nil , nil ))
740+ if tt .wantErrMatch == "" {
741+ g .Expect (err ).ToNot (HaveOccurred ())
742+ } else {
743+ g .Expect (err .Error ()).To (ContainSubstring (tt .wantErrMatch ))
744+ }
745+ })
746+ }
747+ }
0 commit comments