-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzz_plugins_test.go
More file actions
111 lines (95 loc) · 3.28 KB
/
Copy pathzz_plugins_test.go
File metadata and controls
111 lines (95 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: AGPL-3.0-or-later
package main
// Composition tests for the embedded plugin set. Like zz_internal_test.go
// this file avoids `import "C"`, so it exercises registerEmbeddedPlugins
// directly rather than the cgo entry point that calls it.
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/pilot-protocol/pilotprotocol/pkg/daemon"
"github.com/pilot-protocol/runtime"
)
// newTestDaemon builds a daemon that is never started — New() only
// allocates in-memory state, so nothing binds a socket or touches the
// network here.
func newTestDaemon(t *testing.T) *daemon.Daemon {
t.Helper()
dir := t.TempDir()
return daemon.New(daemon.Config{
SocketPath: filepath.Join(dir, "pilot.sock"),
IdentityPath: filepath.Join(dir, "identity.json"),
})
}
func TestRegisterEmbeddedPluginsRegistersWebhook(t *testing.T) {
d := newTestDaemon(t)
rt := runtime.New(d.DaemonAPI())
p, err := registerEmbeddedPlugins(d, rt)
if err != nil {
t.Fatalf("registerEmbeddedPlugins: %v", err)
}
if p.webhook == nil {
t.Fatal("webhook service was not constructed")
}
want := []string{"trustedagents", "handshake", "policy", "webhook"}
got := p.names()
if len(got) != len(want) {
t.Fatalf("plugin names = %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("plugin names = %v, want %v", got, want)
}
}
}
// SetWebhookURL on the daemon only does anything once a WebhookManager is
// registered; unregistered it returns without touching the plugin. The
// plugin persists every URL it is handed, so the persisted file is the
// observable proof that the call reached it.
func TestEmbeddedSetWebhookURLReachesThePlugin(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
d := newTestDaemon(t)
rt := runtime.New(d.DaemonAPI())
if _, err := registerEmbeddedPlugins(d, rt); err != nil {
t.Fatalf("registerEmbeddedPlugins: %v", err)
}
const url = "https://example.com/pilot-hook"
d.SetWebhookURL(url)
data, err := os.ReadFile(filepath.Join(home, ".pilot", "webhook_url"))
if err != nil {
t.Fatalf("SetWebhookURL did not reach the webhook plugin: %v", err)
}
if got := strings.TrimSpace(string(data)); got != url {
t.Fatalf("persisted URL = %q, want %q", got, url)
}
// Clearing must reach it too.
d.SetWebhookURL("")
if _, err := os.Stat(filepath.Join(home, ".pilot", "webhook_url")); !os.IsNotExist(err) {
t.Fatalf("clearing the webhook left the persisted URL in place (err = %v)", err)
}
}
// Registering twice on the same runtime must surface the failure rather
// than leaving a half-composed daemon behind.
func TestRegisterEmbeddedPluginsReportsRegistryErrors(t *testing.T) {
d := newTestDaemon(t)
rt := runtime.New(d.DaemonAPI())
if _, err := registerEmbeddedPlugins(d, rt); err != nil {
t.Fatalf("first registration: %v", err)
}
if err := rt.StartPlugins(t.Context()); err != nil {
t.Fatalf("StartPlugins: %v", err)
}
t.Cleanup(func() { _ = rt.StopPlugins(t.Context()) })
// The registry refuses registration once it has started.
if _, err := registerEmbeddedPlugins(d, rt); err == nil {
t.Fatal("expected an error registering into a started runtime")
}
}
func TestNilEmbeddedPluginsHasNoNames(t *testing.T) {
var p *embeddedPlugins
if got := p.names(); got != nil {
t.Fatalf("names() = %v, want nil", got)
}
}