Skip to content

Commit

Permalink
devonfw/ide#1317: fix docker desktop url updater (#1346)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Sep 17, 2023
1 parent cd96948 commit 723fc8a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ public static String getUrlsFileName(OperatingSystem os, SystemArchitecture arch
return os + "_" + SystemArchitecture.orDefault(arch) + UrlDownloadFile.EXTENSION_URLS;
}

/**
* @return the {@link UrlStatusFile}.
*/
public UrlStatusFile getStatus() {

return (UrlStatusFile) getChild(UrlStatusFile.STATUS_JSON);
}

/**
* @return the {@link UrlStatusFile}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ protected boolean doAddVersion(UrlVersion urlVersion, String downloadUrl, Operat
protected boolean doAddVersion(UrlVersion urlVersion, String url, OperatingSystem os, SystemArchitecture architecture,
String checksum) {

UrlStatusFile status = urlVersion.getStatus();
if ((status != null) && status.getStatusJson().isManual()) {
return true;
}
String version = urlVersion.getName();
url = url.replace("${version}", version);
String major = urlVersion.getVersionIdentifier().getStart().getDigits();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.devonfw.tools.ide.url.updater.aws.AwsUrlUpdater;
import com.devonfw.tools.ide.url.updater.az.AzureUrlUpdater;
import com.devonfw.tools.ide.url.updater.cobigen.CobigenUrlUpdater;
import com.devonfw.tools.ide.url.updater.docker.DockerDesktopUrlUpdater;
import com.devonfw.tools.ide.url.updater.docker.DockerRancherDesktopUrlUpdater;
import com.devonfw.tools.ide.url.updater.dotnet.DotNetUrlUpdater;
import com.devonfw.tools.ide.url.updater.eclipse.EclipseCppUrlUpdater;
Expand Down Expand Up @@ -56,7 +57,7 @@ public class UpdateManager extends AbstractProcessorWithTimeout {
private final UrlRepository urlRepository;

private final List<AbstractUrlUpdater> updaters = Arrays.asList(new AndroidStudioUrlUpdater(), new AwsUrlUpdater(),
new AzureUrlUpdater(), new CobigenUrlUpdater(), new DotNetUrlUpdater(), new EclipseCppUrlUpdater(),
new AzureUrlUpdater(), new CobigenUrlUpdater(), new DockerDesktopUrlUpdater() , new DotNetUrlUpdater(), new EclipseCppUrlUpdater(),
new EclipseJavaUrlUpdater(), new GCloudUrlUpdater(), new GcViewerUrlUpdater(), new GhUrlUpdater(),
new GraalVmCommunityUpdater(), new GraalVmOracleUrlUpdater(), new GradleUrlUpdater(), new HelmUrlUpdater(), new IntellijUrlUpdater(),
new JavaUrlUpdater(), new JenkinsUrlUpdater(), new JmcUrlUpdater(), new KotlincUrlUpdater(), new KotlincNativeUrlUpdater(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.devonfw.tools.ide.url.updater.docker;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.devonfw.tools.ide.url.model.folder.UrlVersion;
import com.devonfw.tools.ide.url.updater.WebsiteUrlUpdater;
import com.devonfw.tools.ide.version.VersionIdentifier;

/**
* {@link WebsiteUrlUpdater} for docker-desktop.
*/
public class DockerDesktopUrlUpdater extends WebsiteUrlUpdater {

private final static Set<VersionIdentifier> WINDOWS_ONLY_VERSIONS = Set.of(VersionIdentifier.of("4.16.3"),
VersionIdentifier.of("4.4.3"), VersionIdentifier.of("4.4.4"), VersionIdentifier.of("4.17.1"),
VersionIdentifier.of("4.5.1"));

@Override
protected String getTool() {

Expand All @@ -19,37 +26,28 @@ protected String getTool() {
@Override
protected void addVersion(UrlVersion urlVersion) {

VersionIdentifier vid = VersionIdentifier.of(urlVersion.getName());
String version = urlVersion.getName().replaceAll("\\.", "");
// get Code for version
String body = doGetResponseBodyAsString("https://docs.docker.com/desktop/release-notes/");
String regex = "<h2 id=\"" + version + "\".*?https://desktop\\.docker\\.com.*?(\\d{5})\\.exe(?!.<h2 id=)";
String regex = "href=#" + version
// ...........1.........................................................2................
+ ".{8,12}(\r\n|\r|\n).{0,350}href=https://desktop\\.docker\\.com.*?(\\d{5,6}).*\\.exe";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(body);
String code;
if (matcher.find()) {
code = matcher.group(1);
code = matcher.group(2);
boolean success = doAddVersion(urlVersion,
"https://desktop.docker.com/win/main/amd64/" + code + "/Docker%20Desktop%20Installer.exe", WINDOWS);
if (!success) {
return;
}
doAddVersion(urlVersion, "https://desktop.docker.com/mac/main/amd64/" + code + "/Docker.dmg", MAC, X64);
doAddVersion(urlVersion, "https://desktop.docker.com/mac/main/arm64/" + code + "/Docker.dmg", MAC, ARM64);
} else {
// For the latest version, there is no code in the url.
// TODO but that means that the implementation is wrong as the URL will then change later and is therefore
// unstable as it is always pointing to the latest version so all newly added versions will always have the same
// URL pointing to the latest version what is dead wrong...
// instead we could add a manual version called "latest" with these URLs but we can not provide checksum files...
boolean success = doAddVersion(urlVersion,
"https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe", WINDOWS);
if (!success) {
return;
if (!WINDOWS_ONLY_VERSIONS.stream().anyMatch(i -> vid.compareVersion(i).isEqual())) {
doAddVersion(urlVersion, "https://desktop.docker.com/mac/main/amd64/" + code + "/Docker.dmg", MAC, X64);
doAddVersion(urlVersion, "https://desktop.docker.com/mac/main/arm64/" + code + "/Docker.dmg", MAC, ARM64);
}
doAddVersion(urlVersion, "https://desktop.docker.com/mac/main/amd64/Docker.dmg", MAC, X64);
doAddVersion(urlVersion, "https://desktop.docker.com/mac/main/arm64/Docker.dmg", MAC, ARM64);
}

}

@Override
Expand All @@ -61,6 +59,6 @@ protected String getVersionUrl() {
@Override
protected Pattern getVersionPattern() {

return Pattern.compile("(4\\.\\d+\\.\\d+)");
return Pattern.compile("(4\\.\\d{1,4}+\\.\\d+)");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.devonfw.tools.ide.integrationtest;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import com.devonfw.tools.ide.url.model.folder.UrlRepository;
import com.devonfw.tools.ide.url.model.folder.UrlVersion;
import com.devonfw.tools.ide.url.updater.AbstractUrlUpdater;
import com.devonfw.tools.ide.url.updater.JsonUrlUpdater;
import com.devonfw.tools.ide.url.updater.UrlUpdater;

/**
* {@link JsonUrlUpdater} test mock for {@link UrlUpdater} using a single urlVersion.
*/
public class UrlUpdaterMockSingle extends UrlUpdaterMock {

private static final Set<String> versions = new HashSet<>(Arrays.asList("1.0"));

@Override
protected void addVersion(UrlVersion urlVersion) {
doAddVersion(urlVersion, "http://localhost:8080/os/windows_x64_url.tgz", WINDOWS, X64, "123");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import com.devonfw.tools.ide.json.mapping.JsonMapping;
import com.devonfw.tools.ide.url.model.file.json.StatusJson;
import com.devonfw.tools.ide.url.model.folder.UrlRepository;
import com.devonfw.tools.ide.url.updater.UrlUpdater;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;

/**
Expand All @@ -23,6 +28,11 @@
@WireMockTest(httpPort = 8080)
public class UrlUpdaterTest extends Assertions {

/**
* Test resource location
*/
private final static String testdataRoot = "src/test/resources/integrationtest/UrlUpdaterTest";

/**
* Tests if the {@link com.devonfw.tools.ide.url.updater.UrlUpdater} can automatically add a missing OS (in this case
* the linux_x64)
Expand Down Expand Up @@ -66,4 +76,38 @@ public void testUrlUpdaterMissingOsGetsAddedAutomatically(@TempDir Path tempDir)

}

@Test
public void testUrlUpdaterIsNotUpdatingWhenStatusManualIsTrue(@TempDir Path tempDir) throws IOException {

// arrange
stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(200).withBody("aBody")));

UrlRepository urlRepository = UrlRepository.load(tempDir);
UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle();

// act
updater.update(urlRepository);
Path versionsPath = Paths.get(testdataRoot).resolve("mocked").resolve("mocked").resolve("1.0");

// assert
assertThat(versionsPath.resolve("windows_x64.urls")).doesNotExist();
assertThat(versionsPath.resolve("windows_x64.urls.sha256")).doesNotExist();

}

private static StatusJson getStatusJson(Path versionsPath) {

ObjectMapper MAPPER = JsonMapping.create();
StatusJson statusJson = new StatusJson();
Path statusJsonPath = versionsPath.resolve("status.json");

if (Files.exists(statusJsonPath)) {
try (BufferedReader reader = Files.newBufferedReader(statusJsonPath)) {
statusJson = MAPPER.readValue(reader, StatusJson.class);
} catch (Exception e) {
throw new IllegalStateException("Failed to load " + statusJsonPath, e);
}
}
return statusJson;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"manual" : true,
"urls" : {
"399490723" : {
"success" : {
"timestamp" : "2023-06-06T03:21:13.620611Z"
}
}
}
}

0 comments on commit 723fc8a

Please sign in to comment.