DockerRegistryPlugin extends AbstractEC2InstancePlugin { @Override protected void process(final EC2InstanceContext context) { if (!context.getRuntime().filter(isEqual("Docker")).isPresent()) { log.info("Unknown or missing Taupage runtime for {}. Skip DockerRegistryPlugin", context); return; } final Optional<String> optionalSource = context.getSource(); if (!optionalSource.isPresent()) { log.warn("Docker source is missing for {}. Skip DockerRegistryPlugin", context); return; } final String source = optionalSource.get(); final Optional<TagSummary> pieroneTag = context.getPieroneTag(); if (!pieroneTag.isPresent()) { violationSink.put( context.violation() .withType(IMAGE_IN_PIERONE_NOT_FOUND) .withPluginFullyQualifiedClassName(DockerRegistryPlugin.class) .withMetaInfo(singletonMap("source", source)) .build()); return; } final Optional<Map<String, String>> optionalScmSource = context.getScmSource(); if (!optionalScmSource.isPresent()) { violationSink.put( context.violation() .withType(SCM_SOURCE_JSON_MISSING) .withPluginFullyQualifiedClassName(DockerRegistryPlugin.class) .withMetaInfo(singletonMap("source", source)) .build()); return; } final Map<String, String> scmSource = optionalScmSource.get(); if (isBlank(scmSource.get("url"))) { violationSink.put( context.violation() .withType(SCM_URL_IS_MISSING_IN_SCM_SOURCE_JSON) .withPluginFullyQualifiedClassName(DockerRegistryPlugin.class) .withMetaInfo(singletonMap("source", source)) .build()); } if (!isBlank(scmSource.get("status")) && !equalsIgnoreCase(scmSource.get("status"), "false")) { final ImmutableMap.Builder<String, String> metaInfo = ImmutableMap.builder(); scmSource.forEach((key, value) -> metaInfo.put("scm_source_" + key, value)); metaInfo.put("source", source); violationSink.put( context.violation() .withType(ARTIFACT_BUILT_FROM_DIRTY_REPOSITORY) .withPluginFullyQualifiedClassName(DockerRegistryPlugin.class) .withMetaInfo(metaInfo.build()) .build()); } } DockerRegistryPlugin(final EC2InstanceContextProvider contextProvider, final ViolationSink violationSink);   }
@Test public void testProcessIsSkippedOnUnknownRuntime() throws Exception { when(mockContext.getRuntime()).thenReturn(empty()); plugin.process(mockContext); }
@Test public void testProcessIsSkippedOnNonDockerRuntime() throws Exception { when(mockContext.getRuntime()).thenReturn(Optional.of("rkt")); plugin.process(mockContext); }
@Test public void testProcessIsSkippedOnMissingSource() throws Exception { when(mockContext.getRuntime()).thenReturn(Optional.of("Docker")); when(mockContext.getSource()).thenReturn(empty()); plugin.process(mockContext); }
@Test public void testProcessThrowsViolationAndSkipsOnMissingPieroneTag() throws Exception { when(mockContext.getRuntime()).thenReturn(Optional.of("Docker")); when(mockContext.getSource()).thenReturn(Optional.of("pierone.example.org/stups/foo:bar1")); when(mockContext.getPieroneTag()).thenReturn(empty()); plugin.process(mockContext); verify(mockViolationSink).put(argThat(hasType(IMAGE_IN_PIERONE_NOT_FOUND))); }
@Test public void testProcessThrowsViolationAndSkipsOnMissingScmSource() throws Exception { when(mockContext.getRuntime()).thenReturn(Optional.of("Docker")); when(mockContext.getSource()).thenReturn(Optional.of("pierone.example.org/stups/foo:bar1")); when(mockContext.getPieroneTag()).thenReturn(Optional.of(new TagSummary("bar1", ZonedDateTime.now(), "test_user"))); when(mockContext.getScmSource()).thenReturn(empty()); plugin.process(mockContext); verify(mockViolationSink).put(argThat(hasType(SCM_SOURCE_JSON_MISSING))); }
@Test public void testProcessThrowsViolationsOnBadScmSourceJson() throws Exception { when(mockContext.getRuntime()).thenReturn(Optional.of("Docker")); when(mockContext.getSource()).thenReturn(Optional.of("pierone.example.org/stups/foo:bar1")); when(mockContext.getPieroneTag()).thenReturn(Optional.of(new TagSummary("bar1", ZonedDateTime.now(), "test_user"))); when(mockContext.getScmSource()).thenReturn(Optional.of(ImmutableMap.of("status", DIRTY_REPO_STATUS))); plugin.process(mockContext); verify(mockViolationSink).put(argThat(hasType(SCM_URL_IS_MISSING_IN_SCM_SOURCE_JSON))); verify(mockViolationSink).put(argThat(hasType(ARTIFACT_BUILT_FROM_DIRTY_REPOSITORY))); }
@Test public void testProcessWithoutViolations() throws Exception { when(mockContext.getRuntime()).thenReturn(Optional.of("Docker")); when(mockContext.getSource()).thenReturn(Optional.of("pierone.example.org/stups/foo:bar1")); when(mockContext.getPieroneTag()).thenReturn(Optional.of(new TagSummary("bar1", ZonedDateTime.now(), "test_user"))); when(mockContext.getScmSource()).thenReturn(Optional.of(ImmutableMap.of( "status", CLEAN_REPO_STATUS, "url", "git@github.com:zalando-stups/fullstop.git"))); plugin.process(mockContext); }
@Test public void testProcessWithoutViolationsOnStatusFalse() throws Exception { when(mockContext.getRuntime()).thenReturn(Optional.of("Docker")); when(mockContext.getSource()).thenReturn(Optional.of("pierone.example.org/stups/foo:bar1")); when(mockContext.getPieroneTag()).thenReturn(Optional.of(new TagSummary("bar1", ZonedDateTime.now(), "test_user"))); when(mockContext.getScmSource()).thenReturn(Optional.of(ImmutableMap.of( "status", "FALSE", "url", "git@github.com:zalando-stups/fullstop.git"))); plugin.process(mockContext); }