aboutsummaryrefslogtreecommitdiff
path: root/lilin_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-08-29 01:37:28 +0700
committerShulhan <ms@kilabit.info>2025-09-26 13:59:58 +0700
commit62161730b5802c4206f0cc169a744b364576bb74 (patch)
tree914389da9748b3b7cff731357deff7026273d22f /lilin_test.go
parentb6530117f0dcc554114bfac8b86d9fb1fde605c4 (diff)
downloadlilin-62161730b5802c4206f0cc169a744b364576bb74.tar.xz
all: implement notification using Mattermost incoming webhook
In the server configuration, one can defined the following section to send the notification using Mattermost, [notif] kind = mattermost webhook_url = # The incoming webhook URL. channel = # The channel where the notification will be placed. down_template = # Message template when service is down. up_template = # Message template when service is up.
Diffstat (limited to 'lilin_test.go')
-rw-r--r--lilin_test.go100
1 files changed, 67 insertions, 33 deletions
diff --git a/lilin_test.go b/lilin_test.go
index e4db531..091f7c7 100644
--- a/lilin_test.go
+++ b/lilin_test.go
@@ -1,37 +1,91 @@
// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info>
// SPDX-License-Identifier: GPL-3.0-only
-package lilin_test
+package lilin
import (
"context"
"errors"
+ "io"
"log"
"net/http"
"testing"
"time"
- "git.sr.ht/~shulhan/lilin"
"git.sr.ht/~shulhan/lilin/internal"
"git.sr.ht/~shulhan/pakakeh.go/lib/net"
)
-var client *lilin.Client
-
const (
dummyHTTPAddress = `127.0.0.1:6330`
)
+type dummyHTTPService struct {
+ mux *http.ServeMux
+ httpd *http.Server
+ reqbodyq chan []byte
+}
+
+var dhs *dummyHTTPService
+var client *Client
+
+func newDummyHTTPService() (dhs *dummyHTTPService) {
+ dhs = &dummyHTTPService{
+ mux: http.NewServeMux(),
+ reqbodyq: make(chan []byte),
+ }
+
+ dhs.mux.HandleFunc(`GET /health`, func(w http.ResponseWriter, _ *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ })
+
+ dhs.mux.HandleFunc(`POST /mattermost`, func(w http.ResponseWriter, req *http.Request) {
+ var logp = `POST /mattermost`
+ var reqbody []byte
+ var err error
+ reqbody, err = io.ReadAll(req.Body)
+ if err != nil {
+ log.Fatalf(`%s: %s`, logp, err)
+ }
+ w.WriteHeader(http.StatusOK)
+ go func() {
+ dhs.reqbodyq <- reqbody
+ }()
+ })
+
+ dhs.httpd = &http.Server{
+ Addr: dummyHTTPAddress,
+ Handler: dhs.mux,
+ ReadTimeout: 5 * time.Second,
+ WriteTimeout: 5 * time.Second,
+ }
+ return dhs
+}
+
+func (dhs *dummyHTTPService) start() {
+ var err = dhs.httpd.ListenAndServe()
+ if err != nil {
+ log.Fatal(err)
+ }
+}
+
func TestMain(m *testing.M) {
internal.Now = func() time.Time {
return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
}
- var server *lilin.Server
+ dhs = newDummyHTTPService()
+ go dhs.start()
+
+ var err = net.WaitAlive(`tcp`, dummyHTTPAddress, 5*time.Second)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var server *Server
server = startServer()
client = createClient(server)
- go dummyHTTPService()
m.Run()
@@ -40,19 +94,19 @@ func TestMain(m *testing.M) {
ctx, cancelfn = context.WithTimeout(context.Background(), 5*time.Second)
defer cancelfn()
- var err = server.Shutdown(ctx)
+ err = server.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
}
-func startServer() (server *lilin.Server) {
- var serverOpts = lilin.ServerConfig{
+func startServer() (server *Server) {
+ var serverOpts = ServerConfig{
BaseDir: `testdata`,
}
var err error
- server, err = lilin.NewServer(serverOpts)
+ server, err = NewServer(serverOpts)
if err != nil {
log.Fatal(err)
}
@@ -71,35 +125,15 @@ func startServer() (server *lilin.Server) {
return server
}
-func createClient(server *lilin.Server) (client *lilin.Client) {
- var clientOpts = lilin.ClientConfig{
+func createClient(server *Server) (client *Client) {
+ var clientOpts = ClientConfig{
ServerURL: `http://` + server.Config.Address,
}
var err error
- client, err = lilin.NewClient(clientOpts)
+ client, err = NewClient(clientOpts)
if err != nil {
log.Fatal(err)
}
return client
}
-
-func dummyHTTPService() {
- var mux = http.NewServeMux()
-
- mux.HandleFunc(`GET /health`, func(w http.ResponseWriter, _ *http.Request) {
- w.WriteHeader(http.StatusOK)
- })
-
- var httpd = http.Server{
- Addr: dummyHTTPAddress,
- Handler: mux,
- ReadTimeout: 5 * time.Second,
- WriteTimeout: 5 * time.Second,
- }
-
- var err = httpd.ListenAndServe()
- if err != nil {
- log.Fatal(err)
- }
-}