MCPcopy Index your code
hub / github.com/kopia/kopia / TestServerNotifications

Function TestServerNotifications

cli/command_server_notifications_test.go:21–104  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestServerNotifications(t *testing.T) {
22 mux := http.NewServeMux()
23
24 notificationsReceived := make(chan string, 100)
25
26 mux.HandleFunc("/notification-webhook", func(w http.ResponseWriter, r *http.Request) {
27 var b bytes.Buffer
28
29 io.Copy(&b, r.Body)
30
31 notificationsReceived <- b.String()
32 })
33
34 server := httptest.NewServer(mux)
35 defer server.Close()
36
37 env := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, testenv.NewInProcRunner(t))
38
39 dir0 := testutil.TempDirectory(t)
40 dir1 := testutil.TempDirectory(t)
41 dir2 := testutil.TempDirectory(t)
42
43 env.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", env.RepoDir, "--override-username=another-user", "--override-hostname=another-host")
44
45 env.RunAndExpectSuccess(t, "snap", "create", dir0)
46
47 env.RunAndExpectSuccess(t, "repo", "connect", "filesystem", "--path", env.RepoDir, "--override-username=test-user", "--override-hostname=test-host")
48 env.RunAndExpectSuccess(t, "snap", "create", dir1)
49 env.RunAndExpectSuccess(t, "snap", "create", dir2)
50
51 // setup webhook notification
52 env.RunAndExpectSuccess(t, "notification", "profile", "configure", "webhook", "--profile-name=mywebhook", "--endpoint="+server.URL+"/notification-webhook", "--method=POST", "--format=html")
53
54 var sp testutil.ServerParameters
55
56 jsonNotificationsReceived := make(chan string, 100)
57
58 wait, kill := env.RunAndProcessStderrAsync(t, sp.ProcessOutput, func(line string) {
59 const prefix = "NOTIFICATION: "
60
61 if strings.HasPrefix(line, prefix) {
62 t.Logf("JSON notification received: %v", line)
63
64 jsonNotificationsReceived <- line[len(prefix):]
65 }
66 }, "server", "start",
67 "--address=localhost:0",
68 "--insecure",
69 "--random-server-control-password",
70 "--kopiaui-notifications",
71 "--shutdown-grace-period", "100ms",
72 )
73
74 defer func() {
75 kill()
76 wait()
77 }()
78

Callers

nothing calls this directly

Calls 12

RunAndExpectSuccessMethod · 0.95
NewCLITestFunction · 0.92
NewInProcRunnerFunction · 0.92
TempDirectoryFunction · 0.92
HasPrefixMethod · 0.80
LogfMethod · 0.80
AfterMethod · 0.80
CloseMethod · 0.65
ErrorMethod · 0.65
StringMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected