diff --git a/pkg/extensions/search/convert/convert_test.go b/pkg/extensions/search/convert/convert_test.go index e6a59606..b2ee14b8 100644 --- a/pkg/extensions/search/convert/convert_test.go +++ b/pkg/extensions/search/convert/convert_test.go @@ -6,6 +6,7 @@ import ( "errors" "strconv" "testing" + "time" "github.com/99designs/gqlgen/graphql" godigest "github.com/opencontainers/go-digest" @@ -19,6 +20,7 @@ import ( "zotregistry.io/zot/pkg/extensions/search/common" "zotregistry.io/zot/pkg/extensions/search/convert" cveinfo "zotregistry.io/zot/pkg/extensions/search/cve" + "zotregistry.io/zot/pkg/extensions/search/gql_generated" "zotregistry.io/zot/pkg/log" "zotregistry.io/zot/pkg/meta/bolt" "zotregistry.io/zot/pkg/meta/repodb" @@ -301,6 +303,44 @@ func TestConvertErrors(t *testing.T) { }) } +func TestUpdateLastUpdatedTimestam(t *testing.T) { + Convey("Image summary is the first image checked for the repo", t, func() { + before := time.Time{} + after := time.Date(2023, time.April, 1, 11, 0, 0, 0, time.UTC) + img := convert.UpdateLastUpdatedTimestamp( + &before, + &gql_generated.ImageSummary{LastUpdated: &before}, + &gql_generated.ImageSummary{LastUpdated: &after}, + ) + + So(*img.LastUpdated, ShouldResemble, after) + }) + + Convey("Image summary is updated after the current latest image", t, func() { + before := time.Date(2022, time.April, 1, 11, 0, 0, 0, time.UTC) + after := time.Date(2023, time.April, 1, 11, 0, 0, 0, time.UTC) + img := convert.UpdateLastUpdatedTimestamp( + &before, + &gql_generated.ImageSummary{LastUpdated: &before}, + &gql_generated.ImageSummary{LastUpdated: &after}, + ) + + So(*img.LastUpdated, ShouldResemble, after) + }) + + Convey("Image summary is updated before the current latest image", t, func() { + before := time.Date(2022, time.April, 1, 11, 0, 0, 0, time.UTC) + after := time.Date(2023, time.April, 1, 11, 0, 0, 0, time.UTC) + img := convert.UpdateLastUpdatedTimestamp( + &after, + &gql_generated.ImageSummary{LastUpdated: &after}, + &gql_generated.ImageSummary{LastUpdated: &before}, + ) + + So(*img.LastUpdated, ShouldResemble, after) + }) +} + func TestBuildImageInfo(t *testing.T) { rootDir := t.TempDir() diff --git a/pkg/extensions/search/convert/repodb.go b/pkg/extensions/search/convert/repodb.go index b6fcba0b..a78a27cb 100644 --- a/pkg/extensions/search/convert/repodb.go +++ b/pkg/extensions/search/convert/repodb.go @@ -75,7 +75,7 @@ func RepoMeta2RepoSummary(ctx context.Context, repoMeta repodb.RepoMetadata, repoVendorsSet[*imageSummary.Vendor] = true } - lastUpdatedImageSummary = UpdateLastUpdatedTimestam(&repoLastUpdatedTimestamp, lastUpdatedImageSummary, imageSummary) + lastUpdatedImageSummary = UpdateLastUpdatedTimestamp(&repoLastUpdatedTimestamp, lastUpdatedImageSummary, imageSummary) repoDownloadCount += repoMeta.Statistics[descriptor.Digest].DownloadCount } @@ -134,8 +134,8 @@ func RepoMeta2RepoSummary(ctx context.Context, repoMeta repodb.RepoMetadata, } } -func UpdateLastUpdatedTimestam(repoLastUpdatedTimestamp *time.Time, lastUpdatedImageSummary *gql_generated.ImageSummary, - imageSummary *gql_generated.ImageSummary, +func UpdateLastUpdatedTimestamp(repoLastUpdatedTimestamp *time.Time, + lastUpdatedImageSummary *gql_generated.ImageSummary, imageSummary *gql_generated.ImageSummary, ) *gql_generated.ImageSummary { newLastUpdatedImageSummary := lastUpdatedImageSummary @@ -617,7 +617,7 @@ func RepoMeta2ExpandedRepoInfo(ctx context.Context, repoMeta repodb.RepoMetadata repoVendorsSet[*imageSummary.Vendor] = true } - lastUpdatedImageSummary = UpdateLastUpdatedTimestam(&repoLastUpdatedTimestamp, lastUpdatedImageSummary, imageSummary) + lastUpdatedImageSummary = UpdateLastUpdatedTimestamp(&repoLastUpdatedTimestamp, lastUpdatedImageSummary, imageSummary) repoDownloadCount += *imageSummary.DownloadCount