f76f50c704
v2.1.2's release CI failed on the Windows build leg: TestApplyHostsOverride_* call applyHostsOverride(), which correctly refuses unconditionally on non-Linux (hosts_override.go) -- but the tests didn't account for `go test ./...` running on every platform the CI matrix builds for, only Linux. Skip them on non-Linux with a clear reason instead.
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/mdns"
|
|
)
|
|
|
|
func withTempHostsFile(t *testing.T, initial string) string {
|
|
t.Helper()
|
|
// applyHostsOverride refuses unconditionally on non-Linux (see
|
|
// hosts_override.go) -- these tests exercise the Linux write path
|
|
// specifically, so they'd fail for the right reason on the Windows CI
|
|
// runner if not skipped. Confirmed the hard way: a real CI run failed
|
|
// here after this was missed.
|
|
if runtime.GOOS != "linux" {
|
|
t.Skip("applyHostsOverride is Linux-only; skipping on " + runtime.GOOS)
|
|
}
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "hosts")
|
|
if initial != "" {
|
|
if err := os.WriteFile(path, []byte(initial), 0644); err != nil {
|
|
t.Fatalf("seeding temp hosts file: %v", err)
|
|
}
|
|
}
|
|
orig := hostsFilePathLinux
|
|
hostsFilePathLinux = path
|
|
t.Cleanup(func() { hostsFilePathLinux = orig })
|
|
return path
|
|
}
|
|
|
|
func TestApplyHostsOverride_AddsManagedBlock(t *testing.T) {
|
|
path := withTempHostsFile(t, "127.0.0.1\tlocalhost\n")
|
|
|
|
if err := applyHostsOverride(map[string]string{"sso.example.com": "10.0.0.5"}); err != nil {
|
|
t.Fatalf("applyHostsOverride: %v", err)
|
|
}
|
|
|
|
got, _ := os.ReadFile(path)
|
|
s := string(got)
|
|
if !strings.Contains(s, "127.0.0.1\tlocalhost") {
|
|
t.Errorf("existing content was clobbered: %q", s)
|
|
}
|
|
if !strings.Contains(s, hostsBlockBegin) || !strings.Contains(s, hostsBlockEnd) {
|
|
t.Errorf("managed block markers missing: %q", s)
|
|
}
|
|
if !strings.Contains(s, "10.0.0.5\tsso.example.com") {
|
|
t.Errorf("override entry missing: %q", s)
|
|
}
|
|
}
|
|
|
|
func TestApplyHostsOverride_ReplacesPriorBlockRatherThanStacking(t *testing.T) {
|
|
path := withTempHostsFile(t, "")
|
|
if err := applyHostsOverride(map[string]string{"sso.example.com": "10.0.0.5"}); err != nil {
|
|
t.Fatalf("first apply: %v", err)
|
|
}
|
|
if err := applyHostsOverride(map[string]string{"sso.example.com": "10.0.0.9"}); err != nil {
|
|
t.Fatalf("second apply: %v", err)
|
|
}
|
|
|
|
got, _ := os.ReadFile(path)
|
|
s := string(got)
|
|
if strings.Count(s, hostsBlockBegin) != 1 {
|
|
t.Fatalf("expected exactly one managed block, got content: %q", s)
|
|
}
|
|
if strings.Contains(s, "10.0.0.5") {
|
|
t.Errorf("stale override (10.0.0.5) should have been replaced, got: %q", s)
|
|
}
|
|
if !strings.Contains(s, "10.0.0.9") {
|
|
t.Errorf("new override missing, got: %q", s)
|
|
}
|
|
}
|
|
|
|
func TestApplyHostsOverride_EmptyEntriesRemovesBlockEntirely(t *testing.T) {
|
|
path := withTempHostsFile(t, "127.0.0.1\tlocalhost\n")
|
|
if err := applyHostsOverride(map[string]string{"sso.example.com": "10.0.0.5"}); err != nil {
|
|
t.Fatalf("apply: %v", err)
|
|
}
|
|
if err := applyHostsOverride(map[string]string{}); err != nil {
|
|
t.Fatalf("clear: %v", err)
|
|
}
|
|
|
|
got, _ := os.ReadFile(path)
|
|
s := string(got)
|
|
if strings.Contains(s, hostsBlockBegin) || strings.Contains(s, "10.0.0.5") {
|
|
t.Errorf("expected no discovery trace left after clearing, got: %q", s)
|
|
}
|
|
if !strings.Contains(s, "127.0.0.1\tlocalhost") {
|
|
t.Errorf("pre-existing content should survive a full clear, got: %q", s)
|
|
}
|
|
}
|
|
|
|
func TestHostFromURL(t *testing.T) {
|
|
cases := map[string]string{
|
|
"https://sso.example.com:443/api": "sso.example.com",
|
|
"http://sso.example.com": "sso.example.com",
|
|
"not a url at all": "",
|
|
"": "",
|
|
}
|
|
for in, want := range cases {
|
|
if got := hostFromURL(in); got != want {
|
|
t.Errorf("hostFromURL(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEntryAnnouncesHost(t *testing.T) {
|
|
entry := &mdns.ServiceEntry{InfoFields: []string{"hosts=sso.example.com,proxy.example.com"}}
|
|
if !entryAnnouncesHost(entry, "sso.example.com") {
|
|
t.Error("expected match for sso.example.com")
|
|
}
|
|
if !entryAnnouncesHost(entry, "proxy.example.com") {
|
|
t.Error("expected match for proxy.example.com")
|
|
}
|
|
if entryAnnouncesHost(entry, "jump.example.com") {
|
|
t.Error("expected no match for a host not in the TXT record")
|
|
}
|
|
}
|