Skip to content

Commit

Permalink
refactor: introduce JibService abstraction layer for build
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa authored May 22, 2024
1 parent bfade49 commit 225d80b
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 858 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@
*/
package org.eclipse.jkube.kit.build.api.helper;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.jkube.kit.build.api.helper.RegistryUtil.getApplicablePullRegistryFrom;
import static org.eclipse.jkube.kit.build.api.helper.RegistryUtil.getApplicablePushRegistryFrom;

class RegistryUtilTest {
@Test
void getApplicablePushRegistryFrom_whenImageNameContainsRegistry_thenUseRegistryFromImageName() {
// Given
ImageConfiguration imageConfiguration = createNewImageConfiguration("word.word/word/word:tag", null);
RegistryConfig registryConfig = createNewRegistryConfig();

final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name("word.word/word/word:tag")
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().registry("registry-config.io").build();
// When + Then
assertThat(RegistryUtil.getApplicablePushRegistryFrom(imageConfiguration, registryConfig))
.isEqualTo("word.word");
Expand All @@ -34,9 +40,11 @@ void getApplicablePushRegistryFrom_whenImageNameContainsRegistry_thenUseRegistry
@Test
void getApplicablePushRegistryFrom_whenImageNameHasNoRegistryAndImageConfigHasRegistry_thenUseImageConfigRegistry() {
// Given
ImageConfiguration imageConfiguration = createNewImageConfiguration("word/word:tag", "word.word");
RegistryConfig registryConfig = createNewRegistryConfig();

final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name("word/word:tag")
.registry("word.word")
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().registry("registry-config.io").build();
// When + Then
assertThat(RegistryUtil.getApplicablePushRegistryFrom(imageConfiguration, registryConfig))
.isEqualTo("word.word");
Expand All @@ -45,9 +53,10 @@ void getApplicablePushRegistryFrom_whenImageNameHasNoRegistryAndImageConfigHasRe
@Test
void getApplicablePushRegistryFrom_whenImageNameNotFullyQualified_thenGivePriorityToOtherSources() {
// Given
ImageConfiguration imageConfiguration = createNewImageConfiguration("word.word/word:tag", null);
RegistryConfig registryConfig = createNewRegistryConfig();

final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name("word.word/word:tag")
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().registry("registry-config.io").build();
// When + Then
assertThat(RegistryUtil.getApplicablePushRegistryFrom(imageConfiguration, registryConfig))
.isEqualTo("registry-config.io");
Expand All @@ -56,9 +65,10 @@ void getApplicablePushRegistryFrom_whenImageNameNotFullyQualified_thenGivePriori
@Test
void getApplicablePushRegistryFrom_whenNoRegistryInImageConfig_thenUseImageConfigRegistry() {
// Given
ImageConfiguration imageConfiguration = createNewImageConfiguration("word/word:tag", null);
RegistryConfig registryConfig = createNewRegistryConfig();

final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name("word/word:tag")
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().registry("registry-config.io").build();
// When + Then
assertThat(RegistryUtil.getApplicablePushRegistryFrom(imageConfiguration, registryConfig))
.isEqualTo("registry-config.io");
Expand All @@ -67,8 +77,7 @@ void getApplicablePushRegistryFrom_whenNoRegistryInImageConfig_thenUseImageConfi
@Test
void getApplicablePullRegistryFrom_whenRegistryInImageName_thenUseRegistryFromImageName() {
// Given
RegistryConfig registryConfig = createNewRegistryConfig();

final RegistryConfig registryConfig = RegistryConfig.builder().registry("registry-config.io").build();
// When + Then
assertThat(RegistryUtil.getApplicablePullRegistryFrom("word.word/word/word:tag", registryConfig))
.isEqualTo("word.word");
Expand All @@ -77,8 +86,7 @@ void getApplicablePullRegistryFrom_whenRegistryInImageName_thenUseRegistryFromIm
@Test
void getApplicablePullRegistryFrom_whenRegistryInRegistryConfig_thenUseRegistryFromRegistryConfig() {
// Given
RegistryConfig registryConfig = createNewRegistryConfig();

final RegistryConfig registryConfig = RegistryConfig.builder().registry("registry-config.io").build();
// When + Then
assertThat(RegistryUtil.getApplicablePullRegistryFrom("word:tag", registryConfig))
.isEqualTo("registry-config.io");
Expand All @@ -87,25 +95,170 @@ void getApplicablePullRegistryFrom_whenRegistryInRegistryConfig_thenUseRegistryF
@Test
void getApplicablePullRegistryFrom_whenRegistryInRegistryConfigAndImageNameNotFullyQualified_thenUseRegistryFromRegistryConfig() {
// Given
RegistryConfig registryConfig = createNewRegistryConfig();

final RegistryConfig registryConfig = RegistryConfig.builder().registry("registry-config.io").build();
// When + Then
assertThat(RegistryUtil.getApplicablePullRegistryFrom("word.word/word:tag", registryConfig))
.isEqualTo("registry-config.io");
}

private ImageConfiguration createNewImageConfiguration(String name, String registry) {
ImageConfiguration.ImageConfigurationBuilder imageConfigurationBuilder = ImageConfiguration.builder();
imageConfigurationBuilder.name(name);
if (StringUtils.isNotBlank(registry)) {
imageConfigurationBuilder.registry(registry);
}
return imageConfigurationBuilder.build();
@ParameterizedTest(name = "pull {0}, when no registry from any source, then pull registry {1}")
@CsvSource({
"word:word,",
"word/word:tag,",
"word.word/word/word:tag,word.word",
"word:5000/word:tag,word:5000",
"word.word:5000/word:tag,word.word:5000",
"word.word/word:tag,word.word",
"word.word/word.word/word:tag,word.word",
"word.word.word/word:tag,word.word.word",
"word.word.word/word/word:tag,word.word.word"
})
void pull_whenRegistryNotPresentFromAnySource_thenReturnRegistryFromImageName(String from, String expectedPullRegistry) {
// Given
final RegistryConfig registryConfig = RegistryConfig.builder().build();
// When
final String pullRegistry = getApplicablePullRegistryFrom(from, registryConfig);
// Then
assertThat(pullRegistry).isEqualTo(expectedPullRegistry);
}

private RegistryConfig createNewRegistryConfig() {
return RegistryConfig.builder()
.registry("registry-config.io")
.build();
@ParameterizedTest(name = "pull {0} when registry present both in image name and registry config, then registry taken from image name {1}")
@CsvSource({
"word.word/word/word:tag,word.word",
"word:5000/word:tag,word:5000",
"word.word:5000/word:tag,word.word:5000",
"word.word/word.word/word:tag,word.word",
"word.word.word/word/word:tag,word.word.word"
})
void pull_whenRegistryPresentInBothImageNameAndRegistryConfig_thenReturnRegistryFromImageName(String from, String expectedPullRegistry) {
// Given
final RegistryConfig registryConfig = RegistryConfig.builder().registry("quay.io").build();
// When
final String pullRegistry = getApplicablePullRegistryFrom(from, registryConfig);
// Then
assertThat(pullRegistry).isEqualTo(expectedPullRegistry);
}
}

@ParameterizedTest(name = "pull {0} and registry from registry config, then registry from registry config {1}")
@CsvSource({
"word:word,quay.io",
"word/word:tag,quay.io",
"word.word/word:tag,quay.io"
})
void pull_whenRegistryFromRegistryConfig_thenReturnRegistryFromRegistryConfig(String from, String expectedPullRegistry) {
// Given
final RegistryConfig registryConfig = RegistryConfig.builder().registry("quay.io").build();
// When
final String pullRegistry = getApplicablePullRegistryFrom(from, registryConfig);
// Then
assertThat(pullRegistry).isEqualTo(expectedPullRegistry);
}

@ParameterizedTest(name = "push {0} when no registry from any source, then push registry from image name {1}")
@CsvSource({
"word:word,",
"word/word:tag,",
"word.word/word:tag,word.word",
"word.word/word/word:tag,word.word",
"word.word/word.word/word:tag,word.word",
"word:5000/word:tag,word:5000",
"word.word:5000/word:tag,word.word:5000",
"word.word.word/word:tag,word.word.word",
"word.word.word/word/word:tag,word.word.word"
})
void push_whenRegistryNotPresentFromAnySource_thenReturnRegistryFromImageName(String imageName, String expectedPushRegistry) {
// Given
final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name(imageName)
.build(BuildConfiguration.builder().from("test-image:latest").build())
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().build();
// When
final String pushRegistry = getApplicablePushRegistryFrom(imageConfiguration, registryConfig);
// Then
assertThat(pushRegistry).isEqualTo(expectedPushRegistry);
}

@ParameterizedTest(name = "push {0} when registry present both in image name and registry config, then registry taken from image name {1}")
@CsvSource({
"word.word/word/word:tag,word.word",
"word.word/word.word/word:tag,word.word",
"word:5000/word:tag,word:5000",
"word.word:5000/word:tag,word.word:5000",
"word.word.word/word/word:tag,word.word.word"
})
void push_whenRegistryInBothImageNameAndRegistryConfig_thenUseRegistryFromImageName(String imageName, String expectedPushRegistry) {
// Given
final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name(imageName)
.build(BuildConfiguration.builder().from("test-image:latest").build())
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().registry("quay.io").build();
// When
final String pushRegistry = getApplicablePushRegistryFrom(imageConfiguration, registryConfig);
// Then
assertThat(pushRegistry).isEqualTo(expectedPushRegistry);
}

@ParameterizedTest(name = "push {0} when registry present both in image name and image config, then registry taken from image name {1}")
@CsvSource({
"word.word/word/word:tag,word.word",
"word.word/word.word/word:tag,word.word",
"word:5000/word:tag,word:5000",
"word.word:5000/word:tag,word.word:5000",
"word.word.word/word/word:tag,word.word.word"
})
void push_whenRegistryPresentInBothImageNameAndImageConfig_thenUseRegistryFromImageName(String imageName, String expectedPushRegistry) {
// Given
final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name(imageName)
.build(BuildConfiguration.builder().from("test-image:latest").build())
.registry("quay.io")
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().build();
// When
final String pushRegistry = getApplicablePushRegistryFrom(imageConfiguration, registryConfig);
// Then
assertThat(pushRegistry).isEqualTo(expectedPushRegistry);
}

@ParameterizedTest(name = "push {0} when registry from image config, then registry used from image config {1}")
@CsvSource({
"word:word,quay.io",
"word/word:tag,quay.io",
"word.word/word:tag,quay.io"
})
void push_whenRegistryFromImageConfig_thenReturnRegistryFromImageConfig(String imageName, String expectedPushRegistry) {
// Given
final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name(imageName)
.build(BuildConfiguration.builder().from("test-image:latest").build())
.registry("quay.io")
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().build();
// When
final String pushRegistry = getApplicablePushRegistryFrom(imageConfiguration, registryConfig);
// Then
assertThat(pushRegistry).isEqualTo(expectedPushRegistry);
}

@ParameterizedTest(name = "push {0} when registry from registry config, then registry used from registry config {1}")
@CsvSource({
"word:word,quay.io",
"word/word:tag,quay.io",
"word.word/word:tag,quay.io"
})
void push_whenRegistryFromRegistryConfig_thenReturnRegistryFromRegistryConfig(String imageName, String expectedPushRegistry) {
// Given
final ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name(imageName)
.build(BuildConfiguration.builder().from("test-image:latest").build())
.build();
final RegistryConfig registryConfig = RegistryConfig.builder().registry("quay.io").build();
// When
final String pushRegistry = getApplicablePushRegistryFrom(imageConfiguration, registryConfig);
// Then
assertThat(pushRegistry).isEqualTo(expectedPushRegistry);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class JibLogger implements Consumer<LogEvent> {
private static final int PROGRESS_BAR_COUNT = 30;
public static final String JIB_LOG_PREFIX = "JIB> ";

private final KitLogger logger;
// Shouldn't be exposed, however, it's needed by AssemblyManager
final KitLogger logger;
private final PrintStream out;

public JibLogger(KitLogger logger) {
Expand Down
Loading

0 comments on commit 225d80b

Please sign in to comment.