| 
									
										
										
										
											2016-04-13 22:08:22 +08:00
										 |  |  | // Copyright 2016 The Prometheus Authors
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-03 20:47:52 +08:00
										 |  |  | package web | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-08-10 22:28:49 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2018-01-13 18:37:38 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2017-09-01 19:12:51 +08:00
										 |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2015-07-03 20:47:52 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2019-06-19 23:56:04 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2020-11-10 07:09:39 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2015-07-03 20:47:52 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2017-09-18 18:32:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-24 21:48:15 +08:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2019-06-19 23:56:04 +08:00
										 |  |  | 	prom_testutil "github.com/prometheus/client_golang/prometheus/testutil" | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2020-10-22 17:00:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 18:17:47 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/config" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/notifier" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/rules" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/scrape" | 
					
						
							| 
									
										
										
										
											2020-02-06 23:58:38 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb" | 
					
						
							| 
									
										
										
										
											2015-07-03 20:47:52 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-21 02:21:10 +08:00
										 |  |  | func TestMain(m *testing.M) { | 
					
						
							|  |  |  | 	// On linux with a global proxy the tests will fail as the go client(http,grpc) tries to connect through the proxy.
 | 
					
						
							|  |  |  | 	os.Setenv("no_proxy", "localhost,127.0.0.1,0.0.0.0,:") | 
					
						
							|  |  |  | 	os.Exit(m.Run()) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-30 00:16:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-03 20:47:52 +08:00
										 |  |  | func TestGlobalURL(t *testing.T) { | 
					
						
							|  |  |  | 	opts := &Options{ | 
					
						
							|  |  |  | 		ListenAddress: ":9090", | 
					
						
							|  |  |  | 		ExternalURL: &url.URL{ | 
					
						
							|  |  |  | 			Scheme: "https", | 
					
						
							|  |  |  | 			Host:   "externalhost:80", | 
					
						
							|  |  |  | 			Path:   "/path/prefix", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		inURL  string | 
					
						
							|  |  |  | 		outURL string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Nothing should change if the input URL is not on localhost, even if the port is our listening port.
 | 
					
						
							|  |  |  | 			inURL:  "http://somehost:9090/metrics", | 
					
						
							|  |  |  | 			outURL: "http://somehost:9090/metrics", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Port and host should change if target is on localhost and port is our listening port.
 | 
					
						
							|  |  |  | 			inURL:  "http://localhost:9090/metrics", | 
					
						
							|  |  |  | 			outURL: "https://externalhost:80/metrics", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Only the host should change if the port is not our listening port, but the host is localhost.
 | 
					
						
							|  |  |  | 			inURL:  "http://localhost:8000/metrics", | 
					
						
							|  |  |  | 			outURL: "http://externalhost:8000/metrics", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Alternative localhost representations should also work.
 | 
					
						
							|  |  |  | 			inURL:  "http://127.0.0.1:9090/metrics", | 
					
						
							|  |  |  | 			outURL: "https://externalhost:80/metrics", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 04:40:10 +08:00
										 |  |  | 	for _, test := range tests { | 
					
						
							| 
									
										
										
										
											2015-07-03 20:47:52 +08:00
										 |  |  | 		inURL, err := url.Parse(test.inURL) | 
					
						
							| 
									
										
										
										
											2017-10-06 04:40:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2017-10-06 04:40:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-03 20:47:52 +08:00
										 |  |  | 		globalURL := tmplFuncs("", opts)["globalURL"].(func(u *url.URL) *url.URL) | 
					
						
							|  |  |  | 		outURL := globalURL(inURL) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.Equal(t, test.outURL, outURL.String()) | 
					
						
							| 
									
										
										
										
											2015-07-03 20:47:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 00:16:14 +08:00
										 |  |  | type dbAdapter struct { | 
					
						
							|  |  |  | 	*tsdb.DB | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (a *dbAdapter) Stats(statsByLabelName string) (*tsdb.Stats, error) { | 
					
						
							|  |  |  | 	return a.Head().Stats(statsByLabelName), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | func TestReadyAndHealthy(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2020-05-06 23:30:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 	dbDir, err := ioutil.TempDir("", "tsdb-ready") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	defer func() { require.NoError(t, os.RemoveAll(dbDir)) }() | 
					
						
							| 
									
										
										
										
											2017-10-06 04:40:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 23:58:38 +08:00
										 |  |  | 	db, err := tsdb.Open(dbDir, nil, nil, nil) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 	opts := &Options{ | 
					
						
							|  |  |  | 		ListenAddress:  ":9090", | 
					
						
							|  |  |  | 		ReadTimeout:    30 * time.Second, | 
					
						
							|  |  |  | 		MaxConnections: 512, | 
					
						
							|  |  |  | 		Context:        nil, | 
					
						
							| 
									
										
										
										
											2020-02-17 19:41:04 +08:00
										 |  |  | 		Storage:        nil, | 
					
						
							| 
									
										
										
										
											2020-04-30 00:16:14 +08:00
										 |  |  | 		LocalStorage:   &dbAdapter{db}, | 
					
						
							|  |  |  | 		TSDBDir:        dbDir, | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 		QueryEngine:    nil, | 
					
						
							| 
									
										
										
										
											2019-02-08 18:17:47 +08:00
										 |  |  | 		ScrapeManager:  &scrape.Manager{}, | 
					
						
							|  |  |  | 		RuleManager:    &rules.Manager{}, | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 		Notifier:       nil, | 
					
						
							|  |  |  | 		RoutePrefix:    "/", | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 		EnableAdminAPI: true, | 
					
						
							| 
									
										
										
										
											2018-09-19 15:20:53 +08:00
										 |  |  | 		ExternalURL: &url.URL{ | 
					
						
							|  |  |  | 			Scheme: "http", | 
					
						
							|  |  |  | 			Host:   "localhost:9090", | 
					
						
							|  |  |  | 			Path:   "/", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-06-24 21:48:15 +08:00
										 |  |  | 		Version:  &PrometheusVersion{}, | 
					
						
							|  |  |  | 		Gatherer: prometheus.DefaultGatherer, | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	opts.Flags = map[string]string{} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-12 02:45:52 +08:00
										 |  |  | 	webHandler := New(nil, opts) | 
					
						
							| 
									
										
										
										
											2019-02-08 18:17:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	webHandler.config = &config.Config{} | 
					
						
							|  |  |  | 	webHandler.notifier = ¬ifier.Manager{} | 
					
						
							| 
									
										
										
										
											2020-12-03 22:33:16 +08:00
										 |  |  | 	l, err := webHandler.Listener() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(fmt.Sprintf("Unable to start web listener: %s", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-08 18:17:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 	ctx, cancel := context.WithCancel(context.Background()) | 
					
						
							|  |  |  | 	defer cancel() | 
					
						
							| 
									
										
										
										
											2017-12-14 21:45:36 +08:00
										 |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2020-12-25 19:45:31 +08:00
										 |  |  | 		err := webHandler.Run(ctx, l, "") | 
					
						
							| 
									
										
										
										
											2017-12-14 21:45:36 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-01-13 18:37:38 +08:00
										 |  |  | 			panic(fmt.Sprintf("Can't start web handler:%s", err)) | 
					
						
							| 
									
										
										
										
											2017-12-14 21:45:36 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Give some time for the web goroutine to run since we need the server
 | 
					
						
							|  |  |  | 	// to be up before starting tests.
 | 
					
						
							|  |  |  | 	time.Sleep(5 * time.Second) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	resp, err := http.Get("http://localhost:9090/-/healthy") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusOK, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, u := range []string{ | 
					
						
							|  |  |  | 		"http://localhost:9090/-/ready", | 
					
						
							| 
									
										
										
										
											2020-11-03 21:51:48 +08:00
										 |  |  | 		"http://localhost:9090/classic/graph", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/flags", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/rules", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/service-discovery", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/targets", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/status", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/config", | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	} { | 
					
						
							|  |  |  | 		resp, err = http.Get(u) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 		cleanupTestResponse(t, resp) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-19 15:20:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/delete_series", "", strings.NewReader("{}")) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2019-02-08 18:17:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 	// Set to ready.
 | 
					
						
							|  |  |  | 	webHandler.Ready() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	for _, u := range []string{ | 
					
						
							|  |  |  | 		"http://localhost:9090/-/healthy", | 
					
						
							|  |  |  | 		"http://localhost:9090/-/ready", | 
					
						
							| 
									
										
										
										
											2020-11-03 21:51:48 +08:00
										 |  |  | 		"http://localhost:9090/classic/graph", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/flags", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/rules", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/service-discovery", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/targets", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/status", | 
					
						
							|  |  |  | 		"http://localhost:9090/classic/config", | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	} { | 
					
						
							|  |  |  | 		resp, err = http.Get(u) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, http.StatusOK, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 		cleanupTestResponse(t, resp) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-19 15:20:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusOK, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	cleanupSnapshot(t, dbDir, resp) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=up", "", nil) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusNoContent, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRoutePrefix(t *testing.T) { | 
					
						
							|  |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 	dbDir, err := ioutil.TempDir("", "tsdb-ready") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	defer func() { require.NoError(t, os.RemoveAll(dbDir)) }() | 
					
						
							| 
									
										
										
										
											2017-10-06 04:40:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 23:58:38 +08:00
										 |  |  | 	db, err := tsdb.Open(dbDir, nil, nil, nil) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 	opts := &Options{ | 
					
						
							|  |  |  | 		ListenAddress:  ":9091", | 
					
						
							|  |  |  | 		ReadTimeout:    30 * time.Second, | 
					
						
							|  |  |  | 		MaxConnections: 512, | 
					
						
							|  |  |  | 		Context:        nil, | 
					
						
							| 
									
										
										
										
											2020-04-30 00:16:14 +08:00
										 |  |  | 		TSDBDir:        dbDir, | 
					
						
							|  |  |  | 		LocalStorage:   &dbAdapter{db}, | 
					
						
							| 
									
										
										
										
											2020-02-17 19:41:04 +08:00
										 |  |  | 		Storage:        nil, | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 		QueryEngine:    nil, | 
					
						
							| 
									
										
										
										
											2017-11-30 06:52:38 +08:00
										 |  |  | 		ScrapeManager:  nil, | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 		RuleManager:    nil, | 
					
						
							|  |  |  | 		Notifier:       nil, | 
					
						
							|  |  |  | 		RoutePrefix:    "/prometheus", | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 		EnableAdminAPI: true, | 
					
						
							| 
									
										
										
										
											2020-02-18 01:19:15 +08:00
										 |  |  | 		ExternalURL: &url.URL{ | 
					
						
							|  |  |  | 			Host:   "localhost.localdomain:9090", | 
					
						
							|  |  |  | 			Scheme: "http", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	opts.Flags = map[string]string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	webHandler := New(nil, opts) | 
					
						
							| 
									
										
										
										
											2020-12-03 22:33:16 +08:00
										 |  |  | 	l, err := webHandler.Listener() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(fmt.Sprintf("Unable to start web listener: %s", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 	ctx, cancel := context.WithCancel(context.Background()) | 
					
						
							|  |  |  | 	defer cancel() | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2020-12-25 19:45:31 +08:00
										 |  |  | 		err := webHandler.Run(ctx, l, "") | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-01-13 18:37:38 +08:00
										 |  |  | 			panic(fmt.Sprintf("Can't start web handler:%s", err)) | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Give some time for the web goroutine to run since we need the server
 | 
					
						
							|  |  |  | 	// to be up before starting tests.
 | 
					
						
							|  |  |  | 	time.Sleep(5 * time.Second) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	resp, err := http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusOK, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/delete_series", "", strings.NewReader("{}")) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Set to ready.
 | 
					
						
							|  |  |  | 	webHandler.Ready() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusOK, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 	resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusOK, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-09-06 20:41:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusOK, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	cleanupSnapshot(t, dbDir, resp) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-10-05 13:38:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 	resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/delete_series?match[]=up", "", nil) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, http.StatusNoContent, resp.StatusCode) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	cleanupTestResponse(t, resp) | 
					
						
							| 
									
										
										
										
											2017-07-26 08:47:45 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-15 21:22:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 19:12:51 +08:00
										 |  |  | func TestDebugHandler(t *testing.T) { | 
					
						
							|  |  |  | 	for _, tc := range []struct { | 
					
						
							|  |  |  | 		prefix, url string | 
					
						
							|  |  |  | 		code        int | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{"/", "/debug/pprof/cmdline", 200}, | 
					
						
							|  |  |  | 		{"/foo", "/foo/debug/pprof/cmdline", 200}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		{"/", "/debug/pprof/goroutine", 200}, | 
					
						
							|  |  |  | 		{"/foo", "/foo/debug/pprof/goroutine", 200}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		{"/", "/debug/pprof/foo", 404}, | 
					
						
							|  |  |  | 		{"/foo", "/bar/debug/pprof/goroutine", 404}, | 
					
						
							|  |  |  | 	} { | 
					
						
							|  |  |  | 		opts := &Options{ | 
					
						
							| 
									
										
										
										
											2020-02-18 01:19:15 +08:00
										 |  |  | 			RoutePrefix:   tc.prefix, | 
					
						
							|  |  |  | 			ListenAddress: "somehost:9090", | 
					
						
							|  |  |  | 			ExternalURL: &url.URL{ | 
					
						
							|  |  |  | 				Host:   "localhost.localdomain:9090", | 
					
						
							|  |  |  | 				Scheme: "http", | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-09-01 19:12:51 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-05 18:16:15 +08:00
										 |  |  | 		handler := New(nil, opts) | 
					
						
							| 
									
										
										
										
											2017-09-01 19:12:51 +08:00
										 |  |  | 		handler.Ready() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		w := httptest.NewRecorder() | 
					
						
							| 
									
										
										
										
											2017-10-06 04:40:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 19:12:51 +08:00
										 |  |  | 		req, err := http.NewRequest("GET", tc.url, nil) | 
					
						
							| 
									
										
										
										
											2017-10-06 04:40:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2017-09-01 19:12:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		handler.router.ServeHTTP(w, req) | 
					
						
							| 
									
										
										
										
											2017-10-06 04:40:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.Equal(t, tc.code, w.Code) | 
					
						
							| 
									
										
										
										
											2017-09-01 19:12:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-19 23:56:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestHTTPMetrics(t *testing.T) { | 
					
						
							|  |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2020-02-18 01:19:15 +08:00
										 |  |  | 	handler := New(nil, &Options{ | 
					
						
							|  |  |  | 		RoutePrefix:   "/", | 
					
						
							|  |  |  | 		ListenAddress: "somehost:9090", | 
					
						
							|  |  |  | 		ExternalURL: &url.URL{ | 
					
						
							|  |  |  | 			Host:   "localhost.localdomain:9090", | 
					
						
							|  |  |  | 			Scheme: "http", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-06-19 23:56:04 +08:00
										 |  |  | 	getReady := func() int { | 
					
						
							|  |  |  | 		t.Helper() | 
					
						
							|  |  |  | 		w := httptest.NewRecorder() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		req, err := http.NewRequest("GET", "/-/ready", nil) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2019-06-19 23:56:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		handler.router.ServeHTTP(w, req) | 
					
						
							|  |  |  | 		return w.Code | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	code := getReady() | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.Equal(t, http.StatusServiceUnavailable, code) | 
					
						
							| 
									
										
										
										
											2019-06-24 21:48:15 +08:00
										 |  |  | 	counter := handler.metrics.requestCounter | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.Equal(t, 1, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable))))) | 
					
						
							| 
									
										
										
										
											2019-06-19 23:56:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	handler.Ready() | 
					
						
							|  |  |  | 	for range [2]int{} { | 
					
						
							|  |  |  | 		code = getReady() | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.Equal(t, http.StatusOK, code) | 
					
						
							| 
									
										
										
										
											2019-06-19 23:56:04 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.Equal(t, 2, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusOK))))) | 
					
						
							|  |  |  | 	require.Equal(t, 1, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable))))) | 
					
						
							| 
									
										
										
										
											2019-06-19 23:56:04 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestShutdownWithStaleConnection(t *testing.T) { | 
					
						
							|  |  |  | 	dbDir, err := ioutil.TempDir("", "tsdb-ready") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	defer func() { require.NoError(t, os.RemoveAll(dbDir)) }() | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	db, err := tsdb.Open(dbDir, nil, nil, nil) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	timeout := 10 * time.Second | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	opts := &Options{ | 
					
						
							|  |  |  | 		ListenAddress:  ":9090", | 
					
						
							|  |  |  | 		ReadTimeout:    timeout, | 
					
						
							|  |  |  | 		MaxConnections: 512, | 
					
						
							|  |  |  | 		Context:        nil, | 
					
						
							|  |  |  | 		Storage:        nil, | 
					
						
							|  |  |  | 		LocalStorage:   &dbAdapter{db}, | 
					
						
							|  |  |  | 		TSDBDir:        dbDir, | 
					
						
							|  |  |  | 		QueryEngine:    nil, | 
					
						
							|  |  |  | 		ScrapeManager:  &scrape.Manager{}, | 
					
						
							|  |  |  | 		RuleManager:    &rules.Manager{}, | 
					
						
							|  |  |  | 		Notifier:       nil, | 
					
						
							|  |  |  | 		RoutePrefix:    "/", | 
					
						
							|  |  |  | 		ExternalURL: &url.URL{ | 
					
						
							|  |  |  | 			Scheme: "http", | 
					
						
							|  |  |  | 			Host:   "localhost:9090", | 
					
						
							|  |  |  | 			Path:   "/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		Version:  &PrometheusVersion{}, | 
					
						
							|  |  |  | 		Gatherer: prometheus.DefaultGatherer, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	opts.Flags = map[string]string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	webHandler := New(nil, opts) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	webHandler.config = &config.Config{} | 
					
						
							|  |  |  | 	webHandler.notifier = ¬ifier.Manager{} | 
					
						
							| 
									
										
										
										
											2020-12-03 22:33:16 +08:00
										 |  |  | 	l, err := webHandler.Listener() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(fmt.Sprintf("Unable to start web listener: %s", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	closed := make(chan struct{}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ctx, cancel := context.WithCancel(context.Background()) | 
					
						
							|  |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2020-12-25 19:45:31 +08:00
										 |  |  | 		err := webHandler.Run(ctx, l, "") | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic(fmt.Sprintf("Can't start web handler:%s", err)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		close(closed) | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Give some time for the web goroutine to run since we need the server
 | 
					
						
							|  |  |  | 	// to be up before starting tests.
 | 
					
						
							|  |  |  | 	time.Sleep(5 * time.Second) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Open a socket, and don't use it. This connection should then be closed
 | 
					
						
							|  |  |  | 	// after the ReadTimeout.
 | 
					
						
							|  |  |  | 	c, err := net.Dial("tcp", "localhost:9090") | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	t.Cleanup(func() { require.NoError(t, c.Close()) }) | 
					
						
							| 
									
										
										
										
											2020-08-17 16:50:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Stop the web handler.
 | 
					
						
							|  |  |  | 	cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	select { | 
					
						
							|  |  |  | 	case <-closed: | 
					
						
							|  |  |  | 	case <-time.After(timeout + 5*time.Second): | 
					
						
							|  |  |  | 		t.Fatalf("Server still running after read timeout.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 07:09:39 +08:00
										 |  |  | func TestHandleMultipleQuitRequests(t *testing.T) { | 
					
						
							|  |  |  | 	opts := &Options{ | 
					
						
							|  |  |  | 		ListenAddress:   ":9090", | 
					
						
							|  |  |  | 		MaxConnections:  512, | 
					
						
							|  |  |  | 		EnableLifecycle: true, | 
					
						
							|  |  |  | 		RoutePrefix:     "/", | 
					
						
							|  |  |  | 		ExternalURL: &url.URL{ | 
					
						
							|  |  |  | 			Scheme: "http", | 
					
						
							|  |  |  | 			Host:   "localhost:9090", | 
					
						
							|  |  |  | 			Path:   "/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	webHandler := New(nil, opts) | 
					
						
							|  |  |  | 	webHandler.config = &config.Config{} | 
					
						
							|  |  |  | 	webHandler.notifier = ¬ifier.Manager{} | 
					
						
							| 
									
										
										
										
											2020-12-03 22:33:16 +08:00
										 |  |  | 	l, err := webHandler.Listener() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(fmt.Sprintf("Unable to start web listener: %s", err)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-10 07:09:39 +08:00
										 |  |  | 	ctx, cancel := context.WithCancel(context.Background()) | 
					
						
							|  |  |  | 	closed := make(chan struct{}) | 
					
						
							|  |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2020-12-25 19:45:31 +08:00
										 |  |  | 		err := webHandler.Run(ctx, l, "") | 
					
						
							| 
									
										
										
										
											2020-11-10 07:09:39 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			panic(fmt.Sprintf("Can't start web handler:%s", err)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		close(closed) | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Give some time for the web goroutine to run since we need the server
 | 
					
						
							|  |  |  | 	// to be up before starting tests.
 | 
					
						
							|  |  |  | 	time.Sleep(5 * time.Second) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	start := make(chan struct{}) | 
					
						
							|  |  |  | 	var wg sync.WaitGroup | 
					
						
							|  |  |  | 	for i := 0; i < 3; i++ { | 
					
						
							|  |  |  | 		wg.Add(1) | 
					
						
							|  |  |  | 		go func() { | 
					
						
							|  |  |  | 			defer wg.Done() | 
					
						
							|  |  |  | 			<-start | 
					
						
							|  |  |  | 			resp, err := http.Post("http://localhost:9090/-/quit", "", strings.NewReader("")) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			require.Equal(t, http.StatusOK, resp.StatusCode) | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	close(start) | 
					
						
							|  |  |  | 	wg.Wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Stop the web handler.
 | 
					
						
							|  |  |  | 	cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	select { | 
					
						
							|  |  |  | 	case <-closed: | 
					
						
							|  |  |  | 	case <-time.After(5 * time.Second): | 
					
						
							|  |  |  | 		t.Fatalf("Server still running after 5 seconds.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | func cleanupTestResponse(t *testing.T, resp *http.Response) { | 
					
						
							|  |  |  | 	_, err := io.Copy(ioutil.Discard, resp.Body) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.NoError(t, resp.Body.Close()) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | func cleanupSnapshot(t *testing.T, dbDir string, resp *http.Response) { | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	snapshot := &struct { | 
					
						
							| 
									
										
										
										
											2020-09-15 15:30:55 +08:00
										 |  |  | 		Data struct { | 
					
						
							|  |  |  | 			Name string `json:"name"` | 
					
						
							|  |  |  | 		} `json:"data"` | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | 	}{} | 
					
						
							|  |  |  | 	b, err := ioutil.ReadAll(resp.Body) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.NoError(t, json.Unmarshal(b, snapshot)) | 
					
						
							|  |  |  | 	require.NotZero(t, snapshot.Data.Name, "snapshot directory not returned") | 
					
						
							|  |  |  | 	require.NoError(t, os.Remove(filepath.Join(dbDir, "snapshots", snapshot.Data.Name))) | 
					
						
							|  |  |  | 	require.NoError(t, os.Remove(filepath.Join(dbDir, "snapshots"))) | 
					
						
							| 
									
										
										
										
											2020-08-29 18:22:00 +08:00
										 |  |  | } |