From 581badb351f9b7eefb7a2cffd862d294b96f120d Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Wed, 13 Jan 2016 22:36:10 +0100 Subject: [PATCH] allow to override builder parameters --- .../docker/tasks/DockerBuildTask.groovy | 14 ++++++++-- .../gesellix/gradle/docker/PortFinder.groovy | 28 +++++++++++++++++++ .../docker/tasks/DockerBuildTaskSpec.groovy | 21 ++++++++++++++ .../tasks/DockerContainerTaskSpec.groovy | 21 +++++++------- 4 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 src/test/groovy/de/gesellix/gradle/docker/PortFinder.groovy diff --git a/src/main/groovy/de/gesellix/gradle/docker/tasks/DockerBuildTask.groovy b/src/main/groovy/de/gesellix/gradle/docker/tasks/DockerBuildTask.groovy index ad67fbb9..0d5f7051 100644 --- a/src/main/groovy/de/gesellix/gradle/docker/tasks/DockerBuildTask.groovy +++ b/src/main/groovy/de/gesellix/gradle/docker/tasks/DockerBuildTask.groovy @@ -22,6 +22,9 @@ class DockerBuildTask extends DockerTask { @InputDirectory @Optional File buildContextDirectory + @Input + @Optional + def buildParams def tarOfBuildcontextTask def targetFile @@ -62,8 +65,11 @@ class DockerBuildTask extends DockerTask { private def configureTarBuildContextTask() { if (tarOfBuildcontextTask == null) { targetFile = new File(getTemporaryDir(), "buildContext_${getNormalizedImageName()}.tar.gz") - tarOfBuildcontextTask = project.task([group: getGroup()], "tarBuildcontextFor${name.capitalize()}").doLast { + tarOfBuildcontextTask = project.task([group: getGroup()], "tarBuildcontextFor${name.capitalize()}") + tarOfBuildcontextTask.doFirst { (targetFile as File).parentFile.mkdirs() + } + tarOfBuildcontextTask.doLast { BuildContextBuilder.archiveTarFilesRecursively(getBuildContextDirectory(), targetFile) } tarOfBuildcontextTask.outputs.file(targetFile.absolutePath) @@ -88,7 +94,11 @@ class DockerBuildTask extends DockerTask { // at this point we need the buildContext assert getBuildContext() - imageId = getDockerClient().build(getBuildContext()) + if (getBuildParams()) { + imageId = getDockerClient().build(getBuildContext(), getBuildParams()) + } else { + imageId = getDockerClient().build(getBuildContext()) + } if (getImageName()) { logger.info "tag $imageId as '${getImageName()}'..." getDockerClient().tag(imageId, getImageName(), true) diff --git a/src/test/groovy/de/gesellix/gradle/docker/PortFinder.groovy b/src/test/groovy/de/gesellix/gradle/docker/PortFinder.groovy new file mode 100644 index 00000000..14d4f7c3 --- /dev/null +++ b/src/test/groovy/de/gesellix/gradle/docker/PortFinder.groovy @@ -0,0 +1,28 @@ +package de.gesellix.gradle.docker + +class PortFinder { + + static int findFreePort() { + ServerSocket socket = null + try { + socket = new ServerSocket(0) + socket.setReuseAddress(true) + int port = socket.getLocalPort() + try { + socket.close() + } catch (IOException ignore) { + // Ignore IOException on close() + } + return port + } catch (IOException ignore) { + } finally { + if (socket != null) { + try { + socket.close() + } catch (IOException ignore) { + } + } + } + throw new IllegalStateException("Could not find a free TCP/IP port") + } +} diff --git a/src/test/groovy/de/gesellix/gradle/docker/tasks/DockerBuildTaskSpec.groovy b/src/test/groovy/de/gesellix/gradle/docker/tasks/DockerBuildTaskSpec.groovy index 8bbc33e8..d334f9c6 100644 --- a/src/test/groovy/de/gesellix/gradle/docker/tasks/DockerBuildTaskSpec.groovy +++ b/src/test/groovy/de/gesellix/gradle/docker/tasks/DockerBuildTaskSpec.groovy @@ -115,6 +115,27 @@ class DockerBuildTaskSpec extends Specification { task.outputs.files.isEmpty() } + def "delegates to dockerClient with buildContext and buildParams"() { + def inputStream = new FileInputStream(File.createTempFile("docker", "test")) + + given: + task.buildContext = inputStream + task.buildParams = [rm: true, dockerfile: './custom.Dockerfile'] + task.imageName = "imageName" + + when: + task.execute() + + then: + 1 * dockerClient.build(inputStream, [rm: true, dockerfile: './custom.Dockerfile']) >> "4711" + + then: + 1 * dockerClient.tag("4711", "imageName", true) + + and: + task.outputs.files.isEmpty() + } + // TODO this should become an integration test def "accepts only task configs with at least one of buildContext or buildContextDirectory"() { given: diff --git a/src/test/groovy/de/gesellix/gradle/docker/tasks/DockerContainerTaskSpec.groovy b/src/test/groovy/de/gesellix/gradle/docker/tasks/DockerContainerTaskSpec.groovy index 4430ad5c..e765b193 100644 --- a/src/test/groovy/de/gesellix/gradle/docker/tasks/DockerContainerTaskSpec.groovy +++ b/src/test/groovy/de/gesellix/gradle/docker/tasks/DockerContainerTaskSpec.groovy @@ -1,9 +1,9 @@ package de.gesellix.gradle.docker.tasks import de.gesellix.docker.client.DockerClient +import de.gesellix.gradle.docker.PortFinder import org.gradle.api.GradleException import org.gradle.testfixtures.ProjectBuilder -import org.gradle.util.AvailablePortFinder import spock.lang.Specification class DockerContainerTaskSpec extends Specification { @@ -465,7 +465,7 @@ class DockerContainerTaskSpec extends Specification { def "healthcheck on already running container - timeout"() { given: - int port = AvailablePortFinder.createPrivate().nextAvailable + int port = PortFinder.findFreePort() when: task.dockerHost = "tcp://127.0.0.1:999" @@ -510,26 +510,26 @@ class DockerContainerTaskSpec extends Specification { def "healthcheck on already running container"() { given: - int port = 0 - Thread server = new Thread() { boolean initialized = false int serverPort = 0 + boolean stopped = false public void run() { - ServerSocket ss; + ServerSocket ss synchronized (this) { - serverPort = AvailablePortFinder.createPrivate().nextAvailable - ss = new ServerSocket(serverPort); + serverPort = PortFinder.findFreePort() + ss = new ServerSocket(serverPort) initialized = true notify() } - while (true) { - Socket client = ss.accept(); + while (!stopped) { + ss.accept() } } } + def port server.start() synchronized (server) { if (!server.initialized) { @@ -574,6 +574,7 @@ class DockerContainerTaskSpec extends Specification { task.changed == false cleanup: - server.stop(); + server.stopped = true + server.stop() } }