Files
tailscale/tstest/natlab/vmtest/openresolv_test.go
Brendan Creane 970cc199fe tstest/natlab/vmtest: add VM coverage for openresolv DNS backend (#20999)
* net/dns: export OSConfigurationReadWarnable

A natlab vmtest checks that a node is not reporting this warning. Exporting
the Warnable lets the test take the warning's text from it, instead of
keeping a copy of the wording that could stop matching without failing.

Updates #20825
Updates tailscale/corp#44793

Signed-off-by: Brendan Creane <[email protected]>

* tstest/natlab/vmtest: add VM coverage for the openresolv DNS backend

dnsMode() picks one of five Linux DNS backends, and natlab could provision
only systemd-resolved and direct. Add a DNSOpenresolv mode and a VM test for
it, so the backend behind #20825 is covered.

No cloud image ships openresolv and a guest cannot download it, so its two
source files are vendored under testdata and installed with cloud-init's
write_files. openresolv's build is a set of sed substitutions with nothing to
compile, so resolvconf.go does the substitutions in process.

Updates #20825
Updates tailscale/corp#44793

Signed-off-by: Brendan Creane <[email protected]>

---------

Signed-off-by: Brendan Creane <[email protected]>
2026-09-10 09:12:12 -07:00

130 lines
4.1 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package vmtest
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
// TestBuildOpenresolv checks that the vendored openresolv sources still build
// into what the guest expects. buildOpenresolv verifies their recorded hashes
// and rejects a surviving build placeholder, so an edited source or an upstream
// bump fails here in milliseconds. The alternative is a puzzling DNS failure
// several minutes into TestOpenresolvDNS.
func TestBuildOpenresolv(t *testing.T) {
files, err := buildOpenresolv()
if err != nil {
t.Fatal(err)
}
if len(files) != len(openresolvInstall) {
t.Fatalf("built %d files, want %d", len(files), len(openresolvInstall))
}
byPath := map[string]cloudInitFile{}
for _, f := range files {
byPath[f.path] = f
}
for _, want := range openresolvInstall {
f, ok := byPath[want.guestPath]
if !ok {
t.Errorf("%s: not built", want.guestPath)
continue
}
if len(f.content) == 0 {
t.Errorf("%s: empty", want.guestPath)
}
if f.mode != want.mode {
t.Errorf("%s: mode %q, want %q", want.guestPath, f.mode, want.mode)
}
if want.srcName != "" && !strings.HasPrefix(string(f.content), "#!/bin/sh") {
t.Errorf("%s: does not start with a /bin/sh shebang", want.guestPath)
}
}
// The substitutions have to have actually happened: resolvconf derives its
// key directory from VARDIR, and net/dns's whole openresolv path hinges on
// that directory being the one the test provisions.
rc := string(byPath[openresolvSbinDir+"/resolvconf"].content)
for _, want := range []string{
"VARDIR=" + openresolvVarDir,
"LIBEXECDIR=" + openresolvLibexecDir,
`KEYDIR="$VARDIR/keys"`,
} {
if !strings.Contains(rc, want) {
t.Errorf("built resolvconf does not contain %q", want)
}
}
}
// TestOpenresolvSHA256 checks the hash check in both directions. On its own,
// TestBuildOpenresolv only ever runs it against sources that match, so a check
// that accepted anything would still look fine there.
func TestOpenresolvSHA256(t *testing.T) {
const content = "not the vendored file"
sum := sha256.Sum256([]byte(content))
if err := checkOpenresolvSHA256("libc.in", []byte(content), hex.EncodeToString(sum[:])); err != nil {
t.Errorf("matching content was rejected: %v", err)
}
if err := checkOpenresolvSHA256("libc.in", []byte(content), strings.Repeat("0", 64)); err == nil {
t.Error("content that does not match its recorded hash was accepted")
}
}
// TestOpenresolvWriteFiles checks that the guest files survive a round trip
// through the cloud-init write_files section. resolvconf.in is 31 kB, so its
// base64 encoding is one very long YAML scalar, and a quoting or line-length
// mistake would show up as a guest that boots with a truncated shell script.
func TestOpenresolvWriteFiles(t *testing.T) {
files, err := buildOpenresolv()
if err != nil {
t.Fatal(err)
}
var ud strings.Builder
ud.WriteString("#cloud-config\n")
writeCloudInitFiles(&ud, files)
var got struct {
WriteFiles []struct {
Path string `yaml:"path"`
Encoding string `yaml:"encoding"`
Permissions string `yaml:"permissions"`
Content string `yaml:"content"`
} `yaml:"write_files"`
}
if err := yaml.Unmarshal([]byte(ud.String()), &got); err != nil {
t.Fatalf("user-data is not valid YAML: %v", err)
}
if len(got.WriteFiles) != len(files) {
t.Fatalf("write_files has %d entries, want %d", len(got.WriteFiles), len(files))
}
for i, want := range files {
g := got.WriteFiles[i]
if g.Path != want.path {
t.Errorf("entry %d: path %q, want %q", i, g.Path, want.path)
}
if g.Encoding != "b64" {
t.Errorf("%s: encoding %q, want b64", want.path, g.Encoding)
}
if g.Permissions != want.mode {
t.Errorf("%s: permissions %q, want %q", want.path, g.Permissions, want.mode)
}
content, err := base64.StdEncoding.DecodeString(g.Content)
if err != nil {
t.Errorf("%s: decoding content: %v", want.path, err)
continue
}
if string(content) != string(want.content) {
t.Errorf("%s: content survived the round trip as %d bytes, want %d",
want.path, len(content), len(want.content))
}
}
}