|
| 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 | + |
| 22 | +/// Coverage for the SDK shape that PR #13 introduces. Wire compatibility is |
| 23 | +/// the contract under test: the daemon does not enforce restart policy yet, |
| 24 | +/// so behavior tests live with the future restart manager (upstream |
| 25 | +/// apple/container#1258), not here. |
| 26 | +struct RestartPolicyTests { |
| 27 | + // MARK: - RestartPolicy round-trip |
| 28 | + |
| 29 | + @Test |
| 30 | + func testEncodesAsBareString() throws { |
| 31 | + let encoder = JSONEncoder() |
| 32 | + let data = try encoder.encode(RestartPolicy.always) |
| 33 | + let s = String(decoding: data, as: UTF8.self) |
| 34 | + #expect(s == "\"always\"") |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + func testDecodesEveryCase() throws { |
| 39 | + let decoder = JSONDecoder() |
| 40 | + for policy in RestartPolicy.allCases { |
| 41 | + let data = try JSONEncoder().encode(policy) |
| 42 | + let decoded = try decoder.decode(RestartPolicy.self, from: data) |
| 43 | + #expect(decoded == policy) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + func testRejectsUnknownString() { |
| 49 | + let bogus = Data("\"unless-stopped\"".utf8) |
| 50 | + #expect(throws: DecodingError.self) { |
| 51 | + try JSONDecoder().decode(RestartPolicy.self, from: bogus) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // MARK: - ContainerCreateOptions forward-compat |
| 56 | + |
| 57 | + /// JSON written by a daemon version that predates `restartPolicy` MUST |
| 58 | + /// still decode — defaulting to `.no`. This is the wire-compatibility |
| 59 | + /// invariant the PR description promises. |
| 60 | + @Test |
| 61 | + func testDecodesLegacyOptionsWithoutRestartPolicy() throws { |
| 62 | + let legacy = Data(#"{"autoRemove":false}"#.utf8) |
| 63 | + let options = try JSONDecoder().decode(ContainerCreateOptions.self, from: legacy) |
| 64 | + #expect(options.autoRemove == false) |
| 65 | + #expect(options.restartPolicy == .no) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + func testDecodesLegacyOptionsWithAutoRemoveAndRootFsOverride() throws { |
| 70 | + // Older clients may emit only autoRemove + rootFsOverride. rootFsOverride |
| 71 | + // is itself optional and may not appear; we test the canonical legacy |
| 72 | + // shape (just autoRemove). |
| 73 | + let legacy = Data(#"{"autoRemove":true}"#.utf8) |
| 74 | + let options = try JSONDecoder().decode(ContainerCreateOptions.self, from: legacy) |
| 75 | + #expect(options.autoRemove == true) |
| 76 | + #expect(options.rootFsOverride == nil) |
| 77 | + #expect(options.restartPolicy == .no) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + func testRoundTripPreservesRestartPolicy() throws { |
| 82 | + let original = ContainerCreateOptions(autoRemove: false, restartPolicy: .onFailure) |
| 83 | + let data = try JSONEncoder().encode(original) |
| 84 | + let decoded = try JSONDecoder().decode(ContainerCreateOptions.self, from: data) |
| 85 | + #expect(decoded.autoRemove == original.autoRemove) |
| 86 | + #expect(decoded.restartPolicy == original.restartPolicy) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + func testEncodedJSONIncludesRestartPolicyField() throws { |
| 91 | + let options = ContainerCreateOptions(autoRemove: false, restartPolicy: .always) |
| 92 | + let data = try JSONEncoder().encode(options) |
| 93 | + let json = String(decoding: data, as: UTF8.self) |
| 94 | + // Field present and lowercase per CodingKeys + raw value. |
| 95 | + #expect(json.contains("\"restartPolicy\":\"always\"")) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + func testDefaultStaticHasNoRestart() { |
| 100 | + #expect(ContainerCreateOptions.default.restartPolicy == .no) |
| 101 | + #expect(ContainerCreateOptions.default.autoRemove == false) |
| 102 | + } |
| 103 | +} |
0 commit comments