mirror of
https://github.com/kopia/kopia.git
synced 2026-05-11 00:04:46 -04:00
feat(server): emit notifications as JSON to stderr when running under KopiaUI (#4322)
* feat(server): emit notifications as JSON to stderr when running under KopiaUI * added tests
This commit is contained in:
@@ -2,15 +2,19 @@
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/kopia/kopia/internal/testutil"
|
||||
"github.com/kopia/kopia/notification/sender"
|
||||
"github.com/kopia/kopia/tests/testenv"
|
||||
)
|
||||
|
||||
@@ -48,21 +52,32 @@ func TestServerNotifications(t *testing.T) {
|
||||
|
||||
var sp testutil.ServerParameters
|
||||
|
||||
env.SetLogOutput(true, "server")
|
||||
jsonNotificationsReceived := make(chan string, 100)
|
||||
|
||||
wait, kill := env.RunAndProcessStderr(t, sp.ProcessOutput,
|
||||
"server", "start",
|
||||
wait, kill := env.RunAndProcessStderrAsync(t, sp.ProcessOutput, func(line string) {
|
||||
const prefix = "NOTIFICATION: "
|
||||
|
||||
if strings.HasPrefix(line, prefix) {
|
||||
t.Logf("JSON notification received: %v", line)
|
||||
|
||||
jsonNotificationsReceived <- line[len(prefix):]
|
||||
}
|
||||
}, "server", "start",
|
||||
"--address=localhost:0",
|
||||
"--insecure",
|
||||
"--random-server-control-password",
|
||||
"--kopiaui-notifications",
|
||||
"--shutdown-grace-period", "100ms",
|
||||
)
|
||||
|
||||
defer func() {
|
||||
kill()
|
||||
wait()
|
||||
}()
|
||||
|
||||
// trigger server snapshot
|
||||
env.RunAndExpectSuccess(t, "server", "snapshot", "--address", sp.BaseURL, "--server-control-password", sp.ServerControlPassword, dir1)
|
||||
|
||||
t.Logf("triggered")
|
||||
|
||||
select {
|
||||
case not := <-notificationsReceived:
|
||||
t.Logf("notification received: %v", not)
|
||||
@@ -71,6 +86,18 @@ func TestServerNotifications(t *testing.T) {
|
||||
t.Error("notification not received in time")
|
||||
}
|
||||
|
||||
kill()
|
||||
wait()
|
||||
select {
|
||||
case not := <-jsonNotificationsReceived:
|
||||
// make sure we received a valid sender.Message JSON
|
||||
dec := json.NewDecoder(strings.NewReader(not))
|
||||
dec.DisallowUnknownFields()
|
||||
|
||||
var msg sender.Message
|
||||
|
||||
require.NoError(t, dec.Decode(&msg))
|
||||
require.Contains(t, msg.Subject, "Kopia success")
|
||||
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Error("notification not received in time")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user