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:
Jarek Kowalski
2024-12-30 15:06:11 -08:00
committed by GitHub
parent d6b9254a4c
commit ef01650665
7 changed files with 182 additions and 11 deletions

View File

@@ -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")
}
}

View File

@@ -21,6 +21,8 @@
"github.com/kopia/kopia/internal/auth"
"github.com/kopia/kopia/internal/server"
"github.com/kopia/kopia/notification"
"github.com/kopia/kopia/notification/sender/jsonsender"
"github.com/kopia/kopia/repo"
)
@@ -68,7 +70,8 @@ type commandServerStart struct {
debugScheduler bool
minMaintenanceInterval time.Duration
shutdownGracePeriod time.Duration
shutdownGracePeriod time.Duration
kopiauiNotifications bool
logServerRequests bool
@@ -123,6 +126,8 @@ func (c *commandServerStart) setup(svc advancedAppServices, parent commandParent
cmd.Flag("shutdown-grace-period", "Grace period for shutting down the server").Default("5s").DurationVar(&c.shutdownGracePeriod)
cmd.Flag("kopiaui-notifications", "Enable notifications to be printed to stdout for KopiaUI").BoolVar(&c.kopiauiNotifications)
c.sf.setup(svc, cmd)
c.co.setup(svc, cmd)
c.svc = svc
@@ -271,6 +276,15 @@ func (c *commandServerStart) run(ctx context.Context) (reterr error) {
onExternalConfigReloadRequest(srv.Refresh)
// enable notification to be printed to stderr where KopiaUI will pick it up
if c.kopiauiNotifications {
notification.AdditionalSenders = append(notification.AdditionalSenders,
jsonsender.NewJSONSender(
"NOTIFICATION: ",
c.out.stderr(),
notification.SeverityVerbose))
}
return c.startServerWithOptionalTLS(ctx, httpServer)
}

View File

@@ -17,7 +17,7 @@ func TestTerminate(t *testing.T) {
var sp testutil.ServerParameters
wait, interrupt := env.RunAndProcessStderrInt(t, sp.ProcessOutput, "server", "start",
wait, interrupt := env.RunAndProcessStderrInt(t, sp.ProcessOutput, nil, "server", "start",
"--address=localhost:0",
"--insecure")