-
Notifications
You must be signed in to change notification settings - Fork 584
Fix stop kill missing containers #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e4960c3
14afdfc
6e9e2a2
6d807c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,13 @@ import ContainerizationError | |
| import ContainerizationOS | ||
| import Foundation | ||
|
|
||
| package protocol ContainerIdentifiable { | ||
| var id: String { get } | ||
| var status: RuntimeStatus { get } | ||
| } | ||
|
|
||
| extension ClientContainer: ContainerIdentifiable {} | ||
|
|
||
| extension Application { | ||
| public struct ContainerStop: AsyncParsableCommand { | ||
| public init() {} | ||
|
|
@@ -45,25 +52,20 @@ extension Application { | |
| var containerIds: [String] = [] | ||
|
|
||
| public func validate() throws { | ||
| if containerIds.count == 0 && !all { | ||
| if containerIds.isEmpty && !all { | ||
| throw ContainerizationError(.invalidArgument, message: "no containers specified and --all not supplied") | ||
| } | ||
| if containerIds.count > 0 && all { | ||
| if !containerIds.isEmpty && all { | ||
| throw ContainerizationError( | ||
| .invalidArgument, message: "explicitly supplied container IDs conflict with the --all flag") | ||
| } | ||
| } | ||
|
|
||
| public mutating func run() async throws { | ||
| let set = Set<String>(containerIds) | ||
| var containers = [ClientContainer]() | ||
| if self.all { | ||
| containers = try await ClientContainer.list() | ||
| } else { | ||
| containers = try await ClientContainer.list().filter { c in | ||
| set.contains(c.id) | ||
| } | ||
| } | ||
| let allContainers = try await ClientContainer.list() | ||
| let containers = try self.all | ||
| ? allContainers | ||
| : Self.containers(matching: containerIds, in: allContainers) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should handle ambiguous prefix matches here. Currently, It would be safer to filter all matches and throw an error if |
||
|
|
||
| let opts = ContainerStopOptions( | ||
| timeoutInSeconds: self.time, | ||
|
|
@@ -104,5 +106,19 @@ extension Application { | |
|
|
||
| return failed | ||
| } | ||
|
|
||
| static func containers<C: ContainerIdentifiable>( | ||
| matching containerIds: [String], | ||
| in allContainers: [C] | ||
| ) throws -> [C] { | ||
| var matched: [C] = [] | ||
| for containerId in containerIds { | ||
| guard let container = allContainers.first(where: { $0.id == containerId || $0.id.starts(with: containerId) }) else { | ||
| throw ContainerizationError(.notFound, message: "no such container: \(containerId)") | ||
| } | ||
| matched.append(container) | ||
| } | ||
| return matched | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2025 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 ContainerClient | ||
| import ContainerCommands | ||
| import ContainerizationError | ||
| import Testing | ||
|
|
||
| @testable import ContainerCommands | ||
|
|
||
| struct ContainerStopValidationTests { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a test case for ambiguous prefixes? We should verify that stop ab throws an error if both |
||
| struct StubContainer: ContainerIdentifiable { | ||
| let id: String | ||
| let status: RuntimeStatus | ||
| } | ||
|
|
||
| @Test | ||
| func resolvesExactIds() throws { | ||
| let containers = [StubContainer(id: "abc123", status: .running)] | ||
| let matched = try Application.ContainerStop.containers(matching: ["abc123"], in: containers) | ||
| #expect(matched.count == 1) | ||
| #expect(matched.first?.id == "abc123") | ||
| } | ||
|
|
||
| @Test | ||
| func resolvesIdPrefixes() throws { | ||
| let containers = [ | ||
| StubContainer(id: "abcdef", status: .running), | ||
| StubContainer(id: "123456", status: .running), | ||
| ] | ||
| let matched = try Application.ContainerStop.containers(matching: ["abc"], in: containers) | ||
| #expect(matched.count == 1) | ||
| #expect(matched.first?.id == "abcdef") | ||
| } | ||
|
|
||
| @Test | ||
| func throwsForMissingContainers() throws { | ||
| let containers = [StubContainer(id: "abcdef", status: .running)] | ||
| #expect { | ||
| _ = try Application.ContainerStop.containers(matching: ["missing"], in: containers) | ||
| } throws: { error in | ||
| guard let error = error as? ContainerizationError else { | ||
| return false | ||
| } | ||
| return error.code == .notFound | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactoring this resolution logic into a shared helper is a great move. It keeps Stop and Kill consistent and makes testing way easier.