Skip to content

Commit

Permalink
Merge pull request #112 from KenSuenobu/ticket-54
Browse files Browse the repository at this point in the history
Ticket 54
  • Loading branch information
KenSuenobu authored Jul 14, 2018
2 parents 15edf0f + 344503a commit 11e66c5
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ jobs:
docker:
- image: circleci/java:latest
steps:
- setup_remote_docker
- checkout
- run:
name: Compile the project
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* _____ __ __ __
* / ___/_________ _/ /_/ /____ ______________ / /_ ___ ________
* \__ \/ ___/ __ `/ __/ __/ _ \/ ___/ ___/ __ \/ __ \/ _ \/ ___/ _ \
* ___/ / /__/ /_/ / /_/ /_/ __/ / (__ ) /_/ / / / / __/ / / __/
* /____/\___/\__,_/\__/\__/\___/_/ /____/ .___/_/ /_/\___/_/ \___/
* /_/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.buildfactory.scattersphere.tasks

import io.buildfactory.scattersphere.core.util.RunnableTask
import io.buildfactory.scattersphere.core.util.logging.SimpleLogger

import scala.sys.process.{Process, ProcessBuilder}

/** [[RunnableTask]] that runs a docker process with a container name, and the given command, in --net host mode
* ("--net host") in docker. These flags can be overridden, but it is not recommended that this be done unless you know
* what you're doing!
*
* @param containerName name of the container to use
* @param command the command to run from the referenced container
*/
class DockerTask(containerName: String, command: String) extends RunnableTask with SimpleLogger {

private var processBuilder: ProcessBuilder = null
private var process: Process = null
private var dockerFlags: String = "--net host"

override def run(): Unit = {
logger.debug(s"Running command ${command}")
processBuilder = Process(s"docker run $dockerFlags $containerName $command")
process = processBuilder.run
}

/** Retrieves the current docker flags.
*
* @return docker flags
*/
def getDockerFlags(): String = dockerFlags

/** Sets the docker flags to use - this will override the default "--net host" flags, so if you still need to use them,
* make sure to include them in the flags statement.
*
* @param flags the flags to send to Docker.
*/
def setDockerFlags(flags: String): Unit = dockerFlags = flags

/** Returns the output (stdout) from the process as a stream of [[String]] data.
*
* @return stream of string objects
*/
def getProcessOutput(): Stream[String] = processBuilder.lineStream

override def onFinished(): Unit = {
logger.trace(s"Command finished: ${command}")

logger.info("Process is still running; terminating.")
process.destroy()

logger.debug(s"Command exit code: ${process.exitValue()}")
}

override def onException(t: Throwable): Unit = {
logger.debug("Exception occurred during run", t)

logger.debug("Process is still running; terminating.")
process.destroy()
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* _____ __ __ __
* / ___/_________ _/ /_/ /____ ______________ / /_ ___ ________
* \__ \/ ___/ __ `/ __/ __/ _ \/ ___/ ___/ __ \/ __ \/ _ \/ ___/ _ \
* ___/ / /__/ /_/ / /_/ /_/ __/ / (__ ) /_/ / / / / __/ / / __/
* /____/\___/\__,_/\__/\__/\___/_/ /____/ .___/_/ /_/\___/_/ \___/
* /_/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.buildfactory.scattersphere.tasks

import io.buildfactory.scattersphere.core.util.execution.JobExecutor
import io.buildfactory.scattersphere.core.util._
import io.buildfactory.scattersphere.core.util.{Job, JobBuilder, Task, TaskBuilder}
import io.buildfactory.scattersphere.core.util.logging.SimpleLogger
import org.scalatest.{FlatSpec, Matchers}

class DockerTaskTest extends FlatSpec with Matchers with SimpleLogger {

"Docker task" should "be able to get ubuntu and run a command within its container" in {
val dockerTask: DockerTask = new DockerTask("centos", "/bin/ls -al /etc/")

dockerTask.getDockerFlags() shouldBe "--net host"

val sTask1: Task = TaskBuilder("DockerTask1").withTask(dockerTask).build()
val job: Job = JobBuilder().withTasks(sTask1).build()
val jExec: JobExecutor = JobExecutor(job)

jExec.queue().run()
job.status shouldBe JobFinished

val output: List[String] = dockerTask.getProcessOutput().toList

assert(output.size > 1)
assert(output(0).toLowerCase() contains "total")
}

it should "be able to run ps -axwww" in {
val dockerTask: DockerTask = new DockerTask("centos", "/bin/ps -axwww")

dockerTask.getDockerFlags() shouldBe "--net host"

val sTask1: Task = TaskBuilder("DockerTaskPs").withTask(dockerTask).build()
val job: Job = JobBuilder().withTasks(sTask1).build()
val jExec: JobExecutor = JobExecutor(job)

jExec.queue().run()
job.status shouldBe JobFinished

val output: List[String] = dockerTask.getProcessOutput().toList

assert(output.size > 1)
assert(output(1).toLowerCase() contains "ps -axwww")
}

it should "be able to run curl with --net host flags" in {
val dockerTask: DockerTask = new DockerTask("centos", "curl http://www.google.com/")

dockerTask.getDockerFlags() shouldBe "--net host"

val sTask1: Task = TaskBuilder("centosTask3").withTask(dockerTask).build()
val job: Job = JobBuilder().withTasks(sTask1).build()
val jExec: JobExecutor = JobExecutor(job)

jExec.queue().run()
job.status shouldBe JobFinished

val output: List[String] = dockerTask.getProcessOutput().toList

assert(output.size > 1)
}

}

0 comments on commit 11e66c5

Please sign in to comment.