mirror of
https://github.com/tailscale/tailscale.git
synced 2025-01-07 08:07:42 +00:00
net/dns: add start of Linux newOSConfigurator tests
Only one test case so far. Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
09a47ea3f1
commit
065c4ffc2c
@ -29,7 +29,6 @@ func (kv kv) String() string {
|
|||||||
func NewOSConfigurator(logf logger.Logf, interfaceName string) (ret OSConfigurator, err error) {
|
func NewOSConfigurator(logf logger.Logf, interfaceName string) (ret OSConfigurator, err error) {
|
||||||
return newOSConfigurator(logf, interfaceName, newOSConfigEnv{
|
return newOSConfigurator(logf, interfaceName, newOSConfigEnv{
|
||||||
fs: directFS{},
|
fs: directFS{},
|
||||||
ReadFile: os.ReadFile,
|
|
||||||
resolvOwner: resolvOwner,
|
resolvOwner: resolvOwner,
|
||||||
resolvedIsActuallyResolver: resolvedIsActuallyResolver,
|
resolvedIsActuallyResolver: resolvedIsActuallyResolver,
|
||||||
dbusPing: dbusPing,
|
dbusPing: dbusPing,
|
||||||
@ -42,8 +41,7 @@ func NewOSConfigurator(logf logger.Logf, interfaceName string) (ret OSConfigurat
|
|||||||
// newOSConfigEnv are the funcs newOSConfigurator needs, pulled out for testing.
|
// newOSConfigEnv are the funcs newOSConfigurator needs, pulled out for testing.
|
||||||
type newOSConfigEnv struct {
|
type newOSConfigEnv struct {
|
||||||
fs wholeFileFS
|
fs wholeFileFS
|
||||||
ReadFile func(name string) ([]byte, error)
|
resolvOwner func(resolvConfContents []byte) string
|
||||||
resolvOwner func(resolvConfContnets []byte) string
|
|
||||||
resolvedIsActuallyResolver func(wholeFileFS) error
|
resolvedIsActuallyResolver func(wholeFileFS) error
|
||||||
dbusPing func(string, string) error
|
dbusPing func(string, string) error
|
||||||
nmIsUsingResolved func() error
|
nmIsUsingResolved func() error
|
||||||
@ -63,7 +61,7 @@ func newOSConfigurator(logf logger.Logf, interfaceName string, env newOSConfigEn
|
|||||||
logf("dns: %v", debug)
|
logf("dns: %v", debug)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
bs, err := env.ReadFile("/etc/resolv.conf")
|
bs, err := env.fs.ReadFile(resolvConf)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
dbg("rc", "missing")
|
dbg("rc", "missing")
|
||||||
return newDirectManager(), nil
|
return newDirectManager(), nil
|
||||||
|
84
net/dns/manager_linux_test.go
Normal file
84
net/dns/manager_linux_test.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLinuxNewOSConfigurator(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
env newOSConfigEnv
|
||||||
|
wantLog string
|
||||||
|
want string // reflect type string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "no_obvious_resolv.conf_owner",
|
||||||
|
env: newOSConfigEnv{
|
||||||
|
fs: memFS{
|
||||||
|
"/etc/resolv.conf": "nameserver 10.0.0.1\n",
|
||||||
|
},
|
||||||
|
resolvOwner: resolvOwner,
|
||||||
|
},
|
||||||
|
wantLog: "dns: [rc=unknown ret=dns.directManager]\n",
|
||||||
|
want: "dns.directManager",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var logBuf bytes.Buffer
|
||||||
|
logf := func(format string, a ...interface{}) {
|
||||||
|
fmt.Fprintf(&logBuf, format, a...)
|
||||||
|
logBuf.WriteByte('\n')
|
||||||
|
}
|
||||||
|
osc, err := newOSConfigurator(logf, "unused_if_name0", tt.env)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got := fmt.Sprintf("%T", osc); got != tt.want {
|
||||||
|
t.Errorf("got %s; want %s", got, tt.want)
|
||||||
|
}
|
||||||
|
if tt.wantLog != string(logBuf.Bytes()) {
|
||||||
|
t.Errorf("log output mismatch:\n got: %q\nwant: %q\n", logBuf.Bytes(), tt.wantLog)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type memFS map[string]interface{} // full path => string for regular files
|
||||||
|
|
||||||
|
func (m memFS) Stat(name string) (isRegular bool, err error) {
|
||||||
|
v, ok := m[name]
|
||||||
|
if !ok {
|
||||||
|
return false, fs.ErrNotExist
|
||||||
|
}
|
||||||
|
if _, ok := v.(string); ok {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m memFS) Rename(oldName, newName string) error { panic("TODO") }
|
||||||
|
func (m memFS) Remove(name string) error { panic("TODO") }
|
||||||
|
func (m memFS) ReadFile(name string) ([]byte, error) {
|
||||||
|
v, ok := m[name]
|
||||||
|
if !ok {
|
||||||
|
return nil, fs.ErrNotExist
|
||||||
|
}
|
||||||
|
if s, ok := v.(string); ok {
|
||||||
|
return []byte(s), nil
|
||||||
|
}
|
||||||
|
panic("TODO")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fs memFS) WriteFile(name string, contents []byte, perm os.FileMode) error {
|
||||||
|
fs[name] = string(contents)
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user