Files
container/Tests/ContainerResourceTests/AttachmentOptionsTests.swift
T
J Logan b3eb70307a Add DNS aliases and subtree aliases for container network attachments (#2390)
- Closes #457.
- A container can now declare additional DNS names for
  a network attachment with a repeatable `alias` property
  on `--network`, including subtree aliases that match
  every name below a domain.
- With the default domain `test`, this container answers
  to `gateway.test` by its name, to `api.test` by the exact
  alias, and to `web.gateway.test`, `a.b.gateway.test`
  and every other name below `gateway.test` by the
  subtree alias. The motivating case is a gateway container
  in front of a local Kubernetes cluster that answers for
  `*.gateway.test`, with an ingress controller routing on
  the `Host` header.
- An alias is an exact name, or a name with a leading
  dot that matches every name below it. A name without
  a dot gets the default domain, as container names do.
  A subtree alias must be below a name the same
  attachment owns, and exact names are unique across
  all containers, so a container cannot claim another
  container's namespace. The rules for one attachment
  are checked when a container is created, again when
  a stored container starts, and again by the network
  helper when it registers the aliases. See
  `docs/architecture/dns.md` for the design, the
  invariants and the security considerations.
2026-10-14 10:11:00 -07:00

81 lines
3.3 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 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 ContainerResource
struct AttachmentOptionsTests {
@Test(
"canonicalHostname lowercases and removes a trailing dot",
arguments: [
("foo.test.", "foo.test"),
("foo.test", "foo.test"),
("Foo.Test.", "foo.test"),
("FOO", "foo"),
("foo", "foo"),
("", ""),
])
func canonicalHostname(input: String, expected: String) {
#expect(AttachmentOptions.canonicalHostname(input) == expected)
}
@Test("Options encoded before aliases existed decode with no aliases")
func decodeWithoutAliases() throws {
let json = #"{"hostname":"foo.test.","mtu":1280}"#
let options = try JSONDecoder().decode(AttachmentOptions.self, from: Data(json.utf8))
#expect(options.hostname == "foo.test.")
#expect(options.mtu == 1280)
#expect(options.aliases.isEmpty)
}
@Test("Options without aliases do not encode an aliases key")
func encodeWithoutAliases() throws {
let data = try JSONEncoder().encode(AttachmentOptions(hostname: "foo.test."))
let json = try #require(String(data: data, encoding: .utf8))
#expect(!json.contains("aliases"))
}
@Test("Aliases round trip through JSON")
func aliasesRoundTrip() throws {
let aliases = ["api.example.test", ".example.test"]
let original = AttachmentOptions(hostname: "foo.test.", mtu: 1500, aliases: aliases)
let decoded = try JSONDecoder().decode(AttachmentOptions.self, from: JSONEncoder().encode(original))
#expect(decoded.hostname == original.hostname)
#expect(decoded.mtu == original.mtu)
#expect(decoded.aliases == aliases)
}
@Test("Aliases round trip inside an attachment configuration")
func aliasesRoundTripInConfiguration() throws {
let original = AttachmentConfiguration(
network: "default",
options: AttachmentOptions(hostname: "foo.test.", aliases: [".foo.test"])
)
let decoded = try JSONDecoder().decode(AttachmentConfiguration.self, from: JSONEncoder().encode(original))
#expect(decoded.network == "default")
#expect(decoded.options.aliases == [".foo.test"])
}
@Test("An empty aliases list in stored options decodes as no aliases")
func decodeEmptyAliases() throws {
let json = #"{"hostname":"foo.test.","aliases":[]}"#
let options = try JSONDecoder().decode(AttachmentOptions.self, from: Data(json.utf8))
#expect(options.aliases.isEmpty)
}
}