From b08d143229d094b76012fbac641c2bfd93cec298 Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Thu, 4 Aug 2022 16:17:31 +0100 Subject: [PATCH] Refactor tests --- cmd/pint/main_test.go | 124 +++++++++++++++++- cmd/pint/tests/0031_ci_bitbucket.txt | 60 +-------- cmd/pint/tests/0032_ci_github.txt | 61 +-------- cmd/pint/tests/0033_ci_github_multi.txt | 61 +-------- cmd/pint/tests/0043_watch_cancel.txt | 62 +-------- .../tests/0054_watch_metrics_prometheus.txt | 80 +---------- cmd/pint/tests/0055_prometheus_failover.txt | 87 +----------- .../0057_watch_metrics_prometheus_ignore.txt | 80 +---------- cmd/pint/tests/0068_skip_ci.txt | 60 +-------- .../tests/0069_bitbucket_skip_unmodified.txt | 60 +-------- cmd/pint/tests/0070_bitbucket_string.txt | 60 +-------- cmd/pint/tests/0071_ci_owner.txt | 60 +-------- .../0072_bitbucket_move_bug_to_modified.txt | 61 +-------- cmd/pint/tests/0075_ci_strict.txt | 60 +-------- cmd/pint/tests/0076_ci_group_errors.txt | 60 +-------- cmd/pint/tests/0080_lint_online.txt | 101 +------------- cmd/pint/tests/0083_github_action.txt | 61 +-------- .../tests/0084_github_action_override.txt | 61 +-------- cmd/pint/tests/0085_github_no_envs.txt | 61 +-------- cmd/pint/tests/0088_rule_link.txt | 85 +----------- 20 files changed, 174 insertions(+), 1231 deletions(-) diff --git a/cmd/pint/main_test.go b/cmd/pint/main_test.go index 2c598e26..9a3f74ce 100644 --- a/cmd/pint/main_test.go +++ b/cmd/pint/main_test.go @@ -1,9 +1,15 @@ package main import ( + "context" "fmt" + "net" + "net/http" "os" + "strconv" + "strings" "testing" + "time" "github.com/rogpeppe/go-internal/testscript" "github.com/rs/zerolog" @@ -44,15 +50,119 @@ func TestScripts(t *testing.T) { testscript.Run(t, testscript.Params{ Dir: "tests", UpdateScripts: os.Getenv("UPDATE_SNAPSHOTS") == "1", + Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ + "http": httpServer, + }, Setup: func(env *testscript.Env) error { - // inject an env variable with the current working directory - // so we can use it to copy files into testscript workdir - cwd, err := os.Getwd() - if err != nil { - return err - } - env.Vars = append(env.Vars, fmt.Sprintf("TESTCWD=%s", cwd)) + env.Values["mocks"] = &httpMocks{responses: map[string][]httpMock{}} return nil }, }) } + +func httpServer(ts *testscript.TestScript, neg bool, args []string) { + mocks := ts.Value("mocks").(*httpMocks) + + if len(args) == 0 { + ts.Fatalf("! http command requires arguments") + } + cmd := args[0] + + switch cmd { + // http response name /200 200 OK + case "response": + if len(args) < 5 { + ts.Fatalf("! http response command requires '$NAME $PATH $CODE $BODY' args, got [%s]", strings.Join(args, " ")) + } + name := args[1] + path := args[2] + code, err := strconv.Atoi(args[3]) + ts.Check(err) + body := strings.Join(args[4:], " ") + mocks.add(name, httpMock{pattern: path, handler: func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(code) + _, err := w.Write([]byte(body)) + ts.Check(err) + }}) + // http response name /200 200 OK + case "slow-response": + if len(args) < 6 { + ts.Fatalf("! http response command requires '$NAME $PATH $DELAY $CODE $BODY' args, got [%s]", strings.Join(args, " ")) + } + name := args[1] + path := args[2] + delay, err := time.ParseDuration(args[3]) + ts.Check(err) + code, err := strconv.Atoi(args[4]) + ts.Check(err) + body := strings.Join(args[5:], " ") + mocks.add(name, httpMock{pattern: path, handler: func(w http.ResponseWriter, r *http.Request) { + time.Sleep(delay) + w.WriteHeader(code) + _, err := w.Write([]byte(body)) + ts.Check(err) + }}) + // http redirect name /foo/src /dst + case "redirect": + if len(args) != 4 { + ts.Fatalf("! http redirect command requires '$NAME $SRCPATH $DSTPATH' args, got [%s]", strings.Join(args, " ")) + } + name := args[1] + srcpath := args[2] + dstpath := args[3] + mocks.add(name, httpMock{pattern: srcpath, handler: func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Location", dstpath) + w.WriteHeader(http.StatusFound) + }}) + // http start name 127.0.0.1:7088 + case "start": + if len(args) != 3 { + ts.Fatalf("! http start command requires '$NAME $LISTEN' args, got [%s]", strings.Join(args, " ")) + } + name := args[1] + listen := args[2] + + mux := http.NewServeMux() + for n, mockList := range mocks.responses { + if n == name { + for _, mock := range mockList { + mock := mock + mux.HandleFunc(mock.pattern, mock.handler) + } + break + } + } + + listener, err := net.Listen("tcp", listen) + ts.Check(err) + server := &http.Server{Addr: listen, Handler: mux} + go func() { + _ = server.Serve(listener) + }() + + ts.Defer(func() { + ts.Logf("http server %s shutting down", name) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + _ = server.Shutdown(ctx) + }) + default: + ts.Fatalf("! unknown http command: %v", args) + } +} + +type httpMock struct { + pattern string + handler func(http.ResponseWriter, *http.Request) +} + +type httpMocks struct { + responses map[string][]httpMock +} + +func (m *httpMocks) add(name string, mock httpMock) { + if _, ok := m.responses[name]; !ok { + m.responses[name] = []httpMock{} + } + m.responses[name] = append(m.responses[name], mock) +} diff --git a/cmd/pint/tests/0031_ci_bitbucket.txt b/cmd/pint/tests/0031_ci_bitbucket.txt index a2348aaf..e96c32e8 100644 --- a/cmd/pint/tests/0031_ci_bitbucket.txt +++ b/cmd/pint/tests/0031_ci_bitbucket.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response bitbucket / 200 OK +http start bitbucket 127.0.0.1:6031 mkdir testrepo cd testrepo @@ -25,7 +25,6 @@ stderr 'level=debug msg="Sending a request to BitBucket" method=PUT' stderr 'level=debug msg="BitBucket request completed" status=200' stderr 'level=debug msg="Sending a request to BitBucket" method=DELETE' stderr 'level=debug msg="BitBucket request completed" status=200' -exec sh -c 'cat ../server.pid | xargs kill' -- src/v1.yml -- - alert: rule1 @@ -56,58 +55,3 @@ repository { repository = "rules" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "OK") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6031") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6031", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0032_ci_github.txt b/cmd/pint/tests/0032_ci_github.txt index 261b0c5f..5aa59033 100644 --- a/cmd/pint/tests/0032_ci_github.txt +++ b/cmd/pint/tests/0032_ci_github.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response github / 200 {} +http start github 127.0.0.1:6032 mkdir testrepo cd testrepo @@ -24,8 +24,6 @@ pint.ok -l debug --offline --no-color ci ! stdout . stderr 'level=info msg="Report submitted" status="200 OK"' -exec sh -c 'cat ../server.pid | xargs kill' - -- src/v1.yml -- - alert: rule1 expr: sum(foo) by(job) @@ -56,58 +54,3 @@ repository { repo = "pint" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "{}") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6032") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6032", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0033_ci_github_multi.txt b/cmd/pint/tests/0033_ci_github_multi.txt index 03a848f3..c05e98dd 100644 --- a/cmd/pint/tests/0033_ci_github_multi.txt +++ b/cmd/pint/tests/0033_ci_github_multi.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response github / 200 {} +http start github 127.0.0.1:6033 mkdir testrepo cd testrepo @@ -24,8 +24,6 @@ pint.error -l debug --no-color ci ! stdout . stderr 'level=info msg="Report submitted" status="200 OK"' -exec sh -c 'cat ../server.pid | xargs kill' - -- src/v1.yml -- - alert: rule1 expr: sum(foo) by(instance) @@ -62,58 +60,3 @@ repository { repo = "pint" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "{}") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6033") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6033", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0043_watch_cancel.txt b/cmd/pint/tests/0043_watch_cancel.txt index 8191be39..3031dec9 100644 --- a/cmd/pint/tests/0043_watch_cancel.txt +++ b/cmd/pint/tests/0043_watch_cancel.txt @@ -1,5 +1,5 @@ -exec bash -x ./prometheus.sh & -exec bash -c 'I=0 ; while [ ! -f prometheus.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http slow-response github / 30s 200 {} +http start github 127.0.0.1:7043 exec bash -x ./test.sh & @@ -15,7 +15,6 @@ stderr 'level=info msg="Background worker finished"' -- test.sh -- sleep 3 cat pint.pid | xargs kill -cat prometheus.pid | xargs kill -- rules/1.yml -- - record: aggregate @@ -30,60 +29,3 @@ prometheus "slow" { timeout = "2m" required = true } - --- prometheus.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(500) - time.Sleep(time.Second*30) - io.WriteString(w, "Fatal error") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:7043") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:7043", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("prometheus.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- prometheus.sh -- -env GOCACHE=$TMPDIR go run prometheus.go diff --git a/cmd/pint/tests/0054_watch_metrics_prometheus.txt b/cmd/pint/tests/0054_watch_metrics_prometheus.txt index 17c77d13..c5b9ca2c 100644 --- a/cmd/pint/tests/0054_watch_metrics_prometheus.txt +++ b/cmd/pint/tests/0054_watch_metrics_prometheus.txt @@ -1,5 +1,6 @@ -exec bash -x ./prometheus.sh & -exec bash -c 'I=0 ; while [ ! -f prometheus.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http slow-response prometheus /api/v1/status/config 100ms 500 Fatal Error +http slow-response prometheus /api/v1/query 400ms 200 {"status":"error","errorType":"bad_data","error":"bogus query"} +http start prometheus 127.0.0.1:7054 exec bash -x ./test.sh & @@ -10,7 +11,6 @@ cmp curl.txt metrics.txt sleep 3 curl -s http://127.0.0.1:6054/metrics | grep 'pint_' | perl -pe "s/^([a-zA-Z].+)[ ]([0-9\.\-\+eE]+)$/\1/g" > curl.txt cat pint.pid | xargs kill -cat prometheus.pid | xargs kill -- rules/1.yml -- - record: broken @@ -83,13 +83,13 @@ pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",p pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="cound't run \"promql/rate\" checks due to prometheus \"prom1\" at http://127.0.0.1:7054 connection error: server_error: server error: 500",reporter="promql/rate",severity="bug"} pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="cound't run \"promql/rate\" checks due to prometheus \"prom2\" at http://127.0.0.1:1054 connection error: connection refused",reporter="promql/rate",severity="bug"} pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="cound't run \"promql/series\" checks due to prometheus \"prom2\" at http://127.0.0.1:1054 connection error: connection refused",reporter="promql/series",severity="bug"} -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="prometheus \"prom1\" at http://127.0.0.1:7054 failed with: bad_response: Unmarshal: there are bytes left after unmarshal, error found in #10 byte of ...|y\"\n }Fatal error|..., bigger context ...|:\"bad_data\",\n \"error\":\"bogus query\"\n }Fatal error|...",reporter="promql/series",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="prometheus \"prom1\" at http://127.0.0.1:7054 failed with: bad_data: bogus query",reporter="promql/series",severity="bug"} pint_problem{filename="rules/1.yml",kind="recording",name="broken",owner="",problem="syntax error: no arguments for aggregate expression provided",reporter="promql/syntax",severity="fatal"} pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="cound't run \"promql/range_query\" checks due to prometheus \"prom2\" at http://127.0.0.1:1054 connection error: connection refused",reporter="promql/range_query",severity="bug"} pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="cound't run \"promql/rate\" checks due to prometheus \"prom1\" at http://127.0.0.1:7054 connection error: server_error: server error: 500",reporter="promql/rate",severity="bug"} pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="cound't run \"promql/rate\" checks due to prometheus \"prom2\" at http://127.0.0.1:1054 connection error: connection refused",reporter="promql/rate",severity="bug"} pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="cound't run \"promql/series\" checks due to prometheus \"prom2\" at http://127.0.0.1:1054 connection error: connection refused",reporter="promql/series",severity="bug"} -pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="prometheus \"prom1\" at http://127.0.0.1:7054 failed with: bad_response: Unmarshal: there are bytes left after unmarshal, error found in #10 byte of ...|y\"\n }Fatal error|..., bigger context ...|:\"bad_data\",\n \"error\":\"bogus query\"\n }Fatal error|...",reporter="promql/series",severity="bug"} +pint_problem{filename="rules/2.yml",kind="alerting",name="comparison",owner="bob and alice",problem="prometheus \"prom1\" at http://127.0.0.1:7054 failed with: bad_data: bogus query",reporter="promql/series",severity="bug"} # HELP pint_problems Total number of problems reported by pint # TYPE pint_problems gauge pint_problems @@ -119,7 +119,7 @@ pint_prometheus_queries_total{endpoint="/api/v1/status/flags",name="prom1"} pint_prometheus_queries_total{endpoint="/api/v1/status/flags",name="prom2"} # HELP pint_prometheus_query_errors_total Total number of failed prometheus queries # TYPE pint_prometheus_query_errors_total counter -pint_prometheus_query_errors_total{endpoint="/api/v1/query",name="prom1",reason="api/bad_response"} +pint_prometheus_query_errors_total{endpoint="/api/v1/query",name="prom1",reason="api/bad_data"} pint_prometheus_query_errors_total{endpoint="/api/v1/query",name="prom2",reason="connection/error"} pint_prometheus_query_errors_total{endpoint="/api/v1/status/config",name="prom1",reason="api/server_error"} pint_prometheus_query_errors_total{endpoint="/api/v1/status/config",name="prom2",reason="connection/error"} @@ -133,71 +133,3 @@ pint_rules_parsed_total{kind="recording"} # HELP pint_version Version information # TYPE pint_version gauge pint_version{version="unknown"} --- prometheus.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/api/v1/status/config", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(500) - time.Sleep(time.Millisecond * 100) - io.WriteString(w, "Fatal error") - }) - - http.HandleFunc("/api/v1/query", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(400) - time.Sleep(time.Millisecond * 200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{ - "status":"error", - "errorType":"bad_data", - "error":"bogus query" - }`)) - io.WriteString(w, "Fatal error") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:7054") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:7054", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("prometheus.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- prometheus.sh -- -env GOCACHE=$TMPDIR go run prometheus.go diff --git a/cmd/pint/tests/0055_prometheus_failover.txt b/cmd/pint/tests/0055_prometheus_failover.txt index 68823201..0a10aa8d 100644 --- a/cmd/pint/tests/0055_prometheus_failover.txt +++ b/cmd/pint/tests/0055_prometheus_failover.txt @@ -1,5 +1,7 @@ -exec bash -x ./prometheus.sh & -exec bash -c 'I=0 ; while [ ! -f prometheus.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response prometheus /api/v1/status/config 200 {"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}} +http response prometheus /api/v1/query_range 200 {"status":"success","data":{"resultType":"matrix","result":[]}} +http response prometheus /api/v1/query 200 {"status":"success","data":{"resultType":"vector","result":[]}} +http start prometheus 127.0.0.1:7055 pint.error --no-color lint rules ! stdout . @@ -7,7 +9,6 @@ stderr 'level=error msg="Query returned an error" error="Post \\"http://127.0.0. stderr 'level=error msg="Query returned an error" error="failed to query Prometheus config: Get \\"http://127.0.0.1:1055/api/v1/status/config\\": dial tcp 127.0.0.1:1055: connect: connection refused" query=/api/v1/status/config uri=http://127.0.0.1:1055' ! stderr 'query="count\(foo offset ' stderr 'rules/1.yml:2: prometheus "prom" at http://127.0.0.1:7055 didn''t have any series for "foo" metric in the last 1w \(promql/series\)' -exec bash -c 'cat prometheus.pid | xargs kill' -- rules/1.yml -- - record: aggregate @@ -23,83 +24,3 @@ prometheus "prom" { parser { relaxed = [".*"] } - --- prometheus.go -- -package main - -import ( - "context" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/api/v1/status/config", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`)) - }) - - http.HandleFunc("/api/v1/query_range", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{ - "status":"success", - "data":{ - "resultType":"matrix", - "result":[] - } - }`)) - }) - - http.HandleFunc("/api/v1/query", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{ - "status":"success", - "data":{ - "resultType":"vector", - "result":[] - } - }`)) - }) - - listener, err := net.Listen("tcp", "127.0.0.1:7055") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:7055", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("prometheus.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- prometheus.sh -- -env GOCACHE=$TMPDIR go run prometheus.go diff --git a/cmd/pint/tests/0057_watch_metrics_prometheus_ignore.txt b/cmd/pint/tests/0057_watch_metrics_prometheus_ignore.txt index ccf780ff..62128ee8 100644 --- a/cmd/pint/tests/0057_watch_metrics_prometheus_ignore.txt +++ b/cmd/pint/tests/0057_watch_metrics_prometheus_ignore.txt @@ -1,5 +1,6 @@ -exec bash -x ./prometheus.sh & -exec bash -c 'I=0 ; while [ ! -f prometheus.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http slow-response prometheus /api/v1/status/config 100ms 500 Fatal error +http slow-response prometheus /api/v1/query 200ms 400 {"status":"error","errorType":"bad_data","error":"bogus query"} +http start prometheus 127.0.0.1:7057 exec bash -x ./test.sh & @@ -10,7 +11,6 @@ cmp curl.txt metrics.txt sleep 3 curl -s http://127.0.0.1:6057/metrics | grep 'pint_' | perl -pe "s/^([a-zA-Z].+)[ ]([0-9\.\-\+eE]+)$/\1/g" > curl.txt cat pint.pid | xargs kill -cat prometheus.pid | xargs kill -- rules/1.yml -- - record: broken @@ -77,8 +77,8 @@ pint_last_run_duration_seconds pint_last_run_time_seconds # HELP pint_problem Prometheus rule problem reported by pint # TYPE pint_problem gauge -pint_problem{filename="rules/1.yml",kind="alerting",name="comparison",owner="",problem="prometheus \"prom1\" at http://127.0.0.1:7057 failed with: bad_response: Unmarshal: there are bytes left after unmarshal, error found in #10 byte of ...|y\"\n }Fatal error|..., bigger context ...|:\"bad_data\",\n \"error\":\"bogus query\"\n }Fatal error|...",reporter="promql/series",severity="bug"} -pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="prometheus \"prom1\" at http://127.0.0.1:7057 failed with: bad_response: Unmarshal: there are bytes left after unmarshal, error found in #10 byte of ...|y\"\n }Fatal error|..., bigger context ...|:\"bad_data\",\n \"error\":\"bogus query\"\n }Fatal error|...",reporter="promql/series",severity="bug"} +pint_problem{filename="rules/1.yml",kind="alerting",name="comparison",owner="",problem="prometheus \"prom1\" at http://127.0.0.1:7057 failed with: bad_data: bogus query",reporter="promql/series",severity="bug"} +pint_problem{filename="rules/1.yml",kind="recording",name="aggregate",owner="",problem="prometheus \"prom1\" at http://127.0.0.1:7057 failed with: bad_data: bogus query",reporter="promql/series",severity="bug"} pint_problem{filename="rules/1.yml",kind="recording",name="broken",owner="",problem="syntax error: no arguments for aggregate expression provided",reporter="promql/syntax",severity="fatal"} # HELP pint_problems Total number of problems reported by pint # TYPE pint_problems gauge @@ -109,7 +109,7 @@ pint_prometheus_queries_total{endpoint="/api/v1/status/flags",name="prom1"} pint_prometheus_queries_total{endpoint="/api/v1/status/flags",name="prom2"} # HELP pint_prometheus_query_errors_total Total number of failed prometheus queries # TYPE pint_prometheus_query_errors_total counter -pint_prometheus_query_errors_total{endpoint="/api/v1/query",name="prom1",reason="api/bad_response"} +pint_prometheus_query_errors_total{endpoint="/api/v1/query",name="prom1",reason="api/bad_data"} pint_prometheus_query_errors_total{endpoint="/api/v1/query",name="prom2",reason="connection/error"} pint_prometheus_query_errors_total{endpoint="/api/v1/status/config",name="prom1",reason="api/server_error"} pint_prometheus_query_errors_total{endpoint="/api/v1/status/config",name="prom2",reason="connection/error"} @@ -123,71 +123,3 @@ pint_rules_parsed_total{kind="recording"} # HELP pint_version Version information # TYPE pint_version gauge pint_version{version="unknown"} --- prometheus.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/api/v1/status/config", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(500) - time.Sleep(time.Millisecond * 100) - io.WriteString(w, "Fatal error") - }) - - http.HandleFunc("/api/v1/query", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(400) - time.Sleep(time.Millisecond * 200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{ - "status":"error", - "errorType":"bad_data", - "error":"bogus query" - }`)) - io.WriteString(w, "Fatal error") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:7057") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:7057", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("prometheus.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- prometheus.sh -- -env GOCACHE=$TMPDIR go run prometheus.go diff --git a/cmd/pint/tests/0068_skip_ci.txt b/cmd/pint/tests/0068_skip_ci.txt index f032a198..e5da7609 100644 --- a/cmd/pint/tests/0068_skip_ci.txt +++ b/cmd/pint/tests/0068_skip_ci.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response bitbucket / 200 OK +http start bitbucket 127.0.0.1:6068 mkdir testrepo cd testrepo @@ -26,7 +26,6 @@ stderr 'level=debug msg="Sending a request to BitBucket" method=PUT' stderr 'level=debug msg="BitBucket request completed" status=200' stderr 'level=debug msg="Sending a request to BitBucket" method=DELETE' stderr 'level=debug msg="BitBucket request completed" status=200' -exec sh -c 'cat ../server.pid | xargs kill' -- src/v1.yml -- - alert: rule1 @@ -55,58 +54,3 @@ repository { repository = "rules" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "OK") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6068") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6068", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0069_bitbucket_skip_unmodified.txt b/cmd/pint/tests/0069_bitbucket_skip_unmodified.txt index f695a8cc..1fa49203 100644 --- a/cmd/pint/tests/0069_bitbucket_skip_unmodified.txt +++ b/cmd/pint/tests/0069_bitbucket_skip_unmodified.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response bitbucket / 200 OK +http start bitbucket 127.0.0.1:6069 mkdir testrepo cd testrepo @@ -23,7 +23,6 @@ pint.ok --no-color ci ! stdout . stderr 'level=info msg="Problems found" Information=1' -exec sh -c 'cat ../server.pid | xargs kill' -- src/v1.yml -- - alert: rule1a expr: sum(foo{job=~"xxx"}) by(job) @@ -54,58 +53,3 @@ repository { repository = "rules" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "OK") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6069") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6069", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0070_bitbucket_string.txt b/cmd/pint/tests/0070_bitbucket_string.txt index 8a20a0b0..d2b34a3a 100644 --- a/cmd/pint/tests/0070_bitbucket_string.txt +++ b/cmd/pint/tests/0070_bitbucket_string.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response bitbucket / 200 OK +http start bitbucket 127.0.0.1:6070 mkdir testrepo cd testrepo @@ -27,7 +27,6 @@ stderr 'level=debug msg="Sending a request to BitBucket" method=PUT' stderr 'level=debug msg="BitBucket request completed" status=200' stderr 'level=debug msg="Sending a request to BitBucket" method=DELETE' stderr 'level=debug msg="BitBucket request completed" status=200' -exec sh -c 'cat ../server.pid | xargs kill' -- src/v1.yml -- - alert: rule1 @@ -56,58 +55,3 @@ repository { repository = "rules" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "OK") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6070") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6070", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0071_ci_owner.txt b/cmd/pint/tests/0071_ci_owner.txt index e61446b1..a828b419 100644 --- a/cmd/pint/tests/0071_ci_owner.txt +++ b/cmd/pint/tests/0071_ci_owner.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response bitbucket / 200 OK +http start bitbucket 127.0.0.1:6071 mkdir testrepo cd testrepo @@ -23,7 +23,6 @@ pint.error -l error --no-color ci --require-owner ! stdout . cd .. cmp stderr stderr.txt -exec sh -c 'cat server.pid | xargs kill' -- stderr.txt -- rules.yml:4-5: rule/owner comments are required in all files, please add a "# pint file/owner $owner" somewhere in this file and/or "# pint rule/owner $owner" on top of each rule (rule/owner) @@ -66,58 +65,3 @@ repository { repository = "rules" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "OK") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6071") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6071", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0072_bitbucket_move_bug_to_modified.txt b/cmd/pint/tests/0072_bitbucket_move_bug_to_modified.txt index 191d8429..e1d3fb68 100644 --- a/cmd/pint/tests/0072_bitbucket_move_bug_to_modified.txt +++ b/cmd/pint/tests/0072_bitbucket_move_bug_to_modified.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response bitbucket / 200 OK +http start bitbucket 127.0.0.1:6072 mkdir testrepo cd testrepo @@ -28,7 +28,7 @@ stderr 'level=debug msg="Sending a request to BitBucket" method=PUT' stderr 'level=debug msg="BitBucket request completed" status=200' stderr 'level=debug msg="Sending a request to BitBucket" method=DELETE' stderr 'level=debug msg="BitBucket request completed" status=200' -exec sh -c 'cat ../server.pid | xargs kill' + -- src/v1.yml -- - alert: rule1 expr: sum(foo) by(job) > 0 @@ -56,58 +56,3 @@ repository { repository = "rules" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "OK") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6072") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6072", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0075_ci_strict.txt b/cmd/pint/tests/0075_ci_strict.txt index 970aa0f3..4a6400e1 100644 --- a/cmd/pint/tests/0075_ci_strict.txt +++ b/cmd/pint/tests/0075_ci_strict.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response bitbucket / 200 OK +http start bitbucket 127.0.0.1:6075 mkdir testrepo cd testrepo @@ -27,7 +27,6 @@ stderr 'level=debug msg="Sending a request to BitBucket" method=DELETE' stderr 'level=debug msg="BitBucket request completed" status=200' stderr 'level=info msg="Problems found" Bug=1 Fatal=1' ! stderr 'parse error: unclosed left parenthesis' -exec sh -c 'cat ../server.pid | xargs kill' -- src/v1.yml -- groups: @@ -53,58 +52,3 @@ repository { repository = "rules" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "OK") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6075") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6075", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0076_ci_group_errors.txt b/cmd/pint/tests/0076_ci_group_errors.txt index cbafce36..0a5163b2 100644 --- a/cmd/pint/tests/0076_ci_group_errors.txt +++ b/cmd/pint/tests/0076_ci_group_errors.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response prometheus / 200 OK +http start prometheus 127.0.0.1:6076 mkdir testrepo cd testrepo @@ -22,7 +22,6 @@ env BITBUCKET_AUTH_TOKEN="12345" pint.error -l warn --no-color ci --require-owner ! stdout . cmp stderr ../stderr.txt -exec sh -c 'cat ../server.pid | xargs kill' -- src/v1.yml -- groups: @@ -136,61 +135,6 @@ rule { } } --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "OK") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6076") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6076", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go - -- stderr.txt -- rules.yml:4-5: link annotation is required (alerts/annotation) 4 | - alert: syntax error diff --git a/cmd/pint/tests/0080_lint_online.txt b/cmd/pint/tests/0080_lint_online.txt index 1a370146..671ca733 100644 --- a/cmd/pint/tests/0080_lint_online.txt +++ b/cmd/pint/tests/0080_lint_online.txt @@ -1,10 +1,13 @@ -exec bash -x ./prometheus.sh & -exec bash -c 'I=0 ; while [ ! -f prometheus.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response prometheus /api/v1/metadata 200 {"status":"success","data":{}} +http response prometheus /api/v1/status/config 200 {"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}} +http response prometheus /api/v1/status/flags 200 {"status":"success","data":{"--storage.tsdb.retention.time": "1d"}} +http response prometheus /api/v1/query_range 200 {"status":"success","data":{"resultType":"matrix","result":[]}} +http response prometheus /api/v1/query 200 {"status":"success","data":{"resultType":"vector","result":[]}} +http start prometheus 127.0.0.1:7080 pint.ok --no-color lint rules ! stdout . cmp stderr stderr.txt -exec bash -c 'cat prometheus.pid | xargs kill' -- stderr.txt -- level=info msg="Loading configuration file" path=.pint.hcl @@ -36,95 +39,3 @@ check "promql/series" { ".+_errors_.+", ] } - --- prometheus.go -- -package main - -import ( - "context" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/api/v1/metadata", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"status":"success","data":{}}`)) - }) - - http.HandleFunc("/api/v1/status/config", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`)) - }) - - http.HandleFunc("/api/v1/status/flags", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"status":"success","data":{"--storage.tsdb.retention.time": "1d"}}`)) - }) - - http.HandleFunc("/api/v1/query_range", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{ - "status":"success", - "data":{ - "resultType":"matrix", - "result":[] - } - }`)) - }) - - http.HandleFunc("/api/v1/query", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{ - "status":"success", - "data":{ - "resultType":"vector", - "result":[] - } - }`)) - }) - - listener, err := net.Listen("tcp", "127.0.0.1:7080") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:7080", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("prometheus.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute * 2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- prometheus.sh -- -env GOCACHE=$TMPDIR go run prometheus.go diff --git a/cmd/pint/tests/0083_github_action.txt b/cmd/pint/tests/0083_github_action.txt index 676feef7..78dc570d 100644 --- a/cmd/pint/tests/0083_github_action.txt +++ b/cmd/pint/tests/0083_github_action.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response github / 200 {} +http start github 127.0.0.1:6083 mkdir testrepo cd testrepo @@ -34,8 +34,6 @@ stderr 'level=info msg="Setting repository name from GITHUB_REPOSITORY env varia stderr 'level=info msg="Setting repository base URI from GITHUB_API_URL env variable" baseuri=http://127.0.0.1:6083' stderr 'level=info msg="Setting repository upload URI from GITHUB_API_URL env variable" uploaduri=http://127.0.0.1:6083' -exec sh -c 'cat ../server.pid | xargs kill' - -- src/v1.yml -- groups: - name: foo @@ -59,58 +57,3 @@ groups: -- src/.pint.hcl -- repository {} - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "{}") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6083") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6083", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0084_github_action_override.txt b/cmd/pint/tests/0084_github_action_override.txt index 6aa66cfd..8d2e3706 100644 --- a/cmd/pint/tests/0084_github_action_override.txt +++ b/cmd/pint/tests/0084_github_action_override.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response github / 200 {} +http start github 127.0.0.1:6084 mkdir testrepo cd testrepo @@ -31,8 +31,6 @@ stderr 'level=info msg="Report submitted" status="200 OK"' stderr 'level=info msg="Setting GITHUB_PULL_REQUEST_NUMBER from GITHUB_REF env variable" pr=123' stderr 'level=info msg="Setting repository base URI from GITHUB_API_URL env variable" baseuri=http://127.0.0.1:6084' -exec sh -c 'cat ../server.pid | xargs kill' - -- src/v1.yml -- groups: - name: foo @@ -62,58 +60,3 @@ repository { repo = "pint" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "{}") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6084") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6084", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0085_github_no_envs.txt b/cmd/pint/tests/0085_github_no_envs.txt index 2177b736..d83b24f9 100644 --- a/cmd/pint/tests/0085_github_no_envs.txt +++ b/cmd/pint/tests/0085_github_no_envs.txt @@ -1,5 +1,5 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f server.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response github / 200 {} +http start github 127.0.0.1:6085 mkdir testrepo cd testrepo @@ -25,8 +25,6 @@ pint.ok -l debug --offline --no-color ci ! stdout . stderr 'level=info msg="Report submitted" status="200 OK"' -exec sh -c 'cat ../server.pid | xargs kill' - -- src/v1.yml -- groups: - name: foo @@ -57,58 +55,3 @@ repository { repo = "pint" } } - --- webserver.go -- -package main - -import ( - "context" - "io" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "{}") - }) - - listener, err := net.Listen("tcp", "127.0.0.1:6085") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:6085", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("server.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute*2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go diff --git a/cmd/pint/tests/0088_rule_link.txt b/cmd/pint/tests/0088_rule_link.txt index 7a3c606e..cd7e2436 100644 --- a/cmd/pint/tests/0088_rule_link.txt +++ b/cmd/pint/tests/0088_rule_link.txt @@ -1,10 +1,13 @@ -exec bash -x ./webserver.sh & -exec bash -c 'I=0 ; while [ ! -f webserver.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done' +http response mock /200 200 OK +http response mock /404 404 Not found +http response mock /500 500 Error +http redirect mock /redirect/200 /200 +http redirect mock /redirect/404 /404 +http start mock 127.0.0.1:7088 pint.ok --no-color lint --min-severity=info rules ! stdout . cmp stderr stderr.txt -exec bash -c 'cat webserver.pid | xargs kill' -- stderr.txt -- level=info msg="Loading configuration file" path=.pint.hcl @@ -44,79 +47,3 @@ rule { timeout = "10s" } } - --- webserver.go -- -package main - -import ( - "context" - "log" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" - "time" -) - -func main() { - http.HandleFunc("/200", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) - _, _ = w.Write([]byte(`Ok`)) - }) - - http.HandleFunc("/404", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(404) - _, _ = w.Write([]byte(`Ok`)) - }) - - http.HandleFunc("/500", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(500) - _, _ = w.Write([]byte(`Error`)) - }) - - http.HandleFunc("/redirect/200", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Location", "/200") - w.WriteHeader(302) - }) - - http.HandleFunc("/redirect/404", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Location", "/404") - w.WriteHeader(302) - }) - - listener, err := net.Listen("tcp", "127.0.0.1:7088") - if err != nil { - log.Fatal(err) - } - - server := &http.Server{ - Addr: "127.0.0.1:7088", - } - - go func() { - _ = server.Serve(listener) - }() - - pid := os.Getpid() - err = os.WriteFile("webserver.pid", []byte(strconv.Itoa(pid)), 0644) - if err != nil { - log.Fatal(err) - } - - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - go func() { - time.Sleep(time.Minute * 2) - stop <- syscall.SIGTERM - }() - <-stop - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - server.Shutdown(ctx) -} - --- webserver.sh -- -env GOCACHE=$TMPDIR go run webserver.go -