Skip to content

Commit

Permalink
Improve config cmd + tests
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso committed Aug 9, 2023
2 parents e1a623f + 64f19a0 commit 5bfdbf1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 27 deletions.
40 changes: 22 additions & 18 deletions app/src/main/java/io/seqera/wavelit/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ public class App implements Runnable {
@Option(names = {"--context"}, description = "Directory path where the build context is stored.")
private String contextDir;


@Option(names = {"--layer"})
private List<String> layerDirs;

@Option(names = {"--config-command"}, description = "Command to be executed in the target Wave container")
@Option(names = {"--config-cmd"}, description = "Overwrite the default CMD (command) of the image.")
private String command;

@Option(names = {"--config-entrypoint"}, description = "Overwrite the default ENTRYPOINT of the image.")
private String entrypoint;

private BuildContext buildContext;

private ContainerConfig containerConfig;
Expand Down Expand Up @@ -168,8 +170,7 @@ protected SubmitContainerTokenRequest createRequest() {
.withTowerAccessToken(towerToken)
.withTowerWorkspaceId(towerWorkspaceId)
.withTowerEndpoint(towerEndpoint)
.withFreezeMode(freeze)
;
.withFreezeMode(freeze);
}

@Override
Expand Down Expand Up @@ -234,16 +235,25 @@ protected BuildContext prepareContext() {
}

protected ContainerConfig prepareConfig() {
final ContainerConfig config = new ContainerConfig();
if( layerDirs==null || layerDirs.size()==0 )
return null;
final ContainerConfig result = new ContainerConfig();

// add the entrypoint if specified
if( entrypoint!=null )
result.entrypoint = List.of(entrypoint);

for( String it : layerDirs ) {
// add the command if specified
if( command != null ){
if( "".equals(command) ) throw new IllegalCliArgumentException("The specified command is an empty string");
result.cmd = List.of(command);
}

// add the layers to the resulting config if specified
if( layerDirs!=null ) for( String it : layerDirs ) {
final Path loc = Path.of(it);
if( !Files.isDirectory(loc) ) throw new IllegalCliArgumentException("Not a valid container layer directory - offering path: "+loc);
ContainerLayer layer;
try {
config.layers.add( layer=new Packer().layer(loc) );
result.layers.add( layer=new Packer().layer(loc) );
}
catch (IOException e ) {
throw new RuntimeException("Unexpected error while packing container layer at path: " + loc, e);
Expand All @@ -253,19 +263,13 @@ protected ContainerConfig prepareConfig() {
}
// check all size
long size = 0;
for(ContainerLayer it : config.layers ) {
for(ContainerLayer it : result.layers ) {
size += it.gzipSize;
}
if( size>=10 * _1MB )
throw new RuntimeException("Compressed container layers cannot exceed 10 MiB");

//Validate command
if(command == ""){
throw new IllegalCliArgumentException("Not a valid container command");
}
config.cmd = List.of(command);

// assign the result
return config;
// return the result
return !result.empty() ? result : null;
}
}
70 changes: 61 additions & 9 deletions app/src/test/groovy/io/seqera/wavelit/AppTest.groovy
Original file line number Diff line number Diff line change
@@ -1,33 +1,85 @@
package io.seqera.wavelit

import spock.lang.Specification
import spock.lang.Subject
import io.seqera.wave.api.ContainerConfig
import io.seqera.wavelit.exception.IllegalCliArgumentException
import picocli.CommandLine
import spock.lang.Specification

class AppTest extends Specification {

@Subject
App app = new App()
def "test valid no entrypoint"() {
given:
def app = new App()
String[] args = []
def cli = new CommandLine(app)

when:
cli.parseArgs(args)
then:
app.@entrypoint == null

when:
def config = app.prepareConfig()
then:
config == null
}


def "test valid entrypoint"() {
given:
def app = new App()
String[] args = ["--config-entrypoint", "entryPoint"]
def cli = new CommandLine(app)

when:
cli.parseArgs(args)
then:
app.@entrypoint == "entryPoint"

when:
def config = app.prepareConfig()
then:
config == new ContainerConfig(entrypoint: ['entryPoint'])
}

def "test invalid entrypoint"() {
given:
def app = new App()
String[] args = ["--config-entrypoint"]

when:
new CommandLine(app).parseArgs(args)

then:
thrown(CommandLine.MissingParameterException)
}

def "test valid command"() {
given:
String[] args = ["--config-command", "command"]
def app = new App()
String[] args = ["--config-cmd", "/some/command"]

when:
new CommandLine(app).parseArgs(args)
then:
app.@command == "/some/command"

when:
def config = app.prepareConfig()
then:
app.command == "command"
config == new ContainerConfig(cmd: ['/some/command'])
}

def "test invalid command"() {
given:
String[] args = ["--config-command"]
def app = new App()
String[] args = ["--config-cmd", ""]

when:
new CommandLine(app).parseArgs(args)

app.prepareConfig()
then:
thrown(CommandLine.MissingParameterException)
thrown(IllegalCliArgumentException)

}
}

0 comments on commit 5bfdbf1

Please sign in to comment.