From d4a8c3b36b2abd56258f9d3eb9fd8a8e69eedb90 Mon Sep 17 00:00:00 2001 From: Ilya Buziuk Date: Thu, 30 May 2019 17:58:26 +0300 Subject: [PATCH] rh-che #1418: Adding 'dockerImage' caching during CDN pre-fetching Signed-off-by: Ilya Buziuk --- .../com/redhat/che/cdn/CdnSupportService.java | 6 +++-- .../redhat/che/cdn/CdnSupportServiceTest.java | 25 ++++++------------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/plugins/fabric8-cdn-support/src/main/java/com/redhat/che/cdn/CdnSupportService.java b/plugins/fabric8-cdn-support/src/main/java/com/redhat/che/cdn/CdnSupportService.java index e2f43c43b..ceaaeb24b 100644 --- a/plugins/fabric8-cdn-support/src/main/java/com/redhat/che/cdn/CdnSupportService.java +++ b/plugins/fabric8-cdn-support/src/main/java/com/redhat/che/cdn/CdnSupportService.java @@ -132,8 +132,10 @@ public String getPaths() throws Exception { throw new NotFoundException("No editor is configured for CDN resource pre-fetching"); } - PluginMeta editorMeta = getEditorMeta(); - dockerImage = getDockerImage(editorMeta); + if (dockerImage == null) { + PluginMeta editorMeta = getEditorMeta(); + dockerImage = getDockerImage(editorMeta); + } JsonNode json = inspectDockerImage(); return json.path("Labels").path(LABEL_NAME).asText("[]"); diff --git a/plugins/fabric8-cdn-support/src/test/java/com/redhat/che/cdn/CdnSupportServiceTest.java b/plugins/fabric8-cdn-support/src/test/java/com/redhat/che/cdn/CdnSupportServiceTest.java index 01d7919b2..3053c5207 100644 --- a/plugins/fabric8-cdn-support/src/test/java/com/redhat/che/cdn/CdnSupportServiceTest.java +++ b/plugins/fabric8-cdn-support/src/test/java/com/redhat/che/cdn/CdnSupportServiceTest.java @@ -37,7 +37,6 @@ import org.mockito.Mock; import org.mockito.testng.MockitoTestNGListener; import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @@ -100,12 +99,6 @@ protected URLConnection openConnection(URL u) throws IOException { }); } - @BeforeMethod - public void setUp() throws Exception { - service = null; - when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN); - } - @Test( expectedExceptions = {RuntimeException.class}, expectedExceptionsMessageRegExp = @@ -131,6 +124,7 @@ public void throwWhenNoPreferredEditor() throws Exception { expectedExceptions = {InfrastructureException.class}, expectedExceptionsMessageRegExp = DOWNLOAD_EXCEPTION_MESSAGE) public void throwWhenEditorNotFound() throws Exception { + when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN); doThrow(new IOException(DOWNLOAD_ERROR)).when(yamlDownloader).getYamlResponseAndParse(any()); service = new CdnSupportService( @@ -142,6 +136,7 @@ public void throwWhenEditorNotFound() throws Exception { expectedExceptions = {ServerException.class}, expectedExceptionsMessageRegExp = SERVER_EXCEPTION_MESSAGE_WHEN_SPEC_IS_NULL) public void throwWhenEditorSpecIsNull() throws Exception { + when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN); doReturn(pluginMeta).when(yamlDownloader).getYamlResponseAndParse(any()); doReturn(null).when(pluginMeta).getSpec(); service = @@ -155,7 +150,7 @@ public void throwWhenEditorSpecIsNull() throws Exception { expectedExceptions = {ServerException.class}, expectedExceptionsMessageRegExp = SERVER_EXCEPTION_MESSAGE_WHEN_NO_CONTAINERS_IN_SPEC) public void throwWhenEditorContainersIsNull() throws Exception { - + when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN); doReturn(pluginMeta).when(yamlDownloader).getYamlResponseAndParse(any()); doReturn(pluginSpec).when(pluginMeta).getSpec(); doReturn(null).when(pluginSpec).getContainers(); @@ -170,6 +165,7 @@ public void throwWhenEditorContainersIsNull() throws Exception { expectedExceptions = {ServerException.class}, expectedExceptionsMessageRegExp = SERVER_EXCEPTION_MESSAGE_WHEN_NO_CONTAINERS_IN_SPEC) public void throwWhenEditorContainersIsEmpty() throws Exception { + when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN); doReturn(pluginMeta).when(yamlDownloader).getYamlResponseAndParse(any()); doReturn(pluginSpec).when(pluginMeta).getSpec(); doReturn(Collections.EMPTY_LIST).when(pluginSpec).getContainers(); @@ -184,6 +180,7 @@ public void throwWhenEditorContainersIsEmpty() throws Exception { expectedExceptions = {ServerException.class}, expectedExceptionsMessageRegExp = SERVER_EXCEPTION_MESSAGE_WHEN_MULTIPLE_CONTAINERS_IN_SPEC) public void throwWhenMultipleEditorContainers() throws Exception { + when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN); doReturn(pluginMeta).when(yamlDownloader).getYamlResponseAndParse(any()); doReturn(pluginSpec).when(pluginMeta).getSpec(); doReturn(containers).when(pluginSpec).getContainers(); @@ -199,6 +196,7 @@ public void throwWhenMultipleEditorContainers() throws Exception { expectedExceptions = {ServerException.class}, expectedExceptionsMessageRegExp = SERVER_EXCEPTION_MESSAGE_WHEN_CONTAINER_IMAGE_IS_NULL) public void throwWhenContainerImageIsNull() throws Exception { + when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN); doReturn(pluginMeta).when(yamlDownloader).getYamlResponseAndParse(any()); doReturn(pluginSpec).when(pluginMeta).getSpec(); doReturn(containers).when(pluginSpec).getContainers(); @@ -244,14 +242,6 @@ public void throwWhenSkopeoFailsWithNonZeroCode() throws Exception { @Test public void reuseExistingImageRefAndReturnLabelWhenSkopeoSucceeds() throws Exception { - doReturn(pluginMeta).when(yamlDownloader).getYamlResponseAndParse(any()); - doReturn(pluginSpec).when(pluginMeta).getSpec(); - doReturn(containers).when(pluginSpec).getContainers(); - doReturn(false).when(containers).isEmpty(); - doReturn(1).when(containers).size(); - doReturn(container).when(containers).get(0); - doReturn(IMAGE_REF).when(container).getImage(); - lenient() .when(commandRunner.runCommand(eq("skopeo"), any(), any(), anyLong(), any(), any(), any())) .thenReturn(skopeoHelpProcess, skopeoInspectProcess); @@ -268,11 +258,12 @@ public void reuseExistingImageRefAndReturnLabelWhenSkopeoSucceeds() throws Excep assertEquals(service.getPaths(), "cdnJsonContent"); - verify(yamlDownloader, times(1)).getYamlResponseAndParse(PLUGIN_URL); + verify(yamlDownloader, times(0)).getYamlResponseAndParse(PLUGIN_URL); } @Test public void searchForImageRefAndReturnLabelWhenSkopeoSucceeds() throws Exception { + when(pluginFQNParser.parsePluginFQN(any())).thenReturn(PLUGIN_FQN); doReturn(pluginMeta).when(yamlDownloader).getYamlResponseAndParse(any()); doReturn(pluginSpec).when(pluginMeta).getSpec(); doReturn(containers).when(pluginSpec).getContainers();