-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
132 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/commonMain/kotlin/me/devnatan/yoki/resource/exec/ExecResourceExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package me.devnatan.yoki.resource.exec | ||
|
||
import me.devnatan.yoki.models.exec.ExecCreateOptions | ||
import me.devnatan.yoki.models.exec.ExecStartOptions | ||
import me.devnatan.yoki.resource.container.ContainerNotFoundException | ||
import me.devnatan.yoki.resource.container.ContainerNotRunningException | ||
|
||
/** | ||
* Creates a new container. | ||
* | ||
* @param id The container id to execute the command. | ||
* @param options Options to customize the container creation. | ||
* @throws ContainerNotFoundException If container instance is not found. | ||
* @throws ContainerNotRunningException If the container is not running. | ||
*/ | ||
public suspend inline fun ExecResource.create(id: String, options: ExecCreateOptions.() -> Unit): String { | ||
return create(id, ExecCreateOptions().apply(options)) | ||
} | ||
|
||
/** | ||
* Starts a previously set up exec instance. | ||
* | ||
* If detach is true, this endpoint returns immediately after starting the command. | ||
* Otherwise, it sets up an interactive session with the command. | ||
* | ||
* @param id The exec instance id to be started. | ||
* @param options Options to customize the exec start. | ||
* @throws ExecNotFoundException If exec instance is not found. | ||
* @throws ContainerNotRunningException If the container in which the exec instance was created is not running. | ||
*/ | ||
public suspend inline fun ExecResource.start(id: String, options: ExecStartOptions.() -> Unit = {}) { | ||
start(id, ExecStartOptions().apply(options)) | ||
} |
56 changes: 56 additions & 0 deletions
56
src/commonTest/kotlin/me/devnatan/yoki/resource/exec/ExecContainerIT.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package me.devnatan.yoki.resource.exec | ||
|
||
import kotlinx.coroutines.test.runTest | ||
import me.devnatan.yoki.resource.ResourceIT | ||
import me.devnatan.yoki.withContainer | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ExecContainerIT : ResourceIT() { | ||
|
||
@Test | ||
fun `exec a command in a container`() = runTest { | ||
testClient.withContainer( | ||
"busybox:latest", | ||
{ | ||
command = listOf("sleep", "infinity") | ||
}, | ||
) { id -> | ||
testClient.containers.start(id) | ||
|
||
val execId = testClient.exec.create(id) { | ||
command = listOf("true") | ||
} | ||
|
||
testClient.exec.start(execId) | ||
|
||
val exec = testClient.exec.inspect(execId) | ||
assertEquals(exec.exitCode, 0) | ||
|
||
testClient.containers.stop(id) | ||
} | ||
} | ||
|
||
@Test | ||
fun `exec a failing command in a container`() = runTest { | ||
testClient.withContainer( | ||
"busybox:latest", | ||
{ | ||
command = listOf("sleep", "infinity") | ||
}, | ||
) { id -> | ||
testClient.containers.start(id) | ||
|
||
val execId = testClient.exec.create(id) { | ||
command = listOf("false") | ||
} | ||
|
||
testClient.exec.start(execId) | ||
|
||
val exec = testClient.exec.inspect(execId) | ||
assertEquals(exec.exitCode, 1) | ||
|
||
testClient.containers.stop(id) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters