ImageStreamService { public void appendImageStreamResource(ImageName imageName, File target) throws MojoExecutionException { String tag = StringUtils.isBlank(imageName.getTag()) ? "latest" : imageName.getTag(); try { ImageStream is = new ImageStreamBuilder() .withNewMetadata() .withName(imageName.getSimpleName()) .endMetadata() .withNewSpec() .addNewTag() .withName(tag) .withNewFrom().withKind("ImageStreamImage").endFrom() .endTag() .endSpec() .build(); createOrUpdateImageStreamTag(client, imageName, is); appendImageStreamToFile(is, target); log.info("ImageStream %s written to %s", imageName.getSimpleName(), target); } catch (KubernetesClientException e) { KubernetesResourceUtil.handleKubernetesClientException(e, this.log); } catch (IOException e) { throw new MojoExecutionException(String.format("Cannot write ImageStream descriptor for %s to %s : %s", imageName.getFullName(), target.getAbsoluteFile(), e.getMessage()),e); } } ImageStreamService(OpenShiftClient client, Logger log); void appendImageStreamResource(ImageName imageName, File target); TagEvent newerTag(TagEvent tag1, TagEvent tag2); static final String DATE_FORMAT; }
@Test public void simple() throws IOException, MojoExecutionException { ImageStreamService service = new ImageStreamService(client, log); final ImageStream lookedUpIs = lookupImageStream("ab12cd"); setupClientMock(lookedUpIs,"test"); ImageName name = new ImageName("test:1.0"); File target = File.createTempFile("ImageStreamServiceTest",".yml"); service.appendImageStreamResource(name, target); assertTrue(target.exists()); Map result = readImageStreamDescriptor(target); Yaml yaml; System.out.println(result.toString()); assertNotNull(result); List<Map> items = getItemsList(result); assertEquals(1, items.size()); Map isRead = (Map<String, Object>) items.get(0); assertNotNull(isRead); assertEquals("ImageStream", isRead.get("kind")); Map spec = (Map<String, Object>) isRead.get("spec"); assertNotNull(spec); List tags = (List) spec.get("tags"); assertNotNull(tags); assertEquals(1,tags.size()); Map tag = (Map) tags.get(0); Map from = (Map) tag.get("from"); assertEquals("ImageStreamImage", from.get("kind")); assertEquals("test@ab12cd", from.get("name")); assertEquals("default", from.get("namespace")); ImageStream secondIs = lookupImageStream("secondIS"); setupClientMock(secondIs, "second-test"); ImageName name2 = new ImageName("second-test:1.0"); service.appendImageStreamResource(name2, target); result = readImageStreamDescriptor(target); System.out.println(result.toString()); items = getItemsList(result); assertEquals(2,items.size()); Set<String> names = new HashSet<>(Arrays.asList("second-test", "test")); for (Map item : items) { assertTrue(names.remove( ((Map) item.get("metadata")).get("name"))); } assertTrue(names.isEmpty()); }