diff options
author | 2018-08-21 08:52:25 -0700 | |
---|---|---|
committer | 2018-08-21 11:52:25 -0400 | |
commit | 8aa55c5ff2f30b2d4b4c5faad5e0a465648f9736 (patch) | |
tree | b0d5675604368eacf794dbaad205936e0b222ef1 /test | |
parent | b87ed01bb24e2c82b931b022dadeb4da3869c782 (diff) | |
download | coredns-8aa55c5ff2f30b2d4b4c5faad5e0a465648f9736.tar.gz coredns-8aa55c5ff2f30b2d4b4c5faad5e0a465648f9736.tar.zst coredns-8aa55c5ff2f30b2d4b4c5faad5e0a465648f9736.zip |
Metrics listener fix (#2036)
* Create test to verify correct listener behavior
* Create Unset function to remove todo items
* Reset address for prometheus listener before restarting
* Add inline documentation for Unset function
* Make shutdownTimeout a constant and change to five seconds
* Revert ForEach behavior in uniq package
Diffstat (limited to 'test')
-rw-r--r-- | test/reload_test.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/test/reload_test.go b/test/reload_test.go index 18639ff03..bc55071c6 100644 --- a/test/reload_test.go +++ b/test/reload_test.go @@ -2,6 +2,7 @@ package test import ( "bytes" + "fmt" "io/ioutil" "net/http" "strings" @@ -125,4 +126,80 @@ func TestReloadMetricsHealth(t *testing.T) { } } +func collectMetricsInfo(addr, proc string) error { + cl := &http.Client{} + resp, err := cl.Get(fmt.Sprintf("http://%s/metrics", addr)) + if err != nil { + return err + } + metrics, _ := ioutil.ReadAll(resp.Body) + if !bytes.Contains(metrics, []byte(proc)) { + return fmt.Errorf("failed to see %s in metric output", proc) + } + return nil +} + +// TestReloadSeveralTimeMetrics ensures that metrics are not pushed to +// prometheus once the metrics plugin is removed and a coredns +// reload is triggered +// 1. check that metrics have not been exported to prometheus before coredns starts +// 2. ensure that build-related metrics have been exported once coredns starts +// 3. trigger multiple reloads without changing the corefile +// 4. remove the metrics plugin and trigger a final reload +// 5. ensure the original prometheus exporter has not received more metrics +func TestReloadSeveralTimeMetrics(t *testing.T) { + //TODO: add a tool that find an available port because this needs to be a port + // that is not used in another test + promAddress := "127.0.0.1:53185" + proc := "coredns_build_info" + corefileWithMetrics := ` + .:0 { + prometheus ` + promAddress + ` + whoami + }` + corefileWithoutMetrics := ` + .:0 { + whoami + }` + if err := collectMetricsInfo(promAddress, proc); err == nil { + t.Errorf("Prometheus is listening before the test started") + } + serverWithMetrics, err := CoreDNSServer(corefileWithMetrics) + if err != nil { + if strings.Contains(err.Error(), inUse) { + return + } + t.Errorf("Could not get service instance: %s", err) + } + // verify prometheus is running + if err := collectMetricsInfo(promAddress, proc); err != nil { + t.Errorf("Prometheus is not listening : %s", err) + } + reloadCount := 2 + for i := 0; i < reloadCount; i++ { + serverReload, err := serverWithMetrics.Restart( + NewInput(corefileWithMetrics), + ) + if err != nil { + t.Errorf("Could not restart CoreDNS : %s, at loop %v", err, i) + } + if err := collectMetricsInfo(promAddress, proc); err != nil { + t.Errorf("Prometheus is not listening : %s", err) + } + serverWithMetrics = serverReload + } + // reload without prometheus + serverWithoutMetrics, err := serverWithMetrics.Restart( + NewInput(corefileWithoutMetrics), + ) + if err != nil { + t.Errorf("Could not restart a second time CoreDNS : %s", err) + } + serverWithoutMetrics.Stop() + // verify that metrics have not been pushed + if err := collectMetricsInfo(promAddress, proc); err == nil { + t.Errorf("Prometheus is still listening") + } +} + const inUse = "address already in use" |