|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "net" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "gjallar/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +// fakeRedis answers PING/AUTH like a real server; password "" = no auth needed. |
| 16 | +func fakeRedis(t *testing.T, password string) (host string, port int) { |
| 17 | + t.Helper() |
| 18 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 19 | + if err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + t.Cleanup(func() { ln.Close() }) |
| 23 | + go func() { |
| 24 | + for { |
| 25 | + conn, err := ln.Accept() |
| 26 | + if err != nil { |
| 27 | + return |
| 28 | + } |
| 29 | + go func(c net.Conn) { |
| 30 | + defer c.Close() |
| 31 | + r := bufio.NewReader(c) |
| 32 | + authed := password == "" |
| 33 | + for { |
| 34 | + line, err := r.ReadString('\n') |
| 35 | + if err != nil { |
| 36 | + return |
| 37 | + } |
| 38 | + switch cmd := strings.TrimSpace(line); { |
| 39 | + case strings.HasPrefix(cmd, "AUTH "): |
| 40 | + if strings.TrimPrefix(cmd, "AUTH ") == password { |
| 41 | + authed = true |
| 42 | + c.Write([]byte("+OK\r\n")) |
| 43 | + } else { |
| 44 | + c.Write([]byte("-ERR invalid password\r\n")) |
| 45 | + } |
| 46 | + case cmd == "PING" && authed: |
| 47 | + c.Write([]byte("+PONG\r\n")) |
| 48 | + default: |
| 49 | + c.Write([]byte("-NOAUTH Authentication required.\r\n")) |
| 50 | + } |
| 51 | + } |
| 52 | + }(conn) |
| 53 | + } |
| 54 | + }() |
| 55 | + addr := ln.Addr().(*net.TCPAddr) |
| 56 | + return addr.IP.String(), addr.Port |
| 57 | +} |
| 58 | + |
| 59 | +func redisMonitor(host string, port int, password string) config.Monitor { |
| 60 | + return config.Monitor{ |
| 61 | + Name: "t", Type: "redis", Host: host, Port: port, Password: password, |
| 62 | + Timeout: config.Duration(3 * time.Second), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestRedisCheck(t *testing.T) { |
| 67 | + host, port := fakeRedis(t, "") |
| 68 | + c, _ := newRedisCheck(redisMonitor(host, port, "")) |
| 69 | + if ok, msg := c.Check(context.Background()); !ok { |
| 70 | + t.Errorf("expected ok, got %q", msg) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestRedisCheckAuth(t *testing.T) { |
| 75 | + host, port := fakeRedis(t, "s3cret") |
| 76 | + c, _ := newRedisCheck(redisMonitor(host, port, "s3cret")) |
| 77 | + if ok, msg := c.Check(context.Background()); !ok { |
| 78 | + t.Errorf("expected ok, got %q", msg) |
| 79 | + } |
| 80 | + |
| 81 | + bad, _ := newRedisCheck(redisMonitor(host, port, "wrong")) |
| 82 | + if ok, msg := bad.Check(context.Background()); ok || !strings.Contains(msg, "auth") { |
| 83 | + t.Errorf("got ok=%v msg=%q", ok, msg) |
| 84 | + } |
| 85 | + |
| 86 | + noauth, _ := newRedisCheck(redisMonitor(host, port, "")) |
| 87 | + if ok, _ := noauth.Check(context.Background()); ok { |
| 88 | + t.Error("expected NOAUTH failure") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestRedisCheckDown(t *testing.T) { |
| 93 | + c, _ := newRedisCheck(redisMonitor("127.0.0.1", 1, "")) |
| 94 | + if ok, _ := c.Check(context.Background()); ok { |
| 95 | + t.Error("expected connection failure") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestRedisDefaultPort(t *testing.T) { |
| 100 | + c, _ := newRedisCheck(config.Monitor{Host: "h"}) |
| 101 | + if c.addr != "h:"+strconv.Itoa(6379) { |
| 102 | + t.Errorf("addr = %q", c.addr) |
| 103 | + } |
| 104 | +} |
0 commit comments