0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

test(convert): added test for consistent coverage for update last-updated timestamp function (#1299)

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
LaurentiuNiculae 2023-03-29 17:39:15 +03:00 committed by GitHub
parent af819e7b76
commit 0ae35e973a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 4 deletions

View file

@ -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()

View file

@ -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