Skip to content

Conversation

@s-yonkov-yonkov
Copy link
Contributor

Add support for CNB

Original PR: #1560

s-yonkov-yonkov and others added 2 commits June 4, 2025 12:59
* add CNB type

* add validation for cnb

* add validation and tests

* update lifecycle to be nullable

* refactoring

* add tests

* update validation
try {
return LifecycleType.valueOf(lifecycleValue.toUpperCase());
} catch (IllegalArgumentException e) {
throw new SLException("Unsupported lifecycle value: " + lifecycleValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract message into constant and avoid string concatenation, just pass lifecycleValue as second argument to SLException constructor. Actually this type of issue is related with user content, it's not error from our side, so this can be org.cloudfoundry.multiapps.common.ContentException instead SLException.

}

private void validateLifecycleType(LifecycleType lifecycleType, List<String> buildpacks, DockerInfo dockerInfo) {
if (lifecycleType == LifecycleType.CNB && (buildpacks == null || buildpacks.isEmpty())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use CollectionUtils to check for empty/null list.

Comment on lines 68 to 80
throw new SLException("Buildpacks must be provided when lifecycle is set to 'cnb'.");
}
// Validate Docker-specific conditions
if (lifecycleType == LifecycleType.DOCKER) {
if (dockerInfo == null) {
throw new SLException("Docker information must be provided when lifecycle is set to 'docker'.");
}
if (buildpacks != null && !buildpacks.isEmpty()) {
throw new SLException("Buildpacks must not be provided when lifecycle is set to 'docker'.");
}
} else if (dockerInfo != null && lifecycleType != null) {
throw new SLException("Docker information must not be provided when lifecycle is set to " + lifecycleType + "'.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Extract string messages into Messages class.
  • Replace throwing SLException with ContentException, this should avoid automatic retry from multiapps cli plugin when error is related with content of mta descriptor.
  • Try to avoid nested if blocks by extracting logic into new methods.

void testValidateLifecycleWithCnbAndValidBuildpacks() {
parametersList.add(Collections.singletonMap("lifecycle", "cnb"));
parametersList.add(Collections.singletonMap("buildpacks", List.of("custom-buildpack-url")));
assertDoesNotThrow(() -> parser.parse(parametersList));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to validate fields of parsed object that includes proper values?

@Test
void testValidateLifecycleWithDockerAndValidDockerInfo() {
parametersList.add(getDockerParams());
assertDoesNotThrow(() -> parser.parse(parametersList));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

@Test
void testValidateLifecycleWithBuildpackAndNoBuildpacks() {
parametersList.add(Collections.singletonMap("lifecycle", "buildpack"));
assertDoesNotThrow(() -> parser.parse(parametersList));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

Comment on lines 61 to 63
} catch (IllegalArgumentException e) {
throw new SLException("Unsupported lifecycle value: " + lifecycleValue);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract the error message as a constant and use MessageFormat.format instead of string concatenation

Comment on lines 72 to 77
if (dockerInfo == null) {
throw new SLException("Docker information must be provided when lifecycle is set to 'docker'.");
}
if (buildpacks != null && !buildpacks.isEmpty()) {
throw new SLException("Buildpacks must not be provided when lifecycle is set to 'docker'.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract the error messages

Comment on lines 78 to 79
} else if (dockerInfo != null && lifecycleType != null) {
throw new SLException("Docker information must not be provided when lifecycle is set to " + lifecycleType + "'.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, extract message and use MessageFormat.format

Comment on lines 39 to 40
assertEquals("Buildpacks must be provided when lifecycle is set to 'cnb'.", exception.getMessage());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract the expected error message

Comment on lines 54 to 55
assertEquals("Buildpacks must not be provided when lifecycle is set to 'docker'.", exception.getMessage());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, extract the expected error message

Comment on lines 28 to 32
@Test
void testValidateLifecycleWithCnbAndValidBuildpacks() {
parametersList.add(Collections.singletonMap("lifecycle", "cnb"));
parametersList.add(Collections.singletonMap("buildpacks", List.of("custom-buildpack-url")));
assertDoesNotThrow(() -> parser.parse(parametersList));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract strings like "buildpacks", "lifecycle" and "cnb" as constants to avoid duplication

Comment on lines 66 to 73
Arguments.of(ImmutableStaging.builder().addBuildpack("buildpack-1").command("command1").build(),
ImmutableStaging.builder().addBuildpack("buildpack-1").command("command2").build(), true),
ImmutableStaging.builder().addBuildpack("buildpack-1").command("command2").build(), true, LifecycleType.BUILDPACK),
Arguments.of(ImmutableStaging.builder().addBuildpack("buildpack-1").build(),
ImmutableStaging.builder().addBuildpack("buildpack-1").build(), false),
ImmutableStaging.builder().addBuildpack("buildpack-1").build(), false, LifecycleType.BUILDPACK),
Arguments.of(
ImmutableStaging.builder().addBuildpack("buildpack-1").command("command1").stackName("stack1")
.healthCheckTimeout(5).healthCheckType("process").isSshEnabled(false).build(),
ImmutableStaging.builder().addBuildpack("buildpack-2").command("command2").stackName("stack2")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract repeated strings like "buildpack-1", "command1", "stack1" and others into constants

theghost5800
theghost5800 previously approved these changes Jun 6, 2025
Copy link
Contributor

@theghost5800 theghost5800 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before merge adopt released version of cf-java-client-sap and squash commits into one

return null;
}
try {
return LifecycleType.valueOf(lifecycleValue.toUpperCase());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use the method LifecycleType::from ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jun 9, 2025

@s-yonkov-yonkov s-yonkov-yonkov merged commit b89298e into master Jun 9, 2025
7 of 8 checks passed
@s-yonkov-yonkov s-yonkov-yonkov deleted the add-support-for-cnb branch June 9, 2025 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants