Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement initial discover service #4665

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

JohnN193
Copy link
Member

@JohnN193 JohnN193 commented Dec 31, 2024

Initial implementation of the discover service in Go. Currently working on GetModelsFromModules in a separate branch to make it easier on reviewers

ticket: https://viam.atlassian.net/browse/RSDK-9620

@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Dec 31, 2024
go.mod Outdated
@@ -431,3 +431,5 @@ require (
github.com/ziutek/mymysql v1.5.4 // indirect
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
)

replace go.viam.com/api => github.com/johnn193/api v0.0.0-20241231164642-99f059defc82
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will revert once api is merged

return discovery.NewRPCServiceServer(injectSvc).(pb.DiscoveryServiceServer), injectDiscovery, injectDiscovery2, nil
}

func TestDiscoveryDo(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is literally copy/paste from the generic service I should probably remove this test, but I'm also fine with leaving it in case we are worried this will somehow break

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is good to test all APIs of a service to ensure that there is adequate coverage. I can't imagine it breaking - but say someone with no context joins viam in the future and removes the DoCommands accidentally - good to have all these tests fail :) . It does not have to be separate from your other test, you can add an injected DoCommand to the injected service in your test.

Good test to have with DoComamnd are: 1. test that it returns errors if no DoCommand is implemented with resource.Named, and 2. responds with the expected responses when a DoCommand is implemented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I just copied the testing structure that other services had, which is why it was separate. Moved all tests into the same test function based on feedback.

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
Comment on lines 166 to 169
// this was taken from proto_conversion_test.go
//
//nolint:thelper
func validateComponent(t *testing.T, actual, expected resource.Config) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same function as in proto_conversion_test.go

unsure if theres a good spot to put this function to use it in both spots, or if its fine to leave here

Copy link
Member

@randhid randhid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with a few nits. I think we use pointers when we want to return structs very fast, check with Nick S if that return type should be changed to just an array of resource.Configs rather than an array of pointers.

services/discovery/discovery.go Outdated Show resolved Hide resolved
services/discovery/discovery.go Show resolved Hide resolved
services/discovery/server.go Show resolved Hide resolved
services/discovery/discovery.go Outdated Show resolved Hide resolved
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 7, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 7, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 7, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 7, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 7, 2025
@JohnN193 JohnN193 requested a review from randhid January 7, 2025 18:23
Copy link
Member

@randhid randhid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits - almost there!

DiscoverResourcesFunc func(ctx context.Context, extra map[string]any) ([]resource.Config, error)
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also usually have a New function for the injected resource. Maybe this wasn't added to all of the inject utils, but it's good the add to create a new injected service - usually takes a name - here's an example of implementation:

func NewBase(name string) *Base {

@@ -101,18 +101,22 @@ func usageCostTypeToProto(costType UsageCostType) pb.UsageCostType {
case UsageCostTypeUnspecified:
return pb.UsageCostType_USAGE_COST_TYPE_UNSPECIFIED
case UsageCostTypeDataUpload:
//nolint:deprecated,staticcheck
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jr22, heads up, we're adding points here to unblock John's pr so the linter doesn't yell at uss - I see the API pr deprecating these constants was merged yesterday https://github.com/viamrobotics/api/pull/612/files

if err != nil {
return nil, err
}
discoveredConfigs := []resource.Config{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this above line 64 so it's closer to where it's populated.

Comment on lines +26 to +46
workingDiscovery := &inject.DiscoveryService{}
failingDiscovery := &inject.DiscoveryService{}

testComponents := []resource.Config{createTestComponent("component-1"), createTestComponent("component-2")}

workingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) {
return testComponents, nil
}
failingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) {
return nil, errDiscoverFailed
}
workingDiscovery.DoFunc = testutils.EchoFunc
failingDiscovery.DoFunc = func(
ctx context.Context,
cmd map[string]interface{},
) (
map[string]interface{},
error,
) {
return nil, errDoFailed
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this organization better because the working service and failing service functions are grouped together - but if you like yours please feel free to leave as is and resolve this comment. Very minor point here as this is a test.

Suggested change
workingDiscovery := &inject.DiscoveryService{}
failingDiscovery := &inject.DiscoveryService{}
testComponents := []resource.Config{createTestComponent("component-1"), createTestComponent("component-2")}
workingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) {
return testComponents, nil
}
failingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) {
return nil, errDiscoverFailed
}
workingDiscovery.DoFunc = testutils.EchoFunc
failingDiscovery.DoFunc = func(
ctx context.Context,
cmd map[string]interface{},
) (
map[string]interface{},
error,
) {
return nil, errDoFailed
}
testComponents := []resource.Config{createTestComponent("component-1"), createTestComponent("component-2")}
workingDiscovery := &inject.DiscoveryService{}
workingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) {
return testComponents, nil
}
workingDiscovery.DoFunc = testutils.EchoFunc
failingDiscovery := &inject.DiscoveryService{}
failingDiscovery.DiscoverResourcesFunc = func(ctx context.Context, extra map[string]any) ([]resource.Config, error) {
return nil, errDiscoverFailed
}
failingDiscovery.DoFunc = func(
ctx context.Context,
cmd map[string]interface{},
) (
map[string]interface{},
error,
) {
return nil, errDoFailed
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test This pull request is marked safe to test from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants