Files
Noah Thornton 9be1020679 Move to TOML configuration for plugin configurations (#1422)
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
2026-04-17 10:56:46 -07:00

332 lines
14 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 Logging
import Testing
@testable import ContainerPlugin
struct PluginFactoryTest {
@Test
func testDefaultFactory() async throws {
let fm = FileManager.default
let tempURL = try fm.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: .temporaryDirectory,
create: true
)
defer { try? FileManager.default.removeItem(at: tempURL) }
let name = tempURL.lastPathComponent
// write config to {name}/config.toml
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)
// write binary to {name}/bin/{name}
let binaryDirURL = tempURL.appending(path: "bin")
try fm.createDirectory(at: binaryDirURL, withIntermediateDirectories: true)
let binaryURL = binaryDirURL.appending(path: name)
try "".write(to: binaryURL, atomically: true, encoding: .utf8)
let factory = DefaultPluginFactory(logger: Logger(label: "test"))
let plugin = try #require(try factory.create(installURL: tempURL))
#expect(plugin.name == name)
#expect(!plugin.shouldBoot)
#expect(plugin.getLaunchdLabel() == "com.apple.container.\(name)")
#expect(plugin.getLaunchdLabel(instanceId: "1") == "com.apple.container.\(name).1")
#expect(plugin.getMachServices() == [])
#expect(plugin.getMachServices(instanceId: "1") == [])
#expect(plugin.getMachService(type: .runtime) == nil)
#expect(plugin.getMachService(instanceId: "1", type: .runtime) == nil)
#expect(!plugin.hasType(.runtime))
#expect(!plugin.hasType(.network))
#expect(plugin.helpText(padding: 40).hasSuffix("Default network management service"))
}
@Test
func testDefaultFactoryByName() async throws {
let fm = FileManager.default
let tempURL = try fm.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: .temporaryDirectory,
create: true
)
defer { try? FileManager.default.removeItem(at: tempURL) }
let name = tempURL.lastPathComponent
// write config to {name}/config.toml
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)
// write binary to {name}/bin/{name}
let binaryDirURL = tempURL.appending(path: "bin")
try fm.createDirectory(at: binaryDirURL, withIntermediateDirectories: true)
let binaryURL = binaryDirURL.appending(path: name)
try "".write(to: binaryURL, atomically: true, encoding: .utf8)
let factory = DefaultPluginFactory(logger: Logger(label: "test"))
let plugin = try #require(try factory.create(parentURL: tempURL.deletingLastPathComponent(), name: name))
#expect(plugin.name == name)
#expect(!plugin.shouldBoot)
#expect(plugin.getLaunchdLabel() == "com.apple.container.\(name)")
#expect(plugin.getLaunchdLabel(instanceId: "1") == "com.apple.container.\(name).1")
#expect(plugin.getMachServices() == [])
#expect(plugin.getMachServices(instanceId: "1") == [])
#expect(plugin.getMachService(type: .runtime) == nil)
#expect(plugin.getMachService(instanceId: "1", type: .runtime) == nil)
#expect(!plugin.hasType(.runtime))
#expect(!plugin.hasType(.network))
#expect(plugin.helpText(padding: 40).hasSuffix("Default network management service"))
}
@Test
func testDefaultFactoryMissingConfig() async throws {
let fm = FileManager.default
let tempURL = try fm.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: .temporaryDirectory,
create: true
)
defer { try? FileManager.default.removeItem(at: tempURL) }
let name = tempURL.lastPathComponent
// write binary to {name}/bin/{name}
let binaryDirURL = tempURL.appending(path: "bin")
try fm.createDirectory(at: binaryDirURL, withIntermediateDirectories: true)
let binaryURL = binaryDirURL.appending(path: name)
try "".write(to: binaryURL, atomically: true, encoding: .utf8)
let factory = DefaultPluginFactory(logger: Logger(label: "test"))
let plugin = try factory.create(installURL: tempURL)
#expect(plugin == nil)
}
@Test
func testDefaultFactoryMissingBinary() async throws {
let fm = FileManager.default
let tempURL = try fm.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: .temporaryDirectory,
create: true
)
defer { try? FileManager.default.removeItem(at: tempURL) }
// write config to {name}/config.toml
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 factory = DefaultPluginFactory(logger: Logger(label: "test"))
let plugin = try factory.create(installURL: tempURL)
#expect(plugin == nil)
}
@Test
func testAppBundleFactory() async throws {
let fm = FileManager.default
let tempURL = try fm.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: .temporaryDirectory,
create: true
)
defer { try? FileManager.default.removeItem(at: tempURL) }
let installURL = tempURL.appending(path: "test.app")
try fm.createDirectory(at: installURL, withIntermediateDirectories: true)
let name = String(installURL.lastPathComponent.dropLast(4))
// write config to {name}/config.toml
let configURL =
installURL
.appending(path: "Contents")
.appending(path: "Resources")
.appending(path: "config.toml")
let configToml = """
abstract = "Default network management service"
author = "Apple"
"""
try fm.createDirectory(at: configURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try configToml.write(to: configURL, atomically: true, encoding: .utf8)
// write binary to {name}/bin/{name}
let binaryURL =
installURL
.appending(path: "Contents")
.appending(path: "MacOS")
.appending(path: name)
try fm.createDirectory(at: binaryURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try "".write(to: binaryURL, atomically: true, encoding: .utf8)
let factory = AppBundlePluginFactory(logger: Logger(label: "test"))
let plugin = try #require(try factory.create(installURL: installURL))
#expect(plugin.name == name)
#expect(!plugin.shouldBoot)
#expect(plugin.getLaunchdLabel() == "com.apple.container.\(name)")
#expect(plugin.getLaunchdLabel(instanceId: "1") == "com.apple.container.\(name).1")
#expect(plugin.getMachServices() == [])
#expect(plugin.getMachServices(instanceId: "1") == [])
#expect(plugin.getMachService(type: .runtime) == nil)
#expect(plugin.getMachService(instanceId: "1", type: .runtime) == nil)
#expect(!plugin.hasType(.runtime))
#expect(!plugin.hasType(.network))
#expect(plugin.helpText(padding: 40).hasSuffix("Default network management service"))
}
@Test
func testAppBundleFactoryByName() async throws {
let fm = FileManager.default
let tempURL = try fm.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: .temporaryDirectory,
create: true
)
defer { try? FileManager.default.removeItem(at: tempURL) }
let installURL = tempURL.appending(path: "test.app")
try fm.createDirectory(at: installURL, withIntermediateDirectories: true)
let name = String(installURL.lastPathComponent.dropLast(4))
// write config to {name}/config.toml
let configURL =
installURL
.appending(path: "Contents")
.appending(path: "Resources")
.appending(path: "config.toml")
let configToml = """
abstract = "Default network management service"
author = "Apple"
"""
try fm.createDirectory(at: configURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try configToml.write(to: configURL, atomically: true, encoding: .utf8)
// write binary to {name}/bin/{name}
let binaryURL =
installURL
.appending(path: "Contents")
.appending(path: "MacOS")
.appending(path: name)
try fm.createDirectory(at: binaryURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try "".write(to: binaryURL, atomically: true, encoding: .utf8)
let factory = AppBundlePluginFactory(logger: Logger(label: "test"))
let plugin = try #require(try factory.create(parentURL: tempURL, name: name))
#expect(plugin.name == name)
#expect(!plugin.shouldBoot)
#expect(plugin.getLaunchdLabel() == "com.apple.container.\(name)")
#expect(plugin.getLaunchdLabel(instanceId: "1") == "com.apple.container.\(name).1")
#expect(plugin.getMachServices() == [])
#expect(plugin.getMachServices(instanceId: "1") == [])
#expect(plugin.getMachService(type: .runtime) == nil)
#expect(plugin.getMachService(instanceId: "1", type: .runtime) == nil)
#expect(!plugin.hasType(.runtime))
#expect(!plugin.hasType(.network))
#expect(plugin.helpText(padding: 40).hasSuffix("Default network management service"))
}
@Test
func testDefaultFactoryFallsBackToJson() async throws {
let fm = FileManager.default
let tempURL = try fm.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: .temporaryDirectory,
create: true
)
defer { try? FileManager.default.removeItem(at: tempURL) }
let name = tempURL.lastPathComponent
// write config to {name}/config.json (no config.toml)
let configURL = tempURL.appending(path: "config.json")
let configJson = """
{"abstract": "JSON fallback service", "author": "Apple"}
"""
try configJson.write(to: configURL, atomically: true, encoding: .utf8)
// write binary to {name}/bin/{name}
let binaryDirURL = tempURL.appending(path: "bin")
try fm.createDirectory(at: binaryDirURL, withIntermediateDirectories: true)
let binaryURL = binaryDirURL.appending(path: name)
try "".write(to: binaryURL, atomically: true, encoding: .utf8)
let factory = DefaultPluginFactory(logger: Logger(label: "test"))
let plugin = try #require(try factory.create(installURL: tempURL))
#expect(plugin.name == name)
#expect(plugin.config.abstract == "JSON fallback service")
#expect(plugin.config.author == "Apple")
}
@Test
func testDefaultFactoryPrefersTomlOverJson() async throws {
let fm = FileManager.default
let tempURL = try fm.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: .temporaryDirectory,
create: true
)
defer { try? FileManager.default.removeItem(at: tempURL) }
let name = tempURL.lastPathComponent
// write config.toml
let tomlURL = tempURL.appending(path: "config.toml")
let configToml = """
abstract = "TOML service"
author = "Apple"
"""
try configToml.write(to: tomlURL, atomically: true, encoding: .utf8)
// write config.json with a different abstract
let jsonURL = tempURL.appending(path: "config.json")
let configJson = """
{"abstract": "JSON service", "author": "Apple"}
"""
try configJson.write(to: jsonURL, atomically: true, encoding: .utf8)
// write binary to {name}/bin/{name}
let binaryDirURL = tempURL.appending(path: "bin")
try fm.createDirectory(at: binaryDirURL, withIntermediateDirectories: true)
let binaryURL = binaryDirURL.appending(path: name)
try "".write(to: binaryURL, atomically: true, encoding: .utf8)
let factory = DefaultPluginFactory(logger: Logger(label: "test"))
let plugin = try #require(try factory.create(installURL: tempURL))
#expect(plugin.name == name)
#expect(plugin.config.abstract == "TOML service")
}
}