MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/cloud-sql-proxy / TestSdNotifyOnLinux

Function TestSdNotifyOnLinux

cmd/root_linux_test.go:79–146  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

77}
78
79func TestSdNotifyOnLinux(t *testing.T) {
80 tcs := []struct {
81 desc string
82 proxyMustFail bool
83 notifyState string
84 }{
85 {
86 desc: "System with systemd Type=notify and proxy started successfully",
87 proxyMustFail: false,
88 notifyState: daemon.SdNotifyReady,
89 },
90 {
91 desc: "System with systemd Type=notify and proxy failed to start",
92 proxyMustFail: true,
93 notifyState: daemon.SdNotifyStopping,
94 },
95 }
96
97 // Create a temp dir for the socket file.
98 testDir, err := os.MkdirTemp("/tmp/", "test-")
99 if err != nil {
100 t.Fatalf("Fail to create the temp dir: %v", err)
101 }
102 defer os.RemoveAll(testDir)
103
104 //Set up the socket stream to listen for notifications.
105 socketAddr := filepath.Join(testDir, "notify-socket.sock")
106 conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: socketAddr, Net: "unixgram"})
107 if err != nil {
108 t.Fatalf("net.ListenUnixgram error: %v", err)
109 }
110
111 // To simulate systemd behavior with Type=notify, set NOTIFY_SOCKET
112 // to the name of the socket that listens for notifications.
113 os.Setenv("NOTIFY_SOCKET", socketAddr)
114 defer os.Unsetenv("NOTIFY_SOCKET")
115
116 s := &spyDialer{}
117 c := NewCommand(WithDialer(s))
118 // Keep the test output quiet
119 c.SilenceUsage = false
120 c.SilenceErrors = false
121 c.SetArgs([]string{"my-project:my-region:my-instance"})
122
123 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
124 defer cancel()
125
126 for _, tc := range tcs {
127 t.Run(tc.desc, func(t *testing.T) {
128
129 if tc.proxyMustFail {
130 c.conf.FUSEDir = "invalid"
131 }
132
133 go c.ExecuteContext(ctx)
134
135 stateReceived := make([]byte, 4096)
136 length, _, err := conn.ReadFromUnix(stateReceived)

Callers

nothing calls this directly

Calls 2

NewCommandFunction · 0.85
WithDialerFunction · 0.85

Tested by

no test coverage detected