From 06fdd6e2e1e34683534f8bf362ba0c9c5177602f Mon Sep 17 00:00:00 2001 From: Kurt S <56745262+Kschappacher@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:10:42 -0500 Subject: [PATCH] RSDK-8767 - populate local_name and remote_path to all resource names sent from RDK (#4519) --- protoutils/messages.go | 10 ++++++---- protoutils/messages_test.go | 17 +++++++++++++++++ resource/name.go | 8 ++++++++ resource/name_test.go | 18 ++++++++++++++++++ robot/server/server_test.go | 26 ++++++++++++++++---------- 5 files changed, 65 insertions(+), 14 deletions(-) diff --git a/protoutils/messages.go b/protoutils/messages.go index 17089355813..e7544a69ac9 100644 --- a/protoutils/messages.go +++ b/protoutils/messages.go @@ -23,10 +23,12 @@ import ( // ResourceNameToProto converts a resource.Name to its proto counterpart. func ResourceNameToProto(name resource.Name) *commonpb.ResourceName { return &commonpb.ResourceName{ - Namespace: string(name.API.Type.Namespace), - Type: name.API.Type.Name, - Subtype: name.API.SubtypeName, - Name: name.ShortName(), + Namespace: string(name.API.Type.Namespace), + Type: name.API.Type.Name, + Subtype: name.API.SubtypeName, + Name: name.ShortName(), + RemotePath: name.RemoteNameToRemoteArray(), + LocalName: name.Name, } } diff --git a/protoutils/messages_test.go b/protoutils/messages_test.go index f0e628fae40..4466cc39435 100644 --- a/protoutils/messages_test.go +++ b/protoutils/messages_test.go @@ -7,6 +7,8 @@ import ( "go.viam.com/test" "google.golang.org/protobuf/types/known/wrapperspb" + + "go.viam.com/rdk/resource" ) func TestStringToAnyPB(t *testing.T) { @@ -40,3 +42,18 @@ func TestStringToAnyPB(t *testing.T) { wrappedVal4 := wrapperspb.String("abcd") test.That(t, anyVal.MessageIs(wrappedVal4), test.ShouldBeTrue) } + +func TestResourceNameToProto(t *testing.T) { + resourceName := resource.Name{ + Name: "totallyLegitResource", + Remote: "remote1:remote2:remote3", + API: resource.NewAPI("space", "fake", "fakeFake"), + } + resourceNameProto := ResourceNameToProto(resourceName) + finalResource := ResourceNameFromProto(resourceNameProto) + + test.That(t, resourceNameProto.LocalName, test.ShouldEqual, "totallyLegitResource") + test.That(t, resourceNameProto.RemotePath, test.ShouldResemble, []string{"remote1", "remote2", "remote3"}) + test.That(t, resourceNameProto.Name, test.ShouldEqual, "remote1:remote2:remote3:totallyLegitResource") + test.That(t, finalResource, test.ShouldResemble, resourceName) +} diff --git a/resource/name.go b/resource/name.go index 42e06a4730a..1f34578e217 100644 --- a/resource/name.go +++ b/resource/name.go @@ -141,6 +141,14 @@ func (n Name) String() string { return name } +// RemoteNameToRemoteArray returns an ordered array of all of remotes in a resource name. +func (n Name) RemoteNameToRemoteArray() []string { + if n.Remote == "" { + return []string{} + } + return strings.Split(n.Remote, ":") +} + // SDPTrackName returns a valid SDP video/audio track name as defined in RFC 4566 (https://www.rfc-editor.org/rfc/rfc4566) // where track names should not include colons. func (n Name) SDPTrackName() string { diff --git a/resource/name_test.go b/resource/name_test.go index c6489b7aabe..779b8e5e8c1 100644 --- a/resource/name_test.go +++ b/resource/name_test.go @@ -191,3 +191,21 @@ func TestNamesToStrings(t *testing.T) { test.That(t, NamesToStrings(tc.input), test.ShouldResemble, tc.output) } } + +func TestRemoteNameToRemoteArray(t *testing.T) { + t.Run("name with multiple remote returns array of remotes", func(t *testing.T) { + nameTest := Name{ + Remote: "foo:bar:wow", + } + remoteArray := nameTest.RemoteNameToRemoteArray() + test.That(t, remoteArray, test.ShouldResemble, []string{"foo", "bar", "wow"}) + }) + + t.Run("name with empty remotes should return empty string array", func(t *testing.T) { + nameTest := Name{ + Remote: "", + } + remoteArray := nameTest.RemoteNameToRemoteArray() + test.That(t, remoteArray, test.ShouldResemble, []string{}) + }) +} diff --git a/robot/server/server_test.go b/robot/server/server_test.go index 3270ac69edd..130b1f03111 100644 --- a/robot/server/server_test.go +++ b/robot/server/server_test.go @@ -51,10 +51,12 @@ var serverNewResource = arm.Named("") var serverOneResourceResponse = []*commonpb.ResourceName{ { - Namespace: string(serverNewResource.API.Type.Namespace), - Type: serverNewResource.API.Type.Name, - Subtype: serverNewResource.API.SubtypeName, - Name: serverNewResource.Name, + Namespace: string(serverNewResource.API.Type.Namespace), + Type: serverNewResource.API.Type.Name, + Subtype: serverNewResource.API.SubtypeName, + Name: serverNewResource.Name, + RemotePath: []string{}, + LocalName: "", }, } @@ -325,17 +327,21 @@ func TestServer(t *testing.T) { expectedResp := []*pb.ResourceRPCSubtype{ { Subtype: &commonpb.ResourceName{ - Namespace: string(serverNewResource.API.Type.Namespace), - Type: serverNewResource.API.Type.Name, - Subtype: serverNewResource.API.SubtypeName, + Namespace: string(serverNewResource.API.Type.Namespace), + Type: serverNewResource.API.Type.Name, + Subtype: serverNewResource.API.SubtypeName, + RemotePath: []string{}, + Name: "", }, ProtoService: desc1.GetFullyQualifiedName(), }, { Subtype: &commonpb.ResourceName{ - Namespace: string(otherAPI.Type.Namespace), - Type: otherAPI.Type.Name, - Subtype: otherAPI.SubtypeName, + Namespace: string(otherAPI.Type.Namespace), + Type: otherAPI.Type.Name, + Subtype: otherAPI.SubtypeName, + RemotePath: []string{}, + Name: "", }, ProtoService: desc2.GetFullyQualifiedName(), },