48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package sdnotify
|
|
|
|
import (
|
|
"net"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDisabledNotifierIsNoOp(t *testing.T) {
|
|
if err := New("").Notify("READY=1"); err != nil {
|
|
t.Fatalf("disabled notifier: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNotifierSendsDatagram(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "notify.sock")
|
|
listener, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: path, Net: "unixgram"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
if err := New(path).Notify("READY=1\nSTATUS=initialized"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := listener.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
buffer := make([]byte, 128)
|
|
read, _, err := listener.ReadFromUnix(buffer)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := string(buffer[:read]); got != "READY=1\nSTATUS=initialized" {
|
|
t.Fatalf("notification=%q", got)
|
|
}
|
|
}
|
|
|
|
func TestNotifierRejectsInvalidState(t *testing.T) {
|
|
if err := New("unused").Notify(""); err == nil {
|
|
t.Fatal("empty state accepted")
|
|
}
|
|
if err := New("unused").Notify("READY=1\x00STATUS=bad"); err == nil {
|
|
t.Fatal("NUL state accepted")
|
|
}
|
|
}
|