diff --git a/pkg/extensions/sync/sync_internal_test.go b/pkg/extensions/sync/sync_internal_test.go index a36b784ef..2227755fd 100644 --- a/pkg/extensions/sync/sync_internal_test.go +++ b/pkg/extensions/sync/sync_internal_test.go @@ -649,3 +649,246 @@ func TestFindRepoMatchingContentID(t *testing.T) { } }) } + +func TestCompareManifest(t *testing.T) { + testCases := []struct { + manifest1 ispec.Manifest + manifest2 ispec.Manifest + expected bool + }{ + { + manifest1: ispec.Manifest{ + Config: ispec.Descriptor{ + Digest: "digest1", + }, + }, + manifest2: ispec.Manifest{ + Config: ispec.Descriptor{ + Digest: "digest2", + }, + }, + expected: false, + }, + { + manifest1: ispec.Manifest{ + Config: ispec.Descriptor{ + Digest: "digest", + }, + }, + manifest2: ispec.Manifest{ + Config: ispec.Descriptor{ + Digest: "digest", + }, + }, + expected: true, + }, + { + manifest1: ispec.Manifest{ + Layers: []ispec.Descriptor{{ + Digest: "digest", + Size: 1, + }}, + }, + manifest2: ispec.Manifest{ + Layers: []ispec.Descriptor{{ + Digest: "digest", + Size: 1, + }}, + }, + expected: true, + }, + { + manifest1: ispec.Manifest{ + Layers: []ispec.Descriptor{{ + Digest: "digest1", + Size: 1, + }}, + }, + manifest2: ispec.Manifest{ + Layers: []ispec.Descriptor{{ + Digest: "digest2", + Size: 2, + }}, + }, + expected: false, + }, + { + manifest1: ispec.Manifest{ + Layers: []ispec.Descriptor{ + { + Digest: "digest", + Size: 1, + }, + { + Digest: "digest1", + Size: 1, + }, + }, + }, + manifest2: ispec.Manifest{ + Layers: []ispec.Descriptor{{ + Digest: "digest", + Size: 1, + }}, + }, + expected: false, + }, + { + manifest1: ispec.Manifest{ + Layers: []ispec.Descriptor{ + { + Digest: "digest1", + Size: 1, + }, + { + Digest: "digest2", + Size: 2, + }, + }, + }, + manifest2: ispec.Manifest{ + Layers: []ispec.Descriptor{ + { + Digest: "digest1", + Size: 1, + }, + { + Digest: "digest2", + Size: 2, + }, + }, + }, + expected: true, + }, + { + manifest1: ispec.Manifest{ + Layers: []ispec.Descriptor{ + { + Digest: "digest", + Size: 1, + }, + { + Digest: "digest1", + Size: 1, + }, + }, + }, + manifest2: ispec.Manifest{ + Layers: []ispec.Descriptor{ + { + Digest: "digest", + Size: 1, + }, + { + Digest: "digest2", + Size: 2, + }, + }, + }, + expected: false, + }, + } + + Convey("Test manifestsEqual()", t, func() { + for _, test := range testCases { + actualResult := manifestsEqual(test.manifest1, test.manifest2) + So(actualResult, ShouldEqual, test.expected) + } + }) +} + +func TestCompareArtifactRefs(t *testing.T) { + testCases := []struct { + refs1 []artifactspec.Descriptor + refs2 []artifactspec.Descriptor + expected bool + }{ + { + refs1: []artifactspec.Descriptor{ + { + Digest: "digest1", + }, + }, + refs2: []artifactspec.Descriptor{ + { + Digest: "digest2", + }, + }, + expected: false, + }, + { + refs1: []artifactspec.Descriptor{ + { + Digest: "digest", + }, + }, + refs2: []artifactspec.Descriptor{ + { + Digest: "digest", + }, + }, + expected: true, + }, + { + refs1: []artifactspec.Descriptor{ + { + Digest: "digest", + }, + { + Digest: "digest2", + }, + }, + refs2: []artifactspec.Descriptor{ + { + Digest: "digest", + }, + }, + expected: false, + }, + { + refs1: []artifactspec.Descriptor{ + { + Digest: "digest1", + }, + { + Digest: "digest2", + }, + }, + refs2: []artifactspec.Descriptor{ + { + Digest: "digest1", + }, + { + Digest: "digest2", + }, + }, + expected: true, + }, + { + refs1: []artifactspec.Descriptor{ + { + Digest: "digest", + }, + { + Digest: "digest1", + }, + }, + refs2: []artifactspec.Descriptor{ + { + Digest: "digest1", + }, + { + Digest: "digest2", + }, + }, + expected: false, + }, + } + + Convey("Test manifestsEqual()", t, func() { + for _, test := range testCases { + actualResult := artifactDescriptorsEqual(test.refs1, test.refs2) + So(actualResult, ShouldEqual, test.expected) + } + }) +} diff --git a/pkg/extensions/sync/utils.go b/pkg/extensions/sync/utils.go index 3a9bc672e..22c243ca9 100644 --- a/pkg/extensions/sync/utils.go +++ b/pkg/extensions/sync/utils.go @@ -412,9 +412,8 @@ func canSkipImage(repo, tag, digest string, imageStore storage.ImageStore, log l func manifestsEqual(manifest1, manifest2 ispec.Manifest) bool { if manifest1.Config.Digest == manifest2.Config.Digest && manifest1.Config.MediaType == manifest2.Config.MediaType && - manifest1.Config.Size == manifest2.Config.Size && - len(manifest1.Layers) == len(manifest2.Layers) { - if descriptorEqual(manifest1.Layers, manifest2.Layers) { + manifest1.Config.Size == manifest2.Config.Size { + if descriptorsEqual(manifest1.Layers, manifest2.Layers) { return true } } @@ -423,31 +422,35 @@ func manifestsEqual(manifest1, manifest2 ispec.Manifest) bool { } func artifactDescriptorsEqual(desc1, desc2 []artifactspec.Descriptor) bool { - if len(desc1) == len(desc2) { - for id, desc := range desc1 { - if desc.Digest == desc2[id].Digest && - desc.Size == desc2[id].Size && - desc.MediaType == desc2[id].MediaType && - desc.ArtifactType == desc2[id].ArtifactType { - return true - } + if len(desc1) != len(desc2) { + return false + } + + for id, desc := range desc1 { + if desc.Digest != desc2[id].Digest || + desc.Size != desc2[id].Size || + desc.MediaType != desc2[id].MediaType || + desc.ArtifactType != desc2[id].ArtifactType { + return false } } - return false + return true } -func descriptorEqual(desc1, desc2 []ispec.Descriptor) bool { - if len(desc1) == len(desc2) { - for id, desc := range desc1 { - if desc.Digest == desc2[id].Digest && - desc.Size == desc2[id].Size && - desc.MediaType == desc2[id].MediaType && - desc.Annotations[static.SignatureAnnotationKey] == desc2[id].Annotations[static.SignatureAnnotationKey] { - return true - } +func descriptorsEqual(desc1, desc2 []ispec.Descriptor) bool { + if len(desc1) != len(desc2) { + return false + } + + for id, desc := range desc1 { + if desc.Digest != desc2[id].Digest || + desc.Size != desc2[id].Size || + desc.MediaType != desc2[id].MediaType || + desc.Annotations[static.SignatureAnnotationKey] != desc2[id].Annotations[static.SignatureAnnotationKey] { + return false } } - return false + return true }