aboutsummaryrefslogtreecommitdiff
path: root/pkg/systemd/notifyproxy/notifyproxy_test.go
diff options
context:
space:
mode:
authorValentin Rothberg <vrothberg@redhat.com>2022-08-05 13:18:57 +0200
committerValentin Rothberg <vrothberg@redhat.com>2022-08-10 21:10:17 +0200
commit67a2e7351b0cea00ba455c0c4e52e13898fec203 (patch)
treeac7143e5ac8b71d75efbcc811ae07a9153fd88b7 /pkg/systemd/notifyproxy/notifyproxy_test.go
parent59ab5cce7d20be703f5528a12a971b2ef1e2a784 (diff)
downloadpodman-67a2e7351b0cea00ba455c0c4e52e13898fec203.tar.gz
podman-67a2e7351b0cea00ba455c0c4e52e13898fec203.tar.bz2
podman-67a2e7351b0cea00ba455c0c4e52e13898fec203.zip
add `pkg/systemd/notifyproxy`
Add a new package for proxying notify sockets and waiting for the READY=1 message to appear. May subject to further changes in future commits. Tests make sure that it behaves properly. Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
Diffstat (limited to 'pkg/systemd/notifyproxy/notifyproxy_test.go')
-rw-r--r--pkg/systemd/notifyproxy/notifyproxy_test.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/pkg/systemd/notifyproxy/notifyproxy_test.go b/pkg/systemd/notifyproxy/notifyproxy_test.go
new file mode 100644
index 000000000..edad95659
--- /dev/null
+++ b/pkg/systemd/notifyproxy/notifyproxy_test.go
@@ -0,0 +1,58 @@
+package notifyproxy
+
+import (
+ "testing"
+ "time"
+
+ "github.com/coreos/go-systemd/v22/daemon"
+ "github.com/stretchr/testify/require"
+)
+
+// Helper function to send the specified message over the socket of the proxy.
+func sendMessage(t *testing.T, proxy *NotifyProxy, message string) {
+ err := SendMessage(proxy.SocketPath(), message)
+ require.NoError(t, err)
+}
+
+func TestNotifyProxy(t *testing.T) {
+ proxy, err := New("")
+ require.NoError(t, err)
+ require.FileExists(t, proxy.SocketPath())
+ require.NoError(t, proxy.close())
+ require.NoFileExists(t, proxy.SocketPath())
+}
+
+func TestWaitAndClose(t *testing.T) {
+ proxy, err := New("")
+ require.NoError(t, err)
+ require.FileExists(t, proxy.SocketPath())
+
+ ch := make(chan error)
+
+ go func() {
+ ch <- proxy.WaitAndClose()
+ }()
+
+ sendMessage(t, proxy, "foo\n")
+ time.Sleep(250 * time.Millisecond)
+ select {
+ case err := <-ch:
+ t.Fatalf("Should stil be waiting but received %v", err)
+ default:
+ }
+
+ sendMessage(t, proxy, daemon.SdNotifyReady+"\nsomething else")
+ done := func() bool {
+ for i := 0; i < 10; i++ {
+ select {
+ case err := <-ch:
+ require.NoError(t, err, "Waiting should succeed")
+ return true
+ default:
+ time.Sleep(time.Duration(i*250) * time.Millisecond)
+ }
+ }
+ return false
+ }()
+ require.True(t, done, "READY MESSAGE SHOULD HAVE ARRIVED")
+}