|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2025 Apple Inc. and the container project authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import ContainerImagesService |
| 18 | +import ContainerizationOCI |
| 19 | +import Foundation |
| 20 | +import Testing |
| 21 | + |
| 22 | +@testable import ContainerImagesService |
| 23 | + |
| 24 | +struct SnapshotStoreTests { |
| 25 | + |
| 26 | + @Test("getSnapshotSize should return 0 when snapshot directory does not exist") |
| 27 | + func testGetSnapshotSizeWhenSnapshotDoesNotExist() async throws { |
| 28 | + // prepare |
| 29 | + let tempDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 30 | + try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true) |
| 31 | + defer { |
| 32 | + try? FileManager.default.removeItem(at: tempDir) |
| 33 | + } |
| 34 | + |
| 35 | + let snapshotStore = try SnapshotStore( |
| 36 | + path: tempDir, |
| 37 | + unpackStrategy: SnapshotStore.defaultUnpackStrategy, |
| 38 | + log: nil |
| 39 | + ) |
| 40 | + |
| 41 | + // Create a descriptor for a non-existent snapshot |
| 42 | + let fakeDigest = "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" |
| 43 | + let platform = Platform(arch: "amd64", os: "linux") |
| 44 | + |
| 45 | + // Create descriptor directly with the properties |
| 46 | + let descriptor = Descriptor( |
| 47 | + mediaType: "application/vnd.oci.image.manifest.v1+json", |
| 48 | + digest: fakeDigest, |
| 49 | + size: 1024, |
| 50 | + platform: platform |
| 51 | + ) |
| 52 | + |
| 53 | + let size = try await snapshotStore.getSnapshotSize(descriptor: descriptor) |
| 54 | + |
| 55 | + #expect(size == 0, "Expected size to be 0 when snapshot doesn't exist, got \(size)") |
| 56 | + } |
| 57 | + |
| 58 | + @Test("getSnapshotSize should return correct size when snapshot exists") |
| 59 | + func testGetSnapshotSizeWhenSnapshotExists() async throws { |
| 60 | + let tempDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 61 | + try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true) |
| 62 | + defer { |
| 63 | + try? FileManager.default.removeItem(at: tempDir) |
| 64 | + } |
| 65 | + |
| 66 | + let snapshotStore = try SnapshotStore( |
| 67 | + path: tempDir, |
| 68 | + unpackStrategy: SnapshotStore.defaultUnpackStrategy, |
| 69 | + log: nil |
| 70 | + ) |
| 71 | + |
| 72 | + // Create a test descriptor |
| 73 | + let fakeDigest = "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" |
| 74 | + let descriptor = Descriptor( |
| 75 | + mediaType: "application/vnd.oci.image.manifest.v1+json", |
| 76 | + digest: fakeDigest, |
| 77 | + size: 1024, |
| 78 | + platform: Platform(arch: "amd64", os: "linux") |
| 79 | + ) |
| 80 | + |
| 81 | + // Create snapshot directory |
| 82 | + let snapshotDir = |
| 83 | + tempDir |
| 84 | + .appendingPathComponent("snapshots") |
| 85 | + .appendingPathComponent(descriptor.digest.trimmingDigestPrefix) |
| 86 | + try FileManager.default.createDirectory(at: snapshotDir, withIntermediateDirectories: true) |
| 87 | + |
| 88 | + // Create test files: 1KB snapshot + 512 bytes info = 1536 bytes total |
| 89 | + let testFile = snapshotDir.appendingPathComponent("snapshot") |
| 90 | + try Data(repeating: 0, count: 1024).write(to: testFile) |
| 91 | + |
| 92 | + let infoFile = snapshotDir.appendingPathComponent("snapshot-info") |
| 93 | + try Data(repeating: 0, count: 512).write(to: infoFile) |
| 94 | + |
| 95 | + let size = try await snapshotStore.getSnapshotSize(descriptor: descriptor) |
| 96 | + |
| 97 | + #expect(size >= 1536, "Expected at least 1536 bytes (1024 + 512), got \(size) allocated") |
| 98 | + } |
| 99 | + |
| 100 | + @Test("getSnapshotSize should handle multiple files in snapshot directory") |
| 101 | + func testGetSnapshotSizeWithMultipleFiles() async throws { |
| 102 | + let tempDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 103 | + try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true) |
| 104 | + defer { |
| 105 | + try? FileManager.default.removeItem(at: tempDir) |
| 106 | + } |
| 107 | + |
| 108 | + let snapshotStore = try SnapshotStore( |
| 109 | + path: tempDir, |
| 110 | + unpackStrategy: SnapshotStore.defaultUnpackStrategy, |
| 111 | + log: nil |
| 112 | + ) |
| 113 | + |
| 114 | + let fakeDigest = "sha256:fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321" |
| 115 | + let platform = Platform(arch: "arm64", os: "linux") |
| 116 | + let descriptor = Descriptor( |
| 117 | + mediaType: "application/vnd.oci.image.manifest.v1+json", |
| 118 | + digest: fakeDigest, |
| 119 | + size: 2048, |
| 120 | + platform: platform |
| 121 | + ) |
| 122 | + |
| 123 | + let snapshotDir = |
| 124 | + tempDir |
| 125 | + .appendingPathComponent("snapshots", isDirectory: true) |
| 126 | + .appendingPathComponent(descriptor.digest.trimmingDigestPrefix, isDirectory: true) |
| 127 | + try FileManager.default.createDirectory(at: snapshotDir, withIntermediateDirectories: true) |
| 128 | + |
| 129 | + // Create multiple files |
| 130 | + let file1 = snapshotDir.appendingPathComponent("snapshot", isDirectory: false) |
| 131 | + try Data(repeating: 1, count: 2048).write(to: file1) // 2KB |
| 132 | + |
| 133 | + let file2 = snapshotDir.appendingPathComponent("snapshot-info", isDirectory: false) |
| 134 | + try Data(repeating: 2, count: 1024).write(to: file2) // 1KB |
| 135 | + |
| 136 | + let subdir = snapshotDir.appendingPathComponent("subdir", isDirectory: true) |
| 137 | + try FileManager.default.createDirectory(at: subdir, withIntermediateDirectories: true) |
| 138 | + let file3 = subdir.appendingPathComponent("extra", isDirectory: false) |
| 139 | + try Data(repeating: 3, count: 512).write(to: file3) // 512 bytes |
| 140 | + |
| 141 | + let expectedMinSize: UInt64 = 2048 + 1024 + 512 // 3.5KB |
| 142 | + |
| 143 | + let size = try await snapshotStore.getSnapshotSize(descriptor: descriptor) |
| 144 | + |
| 145 | + #expect(size >= expectedMinSize, "Expected size to include all files (at least \(expectedMinSize) bytes), got \(size)") |
| 146 | + } |
| 147 | + |
| 148 | + @Test("getSnapshotSize should handle empty snapshot directory") |
| 149 | + func testGetSnapshotSizeWithEmptyDirectory() async throws { |
| 150 | + let tempDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 151 | + try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true) |
| 152 | + defer { |
| 153 | + try? FileManager.default.removeItem(at: tempDir) |
| 154 | + } |
| 155 | + |
| 156 | + let snapshotStore = try SnapshotStore( |
| 157 | + path: tempDir, |
| 158 | + unpackStrategy: SnapshotStore.defaultUnpackStrategy, |
| 159 | + log: nil |
| 160 | + ) |
| 161 | + |
| 162 | + let fakeDigest = "sha256:0000000000000000000000000000000000000000000000000000000000000000" |
| 163 | + let platform = Platform(arch: "amd64", os: "linux") |
| 164 | + let descriptor = Descriptor( |
| 165 | + mediaType: "application/vnd.oci.image.manifest.v1+json", |
| 166 | + digest: fakeDigest, |
| 167 | + size: 0, |
| 168 | + platform: platform |
| 169 | + ) |
| 170 | + |
| 171 | + // Create empty snapshot directory |
| 172 | + let snapshotDir = |
| 173 | + tempDir |
| 174 | + .appendingPathComponent("snapshots", isDirectory: true) |
| 175 | + .appendingPathComponent(descriptor.digest.trimmingDigestPrefix, isDirectory: true) |
| 176 | + try FileManager.default.createDirectory(at: snapshotDir, withIntermediateDirectories: true) |
| 177 | + |
| 178 | + |
| 179 | + let size = try await snapshotStore.getSnapshotSize(descriptor: descriptor) |
| 180 | + |
| 181 | + // Empty directory should return 0 or a small overhead value |
| 182 | + #expect(size >= 0, "Expected size to be non-negative, got \(size)") |
| 183 | + } |
| 184 | +} |
0 commit comments