Skip to content

Commit

Permalink
test : Add TestHttpBuildPackArtifactsServer in jkube-kit/common
Browse files Browse the repository at this point in the history
Add TestHttpBuildPackArtifactsServer to host build pack download
artifacts so that this test utility class can be reused in dependent
modules

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Jan 31, 2024
1 parent 7561af9 commit b7fc562
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import org.eclipse.jkube.kit.common.KitLogger;

import org.eclipse.jkube.kit.common.TestHttpStaticServer;
import org.eclipse.jkube.kit.common.TestHttpBuildPacksArtifactsServer;
import org.eclipse.jkube.kit.common.util.EnvUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -45,9 +45,8 @@ abstract class AbstractBuildPackCliDownloaderTest {
@TempDir
private File temporaryFolder;
private File oldPackCliInJKubeDir;
private TestHttpStaticServer server;
private TestHttpBuildPacksArtifactsServer server;
private BuildPackCliDownloader buildPackCliDownloader;
private String serverBaseUrl;
private Properties packProperties;

abstract String getApplicablePackBinary();
Expand All @@ -67,16 +66,14 @@ void setUp() {
overriddenEnvironmentVariables.put("PATH", temporaryFolder.toPath().resolve("bin").toFile().getAbsolutePath());
EnvUtil.overridePropertyGetter(overriddenSystemProperties::get);
EnvUtil.overrideEnvGetter(overriddenEnvironmentVariables::get);
File remoteDirectory = new File(Objects.requireNonNull(getClass().getResource("/")).getFile());
server = new TestHttpStaticServer(remoteDirectory);
serverBaseUrl = String.format("http://localhost:%d/", server.getPort());
server = new TestHttpBuildPacksArtifactsServer();
packProperties = new Properties();
packProperties.put("version", TEST_PACK_VERSION);
packProperties.put("linux.artifact", serverBaseUrl + "artifacts/pack-v" + TEST_PACK_VERSION + "-linux.tgz");
packProperties.put("linux-arm64.artifact", serverBaseUrl + "artifacts/pack-v" + TEST_PACK_VERSION + "-linux-arm64.tgz");
packProperties.put("macos.artifact", serverBaseUrl + "artifacts/pack-v" + TEST_PACK_VERSION + "-macos.tgz");
packProperties.put("macos-arm64.artifact", serverBaseUrl + "artifacts/pack-v" + TEST_PACK_VERSION + "-macos-arm64.tgz");
packProperties.put("windows.artifact", serverBaseUrl + "artifacts/pack-v" + TEST_PACK_VERSION + "-windows.zip");
packProperties.put("linux.artifact", server.getLinuxArtifactUrl());
packProperties.put("linux-arm64.artifact", server.getLinuxArm64ArtifactUrl());
packProperties.put("macos.artifact", server.getMacosArtifactUrl());
packProperties.put("macos-arm64.artifact", server.getMacosArm64ArtifactUrl());
packProperties.put("windows.artifact", server.getWindowsArtifactUrl());
packProperties.put("windows.binary-extension", "bat");
buildPackCliDownloader = new BuildPackCliDownloader(kitLogger, packProperties);
}
Expand Down Expand Up @@ -159,11 +156,11 @@ void fileExistsAndHasTheRightSize() {
class DownloadFails {
@BeforeEach
void setUp() {
packProperties.put("linux.artifact", serverBaseUrl + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-linux.tgz");
packProperties.put("linux-arm64.artifact", serverBaseUrl + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-linux-arm64.tgz");
packProperties.put("macos.artifact", serverBaseUrl + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-macos.tgz");
packProperties.put("macos-arm64.artifact", serverBaseUrl + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-macos-arm64.tgz");
packProperties.put("windows.artifact", serverBaseUrl + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-windows.zip");
packProperties.put("linux.artifact", server.getBaseUrl() + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-linux.tgz");
packProperties.put("linux-arm64.artifact", server.getBaseUrl() + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-linux-arm64.tgz");
packProperties.put("macos.artifact", server.getBaseUrl() + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-macos.tgz");
packProperties.put("macos-arm64.artifact", server.getBaseUrl() + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-macos-arm64.tgz");
packProperties.put("windows.artifact", server.getBaseUrl() + "invalid-artifacts/pack-v" + TEST_PACK_VERSION + "-windows.zip");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common;

import org.apache.commons.io.FileUtils;
import org.eclipse.jkube.kit.common.util.FileUtil;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class TestHttpBuildPacksArtifactsServer implements Closeable {
private final TestHttpStaticServer testHttpStaticServer;
private static final String LINUX_ARTIFACT = "pack-v0.32.1-linux.tgz";
private static final String LINUX_ARM64_ARTIFACT = "pack-v0.32.1-linux-arm64.tgz";
private static final String MACOS_ARTIFACT = "pack-v0.32.1-macos.tgz";
private static final String MACOS_ARM64_ARTIFACT = "pack-v0.32.1-macos-arm64.tgz";
private static final String WINDOWS_ARTIFACT = "pack-v0.32.1-windows.zip";
private final File remoteBuildPackArtifactsDir;

public TestHttpBuildPacksArtifactsServer() {
remoteBuildPackArtifactsDir = createTemporaryArtifactsDir();
testHttpStaticServer = new TestHttpStaticServer(remoteBuildPackArtifactsDir);
}

public String getLinuxArtifactUrl() {
return createUrlForArtifact(LINUX_ARTIFACT);
}

public String getLinuxArm64ArtifactUrl() {
return createUrlForArtifact(LINUX_ARM64_ARTIFACT);
}

public String getMacosArtifactUrl() {
return createUrlForArtifact(MACOS_ARTIFACT);
}

public String getMacosArm64ArtifactUrl() {
return createUrlForArtifact(MACOS_ARM64_ARTIFACT);
}

public String getWindowsArtifactUrl() {
return createUrlForArtifact(WINDOWS_ARTIFACT);
}

public String getBaseUrl() {
return String.format("http://localhost:%d", testHttpStaticServer.getPort());
}

private String createUrlForArtifact(String artifactName) {
return String.format("%s/%s", getBaseUrl(), artifactName);
}

private File createTemporaryArtifactsDir() {
try {
File artifactDir = FileUtil.createTempDirectory();

FileUtils.copyInputStreamToFile(Objects.requireNonNull(TestHttpBuildPacksArtifactsServer.class.getResourceAsStream(String.format("/buildpack-download-artifacts/%s", LINUX_ARTIFACT))), new File(artifactDir, LINUX_ARTIFACT));
FileUtils.copyInputStreamToFile(Objects.requireNonNull(TestHttpBuildPacksArtifactsServer.class.getResourceAsStream(String.format("/buildpack-download-artifacts/%s", LINUX_ARM64_ARTIFACT))), new File(artifactDir, LINUX_ARM64_ARTIFACT));
FileUtils.copyInputStreamToFile(Objects.requireNonNull(TestHttpBuildPacksArtifactsServer.class.getResourceAsStream(String.format("/buildpack-download-artifacts/%s", MACOS_ARTIFACT))), new File(artifactDir, MACOS_ARTIFACT));
FileUtils.copyInputStreamToFile(Objects.requireNonNull(TestHttpBuildPacksArtifactsServer.class.getResourceAsStream(String.format("/buildpack-download-artifacts/%s", MACOS_ARM64_ARTIFACT))), new File(artifactDir, MACOS_ARM64_ARTIFACT));
FileUtils.copyInputStreamToFile(Objects.requireNonNull(TestHttpBuildPacksArtifactsServer.class.getResourceAsStream(String.format("/buildpack-download-artifacts/%s", WINDOWS_ARTIFACT))), new File(artifactDir, WINDOWS_ARTIFACT));
return artifactDir;
} catch (IOException ioException) {
throw new IllegalStateException("Failure in creating build pack artifacts server : ", ioException);
}
}

@Override
public void close() throws IOException {
testHttpStaticServer.close();
FileUtil.cleanDirectory(remoteBuildPackArtifactsDir);
}
}

0 comments on commit b7fc562

Please sign in to comment.