BaseGenerator implements Generator { protected void addFrom(BuildImageConfiguration.Builder builder) { String fromMode = getConfigWithFallback(Config.fromMode, FABRIC8_GENERATOR_FROM_MODE, getFromModeDefault(context.getRuntimeMode())); String from = getConfigWithFallback(Config.from, FABRIC8_GENERATOR_FROM, null); if ("docker".equalsIgnoreCase(fromMode)) { String fromImage = from; if (fromImage == null) { fromImage = fromSelector != null ? fromSelector.getFrom() : null; } builder.from(fromImage); log.info("Using Container image %s as base / builder", fromImage); } else if ("istag".equalsIgnoreCase(fromMode)) { Map<String, String> fromExt = new HashMap<>(); if (from != null) { ImageName iName = new ImageName(from); String tag = iName.getTag(); if (StringUtils.isBlank(tag)) { tag = "latest"; } fromExt.put(OpenShiftBuildStrategy.SourceStrategy.name.key(), iName.getSimpleName() + ":" + tag); if (iName.getUser() != null) { fromExt.put(OpenShiftBuildStrategy.SourceStrategy.namespace.key(), iName.getUser()); } fromExt.put(OpenShiftBuildStrategy.SourceStrategy.kind.key(), "ImageStreamTag"); } else { fromExt = fromSelector != null ? fromSelector.getImageStreamTagFromExt() : null; } if (fromExt != null) { String namespace = fromExt.get(OpenShiftBuildStrategy.SourceStrategy.namespace.key()); if (namespace != null) { log.info("Using ImageStreamTag '%s' from namespace '%s' as builder image", fromExt.get(OpenShiftBuildStrategy.SourceStrategy.name.key()), namespace); } else { log.info("Using ImageStreamTag '%s' as builder image", fromExt.get(OpenShiftBuildStrategy.SourceStrategy.name.key())); } builder.fromExt(fromExt); } } else { throw new IllegalArgumentException(String.format("Invalid 'fromMode' in generator configuration for '%s'", getName())); } } BaseGenerator(GeneratorContext context, String name);  BaseGenerator(GeneratorContext context, String name, FromSelector selector); String getName(); GeneratorContext getContext();  }
@Test public void defaultAddFrom() { Properties props = new Properties(); for (boolean isOpenShift : new Boolean[]{false, true}) { for (boolean isRedHat : new Boolean[]{false, true}) { for (TestFromSelector selector : new TestFromSelector[]{null, new TestFromSelector(ctx, isRedHat, logger)}) { for (String from : new String[]{null, "openshift/testfrom"}) { setupContext(props, isOpenShift, from, null); BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder(); BaseGenerator generator = createGenerator(selector); generator.addFrom(builder); BuildImageConfiguration config = builder.build(); if (isRedHat && isOpenShift && selector != null) { if (from != null) { assertEquals("testfrom:latest", config.getFrom()); assertFromExt(config.getFromExt(), "testfrom:latest", "openshift"); } else { assertEquals("selectorIstagFromRedhat", config.getFrom()); assertFromExt(config.getFromExt(), selector.getIstagFrom(), "openshift"); } } else { if (from != null) { assertEquals(config.getFrom(), from); assertNull(config.getFromExt()); } else { System.out.println(isRedHat + " " + isOpenShift); assertNull(config.getFromExt()); assertEquals(config.getFrom(), selector != null ? (isOpenShift ? selector.getS2iBuildFrom() : selector.getDockerBuildFrom()) : null); } } } } } } }
@Test public void addFromIstagModeWithSelector() { Properties props = new Properties(); props.put("fabric8.generator.fromMode","istag"); for (String from : new String[] { null, "test_namespace/test_image:2.0"}) { setupContext(props, false, from, null); BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder(); BaseGenerator generator = createGenerator(new TestFromSelector(ctx, false, logger)); generator.addFrom(builder); BuildImageConfiguration config = builder.build(); assertEquals(from == null ? "selectorIstagFromUpstream" : "test_image:2.0", config.getFrom()); Map<String, String> fromExt = config.getFromExt(); if (from != null) { assertFromExt(fromExt,"test_image:2.0", "test_namespace"); } else { assertFromExt(fromExt, "selectorIstagFromUpstream", "openshift"); } } }
@Test public void addFromIstagModeWithoutSelector() { Properties props = new Properties(); props.put("fabric8.generator.fromMode","istag"); for (String from : new String[] { null, "test_namespace/test_image:2.0"}) { setupContext(props, false, from, null); BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder(); BaseGenerator generator = createGenerator(null); generator.addFrom(builder); BuildImageConfiguration config = builder.build(); assertEquals(from == null ? null : "test_image:2.0", config.getFrom()); Map<String, String> fromExt = config.getFromExt(); if (from == null) { assertNull(fromExt); } else { assertFromExt(fromExt, "test_image:2.0", "test_namespace"); } } }
@Test public void addFromIstagWithNameWithoutTag() { Properties props = new Properties(); setupContext(props, false, "test_namespace/test_image", "istag"); BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder(); BaseGenerator generator = createGenerator(null); generator.addFrom(builder); BuildImageConfiguration config = builder.build(); assertEquals("test_image:latest",config.getFrom()); }
@Test public void addFromInvalidMode() { try { Properties props = new Properties(); setupContextKubernetes(props, null, "blub"); BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder(); BaseGenerator generator = createGenerator(null); generator.addFrom(builder); fail(); } catch (IllegalArgumentException exp) { assertTrue(exp.getMessage().contains("fromMode")); assertTrue(exp.getMessage().contains("test-generator")); } }