mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 17:53:21 -04:00 
			
		
		
		
	* reload: use OnRestart
Close the listener on OnRestart for health and metrics so the default
setup function can setup the listener when the plugin is "starting up".
Lightly test with some SIGUSR1-ing. Also checked the reload plugin with
this, seems fine:
.com.:1043
.:1043
2018/04/20 15:01:25 [INFO] CoreDNS-1.1.1
2018/04/20 15:01:25 [INFO] linux/amd64, go1.10,
CoreDNS-1.1.1
linux/amd64, go1.10,
2018/04/20 15:01:25 [INFO] Running configuration MD5 = aa8b3f03946fb60546ca1f725d482714
2018/04/20 15:02:01 [INFO] Reloading
2018/04/20 15:02:01 [INFO] Running configuration MD5 = b34a96d99e01db4015a892212560155f
2018/04/20 15:02:01 [INFO] Reloading complete
^C2018/04/20 15:02:06 [INFO] SIGINT: Shutting down
With this corefile:
.com {
  proxy . 127.0.0.1:53
  prometheus :9054
  whoami
  reload
}
. {
  proxy . 127.0.0.1:53
  prometheus :9054
  whoami
  reload
}
The prometheus port was 9053, changed that to 54 so reload would pick it
up.
From a cursory look it seems this also fixes:
Fixes #1604 #1618 #1686 #1492
* At least make it test
* Use onfinalshutdown
* reload: add reload test
This test #1604 adn right now fails.
* Address review comments
* Add bug section explaining things a bit
* compile tests
* Fix tests
* fixes
* slightly less crazy
* try to make prometheus setup less confusing
* Use ephermal port for test
* Don't use the listener
* These are shared between goroutines, just use the boolean in the main
  structure.
* Fix text in the reload README,
* Set addr to TODO once stopping it
* Morph fturb's comment into test, to test reload and scrape health and
  metric endpoint
		
	
		
			
				
	
	
		
			78 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package health
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"io/ioutil"
 | |
| 	"net/http"
 | |
| 	"testing"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/coredns/coredns/plugin/erratic"
 | |
| )
 | |
| 
 | |
| func TestHealth(t *testing.T) {
 | |
| 	h := newHealth(":0")
 | |
| 	h.h = append(h.h, &erratic.Erratic{})
 | |
| 
 | |
| 	if err := h.OnStartup(); err != nil {
 | |
| 		t.Fatalf("Unable to startup the health server: %v", err)
 | |
| 	}
 | |
| 	defer h.OnFinalShutdown()
 | |
| 
 | |
| 	go func() {
 | |
| 		<-h.pollstop
 | |
| 		return
 | |
| 	}()
 | |
| 
 | |
| 	// Reconstruct the http address based on the port allocated by operating system.
 | |
| 	address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), path)
 | |
| 
 | |
| 	// Nothing set should return unhealthy
 | |
| 	response, err := http.Get(address)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Unable to query %s: %v", address, err)
 | |
| 	}
 | |
| 	if response.StatusCode != 503 {
 | |
| 		t.Errorf("Invalid status code: expecting '503', got '%d'", response.StatusCode)
 | |
| 	}
 | |
| 	response.Body.Close()
 | |
| 
 | |
| 	h.poll()
 | |
| 
 | |
| 	response, err = http.Get(address)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Unable to query %s: %v", address, err)
 | |
| 	}
 | |
| 	if response.StatusCode != 200 {
 | |
| 		t.Errorf("Invalid status code: expecting '200', got '%d'", response.StatusCode)
 | |
| 	}
 | |
| 	content, err := ioutil.ReadAll(response.Body)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Unable to get response body from %s: %v", address, err)
 | |
| 	}
 | |
| 	response.Body.Close()
 | |
| 
 | |
| 	if string(content) != ok {
 | |
| 		t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestHealthLameduck(t *testing.T) {
 | |
| 	h := newHealth(":0")
 | |
| 	h.lameduck = 250 * time.Millisecond
 | |
| 	h.h = append(h.h, &erratic.Erratic{})
 | |
| 
 | |
| 	if err := h.OnStartup(); err != nil {
 | |
| 		t.Fatalf("Unable to startup the health server: %v", err)
 | |
| 	}
 | |
| 
 | |
| 	// Both these things are behind a sync.Once, fake reading from the channels.
 | |
| 	go func() {
 | |
| 		<-h.pollstop
 | |
| 		<-h.stop
 | |
| 		return
 | |
| 	}()
 | |
| 
 | |
| 	h.OnFinalShutdown()
 | |
| }
 |