clientconfig_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package dns
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. )
  8. const normal string = `
  9. # Comment
  10. domain somedomain.com
  11. nameserver 10.28.10.2
  12. nameserver 11.28.10.1
  13. `
  14. const missingNewline string = `
  15. domain somedomain.com
  16. nameserver 10.28.10.2
  17. nameserver 11.28.10.1` // <- NOTE: NO newline.
  18. func testConfig(t *testing.T, data string) {
  19. tempDir, err := ioutil.TempDir("", "")
  20. if err != nil {
  21. t.Fatalf("tempDir: %v", err)
  22. }
  23. defer os.RemoveAll(tempDir)
  24. path := filepath.Join(tempDir, "resolv.conf")
  25. if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
  26. t.Fatalf("writeFile: %v", err)
  27. }
  28. cc, err := ClientConfigFromFile(path)
  29. if err != nil {
  30. t.Errorf("error parsing resolv.conf: %v", err)
  31. }
  32. if l := len(cc.Servers); l != 2 {
  33. t.Errorf("incorrect number of nameservers detected: %d", l)
  34. }
  35. if l := len(cc.Search); l != 1 {
  36. t.Errorf("domain directive not parsed correctly: %v", cc.Search)
  37. } else {
  38. if cc.Search[0] != "somedomain.com" {
  39. t.Errorf("domain is unexpected: %v", cc.Search[0])
  40. }
  41. }
  42. }
  43. func TestNameserver(t *testing.T) { testConfig(t, normal) }
  44. func TestMissingFinalNewLine(t *testing.T) { testConfig(t, missingNewline) }