11package me.devnatan.dockerkt.resource.container
22
3- import kotlinx.coroutines.delay
43import kotlinx.coroutines.test.runTest
54import kotlinx.io.files.Path
65import me.devnatan.dockerkt.io.FileSystemUtils
7- import me.devnatan.dockerkt.models.exec.ExecStartOptions
8- import me.devnatan.dockerkt.models.exec.ExecStartResult
96import me.devnatan.dockerkt.resource.ResourceIT
10- import me.devnatan.dockerkt.resource.exec.create
117import me.devnatan.dockerkt.sleepForever
128import me.devnatan.dockerkt.withContainer
139import kotlin.test.Test
@@ -24,13 +20,11 @@ class CopyContainerArchivesIT : ResourceIT() {
2420 testClient.withContainer(
2521 testImage,
2622 {
27- command = listOf (" sh" , " -c" , " echo 'test content' > /tmp/test.txt && sleep infinity " )
23+ command = listOf (" sh" , " -c" , " echo 'test content' > /tmp/test.txt" )
2824 },
2925 ) { id ->
3026 testClient.containers.start(id)
31-
32- // Wait for file to be created
33- delay(500 )
27+ testClient.containers.wait(id)
3428
3529 val tempDir = FileSystemUtils .createTempDirectory()
3630 try {
@@ -50,7 +44,6 @@ class CopyContainerArchivesIT : ResourceIT() {
5044 )
5145 } finally {
5246 FileSystemUtils .deleteRecursively(tempDir)
53- testClient.containers.stop(id)
5447 }
5548 }
5649 }
@@ -76,15 +69,22 @@ class CopyContainerArchivesIT : ResourceIT() {
7669 " /tmp/" ,
7770 )
7871
79- val execId =
80- testClient.exec.create(id) {
81- command = listOf (" cat" , " /tmp/${tempFile.name} " )
82- attachStdout = true
83- }
84-
85- val result = testClient.exec.start(execId, ExecStartOptions ())
86- assertTrue(result is ExecStartResult .Complete )
87- assertTrue(result.output.contains(" hello from host" ))
72+ val verifyDir = FileSystemUtils .createTempDirectory()
73+ try {
74+ testClient.containers.copyFileFrom(
75+ id,
76+ " /tmp/${tempFile.name} " ,
77+ verifyDir.toString(),
78+ )
79+ val copiedBack = Path (verifyDir, tempFile.name)
80+ assertTrue(FileSystemUtils .exists(copiedBack))
81+ assertEquals(
82+ expected = " hello from host" ,
83+ actual = FileSystemUtils .readFile(copiedBack).decodeToString(),
84+ )
85+ } finally {
86+ FileSystemUtils .deleteRecursively(verifyDir)
87+ }
8888 } finally {
8989 FileSystemUtils .delete(tempFile)
9090 testClient.containers.stop(id)
@@ -102,13 +102,12 @@ class CopyContainerArchivesIT : ResourceIT() {
102102 listOf (
103103 " sh" ,
104104 " -c" ,
105- " mkdir -p /tmp/testdir && echo 'file1' > /tmp/testdir/file1.txt && echo 'file2' > /tmp/testdir/file2.txt && sleep infinity " ,
105+ " mkdir -p /tmp/testdir && echo 'file1' > /tmp/testdir/file1.txt && echo 'file2' > /tmp/testdir/file2.txt" ,
106106 )
107107 },
108108 ) { id ->
109109 testClient.containers.start(id)
110-
111- delay(500 )
110+ testClient.containers.wait(id)
112111
113112 val tempDir = FileSystemUtils .createTempDirectory()
114113 try {
@@ -134,7 +133,6 @@ class CopyContainerArchivesIT : ResourceIT() {
134133 )
135134 } finally {
136135 FileSystemUtils .deleteRecursively(tempDir)
137- testClient.containers.stop(id)
138136 }
139137 }
140138 }
@@ -165,24 +163,19 @@ class CopyContainerArchivesIT : ResourceIT() {
165163 " /tmp/" ,
166164 )
167165
168- val execId =
169- testClient.exec.create(id) {
170- command = listOf (" sh" , " -c" , " cat /tmp/file1.txt && cat /tmp/file2.txt" )
171- attachStdout = true
172- }
173-
174- val result = testClient.exec.start(execId, ExecStartOptions ())
175- assertTrue(result is ExecStartResult .Complete )
176-
177- val output = result.output
178- assertTrue(
179- actual = output.contains(" content1" ),
180- message = " Expected 'content1' in output, but got: $output " ,
181- )
182- assertTrue(
183- actual = output.contains(" content2" ),
184- message = " Expected 'content2' in output, but got: $output " ,
185- )
166+ val verifyDir = FileSystemUtils .createTempDirectory()
167+ try {
168+ testClient.containers.copyFileFrom(id, " /tmp/file1.txt" , verifyDir.toString())
169+ testClient.containers.copyFileFrom(id, " /tmp/file2.txt" , verifyDir.toString())
170+ val copiedFile1 = Path (verifyDir, " file1.txt" )
171+ val copiedFile2 = Path (verifyDir, " file2.txt" )
172+ assertTrue(FileSystemUtils .exists(copiedFile1))
173+ assertTrue(FileSystemUtils .exists(copiedFile2))
174+ assertEquals(" content1" , FileSystemUtils .readFile(copiedFile1).decodeToString())
175+ assertEquals(" content2" , FileSystemUtils .readFile(copiedFile2).decodeToString())
176+ } finally {
177+ FileSystemUtils .deleteRecursively(verifyDir)
178+ }
186179 } finally {
187180 FileSystemUtils .deleteRecursively(tempDir)
188181 testClient.containers.stop(id)
0 commit comments