Skip to content
Merged
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
112 changes: 55 additions & 57 deletions Tests/CLITests/Subcommands/Run/TestCLIRunOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,61 +367,69 @@ class TestCLIRunCommand: CLITest {
}
}

@Test func testRunCommandDNS() throws {
@Test func testRunCommandDefaultResolvConf() throws {
do {
let name: String! = Test.current?.name.trimmingCharacters(in: ["(", ")"])
let dns = "8.8.8.8"
try doLongRun(name: name, args: ["--dns", dns])
try doLongRun(name: name, args: [])
defer {
try? doStop(name: name)
}
var output = try doExec(name: name, cmd: ["cat", "/etc/resolv.conf"])
output = output.trimmingCharacters(in: .whitespacesAndNewlines)
let words = output.split(separator: " ")
#expect(words.count == 2, "expected 'nameserver \(dns)', instead got '\(output)'")
#expect(words[1].lowercased() == dns, "expected 'nameserver \(dns)', instead got '\(output)'")
} catch {
Issue.record("failed to run container \(error)")
return
}
}

@Test func testRunCommandDNSDomain() throws {
do {
let name: String! = Test.current?.name.trimmingCharacters(in: ["(", ")"])
let dnsDomain = "example.com"
try doLongRun(name: name, args: ["--dns-domain", dnsDomain])
defer {
try? doStop(name: name)
}
let output = try doExec(name: name, cmd: ["cat", "/etc/resolv.conf"])
let lines = output.split(separator: "\n")
#expect(lines.count == 2, "expected two lines of info in /etc/resolv.conf, got \(output)")
let words = lines[1].split(separator: " ")
#expect(words.count == 2, "expected 'domain \(dnsDomain)', instead got '\(lines[1])'")
#expect(words[0].lowercased() == "domain", "expected entry to list domain, instead got '\(words[0])'")
#expect(words[1].lowercased() == dnsDomain, "expected '\(dnsDomain)' search domain, instead got '\(words[1])'")
let actualLines = output.components(separatedBy: .newlines)
.filter { !$0.isEmpty }
.map { $0.components(separatedBy: .whitespaces) }
.map { $0.joined(separator: " ") }

let inspectOutput = try inspectContainer(name)
let ip = String(inspectOutput.networks[0].address.split(separator: "/")[0])
let ipv4Address = try IPv4Address(ip)
let expectedNameserver = IPv4Address(fromValue: ipv4Address.prefix(prefixLength: 24).value + 1).description
let defaultDomain = try getDefaultDomain()
let expectedLines: [String] = [
"nameserver \(expectedNameserver)",
defaultDomain.map { "domain \($0)" },
].compactMap { $0 }

#expect(expectedLines == actualLines)
} catch {
Issue.record("failed to run container \(error)")
return
}
}

@Test func testRunCommandDNSSearch() throws {
@Test func testRunCommandNonDefaultResolvConf() throws {
do {
let expectedDns: String = "8.8.8.8"
let expectedDomain = "example.com"
let expectedSearch = "test.com"
let expectedOption = "debug"
let name: String! = Test.current?.name.trimmingCharacters(in: ["(", ")"])
let dnsSearch = "test.com"
try doLongRun(name: name, args: ["--dns-search", dnsSearch])
try doLongRun(
name: name,
args: [
"--dns", expectedDns,
"--dns-domain", expectedDomain,
"--dns-search", expectedSearch,
"--dns-option", expectedOption,
])
defer {
try? doStop(name: name)
}

let output = try doExec(name: name, cmd: ["cat", "/etc/resolv.conf"])
let lines = output.split(separator: "\n")
#expect(lines.count == 2, "expected two lines of info in /etc/resolv.conf, got \(output)")
let words = lines[1].split(separator: " ")
#expect(words.count == 2, "expected 'search \(dnsSearch)', instead got '\(lines[1])'")
#expect(words[0].lowercased() == "search", "expected entry to list search domains, instead got '\(words[0])'")
#expect(words[1].lowercased() == dnsSearch, "expected '\(dnsSearch)' search domain, instead got '\(words[1])'")
let actualLines = output.components(separatedBy: .newlines)
.filter { !$0.isEmpty }
.map { $0.components(separatedBy: .whitespaces) }
.map { $0.joined(separator: " ") }

let expectedLines: [String] = [
"nameserver \(expectedDns)",
"domain \(expectedDomain)",
"search \(expectedSearch)",
"opts \(expectedOption)",
]
#expect(expectedLines == actualLines)
} catch {
Issue.record("failed to run container \(error)")
return
Expand Down Expand Up @@ -457,27 +465,6 @@ class TestCLIRunCommand: CLITest {
}
}

@Test func testRunCommandDNSOption() throws {
do {
let name: String! = Test.current?.name.trimmingCharacters(in: ["(", ")"])
let dnsOption = "debug"
try doLongRun(name: name, args: ["--dns-option", dnsOption])
defer {
try? doStop(name: name)
}
let output = try doExec(name: name, cmd: ["cat", "/etc/resolv.conf"])
let lines = output.split(separator: "\n")
#expect(lines.count == 2, "expected two lines of info in /etc/resolv.conf, got \(output)")
let words = lines[1].split(separator: " ")
#expect(words.count == 2, "expected 'opts \(dnsOption)', instead got '\(lines[1])'")
#expect(words[0].lowercased() == "opts", "expected entry to list dns options, instead got '\(words[0])'")
#expect(words[1].lowercased() == dnsOption, "expected option '\(dnsOption)', instead got '\(words[1])'")
} catch {
Issue.record("failed to run container \(error)")
return
}
}

@Test func testForwardTCP() async throws {
let retries = 10
let retryDelaySeconds = Int64(3)
Expand Down Expand Up @@ -521,4 +508,15 @@ class TestCLIRunCommand: CLITest {
return
}
}

func getDefaultDomain() throws -> String? {
let (output, err, status) = try run(arguments: ["system", "dns", "default", "inspect"])
try #require(status == 0, "default DNS domain retrieval returned status \(status): \(err)")
let trimmedOutput = output.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmedOutput == "" {
return nil
}

return trimmedOutput
}
}