| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-01-12 08:13:50 +00:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2016-10-08 16:44:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy" | 
					
						
							| 
									
										
										
										
											2020-01-30 09:19:26 +00:00
										 |  |  | 	_ "github.com/coredns/coredns/core" // Hook in CoreDNS. | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver" | 
					
						
							| 
									
										
										
										
											2020-01-30 09:19:26 +00:00
										 |  |  | 	_ "github.com/coredns/coredns/core/plugin" // Load all managed plugins in github.com/coredns/coredns. | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 08:13:50 +00:00
										 |  |  | var mu sync.Mutex | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | // CoreDNSServer returns a CoreDNS test server. It just takes a normal Corefile as input. | 
					
						
							|  |  |  | func CoreDNSServer(corefile string) (*caddy.Instance, error) { | 
					
						
							| 
									
										
										
										
											2017-01-12 08:13:50 +00:00
										 |  |  | 	mu.Lock() | 
					
						
							|  |  |  | 	defer mu.Unlock() | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 	caddy.Quiet = true | 
					
						
							| 
									
										
										
										
											2016-10-08 16:44:43 +01:00
										 |  |  | 	dnsserver.Quiet = true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 	return caddy.Start(NewInput(corefile)) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | // CoreDNSServerStop stops a server. | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func CoreDNSServerStop(i *caddy.Instance) { i.Stop() } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | // CoreDNSServerPorts returns the ports the instance is listening on. The integer k indicates | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | // which ServerListener you want. | 
					
						
							|  |  |  | func CoreDNSServerPorts(i *caddy.Instance, k int) (udp, tcp string) { | 
					
						
							|  |  |  | 	srvs := i.Servers() | 
					
						
							|  |  |  | 	if len(srvs) < k+1 { | 
					
						
							|  |  |  | 		return "", "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	u := srvs[k].LocalAddr() | 
					
						
							|  |  |  | 	t := srvs[k].Addr() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if u != nil { | 
					
						
							|  |  |  | 		udp = u.String() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if t != nil { | 
					
						
							|  |  |  | 		tcp = t.String() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | // CoreDNSServerAndPorts combines CoreDNSServer and CoreDNSServerPorts to start a CoreDNS | 
					
						
							|  |  |  | // server and returns the udp and tcp ports of the first instance. | 
					
						
							|  |  |  | func CoreDNSServerAndPorts(corefile string) (i *caddy.Instance, udp, tcp string, err error) { | 
					
						
							|  |  |  | 	i, err = CoreDNSServer(corefile) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, "", "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	udp, tcp = CoreDNSServerPorts(i, 0) | 
					
						
							|  |  |  | 	return i, udp, tcp, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | // Input implements the caddy.Input interface and acts as an easy way to use a string as a Corefile. | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | type Input struct { | 
					
						
							|  |  |  | 	corefile []byte | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | // NewInput returns a pointer to Input, containing the corefile string as input. | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func NewInput(corefile string) *Input { | 
					
						
							|  |  |  | 	return &Input{corefile: []byte(corefile)} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | // Body implements the Input interface. | 
					
						
							|  |  |  | func (i *Input) Body() []byte { return i.corefile } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Path implements the Input interface. | 
					
						
							|  |  |  | func (i *Input) Path() string { return "Corefile" } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServerType implements the Input interface. | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func (i *Input) ServerType() string { return "dns" } |