github.com/mailgun/holster/v4@v4.20.0/mxresolv/dns_test.go (about)

     1  package mxresolv_test
     2  
     3  import (
     4  	"net"
     5  	"sync"
     6  
     7  	"github.com/foxcpp/go-mockdns"
     8  )
     9  
    10  type MockDNS struct {
    11  	Server *mockdns.Server
    12  	mu     sync.Mutex
    13  }
    14  
    15  func SpawnMockDNS(zones map[string]mockdns.Zone) (*MockDNS, error) {
    16  	server, err := mockdns.NewServerWithLogger(zones, nullLogger{}, false)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	return &MockDNS{
    21  		Server: server,
    22  	}, nil
    23  }
    24  
    25  func (f *MockDNS) Stop() {
    26  	_ = f.Server.Close()
    27  }
    28  
    29  func (f *MockDNS) Patch(r *net.Resolver) {
    30  	f.mu.Lock()
    31  	defer f.mu.Unlock()
    32  	f.Server.PatchNet(r)
    33  }
    34  
    35  func (f *MockDNS) UnPatch(r *net.Resolver) {
    36  	f.mu.Lock()
    37  	defer f.mu.Unlock()
    38  	mockdns.UnpatchNet(r)
    39  }
    40  
    41  type nullLogger struct{}
    42  
    43  func (l nullLogger) Printf(_ string, _ ...interface{}) {}