Skip to content

Commit 4e5d1bf

Browse files
committed
ovsdb: implement Client echo RPC
1 parent 28aa200 commit 4e5d1bf

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

ovsdb/client_integration_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ func TestClientIntegration(t *testing.T) {
2828
c := dialOVSDB(t)
2929
defer c.Close()
3030

31+
t.Run("echo", func(t *testing.T) {
32+
testClientEcho(t, c)
33+
})
3134
t.Run("databases", func(t *testing.T) {
3235
testClientDatabases(t, c)
3336
})
@@ -85,6 +88,12 @@ func testClientDatabases(t *testing.T, c *ovsdb.Client) {
8588
}
8689
}
8790

91+
func testClientEcho(t *testing.T, c *ovsdb.Client) {
92+
if err := c.Echo(context.Background()); err != nil {
93+
t.Fatalf("failed to echo: %v", err)
94+
}
95+
}
96+
8897
func dialOVSDB(t *testing.T) *ovsdb.Client {
8998
t.Helper()
9099

ovsdb/rpc.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
package ovsdb
1616

17-
import "context"
17+
import (
18+
"context"
19+
"fmt"
20+
)
1821

1922
// ListDatabases returns the name of all databases known to the OVSDB server.
2023
func (c *Client) ListDatabases(ctx context.Context) ([]string, error) {
@@ -25,3 +28,20 @@ func (c *Client) ListDatabases(ctx context.Context) ([]string, error) {
2528

2629
return dbs, nil
2730
}
31+
32+
// Echo verifies that the OVSDB connection is alive, and can be used to keep
33+
// the connection alive.
34+
func (c *Client) Echo(ctx context.Context) error {
35+
const req = "github.com/digitalocean/go-openvswitch/ovsdb"
36+
37+
var res [1]string
38+
if err := c.rpc(ctx, "echo", &res, req); err != nil {
39+
return err
40+
}
41+
42+
if res[0] != req {
43+
return fmt.Errorf("invalid echo response: %q", res[0])
44+
}
45+
46+
return nil
47+
}

ovsdb/rpc_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,41 @@ func TestClientListDatabases(t *testing.T) {
5050
t.Fatalf("unexpected databases (-want +got):\n%s", diff)
5151
}
5252
}
53+
54+
func TestClientEchoError(t *testing.T) {
55+
c, _, done := testClient(t, func(req jsonrpc.Request) jsonrpc.Response {
56+
return jsonrpc.Response{
57+
ID: intPtr(1),
58+
Result: mustMarshalJSON(t, []string{"foo"}),
59+
}
60+
})
61+
defer done()
62+
63+
if err := c.Echo(context.Background()); err == nil {
64+
t.Fatal("expected an error, but none occurred")
65+
}
66+
}
67+
68+
func TestClientEchoOK(t *testing.T) {
69+
const echo = "github.com/digitalocean/go-openvswitch/ovsdb"
70+
71+
c, _, done := testClient(t, func(req jsonrpc.Request) jsonrpc.Response {
72+
if diff := cmp.Diff("echo", req.Method); diff != "" {
73+
panicf("unexpected RPC method (-want +got):\n%s", diff)
74+
}
75+
76+
if diff := cmp.Diff([]interface{}{echo}, req.Params); diff != "" {
77+
panicf("unexpected RPC parameters (-want +got):\n%s", diff)
78+
}
79+
80+
return jsonrpc.Response{
81+
ID: intPtr(1),
82+
Result: mustMarshalJSON(t, []string{echo}),
83+
}
84+
})
85+
defer done()
86+
87+
if err := c.Echo(context.Background()); err != nil {
88+
t.Fatalf("failed to echo: %v", err)
89+
}
90+
}

0 commit comments

Comments
 (0)