Skip to content

Commit 28b3dc4

Browse files
committed
refactored
Signed-off-by: munishchouhan <hrma017@gmail.com>
1 parent 5815702 commit 28b3dc4

2 files changed

Lines changed: 91 additions & 24 deletions

File tree

app/src/main/java/io/seqera/wave/cli/App.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public class App implements Runnable {
132132
@Option(names = {"--freeze", "-F"}, paramLabel = "false", description = "Request a container freeze.")
133133
private boolean freeze;
134134

135-
@Option(names = {"--platform"}, paramLabel = "''", description = "Platform to be used for the container build. One of: linux/amd64, linux/arm64, linux/amd64,linux/arm64 for multi-arch builds, or 'all'.")
135+
@Option(names = {"--platform"}, paramLabel = "''", description = "Platform to be used for the container build. One of: linux/amd64, linux/arm64. Use 'all' for multi-arch builds.")
136136
private String platform;
137137

138138
@Option(names = {"--await"}, paramLabel = "false", arity = "0..1", description = "Await the container build to be available. you can provide a timeout like --await 10m or 2s, by default its 15 minutes.")
@@ -426,17 +426,12 @@ protected void validateArgs() {
426426
if( dryRun && await != null )
427427
throw new IllegalCliArgumentException("Options --dry-run and --await conflicts each other");
428428

429-
if( !isEmpty(platform) )
430-
validatePlatform(platform);
431-
432-
}
429+
if( !isEmpty(platform) ) {
430+
platform = normalizePlatform(platform);
431+
if( platform.contains(",") )
432+
validateMultiArch();
433+
}
433434

434-
protected void validatePlatform(String platform) {
435-
// normalizePlatform validates each platform value via canonicalPlatform
436-
normalizePlatform(platform);
437-
// check multi-arch specific constraints
438-
if( PLATFORM_ALL.equals(platform) || platform.contains(",") )
439-
validateMultiArch();
440435
}
441436

442437
private void validateMultiArch() {
@@ -455,7 +450,7 @@ protected SubmitContainerTokenRequest createRequest() {
455450
.withContainerImage(image)
456451
.withContainerFile(containerFileBase64())
457452
.withPackages(packagesSpec())
458-
.withContainerPlatform(normalizePlatform(platform))
453+
.withContainerPlatform(platform)
459454
.withTimestamp(OffsetDateTime.now())
460455
.withBuildRepository(buildRepository)
461456
.withCacheRepository(cacheRepository)
@@ -484,19 +479,20 @@ protected String normalizePlatform(String platform) {
484479
return MULTI_ARCH_PLATFORM;
485480
if( platform.contains(",") ) {
486481
return Arrays.stream(platform.split(","))
487-
.map(String::trim)
488482
.map(App::canonicalPlatform)
483+
.distinct()
489484
.collect(Collectors.joining(","));
490485
}
491486
return canonicalPlatform(platform);
492487
}
493488

494489
private static String canonicalPlatform(String value) {
495-
if( AMD64_ALIASES.contains(value) )
490+
final String v = value.trim();
491+
if( AMD64_ALIASES.contains(v) )
496492
return "linux/amd64";
497-
if( ARM64_ALIASES.contains(value) )
493+
if( ARM64_ALIASES.contains(v) )
498494
return "linux/arm64";
499-
throw new IllegalCliArgumentException(String.format("Unsupported container platform: '%s'", value));
495+
throw new IllegalCliArgumentException(String.format("Unsupported container platform: '%s'", v));
500496
}
501497

502498
BuildCompression compression(BuildCompression.Mode mode) {

app/src/test/groovy/io/seqera/wave/cli/AppTest.groovy

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,16 @@ class AppTest extends Specification {
388388
and:
389389
app.validateArgs()
390390
then:
391-
app.@platform == PLATFORM
391+
app.@platform == EXPECTED
392392

393393
where:
394-
PLATFORM || _
395-
'amd64' || _
396-
'x86_64' || _
397-
'arm64' || _
398-
'linux/amd64' || _
399-
'linux/x86_64' || _
400-
'linux/arm64' || _
394+
PLATFORM || EXPECTED
395+
'amd64' || 'linux/amd64'
396+
'x86_64' || 'linux/amd64'
397+
'arm64' || 'linux/arm64'
398+
'linux/amd64' || 'linux/amd64'
399+
'linux/x86_64' || 'linux/amd64'
400+
'linux/arm64' || 'linux/arm64'
401401
}
402402

403403
@Unroll
@@ -786,6 +786,77 @@ class AppTest extends Specification {
786786
noExceptionThrown()
787787
}
788788

789+
def 'should normalize platform with whitespace in comma-separated values' () {
790+
given:
791+
def folder = Files.createTempDirectory('test')
792+
def containerFile = folder.resolve('Dockerfile')
793+
containerFile.text = 'FROM ubuntu:latest'
794+
and:
795+
def app = new App()
796+
String[] args = ["-f", containerFile.toString(), "--platform", "linux/amd64, linux/arm64"]
797+
798+
when:
799+
new CommandLine(app).parseArgs(args)
800+
and:
801+
app.validateArgs()
802+
then:
803+
noExceptionThrown()
804+
805+
when:
806+
def req = app.createRequest()
807+
then:
808+
req.containerPlatform == 'linux/amd64,linux/arm64'
809+
810+
cleanup:
811+
folder?.deleteDir()
812+
}
813+
814+
def 'should fail platform all with singularity' () {
815+
given:
816+
def folder = Files.createTempDirectory('test')
817+
def containerFile = folder.resolve('Dockerfile')
818+
containerFile.text = 'FROM ubuntu:latest'
819+
and:
820+
def app = new App()
821+
String[] args = ["-f", containerFile.toString(), "--platform", "all", "--singularity", "--freeze", "--build-repo", "docker.io/foo", "--tower-token", "xyz"]
822+
823+
when:
824+
new CommandLine(app).parseArgs(args)
825+
and:
826+
app.validateArgs()
827+
then:
828+
def e = thrown(IllegalCliArgumentException)
829+
e.message == 'Multi-arch build is not compatible with Singularity format'
830+
831+
cleanup:
832+
folder?.deleteDir()
833+
}
834+
835+
def 'should deduplicate repeated platforms' () {
836+
given:
837+
def folder = Files.createTempDirectory('test')
838+
def containerFile = folder.resolve('Dockerfile')
839+
containerFile.text = 'FROM ubuntu:latest'
840+
and:
841+
def app = new App()
842+
String[] args = ["-f", containerFile.toString(), "--platform", "amd64,amd64"]
843+
844+
when:
845+
new CommandLine(app).parseArgs(args)
846+
and:
847+
app.validateArgs()
848+
then:
849+
noExceptionThrown()
850+
851+
when:
852+
def req = app.createRequest()
853+
then:
854+
req.containerPlatform == 'linux/amd64'
855+
856+
cleanup:
857+
folder?.deleteDir()
858+
}
859+
789860
def 'should fail when comma-separated platform contains invalid value' () {
790861
given:
791862
def folder = Files.createTempDirectory('test')

0 commit comments

Comments
 (0)