mirror of
https://github.com/apple/container.git
synced 2026-07-12 20:47:04 +00:00
9be1020679
This change migrates plugin config format away from using JSON to use TOML. - Addresses the plugin portion of discussion #1336 ## Type of Change - [ ] Bug fix - [X] New feature - [ ] Breaking change - [ ] Documentation update ## Motivation and Context Context within https://github.com/apple/container/discussions/1336 > We're looking to improve the user experience and overall functionality of setting defaults for container to better enable future use cases. > > Today container uses macOS's [UserDefaults](https://developer.apple.com/documentation/foundation/userdefaults) to configure settings needed at runtime. As mentioned in https://github.com/apple/container/issues/608, UserDefaults may be idiomatic for macOS, but they apply globally to all sessions and do not handle representing complex, hierarchical data well. Additionally, we currently have two ways of setting these defaults, either directly with macOS's defaults command or through container system property. > > https://github.com/apple/container/issues/608 proposes moving to use environment variables in place of UserDefaults. However, we do not believe this is sufficient. Environment variables are not in a consistent location, are not sourced from data, and also do not handle representing complex, hierarchical data well. As part of this migration to TOML for user settings we want to also move plugin settings to use TOML so that we can have a singular file format for `container` configurations. ## Testing - [X] Tested locally - [X] Added/updated tests - [ ] Added/updated docs
124 lines
4.5 KiB
Swift
124 lines
4.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
// Copyright © 2025-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 Foundation
|
|
import Testing
|
|
|
|
@testable import ContainerPlugin
|
|
|
|
struct PluginConfigTest {
|
|
@Test
|
|
func testCLIPluginConfigLoad() async throws {
|
|
let tempURL = try FileManager.default.url(
|
|
for: .itemReplacementDirectory,
|
|
in: .userDomainMask,
|
|
appropriateFor: .temporaryDirectory,
|
|
create: true
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: tempURL) }
|
|
let configURL = tempURL.appending(path: "config.toml")
|
|
let configToml = """
|
|
abstract = "Default network management service"
|
|
author = "Apple"
|
|
"""
|
|
try configToml.write(to: configURL, atomically: true, encoding: .utf8)
|
|
let config = try #require(try PluginConfig(configURL: configURL))
|
|
|
|
#expect(config.isCLI)
|
|
#expect(config.abstract == "Default network management service")
|
|
#expect(config.author == "Apple")
|
|
}
|
|
|
|
@Test
|
|
func testServicePluginConfigLoad() async throws {
|
|
let tempURL = try FileManager.default.url(
|
|
for: .itemReplacementDirectory,
|
|
in: .userDomainMask,
|
|
appropriateFor: .temporaryDirectory,
|
|
create: true
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: tempURL) }
|
|
let configURL = tempURL.appending(path: "config.toml")
|
|
let configToml = """
|
|
abstract = "Default network management service"
|
|
author = "Apple"
|
|
version = 0.1
|
|
|
|
[servicesConfig]
|
|
loadAtBoot = true
|
|
runAtLoad = true
|
|
defaultArguments = ["start"]
|
|
|
|
[[servicesConfig.services]]
|
|
type = "network"
|
|
description = "foo"
|
|
"""
|
|
try configToml.write(to: configURL, atomically: true, encoding: .utf8)
|
|
let config = try #require(try PluginConfig(configURL: configURL))
|
|
|
|
#expect(!config.isCLI)
|
|
#expect(config.abstract == "Default network management service")
|
|
#expect(config.author == "Apple")
|
|
|
|
let servicesConfig = try #require(config.servicesConfig)
|
|
#expect(servicesConfig.loadAtBoot)
|
|
#expect(servicesConfig.runAtLoad)
|
|
#expect(servicesConfig.services.count == 1)
|
|
#expect(servicesConfig.services[0].type == .network)
|
|
#expect(servicesConfig.services[0].description == "foo")
|
|
#expect(servicesConfig.defaultArguments == ["start"])
|
|
}
|
|
|
|
@Test
|
|
func testMalformedTomlThrows() async throws {
|
|
let tempURL = try FileManager.default.url(
|
|
for: .itemReplacementDirectory,
|
|
in: .userDomainMask,
|
|
appropriateFor: .temporaryDirectory,
|
|
create: true
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: tempURL) }
|
|
let configURL = tempURL.appending(path: "config.toml")
|
|
let malformedToml = """
|
|
abstract = "unclosed string
|
|
[invalid
|
|
"""
|
|
try malformedToml.write(to: configURL, atomically: true, encoding: .utf8)
|
|
#expect(throws: (any Error).self) {
|
|
try PluginConfig(configURL: configURL)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func testUnsupportedExtensionReturnsNil() async throws {
|
|
let tempURL = try FileManager.default.url(
|
|
for: .itemReplacementDirectory,
|
|
in: .userDomainMask,
|
|
appropriateFor: .temporaryDirectory,
|
|
create: true
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: tempURL) }
|
|
let configURL = tempURL.appending(path: "config.yaml")
|
|
let content = """
|
|
abstract: "YAML config"
|
|
author: "Apple"
|
|
"""
|
|
try content.write(to: configURL, atomically: true, encoding: .utf8)
|
|
let config = try PluginConfig(configURL: configURL)
|
|
#expect(config == nil)
|
|
}
|
|
}
|