Skip to content

Commit cfb00e7

Browse files
Merge pull request #16 from blackducksoftware/ac_refactor_descriptor_parser
Refactor OciManifestorDescriptorParser, add tests for OciManifestorDe…
2 parents 4404905 + 3d6ac00 commit cfb00e7

File tree

7 files changed

+107
-38
lines changed

7 files changed

+107
-38
lines changed

src/main/java/com/synopsys/integration/blackduck/imageinspector/image/common/ManifestRepoTagMatcher.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,31 @@ public class ManifestRepoTagMatcher {
2020
public Optional<String> findMatch(List<String> manifestRepoTags, String targetRepoTag) {
2121
logger.debug(String.format("findRepoTag(): specifiedRepoTag: %s", targetRepoTag));
2222
for (final String repoTag : manifestRepoTags) {
23-
logger.trace(String.format("Target repo tag %s; checking %s", targetRepoTag, repoTag));
24-
if (StringUtils.compare(repoTag, targetRepoTag) == 0) {
25-
logger.trace(String.format("Found the targetRepoTag %s", targetRepoTag));
26-
return Optional.of(repoTag);
27-
}
28-
if (targetRepoTag.endsWith("/" + repoTag)) {
29-
logger.trace(String.format("Matched the targetRepoTag %s to %s by ignoring the repository prefix", targetRepoTag, repoTag));
23+
if (doesMatch(repoTag, targetRepoTag)) {
3024
return Optional.of(repoTag);
3125
}
3226
}
3327
return Optional.empty();
3428
}
29+
30+
public Optional<String> findMatch(String manifestRepoTag, String targetRepoTag) {
31+
if (doesMatch(manifestRepoTag, targetRepoTag)) {
32+
return Optional.of(manifestRepoTag);
33+
} else {
34+
return Optional.empty();
35+
}
36+
}
37+
38+
private boolean doesMatch(String manifestRepoTag, String targetRepoTag) {
39+
logger.trace(String.format("Target repo tag %s; checking %s", targetRepoTag, manifestRepoTag));
40+
if (StringUtils.compare(manifestRepoTag, targetRepoTag) == 0) {
41+
logger.trace(String.format("Found the targetRepoTag %s", targetRepoTag));
42+
return true;
43+
}
44+
if (targetRepoTag.endsWith("/" + manifestRepoTag)) {
45+
logger.trace(String.format("Matched the targetRepoTag %s to %s by ignoring the repository prefix", targetRepoTag, manifestRepoTag));
46+
return true;
47+
}
48+
return false;
49+
}
3550
}

src/main/java/com/synopsys/integration/blackduck/imageinspector/image/oci/OciImageDirectoryExtractor.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,6 @@ private File findBlob(File blobsDir, String pathToBlob) throws IntegrationExcept
178178
return blob;
179179
}
180180

