Skip to content

Commit

Permalink
test (jkube-kit/build/service/buildpacks) : Refactor AbstractBuildPac…
Browse files Browse the repository at this point in the history
…kCliDownloaderTest to initialize server only once

Follow up to #2495

Update TestHttpStaticServer used in tests to be initialized only once,
holding both valid and invalid upload directories. Only change the
download url properties in setup methods where needed.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and manusa committed Jan 18, 2024
1 parent 9fe321e commit 6a64e3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ abstract class AbstractBuildPackCliDownloaderTest {
private KitLogger kitLogger;
@TempDir
private File temporaryFolder;
private File remoteDirectory;
private File oldPackCliInJKubeDir;
private TestHttpStaticServer server;
private BuildPackCliDownloader buildPackCliDownloader;
private String serverBaseUrl;
private Properties packProperties;

abstract String getApplicablePackBinary();
abstract String getInvalidApplicablePackBinary();
Expand All @@ -66,9 +67,18 @@ void setUp() {
overriddenEnvironmentVariables.put("PATH", temporaryFolder.toPath().resolve("bin").toFile().getAbsolutePath());
EnvUtil.overridePropertyGetter(overriddenSystemProperties::get);
EnvUtil.overrideEnvGetter(overriddenEnvironmentVariables::get);
remoteDirectory = new File(Objects.requireNonNull(getClass().getResource("/artifacts")).getFile());
File remoteDirectory = new File(Objects.requireNonNull(getClass().getResource("/")).getFile());
server = new TestHttpStaticServer(remoteDirectory);
buildPackCliDownloader = new BuildPackCliDownloader(kitLogger, createPackProperties(server));
serverBaseUrl = String.format("http://localhost:%d/", server.getPort());
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("windows.binary-extension", "bat");
buildPackCliDownloader = new BuildPackCliDownloader(kitLogger, packProperties);
}

@AfterEach
Expand Down Expand Up @@ -149,9 +159,11 @@ void fileExistsAndHasTheRightSize() {
class DownloadFails {
@BeforeEach
void setUp() {
remoteDirectory = new File(Objects.requireNonNull(getClass().getResource("/invalid-artifacts")).getFile());
server = new TestHttpStaticServer(remoteDirectory);
buildPackCliDownloader = new BuildPackCliDownloader(kitLogger, createPackProperties(server));
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");
}

@Test
Expand Down Expand Up @@ -219,19 +231,6 @@ private void givenPackCliPresentOnUserPath(String packResource) throws IOExcepti
}
}

private Properties createPackProperties(TestHttpStaticServer server) {
Properties packProperties = new Properties();
String baseUrl = String.format("http://localhost:%d/", server.getPort());
packProperties.put("version", TEST_PACK_VERSION);
packProperties.put("linux.artifact", baseUrl + "pack-v" + TEST_PACK_VERSION + "-linux.tgz");
packProperties.put("linux-arm64.artifact", baseUrl + "pack-v" + TEST_PACK_VERSION + "-linux-arm64.tgz");
packProperties.put("macos.artifact", baseUrl + "pack-v" + TEST_PACK_VERSION + "-macos.tgz");
packProperties.put("macos-arm64.artifact", baseUrl + "pack-v" + TEST_PACK_VERSION + "-macos-arm64.tgz");
packProperties.put("windows.artifact", baseUrl + "pack-v" + TEST_PACK_VERSION + "-windows.zip");
packProperties.put("windows.binary-extension", "bat");
return packProperties;
}

private void givenPackCliAlreadyDownloaded() throws IOException {
File jKubeDownloadDir = new File(temporaryFolder, ".jkube");
Files.createDirectory(jKubeDownloadDir.toPath());
Expand Down
Empty file.

0 comments on commit 6a64e3b

Please sign in to comment.