|
| 1 | +package discovery_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "go.viam.com/test" |
| 9 | + "go.viam.com/utils/rpc" |
| 10 | + |
| 11 | + viamgrpc "go.viam.com/rdk/grpc" |
| 12 | + "go.viam.com/rdk/logging" |
| 13 | + "go.viam.com/rdk/resource" |
| 14 | + "go.viam.com/rdk/services/discovery" |
| 15 | + "go.viam.com/rdk/testutils" |
| 16 | + "go.viam.com/rdk/testutils/inject" |
| 17 | +) |
| 18 | + |
| 19 | +func TestClient(t *testing.T) { |
| 20 | + logger := logging.NewTestLogger(t) |
| 21 | + listener1, err := net.Listen("tcp", "localhost:0") |
| 22 | + test.That(t, err, test.ShouldBeNil) |
| 23 | + rpcServer, err := rpc.NewServer(logger, rpc.WithUnauthenticated()) |
| 24 | + test.That(t, err, test.ShouldBeNil) |
| 25 | + |
| 26 | + testComponents := []resource.Config{createTestComponent("component-1"), createTestComponent("component-2")} |
| 27 | + |
| 28 | + workingDiscovery := inject.NewDiscoveryService(testDiscoveryName) |
| 29 | + workingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) { |
| 30 | + return testComponents, nil |
| 31 | + } |
| 32 | + workingDiscovery.DoFunc = testutils.EchoFunc |
| 33 | + |
| 34 | + failingDiscovery := inject.NewDiscoveryService(failDiscoveryName) |
| 35 | + failingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) { |
| 36 | + return nil, errDiscoverFailed |
| 37 | + } |
| 38 | + failingDiscovery.DoFunc = func( |
| 39 | + ctx context.Context, |
| 40 | + cmd map[string]interface{}, |
| 41 | + ) ( |
| 42 | + map[string]interface{}, |
| 43 | + error, |
| 44 | + ) { |
| 45 | + return nil, errDoFailed |
| 46 | + } |
| 47 | + |
| 48 | + resourceMap := map[resource.Name]discovery.Service{ |
| 49 | + discovery.Named(testDiscoveryName): workingDiscovery, |
| 50 | + discovery.Named(failDiscoveryName): failingDiscovery, |
| 51 | + } |
| 52 | + discoverySvc, err := resource.NewAPIResourceCollection(discovery.API, resourceMap) |
| 53 | + test.That(t, err, test.ShouldBeNil) |
| 54 | + resourceAPI, ok, err := resource.LookupAPIRegistration[discovery.Service](discovery.API) |
| 55 | + test.That(t, err, test.ShouldBeNil) |
| 56 | + test.That(t, ok, test.ShouldBeTrue) |
| 57 | + test.That(t, resourceAPI.RegisterRPCService(context.Background(), rpcServer, discoverySvc), test.ShouldBeNil) |
| 58 | + |
| 59 | + go rpcServer.Serve(listener1) |
| 60 | + defer rpcServer.Stop() |
| 61 | + |
| 62 | + t.Run("Failing client", func(t *testing.T) { |
| 63 | + cancelCtx, cancel := context.WithCancel(context.Background()) |
| 64 | + cancel() |
| 65 | + _, err = viamgrpc.Dial(cancelCtx, listener1.Addr().String(), logger) |
| 66 | + test.That(t, err, test.ShouldNotBeNil) |
| 67 | + test.That(t, err, test.ShouldBeError, context.Canceled) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("client tests for working discovery", func(t *testing.T) { |
| 71 | + conn, err := viamgrpc.Dial(context.Background(), listener1.Addr().String(), logger) |
| 72 | + test.That(t, err, test.ShouldBeNil) |
| 73 | + workingDiscoveryClient, err := discovery.NewClientFromConn(context.Background(), conn, "", discovery.Named(testDiscoveryName), logger) |
| 74 | + test.That(t, err, test.ShouldBeNil) |
| 75 | + |
| 76 | + respDis, err := workingDiscoveryClient.DiscoverResources(context.Background(), nil) |
| 77 | + test.That(t, err, test.ShouldBeNil) |
| 78 | + test.That(t, len(respDis), test.ShouldEqual, len(testComponents)) |
| 79 | + for index, actual := range respDis { |
| 80 | + expected := testComponents[index] |
| 81 | + validateComponent(t, actual, expected) |
| 82 | + } |
| 83 | + |
| 84 | + resp, err := workingDiscoveryClient.DoCommand(context.Background(), testutils.TestCommand) |
| 85 | + test.That(t, err, test.ShouldBeNil) |
| 86 | + test.That(t, resp["cmd"], test.ShouldEqual, testutils.TestCommand["cmd"]) |
| 87 | + test.That(t, resp["data"], test.ShouldEqual, testutils.TestCommand["data"]) |
| 88 | + |
| 89 | + test.That(t, workingDiscoveryClient.Close(context.Background()), test.ShouldBeNil) |
| 90 | + test.That(t, conn.Close(), test.ShouldBeNil) |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("client tests for failing discovery", func(t *testing.T) { |
| 94 | + conn, err := viamgrpc.Dial(context.Background(), listener1.Addr().String(), logger) |
| 95 | + test.That(t, err, test.ShouldBeNil) |
| 96 | + failingDiscoveryClient, err := discovery.NewClientFromConn(context.Background(), conn, "", discovery.Named(failDiscoveryName), logger) |
| 97 | + test.That(t, err, test.ShouldBeNil) |
| 98 | + |
| 99 | + _, err = failingDiscoveryClient.DiscoverResources(context.Background(), nil) |
| 100 | + test.That(t, err, test.ShouldNotBeNil) |
| 101 | + test.That(t, err.Error(), test.ShouldContainSubstring, errDiscoverFailed.Error()) |
| 102 | + |
| 103 | + _, err = failingDiscoveryClient.DoCommand(context.Background(), testutils.TestCommand) |
| 104 | + test.That(t, err, test.ShouldNotBeNil) |
| 105 | + test.That(t, err.Error(), test.ShouldContainSubstring, errDoFailed.Error()) |
| 106 | + |
| 107 | + test.That(t, failingDiscoveryClient.Close(context.Background()), test.ShouldBeNil) |
| 108 | + test.That(t, conn.Close(), test.ShouldBeNil) |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("client tests for failing discovery due to nil response", func(t *testing.T) { |
| 112 | + conn, err := viamgrpc.Dial(context.Background(), listener1.Addr().String(), logger) |
| 113 | + test.That(t, err, test.ShouldBeNil) |
| 114 | + failingDiscoveryClient, err := discovery.NewClientFromConn(context.Background(), conn, "", discovery.Named(failDiscoveryName), logger) |
| 115 | + test.That(t, err, test.ShouldBeNil) |
| 116 | + |
| 117 | + failingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) { |
| 118 | + return nil, nil |
| 119 | + } |
| 120 | + _, err = failingDiscoveryClient.DiscoverResources(context.Background(), nil) |
| 121 | + test.That(t, err, test.ShouldNotBeNil) |
| 122 | + test.That(t, err.Error(), test.ShouldContainSubstring, discovery.ErrNilResponse.Error()) |
| 123 | + |
| 124 | + test.That(t, failingDiscoveryClient.Close(context.Background()), test.ShouldBeNil) |
| 125 | + test.That(t, conn.Close(), test.ShouldBeNil) |
| 126 | + }) |
| 127 | + |
| 128 | + t.Run("dialed client tests for working discovery", func(t *testing.T) { |
| 129 | + conn, err := viamgrpc.Dial(context.Background(), listener1.Addr().String(), logger) |
| 130 | + test.That(t, err, test.ShouldBeNil) |
| 131 | + client, err := resourceAPI.RPCClient(context.Background(), conn, "", discovery.Named(testDiscoveryName), logger) |
| 132 | + test.That(t, err, test.ShouldBeNil) |
| 133 | + |
| 134 | + resp, err := client.DoCommand(context.Background(), testutils.TestCommand) |
| 135 | + test.That(t, err, test.ShouldBeNil) |
| 136 | + test.That(t, resp["cmd"], test.ShouldEqual, testutils.TestCommand["cmd"]) |
| 137 | + test.That(t, resp["data"], test.ShouldEqual, testutils.TestCommand["data"]) |
| 138 | + |
| 139 | + test.That(t, conn.Close(), test.ShouldBeNil) |
| 140 | + }) |
| 141 | +} |
0 commit comments