|
17 | 17 | import Foundation |
18 | 18 | import Testing |
19 | 19 |
|
20 | | -@Suite(.serialSuites) |
| 20 | +@Suite |
21 | 21 | class TestCLIClean: CLITest { |
| 22 | + private struct StatusJSON: Codable { |
| 23 | + let appRoot: String |
| 24 | + } |
| 25 | + |
22 | 26 | private func getTestName() -> String { |
23 | 27 | Test.current!.name.trimmingCharacters(in: ["(", ")"]).lowercased() |
24 | 28 | } |
25 | 29 |
|
26 | | - @Test func testCleanRunningContainer() throws { |
27 | | - let name = getTestName() |
28 | | - try doLongRun(name: name, autoRemove: false) |
29 | | - defer { |
30 | | - try? doStop(name: name) |
31 | | - try? doRemove(name: name) |
| 30 | + private func appRoot() throws -> URL { |
| 31 | + let result = try run(arguments: ["system", "status", "--format", "json"]).check() |
| 32 | + let status = try JSONDecoder().decode(StatusJSON.self, from: result.outputData) |
| 33 | + return URL(fileURLWithPath: status.appRoot, isDirectory: true) |
| 34 | + } |
| 35 | + |
| 36 | + private func allocatedBytes(at url: URL) throws -> Int64 { |
| 37 | + let values = try url.resourceValues(forKeys: [.fileAllocatedSizeKey, .totalFileAllocatedSizeKey]) |
| 38 | + let allocated = values.totalFileAllocatedSize ?? values.fileAllocatedSize |
| 39 | + guard let allocated else { |
| 40 | + throw CLIError.executionFailed("failed to read allocated size for \(url.path)") |
32 | 41 | } |
| 42 | + return Int64(allocated) |
| 43 | + } |
33 | 44 |
|
34 | | - try waitForContainerRunning(name) |
| 45 | + private func containerRootfsBlockURL(name: String) throws -> URL { |
| 46 | + let id = try getContainerId(name) |
| 47 | + return try appRoot() |
| 48 | + .appendingPathComponent("containers", isDirectory: true) |
| 49 | + .appendingPathComponent(id, isDirectory: true) |
| 50 | + .appendingPathComponent("rootfs.ext4", isDirectory: false) |
| 51 | + } |
35 | 52 |
|
36 | | - // Clean should succeed on running container |
37 | | - try doClean(name: name) |
| 53 | + private func volumeBlockURL(name: String) throws -> URL { |
| 54 | + try appRoot() |
| 55 | + .appendingPathComponent("volumes", isDirectory: true) |
| 56 | + .appendingPathComponent(name, isDirectory: true) |
| 57 | + .appendingPathComponent("volume.img", isDirectory: false) |
| 58 | + } |
38 | 59 |
|
39 | | - // Container should still be running after clean |
40 | | - let status = try getContainerStatus(name) |
41 | | - #expect(status == "running") |
| 60 | + private func assertCleanReclaimedSpace(beforeWrite: Int64, afterWrite: Int64, afterClean: Int64) { |
| 61 | + let writeAllocated = afterWrite - beforeWrite |
| 62 | + #expect(writeAllocated > 0) |
| 63 | + |
| 64 | + let reclaimed = afterWrite - afterClean |
| 65 | + #expect(reclaimed > 0) |
| 66 | + |
| 67 | + let minExpectedReclaimed = Int64(Double(writeAllocated) * 0.8) |
| 68 | + #expect(reclaimed >= minExpectedReclaimed) |
42 | 69 | } |
43 | 70 |
|
44 | 71 | @Test func testCleanStoppedContainerFails() throws { |
@@ -95,12 +122,21 @@ class TestCLIClean: CLITest { |
95 | 122 |
|
96 | 123 | try waitForContainerRunning(name) |
97 | 124 |
|
| 125 | + let rootfsBlockURL = try containerRootfsBlockURL(name: name) |
| 126 | + let beforeWrite = try allocatedBytes(at: rootfsBlockURL) |
| 127 | + |
98 | 128 | // Create some files to exercise the filesystem trim path |
99 | | - _ = try doExec(name: name, cmd: ["sh", "-c", "dd if=/dev/zero of=/test-file bs=1M count=10"]) |
| 129 | + _ = try doExec(name: name, cmd: ["sh", "-c", "dd if=/dev/urandom of=/test-file bs=1M count=10"]) |
| 130 | + _ = try doExec(name: name, cmd: ["sync"]) |
| 131 | + let afterWrite = try allocatedBytes(at: rootfsBlockURL) |
| 132 | + |
100 | 133 | _ = try doExec(name: name, cmd: ["rm", "/test-file"]) |
101 | 134 |
|
102 | 135 | // Clean should succeed |
103 | 136 | try doClean(name: name) |
| 137 | + _ = try doExec(name: name, cmd: ["sync"]) |
| 138 | + let afterClean = try allocatedBytes(at: rootfsBlockURL) |
| 139 | + assertCleanReclaimedSpace(beforeWrite: beforeWrite, afterWrite: afterWrite, afterClean: afterClean) |
104 | 140 |
|
105 | 141 | // Container should still be running |
106 | 142 | let status = try getContainerStatus(name) |
@@ -140,12 +176,21 @@ class TestCLIClean: CLITest { |
140 | 176 |
|
141 | 177 | try waitForContainerRunning(name) |
142 | 178 |
|
| 179 | + let volumeBlockURL = try volumeBlockURL(name: volumeName) |
| 180 | + let beforeWrite = try allocatedBytes(at: volumeBlockURL) |
| 181 | + |
143 | 182 | // Write to volume |
144 | | - _ = try doExec(name: name, cmd: ["sh", "-c", "dd if=/dev/zero of=/mnt/vol/test bs=1M count=5"]) |
| 183 | + _ = try doExec(name: name, cmd: ["sh", "-c", "dd if=/dev/urandom of=/mnt/vol/test bs=1M count=5"]) |
| 184 | + _ = try doExec(name: name, cmd: ["sync"]) |
| 185 | + let afterWrite = try allocatedBytes(at: volumeBlockURL) |
| 186 | + |
145 | 187 | _ = try doExec(name: name, cmd: ["rm", "/mnt/vol/test"]) |
146 | 188 |
|
147 | 189 | // Clean should succeed and also trim the volume |
148 | 190 | try doClean(name: name) |
| 191 | + _ = try doExec(name: name, cmd: ["sync"]) |
| 192 | + let afterClean = try allocatedBytes(at: volumeBlockURL) |
| 193 | + assertCleanReclaimedSpace(beforeWrite: beforeWrite, afterWrite: afterWrite, afterClean: afterClean) |
149 | 194 |
|
150 | 195 | // Container should still be running |
151 | 196 | let status = try getContainerStatus(name) |
|
0 commit comments