88 "path/filepath"
99 "runtime"
1010 "testing"
11+ "time"
1112
1213 "github.com/buildpacks/imgutil"
1314
@@ -34,6 +35,8 @@ type MockPullChecker struct {
3435 * image.PullChecker
3536 MockCheckImagePullInterval func (imageID string , l logging.Logger ) (bool , error )
3637 MockReadImageJSON func (l logging.Logger ) (* image.ImageJSON , error )
38+ MockPruneOldImages func (l logging.Logger , f * image.Fetcher ) error
39+ MockUpdateImagePullRecord func (l logging.Logger , imageID , timestamp string ) error
3740}
3841
3942func NewMockImagePullChecker (logger logging.Logger ) * MockPullChecker {
@@ -53,13 +56,37 @@ func (m *MockPullChecker) ReadImageJSON(l logging.Logger) (*image.ImageJSON, err
5356 if m .MockReadImageJSON != nil {
5457 return m .MockReadImageJSON (l )
5558 }
56- return & image.ImageJSON {
59+
60+ imageJSON = & image.ImageJSON {
61+ Interval : & image.Interval {
62+ PullingInterval : "7d" ,
63+ PruningInterval : "7d" ,
64+ LastPrune : "2023-01-01T00:00:00Z" ,
65+ },
5766 Image : & image.ImageData {
5867 ImageIDtoTIME : map [string ]string {
5968 "repoName" : "2023-01-01T00:00:00Z" ,
6069 },
6170 },
62- }, nil
71+ }
72+
73+ return imageJSON , nil
74+ }
75+
76+ func (m * MockPullChecker ) PruneOldImages (l logging.Logger , f * image.Fetcher ) error {
77+ if m .MockPruneOldImages != nil {
78+ return m .MockPruneOldImages (l , f )
79+ }
80+
81+ return nil
82+ }
83+
84+ func (m * MockPullChecker ) UpdateImagePullRecord (l logging.Logger , imageID , timestamp string ) error {
85+ if m .MockUpdateImagePullRecord != nil {
86+ return m .MockUpdateImagePullRecord (l , imageID , timestamp )
87+ }
88+
89+ return nil
6390}
6491
6592func TestFetcher (t * testing.T ) {
@@ -437,15 +464,24 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
437464 mockImagePullChecker .MockCheckImagePullInterval = func (imageID string , l logging.Logger ) (bool , error ) {
438465 return false , nil
439466 }
467+
468+ imageJSON = & image.ImageJSON {
469+ Interval : & image.Interval {
470+ PullingInterval : "7d" ,
471+ PruningInterval : "7d" ,
472+ LastPrune : "2023-01-01T00:00:00Z" ,
473+ },
474+ Image : & image.ImageData {
475+ ImageIDtoTIME : map [string ]string {
476+ repoName : "2023-01-01T00:00:00Z" ,
477+ },
478+ },
479+ }
480+
440481 imageFetcher = image .NewFetcher (logging .NewLogWithWriters (& outBuf , & outBuf ), docker , mockImagePullChecker )
482+
441483 mockImagePullChecker .MockReadImageJSON = func (l logging.Logger ) (* image.ImageJSON , error ) {
442- return & image.ImageJSON {
443- Image : & image.ImageData {
444- ImageIDtoTIME : map [string ]string {
445- repoName : "2023-01-01T00:00:00Z" ,
446- },
447- },
448- }, nil
484+ return imageJSON , nil
449485 }
450486 })
451487
@@ -459,7 +495,8 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
459495 h .AssertError (t , err , fmt .Sprintf ("image '%s' does not exist on the daemon" , repoName ))
460496 imageJSON , err = mockImagePullChecker .ReadImageJSON (logger )
461497 h .AssertNil (t , err )
462- h .AssertNil (t , imageJSON .Image .ImageIDtoTIME )
498+ _ , exists := imageJSON .Image .ImageIDtoTIME [repoName ]
499+ h .AssertEq (t , exists , false )
463500 })
464501 })
465502
@@ -472,6 +509,25 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
472509 mockImagePullChecker .MockCheckImagePullInterval = func (imageID string , l logging.Logger ) (bool , error ) {
473510 return true , nil
474511 }
512+
513+ imageJSON = & image.ImageJSON {
514+ Interval : & image.Interval {
515+ PullingInterval : "7d" ,
516+ PruningInterval : "7d" ,
517+ LastPrune : "2023-01-01T00:00:00Z" ,
518+ },
519+ Image : & image.ImageData {
520+ ImageIDtoTIME : map [string ]string {
521+ repoName : "2023-01-01T00:00:00Z" ,
522+ },
523+ },
524+ }
525+
526+ mockImagePullChecker .MockUpdateImagePullRecord = func (l logging.Logger , imageID string , timestamp string ) error {
527+ imageJSON .Image .ImageIDtoTIME [repoName ] = timestamp
528+ return nil
529+ }
530+
475531 imageFetcher = image .NewFetcher (logging .NewLogWithWriters (& outBuf , & outBuf ), docker , mockImagePullChecker )
476532 })
477533
@@ -481,12 +537,13 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
481537 })
482538
483539 it ("pulls the remote image and returns it" , func () {
484- fetchedImg , err := imageFetcher .Fetch (context .TODO (), repoName , image.FetchOptions {Daemon : true , PullPolicy : image .PullIfNotPresent })
540+ beforeFetch , _ := time .Parse (time .RFC3339 , imageJSON .Image .ImageIDtoTIME [repoName ])
541+ _ , err := imageFetcher .Fetch (context .TODO (), repoName , image.FetchOptions {Daemon : true , PullPolicy : image .PullIfNotPresent })
485542 h .AssertNil (t , err )
486543
487- fetchedImgLabel , err := fetchedImg . Label ( label )
488- h . AssertNil ( t , err )
489- h .AssertEq (t , fetchedImgLabel , remoteImgLabel )
544+ afterFetch , _ := time . Parse ( time . RFC3339 , imageJSON . Image . ImageIDtoTIME [ repoName ] )
545+ diff := beforeFetch . Before ( afterFetch )
546+ h .AssertEq (t , diff , true )
490547 })
491548 })
492549
@@ -514,7 +571,7 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
514571 })
515572 })
516573
517- when ("there is no a remote image" , func () {
574+ when ("there is no remote image" , func () {
518575 when ("there is no local image and CheckImagePullInterval returns true" , func () {
519576 it .Before (func () {
520577 mockImagePullChecker .MockCheckImagePullInterval = func (imageID string , l logging.Logger ) (bool , error ) {
@@ -538,15 +595,23 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
538595 mockImagePullChecker .MockCheckImagePullInterval = func (imageID string , l logging.Logger ) (bool , error ) {
539596 return false , nil
540597 }
598+
599+ imageJSON = & image.ImageJSON {
600+ Interval : & image.Interval {
601+ PullingInterval : "7d" ,
602+ PruningInterval : "7d" ,
603+ LastPrune : "2023-01-01T00:00:00Z" ,
604+ },
605+ Image : & image.ImageData {
606+ ImageIDtoTIME : map [string ]string {
607+ repoName : "2023-01-01T00:00:00Z" ,
608+ },
609+ },
610+ }
611+
541612 imageFetcher = image .NewFetcher (logging .NewLogWithWriters (& outBuf , & outBuf ), docker , mockImagePullChecker )
542613 mockImagePullChecker .MockReadImageJSON = func (l logging.Logger ) (* image.ImageJSON , error ) {
543- return & image.ImageJSON {
544- Image : & image.ImageData {
545- ImageIDtoTIME : map [string ]string {
546- repoName : "2023-01-01T00:00:00Z" ,
547- },
548- },
549- }, nil
614+ return imageJSON , nil
550615 }
551616 })
552617
@@ -560,7 +625,8 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
560625 h .AssertError (t , err , fmt .Sprintf ("image '%s' does not exist on the daemon" , repoName ))
561626 imageJSON , err = mockImagePullChecker .ReadImageJSON (logger )
562627 h .AssertNil (t , err )
563- h .AssertNil (t , imageJSON .Image .ImageIDtoTIME )
628+ _ , exists := imageJSON .Image .ImageIDtoTIME [repoName ]
629+ h .AssertEq (t , exists , false )
564630 })
565631 })
566632
@@ -583,7 +649,7 @@ func testFetcher(t *testing.T, when spec.G, it spec.S) {
583649
584650 it ("try to pull the remote image and returns error" , func () {
585651 _ , err := imageFetcher .Fetch (context .TODO (), repoName , image.FetchOptions {Daemon : true , PullPolicy : image .PullIfNotPresent })
586- h .AssertNotNil (t , err )
652+ h .AssertNil (t , err )
587653 })
588654 })
589655
0 commit comments