Skip to content

Commit

Permalink
[RSDK-9315] Fix modular discovery response (#4568)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexbabe authored Nov 19, 2024
1 parent 20a332d commit 84f9916
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
10 changes: 8 additions & 2 deletions module/modmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,8 +1245,14 @@ func (m *module) registerResources(mgr modmaninterface.ModuleManager, logger log
m.logger.Errorf("error in modular DiscoverComponents: %s", err)
return nil, err
}

return res, nil
switch len(res.Discovery) {
case 0:
return nil, errors.New("modular DiscoverComponents response did not contain any discoveries")
case 1:
return res.Discovery[0].Results.AsMap(), nil
default:
return nil, errors.New("modular DiscoverComponents response contains more than one discovery")
}
},
})
}
Expand Down
25 changes: 6 additions & 19 deletions module/modmanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,7 @@ import (
rutils "go.viam.com/rdk/utils"
)

type testDiscoveryResult struct {
Discovery []testDiscoveryItem `json:"discovery"`
}

type testDiscoveryItem struct {
Query testDiscoveryQuery `json:"query"`
Results map[string]string `json:"results"`
}

type testDiscoveryQuery struct {
Subtype string `json:"subtype"`
Model string `json:"model"`
}
type testDiscoveryResult map[string]interface{}

func setupSocketWithRobot(t *testing.T) string {
t.Helper()
Expand Down Expand Up @@ -1369,7 +1357,7 @@ func TestBadModuleFailsFast(t *testing.T) {
test.That(t, err.Error(), test.ShouldContainSubstring, "module test-module exited too quickly after attempted startup")
}

func TestModularDiscovery(t *testing.T) {
func TestModularDiscoverFunc(t *testing.T) {
ctx := context.Background()
logger := logging.NewTestLogger(t)

Expand Down Expand Up @@ -1425,11 +1413,10 @@ func TestModularDiscovery(t *testing.T) {
test.That(t, err, test.ShouldBeNil)
t.Logf("Casted struct: %+v", discoveryResult)

test.That(t, len(discoveryResult.Discovery), test.ShouldEqual, 1)
discovery := discoveryResult.Discovery[0]
test.That(t, discovery.Query.Subtype, test.ShouldEqual, "rdk:component:generic")
test.That(t, discovery.Query.Model, test.ShouldEqual, "rdk:test:helper")
test.That(t, discovery.Results["extra"], test.ShouldEqual, tc.expectedExtra)
test.That(t, len(discoveryResult), test.ShouldEqual, 1)
extraStr, ok := discoveryResult["extra"].(string)
test.That(t, ok, test.ShouldBeTrue)
test.That(t, extraStr, test.ShouldEqual, tc.expectedExtra)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion module/testmodule/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func mainWithArgs(ctx context.Context, args []string, logger logging.Logger) err
if !ok {
return nil, errors.New("'extra' value must be a string")
}
return map[string]string{
return map[string]interface{}{
"extra": extraValStr,
}, nil
},
Expand Down

0 comments on commit 84f9916

Please sign in to comment.