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