76 lines
2.5 KiB
Nu
76 lines
2.5 KiB
Nu
use std/assert
|
|
use std/testing *
|
|
source ../lib/common.nu
|
|
source ../lib/guest.nu
|
|
source ../host/migrate.nu
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers that mirror the logic in `main migrate connections` so we can unit-
|
|
# test the tricky parts (tar arg assembly, sed expression building) without
|
|
# actually touching the filesystem or a guest.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# Reproduce the sed-expression builder from `main migrate connections`.
|
|
def build-sed-expr [from: list<string>, to: list<string>] {
|
|
let last_idx = ($from | length) - 1
|
|
(0..$last_idx
|
|
| each {|i| $"s#($from | get $i)#($to | get $i)#g" }
|
|
| str join "; ")
|
|
}
|
|
|
|
# Reproduce the tar-targets assembly (positional args after `tar -cf -`).
|
|
def build-tar-targets [files: list<string>, dirs: list<string>] {
|
|
$files | append $dirs
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
@test
|
|
def "sed expr substitutes each pair" [] {
|
|
let expr = build-sed-expr [host.docker.internal foo.internal] [10.77.0.1 10.0.0.1]
|
|
assert ($expr | str contains "s#host.docker.internal#10.77.0.1#g")
|
|
assert ($expr | str contains "s#foo.internal#10.0.0.1#g")
|
|
}
|
|
|
|
@test
|
|
def "sed expr pairs are joined with semicolons" [] {
|
|
let expr = build-sed-expr [a b c] [x y z]
|
|
let parts = $expr | split row "; "
|
|
assert equal ($parts | length) 3
|
|
}
|
|
|
|
@test
|
|
def "tar targets: contains all files and dirs" [] {
|
|
let targets = build-tar-targets [config.yaml auth.json .env] [connections]
|
|
assert ("config.yaml" in $targets)
|
|
assert ("auth.json" in $targets)
|
|
assert (".env" in $targets)
|
|
assert ("connections" in $targets)
|
|
}
|
|
|
|
@test
|
|
def "tar targets: no --include flags or globs" [] {
|
|
let targets = build-tar-targets [config.yaml] [connections]
|
|
assert (not ("--include" in $targets))
|
|
assert (not (($targets | any {|x| $x | str contains "**" })))
|
|
}
|
|
|
|
@test
|
|
def "tar targets: result is a flat list of strings spreadable with spread operator" [] {
|
|
let targets = build-tar-targets [config.yaml auth.json] [connections]
|
|
let all_strings = $targets | all {|x| ($x | describe) == "string" }
|
|
assert $all_strings
|
|
}
|
|
|
|
@test
|
|
def "migrate connections is registered in the command tree" [] {
|
|
let names = scope commands | where name =~ '^main' | get name
|
|
assert ("main migrate connections" in $names)
|
|
}
|
|
|
|
@test
|
|
def "main migrate is registered in the command tree" [] {
|
|
let names = scope commands | where name =~ '^main' | get name
|
|
assert ("main migrate" in $names)
|
|
}
|