Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/Services/ContainerSandboxService/SandboxService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ public actor SandboxService {
czConfig.process.stdout = stdout
czConfig.process.stderr = stderr
czConfig.process.stdin = stdin
// NOTE: We can support a user providing new entries eventually, but for now craft
// a default /etc/hosts.
var hostsEntries = [Hosts.Entry.localHostIPV4()]
if !interfaces.isEmpty {
let primaryIfaceAddr = interfaces[0].address
let ip = primaryIfaceAddr.split(separator: "/")
hostsEntries.append(
Hosts.Entry(
ipAddress: String(ip[0]),
hostnames: [czConfig.hostname],
))
}
czConfig.hosts = Hosts(entries: hostsEntries)
}

await self.setContainer(
Expand Down
29 changes: 29 additions & 0 deletions Tests/CLITests/Subcommands/Run/TestCLIRunOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,35 @@ class TestCLIRunCommand: CLITest {
}
}

@Test func testRunDefaultHostsEntries() throws {
do {
let name: String! = Test.current?.name.trimmingCharacters(in: ["(", ")"])
try doLongRun(name: name)
defer {
try? doStop(name: name)
}

let inspectOutput = try inspectContainer(name)
let ip = String(inspectOutput.networks[0].address.split(separator: "/")[0])

let output = try doExec(name: name, cmd: ["cat", "/etc/hosts"])
let lines = output.split(separator: "\n")

let expectedEntries = [("127.0.0.1", "localhost"), (ip, name)]

for (i, line) in lines.enumerated() {
let words = line.split(separator: " ").map { String($0) }
#expect(words.count >= 2, "expected /etc/hosts entry to have 2 or more entries")
let expected = expectedEntries[i]
#expect(expected.0 == words[0], "expected /etc/hosts entries IP to be \(expected.0), instead got \(words[0])")
#expect(expected.1 == words[1], "expected /etc/hosts entries host to be \(expected.1), instead got \(words[1])")
}
} catch {
Issue.record("failed to run container \(error)")
return
}
}

@Test func testRunCommandDNSOption() throws {
do {
let name: String! = Test.current?.name.trimmingCharacters(in: ["(", ")"])
Expand Down