|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2026 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 Foundation |
| 18 | +import Testing |
| 19 | + |
| 20 | +@testable import ContainerResource |
| 21 | +@testable import ContainerizationOS |
| 22 | + |
| 23 | +struct RegistryResourceTests { |
| 24 | + |
| 25 | + // Mock RegistryInfo for testing |
| 26 | + func createMockRegistryInfo( |
| 27 | + hostname: String = "docker.io", |
| 28 | + username: String = "testuser" |
| 29 | + ) -> RegistryInfo { |
| 30 | + RegistryInfo( |
| 31 | + hostname: hostname, |
| 32 | + username: username, |
| 33 | + modifiedDate: Date(timeIntervalSince1970: 1_700_000_000), |
| 34 | + createdDate: Date(timeIntervalSince1970: 1_690_000_000) |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + @Test("RegistryResource id and name are both hostname") |
| 39 | + func testRegistryResourceIdAndName() { |
| 40 | + let hostname = "ghcr.io" |
| 41 | + let registryInfo = createMockRegistryInfo(hostname: hostname, username: "myuser") |
| 42 | + let resource = RegistryResource(from: registryInfo) |
| 43 | + |
| 44 | + #expect(resource.id == hostname, "id should be the hostname") |
| 45 | + #expect(resource.name == hostname, "name should be the hostname") |
| 46 | + #expect(resource.id == resource.name, "id and name should be identical") |
| 47 | + } |
| 48 | + |
| 49 | + @Test("RegistryResource maps RegistryInfo correctly") |
| 50 | + func testRegistryResourceMapping() { |
| 51 | + let hostname = "registry.example.com:5000" |
| 52 | + let username = "developer" |
| 53 | + let registryInfo = createMockRegistryInfo(hostname: hostname, username: username) |
| 54 | + |
| 55 | + let resource = RegistryResource(from: registryInfo) |
| 56 | + |
| 57 | + #expect(resource.id == hostname) |
| 58 | + #expect(resource.name == hostname) |
| 59 | + #expect(resource.username == username) |
| 60 | + #expect(resource.creationDate == registryInfo.createdDate) |
| 61 | + #expect(resource.modifiedDate == registryInfo.modifiedDate) |
| 62 | + #expect(resource.labels.isEmpty, "default labels should be empty") |
| 63 | + } |
| 64 | + |
| 65 | + @Test("RegistryResource implements ManagedResource") |
| 66 | + func testManagedResourceConformance() { |
| 67 | + let registryInfo = createMockRegistryInfo() |
| 68 | + let resource = RegistryResource(from: registryInfo) |
| 69 | + |
| 70 | + // Test that it conforms to ManagedResource protocol |
| 71 | + let managedResource: any ManagedResource = resource |
| 72 | + #expect(managedResource.id == "docker.io") |
| 73 | + #expect(managedResource.name == "docker.io") |
| 74 | + #expect(managedResource.creationDate == registryInfo.createdDate) |
| 75 | + #expect(managedResource.labels.isEmpty) |
| 76 | + } |
| 77 | + |
| 78 | + @Test("RegistryResource is Codable - JSON encoding") |
| 79 | + func testRegistryResourceJSONEncoding() throws { |
| 80 | + let hostname = "docker.io" |
| 81 | + let username = "testuser" |
| 82 | + let registryInfo = createMockRegistryInfo(hostname: hostname, username: username) |
| 83 | + let resource = RegistryResource(from: registryInfo) |
| 84 | + |
| 85 | + // Encode to JSON |
| 86 | + let encoder = JSONEncoder() |
| 87 | + encoder.outputFormatting = [.sortedKeys, .prettyPrinted] |
| 88 | + let jsonData = try encoder.encode(resource) |
| 89 | + let jsonString = String(data: jsonData, encoding: .utf8)! |
| 90 | + |
| 91 | + // Verify JSON contains expected fields |
| 92 | + #expect(jsonString.contains("\"id\""), "JSON should contain id field") |
| 93 | + #expect(jsonString.contains("\"name\""), "JSON should contain name field") |
| 94 | + #expect(jsonString.contains("\"username\""), "JSON should contain username field") |
| 95 | + #expect(jsonString.contains("\"creationDate\""), "JSON should contain creationDate field") |
| 96 | + #expect(jsonString.contains("\"modifiedDate\""), "JSON should contain modifiedDate field") |
| 97 | + #expect(jsonString.contains(hostname), "JSON should contain the hostname") |
| 98 | + #expect(jsonString.contains(username), "JSON should contain the username") |
| 99 | + } |
| 100 | + |
| 101 | + @Test("RegistryResource is Codable - round trip") |
| 102 | + func testRegistryResourceRoundTrip() throws { |
| 103 | + let hostname = "ghcr.io" |
| 104 | + let username = "developer" |
| 105 | + let registryInfo = createMockRegistryInfo(hostname: hostname, username: username) |
| 106 | + let original = RegistryResource(from: registryInfo) |
| 107 | + |
| 108 | + // Encode |
| 109 | + let encoder = JSONEncoder() |
| 110 | + let jsonData = try encoder.encode(original) |
| 111 | + |
| 112 | + // Decode |
| 113 | + let decoder = JSONDecoder() |
| 114 | + let decoded = try decoder.decode(RegistryResource.self, from: jsonData) |
| 115 | + |
| 116 | + // Verify |
| 117 | + #expect(decoded.id == original.id) |
| 118 | + #expect(decoded.name == original.name) |
| 119 | + #expect(decoded.username == original.username) |
| 120 | + #expect(decoded.creationDate.timeIntervalSince1970 == original.creationDate.timeIntervalSince1970) |
| 121 | + #expect(decoded.modifiedDate.timeIntervalSince1970 == original.modifiedDate.timeIntervalSince1970) |
| 122 | + #expect(decoded.labels == original.labels) |
| 123 | + } |
| 124 | + |
| 125 | + @Test("RegistryResource nameValid validates hostnames") |
| 126 | + func testRegistryResourceNameValidation() { |
| 127 | + // Valid hostnames |
| 128 | + #expect(RegistryResource.nameValid("docker.io"), "docker.io should be valid") |
| 129 | + #expect(RegistryResource.nameValid("ghcr.io"), "ghcr.io should be valid") |
| 130 | + #expect(RegistryResource.nameValid("registry.example.com"), "registry.example.com should be valid") |
| 131 | + #expect(RegistryResource.nameValid("localhost:5000"), "localhost:5000 should be valid") |
| 132 | + #expect(RegistryResource.nameValid("registry.k8s.io"), "registry.k8s.io should be valid") |
| 133 | + |
| 134 | + // Invalid hostnames |
| 135 | + #expect(!RegistryResource.nameValid(""), "empty string should be invalid") |
| 136 | + #expect(!RegistryResource.nameValid("-invalid.com"), "hostname starting with hyphen should be invalid") |
| 137 | + #expect(!RegistryResource.nameValid("invalid-.com"), "hostname ending with hyphen should be invalid") |
| 138 | + } |
| 139 | + |
| 140 | + @Test("RegistryResource can have labels") |
| 141 | + func testRegistryResourceWithLabels() { |
| 142 | + let hostname = "docker.io" |
| 143 | + let username = "testuser" |
| 144 | + let labels = [ |
| 145 | + "environment": "production", |
| 146 | + ResourceLabelKeys.role: "primary", |
| 147 | + ] |
| 148 | + |
| 149 | + let resource = RegistryResource( |
| 150 | + hostname: hostname, |
| 151 | + username: username, |
| 152 | + creationDate: Date(), |
| 153 | + modifiedDate: Date(), |
| 154 | + labels: labels |
| 155 | + ) |
| 156 | + |
| 157 | + #expect(resource.labels.count == 2) |
| 158 | + #expect(resource.labels["environment"] == "production") |
| 159 | + #expect(resource.labels[ResourceLabelKeys.role] == "primary") |
| 160 | + } |
| 161 | + |
| 162 | + @Test("RegistryResource handles hostname with port") |
| 163 | + func testRegistryResourceWithPort() { |
| 164 | + let hostname = "localhost:5000" |
| 165 | + let registryInfo = createMockRegistryInfo(hostname: hostname, username: "admin") |
| 166 | + let resource = RegistryResource(from: registryInfo) |
| 167 | + |
| 168 | + #expect(resource.id == hostname) |
| 169 | + #expect(resource.name == hostname) |
| 170 | + #expect(RegistryResource.nameValid(hostname)) |
| 171 | + } |
| 172 | + |
| 173 | + @Test("Multiple RegistryResources can be encoded as array") |
| 174 | + func testMultipleRegistryResourcesJSONEncoding() throws { |
| 175 | + let registries = [ |
| 176 | + RegistryResource(from: createMockRegistryInfo(hostname: "docker.io", username: "user1")), |
| 177 | + RegistryResource(from: createMockRegistryInfo(hostname: "ghcr.io", username: "user2")), |
| 178 | + RegistryResource(from: createMockRegistryInfo(hostname: "quay.io", username: "user3")), |
| 179 | + ] |
| 180 | + |
| 181 | + let encoder = JSONEncoder() |
| 182 | + encoder.outputFormatting = [.sortedKeys, .prettyPrinted] |
| 183 | + let jsonData = try encoder.encode(registries) |
| 184 | + let jsonString = String(data: jsonData, encoding: .utf8)! |
| 185 | + |
| 186 | + // Verify all hostnames are present |
| 187 | + #expect(jsonString.contains("docker.io")) |
| 188 | + #expect(jsonString.contains("ghcr.io")) |
| 189 | + #expect(jsonString.contains("quay.io")) |
| 190 | + |
| 191 | + // Verify all usernames are present |
| 192 | + #expect(jsonString.contains("user1")) |
| 193 | + #expect(jsonString.contains("user2")) |
| 194 | + #expect(jsonString.contains("user3")) |
| 195 | + |
| 196 | + // Print for manual verification |
| 197 | + print("Encoded JSON:") |
| 198 | + print(jsonString) |
| 199 | + } |
| 200 | +} |
0 commit comments