| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | package health
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:52:40 +00:00
										 |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"net/http"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:52:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/erratic"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | func TestHealth(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 	h := newHealth(":0")
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 	h.h = append(h.h, &erratic.Erratic{})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	if err := h.OnStartup(); err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | 		t.Fatalf("Unable to startup the health server: %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	defer h.OnShutdown()
 | 
					
						
							| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 	go func() {
 | 
					
						
							|  |  |  | 		<-h.pollstop
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | 	// Reconstruct the http address based on the port allocated by operating system.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), path)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:52:40 +00:00
										 |  |  | 	// Nothing set should return unhealthy
 | 
					
						
							| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | 	response, err := http.Get(address)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Unable to query %s: %v", address, err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 	if response.StatusCode != 503 {
 | 
					
						
							|  |  |  | 		t.Errorf("Invalid status code: expecting '503', got '%d'", response.StatusCode)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	response.Body.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 09:52:40 +00:00
										 |  |  | 	h.poll()
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	response, err = http.Get(address)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Unable to query %s: %v", address, err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | 	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)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 	response.Body.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if string(content) != ok {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 02:01:27 -07:00
										 |  |  | 		t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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.OnShutdown()
 | 
					
						
							|  |  |  | }
 |