181-
private String ensurePopulated(String field, String defaultValue) {
182-
if (StringUtils.isBlank(field)) {
183-
field = defaultValue;
184-
}
185-
return field;
186-
}
187-
188181
private String findImageConfigFilePath(OciImageManifest imageManifest) throws IntegrationException {
189182
OciDescriptor imageConfig = imageManifest.getConfig();
190183
if (imageConfig.getMediaType().equals(CONFIG_FILE_MEDIA_TYPE)) {

src/main/java/com/synopsys/integration/blackduck/imageinspector/image/oci/OciManifestDescriptorParser.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public OciManifestDescriptorParser(ManifestRepoTagMatcher manifestRepoTagMatcher
2828

2929
public OciDescriptor getManifestDescriptor(OciImageIndex ociImageIndex,
3030
@Nullable String givenRepo, @Nullable String givenTag) throws IntegrationException {
31-
// Probably also need to select one of multiple based on arch
31+
// TODO- Probably also need to select one of multiple based on arch
3232
List<OciDescriptor> trueManifests =
3333
ociImageIndex.getManifests().stream()
3434
.filter(man -> MANIFEST_FILE_MEDIA_TYPE.equals(man.getMediaType()))
@@ -42,32 +42,16 @@ public OciDescriptor getManifestDescriptor(OciImageIndex ociImageIndex,
4242
if (StringUtils.isBlank(givenTag)) {
4343
givenTag = "latest";
4444
}
45-
// Both repo and tag have values
45+
// Safe to assume both repo and tag have values
4646
String givenRepoTag = String.format("%s:%s", givenRepo, givenTag);
4747

48-
// TODO is there a simpler way to do this?
49-
List <String> manifestRepTagStrings = trueManifests.stream()
50-
.map(OciDescriptor::getRepoTagString)
51-
.filter(Optional::isPresent)
52-
.map(Optional::get)
53-
.collect(Collectors.toList());
54-
Optional<String> matchingManifestRepoTagString = manifestRepoTagMatcher.findMatch(manifestRepTagStrings, givenRepoTag);
55-
if (!matchingManifestRepoTagString.isPresent()) {
56-
throw new IntegrationException(String.format("No manifest found matching given repo:tag: %s", givenRepoTag));
57-
}
58-
// We know which repo:tag we want; return manifest matching that
5948
Optional<OciDescriptor> matchingManifest = trueManifests.stream()
60-
.filter(m -> m.getRepoTagString().isPresent())
61-
.filter(m -> m.getRepoTagString().get().equals(matchingManifestRepoTagString.get()))
62-
.findFirst();
49+
.filter(m -> m.getRepoTagString().isPresent())
50+
.filter(m -> manifestRepoTagMatcher.findMatch(m.getRepoTagString().get(), givenRepoTag).isPresent())
51+
.findFirst();
6352
if (!matchingManifest.isPresent()) {
64-
throw new IntegrationException(String.format("Unable to find manifest matching repo:tag: %s", matchingManifestRepoTagString.get()));
53+
throw new IntegrationException(String.format("Unable to find manifest matching repo:tag: %s", givenRepoTag));
6554
}
6655
return matchingManifest.get();
6756
}
68-
69-
public String getManifestFileDigest(OciImageIndex ociImageIndex,
70-
@Nullable String givenRepo, @Nullable String givenTag) throws IntegrationException {
71-
return getManifestDescriptor(ociImageIndex, givenRepo, givenTag).getDigest();
72-
}
7357
}

src/main/java/com/synopsys/integration/blackduck/imageinspector/image/oci/model/OciDescriptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public class OciDescriptor {
2929
@SerializedName("annotations")
3030
private Map<String, String> annotations;
3131

32-
public OciDescriptor(final String mediaType, final String digest, final String size) {
32+
public OciDescriptor(final String mediaType, final String digest, final String size, final Map<String, String> annotations) {
3333
this.mediaType = mediaType;
3434
this.digest = digest;
3535
this.size = size;
36+
this.annotations = annotations;
3637
}
3738

3839
public String getMediaType() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.synopsys.integration.blackduck.imageinspector.image.common;
2+
3+
import static org.junit.jupiter.params.provider.Arguments.arguments;
4+
5+
import java.util.stream.Stream;
6+
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class ManifestRepoTagMatcherTest {
13+
@ParameterizedTest
14+
@MethodSource("testFindMatchProvider")
15+
public void testFindMatch(String manifestRepoTag, String targetRepoTag, boolean matches) {
16+
ManifestRepoTagMatcher matcher = new ManifestRepoTagMatcher();
17+
Assertions.assertEquals(matches, matcher.findMatch(manifestRepoTag, targetRepoTag).isPresent());
18+
}
19+
20+
private static Stream<Arguments> testFindMatchProvider() {
21+
return Stream.of(
22+
arguments("repo:tag", "org/repo:tag", true),
23+
arguments("repo:tag", "repo:tag", true),
24+
arguments("org/repo", "repo:tag", false),
25+
arguments("org/repo:tag", "repo:tag", false) //TODO- should this be expected?
26+
);
27+
}
28+
}

src/test/java/com/synopsys/integration/blackduck/imageinspector/image/oci/OciImageIndexFileParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void testManifestDigest() throws IntegrationException {
2626
// TODO this test is now cluttered / testing different classes
2727
// TODO need a test class for OciManifestDescriptorParser
2828
OciManifestDescriptorParser ociManifestDescriptorParser = new OciManifestDescriptorParser(new ManifestRepoTagMatcher());
29-
String manifestDigest = ociManifestDescriptorParser.getManifestFileDigest(ociImageIndex, "", "");
29+
String manifestDigest = ociManifestDescriptorParser.getManifestDescriptor(ociImageIndex, "", "").getDigest();
3030
//assertEquals(1, ociImageIndex.getManifests().size());
3131
//String manifestDigest = ociImageIndexFileParser.parseManifestFileDigestFromImageIndex(ociImageIndex);
3232
assertEquals("sha256:8bd1d67ebe6aeae405d824c21560ec9aa2371ed48aa0c4a833e4672cadb0cf3e", manifestDigest);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.synopsys.integration.blackduck.imageinspector.image.oci;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
import com.synopsys.integration.blackduck.imageinspector.image.common.ManifestRepoTagMatcher;
12+
import com.synopsys.integration.blackduck.imageinspector.image.oci.model.OciDescriptor;
13+
import com.synopsys.integration.blackduck.imageinspector.image.oci.model.OciImageIndex;
14+
import com.synopsys.integration.exception.IntegrationException;
15+
16+
public class OciManifestDescriptorParserTest {
17+
String manifestMediaType = "application/vnd.oci.image.manifest.v1+json";
18+
19+
@Test
20+
public void testFindMatchingManifest() throws IntegrationException {
21+
Map<String, String> annotations1 = new HashMap<>();
22+
annotations1.put("org.opencontainers.image.ref.name", "repo:tag");
23+
annotations1.put("dummy", "dummy");
24+
OciDescriptor manifest1 = new OciDescriptor(manifestMediaType, "", "", annotations1);
25+
26+
Map<String, String> annotations2 = new HashMap<>();
27+
annotations2.put("org.opencontainers.image.ref.name", "tag:repo");
28+
OciDescriptor manifest2 = new OciDescriptor(manifestMediaType, "", "", annotations2);
29+
30+
OciDescriptor manifest3 = new OciDescriptor(manifestMediaType, "", "", null);
31+
32+
List<OciDescriptor> manifests = Arrays.asList(manifest1, manifest2, manifest3);
33+
OciImageIndex ociImageIndex = new OciImageIndex(manifests);
34+
35+
OciManifestDescriptorParser parser = new OciManifestDescriptorParser(new ManifestRepoTagMatcher());
36+
Assertions.assertEquals(manifest1, parser.getManifestDescriptor(ociImageIndex, "repo", "tag"));
37+
}
38+
39+
@Test
40+
public void testThrowsExceptionWhenNoMatchingManifests() {
41+
List<OciDescriptor> manifests = Arrays.asList(
42+
new OciDescriptor(manifestMediaType, "", "", new HashMap<>())
43+
);
44+
OciImageIndex ociImageIndex = new OciImageIndex(manifests);
45+
OciManifestDescriptorParser parser = new OciManifestDescriptorParser(new ManifestRepoTagMatcher());
46+
Assertions.assertThrows(IntegrationException.class, () -> parser.getManifestDescriptor(ociImageIndex, "repo", "tag"));
47+
}
48+
}

0 commit comments

Comments
 (0)