# Uses proxy_url to connect to upstream

exec bash -x ./test.sh &
exec bash -c 'I=0 ; while [ ! -f alertmanager.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done'
exec karma --pid-file=karma.pid
! stdout .
stderr 'level=INFO msg="Configured Alertmanager source" name=proxy-url cluster=proxy-url uri=http://am.example.com proxy=false readonly=false'
stderr 'level=INFO msg="GET request" uri=http://am.example.com/metrics timeout=10s'
stderr 'level=INFO msg="Upstream version" version=0.24.0 alertmanager=proxy-url'
stderr 'level=INFO msg="Got silences" alertmanager=proxy-url silences=0 duration=.+'
stderr 'level=INFO msg="Collected alert groups" alertmanager=proxy-url groups=0 duration=.+'
stderr 'level=INFO msg="Deduplicating alert groups" alertmanager=proxy-url groups=0'
stderr 'level=INFO msg="Processing deduplicated alert groups" alertmanager=proxy-url groups=0'
stderr 'level=INFO msg="Merging autocomplete hints" alertmanager=proxy-url hints=0'
stderr 'level=INFO msg="Collection completed"'
wait

-- test.sh --
env GOCACHE=$TMPDIR go run alertmanager.go &

I=0
while [ ! -f karma.pid ] && [ $I -lt 30 ]; do sleep 1; I=$((I+1)); done

sleep 5
cat karma.pid | xargs kill
cat alertmanager.pid | xargs kill

-- karma.yaml --
alertmanager:
  interval: 1h
  servers:
    - name: proxy-url
      uri: http://am.example.com
      timeout: 10s
      proxy_url: http://127.0.0.1:9098
listen:
  address: 127.0.0.1
  port: 8098

-- alertmanager.go --
package main

import (
	"context"
	"io"
	"log"
	"net"
	"net/http"
	"os"
	"os/signal"
	"strconv"
	"syscall"
	"time"
)

func metrics(w http.ResponseWriter, r *http.Request) {
	if r.Host != "am.example.com" {
		w.WriteHeader(400)
		return
	}

    w.WriteHeader(200)
	w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
	io.WriteString(w, `alertmanager_build_info{version="0.24.0"} 1
`)
}

func empty(w http.ResponseWriter, r *http.Request) {
	if r.Host != "am.example.com" {
		w.WriteHeader(400)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	io.WriteString(w, "[]")
}

func main() {
	pid := os.Getpid()
	err := os.WriteFile("alertmanager.pid", []byte(strconv.Itoa(pid)), 0644)
	if err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/metrics", metrics)
	http.HandleFunc("/api/v2/silences", empty)
	http.HandleFunc("/api/v2/alerts/groups", empty)

	listener, err := net.Listen("tcp", "127.0.0.1:9098")
	if err != nil {
		log.Fatal(err)
	}

	server := &http.Server{
		Addr: "127.0.0.1:9098",
	}

	go func() {
		err := server.Serve(listener)
		if err != nil && err != http.ErrServerClosed {
			log.Printf("Serve returned error: %v", err)
		}
	}()

	stop := make(chan os.Signal, 1)
	signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
	<-stop
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	server.Shutdown(ctx)
}
