Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
RSDK-2004 Move setup code to slam repo (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaporter-work authored Mar 8, 2023
1 parent 4e009ba commit 51c0d5c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 21 deletions.
56 changes: 56 additions & 0 deletions config/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package config

import (
"context"
"os"
"path/filepath"
"time"

"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.opencensus.io/trace"
pb "go.viam.com/api/service/slam/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

// This increases the message size from 4MB to 32MB to match the RDK.
// This is necessary for transmitting large pointclouds.
var grpcMaxMessageSize = 32 * 1024 * 1024

// SetupDirectories creates the data directory at the specified path along with
// its data, map, and config subdirectories.
func SetupDirectories(dataDirectory string, logger golog.Logger) error {
for _, directoryName := range [4]string{"", "data", "map", "config"} {
directoryPath := filepath.Join(dataDirectory, directoryName)
if _, err := os.Stat(directoryPath); err != nil {
// This error includes the directoryPath
logger.Warnf("setup directories: %v", err)
if err := os.Mkdir(directoryPath, os.ModePerm); err != nil {
return errors.Errorf("issue creating directory at %v: %v", directoryPath, err)
}
}
}
return nil
}

// SetupGRPCConnection uses the defined port to create a GRPC client for communicating with the SLAM algorithms.
func SetupGRPCConnection(
ctx context.Context,
port string,
dialMaxTimeoutSec int,
logger golog.Logger,
) (pb.SLAMServiceClient, func() error, error) {
ctx, span := trace.StartSpan(ctx, "slam::SetupGRPCConnection")
defer span.End()
ctx, timeoutCancel := context.WithTimeout(ctx, time.Duration(dialMaxTimeoutSec)*time.Second)
defer timeoutCancel()
maxMsgSizeOption := grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(grpcMaxMessageSize))
// TODO: If we support running SLAM in the cloud, we need to pass credentials to this function
connLib, err := grpc.DialContext(ctx, port, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock(), maxMsgSizeOption)
if err != nil {
logger.Errorw("error connecting to slam process", "error", err)
return nil, nil, err
}
return pb.NewSLAMServiceClient(connLib), connLib.Close, err
}
74 changes: 74 additions & 0 deletions config/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package config

import (
"context"
"fmt"
"net"
"os"
"testing"

"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.viam.com/test"
"google.golang.org/grpc"
)

func setupTestGRPCServer(tb testing.TB) (*grpc.Server, int) {
listener, err := net.Listen("tcp", ":0")
test.That(tb, err, test.ShouldBeNil)
grpcServer := grpc.NewServer()
go grpcServer.Serve(listener)

return grpcServer, listener.Addr().(*net.TCPAddr).Port
}

func TestGRPCConnection(t *testing.T) {
logger := golog.NewTestLogger(t)
t.Run("Invalid grpc connection", func(t *testing.T) {
port := "invalid_unused_port:0"
_, _, err := SetupGRPCConnection(context.Background(), port, 1, logger)
test.That(t, err, test.ShouldBeError, errors.New("context deadline exceeded"))
})
t.Run("Valid grpc connection", func(t *testing.T) {
// Setup grpc server and attempt to connect to that one
grpcServer, portNum := setupTestGRPCServer(t)
defer grpcServer.Stop()
port := fmt.Sprintf(":%d", portNum)
_, _, err := SetupGRPCConnection(context.Background(), port, 1, logger)
test.That(t, err, test.ShouldBeNil)
})
}

func TestSetupDirectories(t *testing.T) {
logger := golog.NewTestLogger(t)
t.Run("Valid directories", func(t *testing.T) {
tempDir, err := os.MkdirTemp("", "*")
defer os.RemoveAll(tempDir)
test.That(t, err, test.ShouldBeNil)
err = SetupDirectories(tempDir, logger)
test.That(t, err, test.ShouldBeNil)
// Ensure that all of the directories have been created
_, errData := os.Stat(tempDir + "/data")
test.That(t, errData, test.ShouldBeNil)
_, errMap := os.Stat(tempDir + "/map")
test.That(t, errMap, test.ShouldBeNil)
_, errConfig := os.Stat(tempDir + "/config")
test.That(t, errConfig, test.ShouldBeNil)
// Ensure that the tests work
_, errFoo := os.Stat(tempDir + "/foodir")
test.That(t, errFoo, test.ShouldBeError)
})
t.Run("Invalid permissions", func(t *testing.T) {
tempDir, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer os.RemoveAll(tempDir)
noPermsDir := tempDir + "/no_permissions"
// create a directory in the temp folder
// that doesn't have write permissions
// in rwx format: --- --- ---
err = os.Mkdir(noPermsDir, 0o000)
test.That(t, err, test.ShouldBeNil)
err = SetupDirectories(noPermsDir, logger)
test.That(t, fmt.Sprint(err), test.ShouldContainSubstring, "issue creating directory at")
})
}
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ require (
github.com/golangci/golangci-lint v1.51.2
github.com/pkg/errors v0.9.1
github.com/rhysd/actionlint v1.6.23
go.opencensus.io v0.24.0
go.viam.com/api v0.1.86
go.viam.com/rdk v0.2.23
go.viam.com/test v1.1.1-0.20220909204145-f61b7c01c33e
go.viam.com/utils v0.1.15
google.golang.org/grpc v1.53.0
)

require (
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
4d63.com/gochecknoglobals v0.2.1 // indirect
cloud.google.com/go v0.105.0 // indirect
cloud.google.com/go/compute v1.13.0 // indirect
cloud.google.com/go/compute/metadata v0.2.1 // indirect
cloud.google.com/go v0.107.0 // indirect
cloud.google.com/go/compute v1.15.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.8.0 // indirect
cloud.google.com/go/storage v1.27.0 // indirect
git.sr.ht/~sbinet/gg v0.3.1 // indirect
Expand Down Expand Up @@ -48,7 +51,7 @@ require (
github.com/campoy/embedmd v1.0.0 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charithe/durationcheck v0.0.9 // indirect
github.com/chavacava/garif v0.0.0-20221024190013-b3ef35877348 // indirect
github.com/curioswitch/go-reassign v0.2.0 // indirect
Expand Down Expand Up @@ -242,19 +245,17 @@ require (
gitlab.com/bosi/decorder v0.2.3 // indirect
go-hep.org/x/hep v0.31.1 // indirect
go.mongodb.org/mongo-driver v1.12.0-prerelease.0.20221109213319-d3466eeae7a7 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/goleak v1.2.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
go.viam.com/api v0.1.84 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/exp/typeparams v0.0.0-20230203172020-98cc5a0785f9 // indirect
golang.org/x/image v0.3.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
Expand All @@ -265,7 +266,6 @@ require (
google.golang.org/api v0.103.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 // indirect
google.golang.org/grpc v1.52.3 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
27 changes: 14 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ cloud.google.com/go v0.65.0/go.mod h1:O5N8zS7uWy9vkA9vayVHs65eM1ubvY4h553ofrNHOb
cloud.google.com/go v0.72.0/go.mod h1:M+5Vjvlc2wnp6tjzE102Dw08nGShTscUx2nZMufOKPI=
cloud.google.com/go v0.74.0/go.mod h1:VV1xSbzvo+9QJOxLDaJfTjx5e+MePCpCWwvftOeQmWk=
cloud.google.com/go v0.75.0/go.mod h1:VGuuCn7PG0dwsd5XPVm2Mm3wlh3EL55/79EKB6hlPTY=
cloud.google.com/go v0.105.0 h1:DNtEKRBAAzeS4KyIory52wWHuClNaXJ5x1F7xa4q+5Y=
cloud.google.com/go v0.105.0/go.mod h1:PrLgOJNe5nfE9UMxKxgXj4mD3voiP+YQ6gdt6KMFOKM=
cloud.google.com/go v0.107.0 h1:qkj22L7bgkl6vIeZDlOY2po43Mx/TIa2Wsa7VR+PEww=
cloud.google.com/go v0.107.0/go.mod h1:wpc2eNrD7hXUTy8EKS10jkxpZBjASrORK7goS+3YX2I=
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc=
cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg=
cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc=
cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ=
cloud.google.com/go/compute v1.13.0 h1:AYrLkB8NPdDRslNp4Jxmzrhdr03fUAIDbiGFjLWowoU=
cloud.google.com/go/compute v1.13.0/go.mod h1:5aPTS0cUNMIc1CE546K+Th6weJUNQErARyZtRXDJ8GE=
cloud.google.com/go/compute/metadata v0.2.1 h1:efOwf5ymceDhK6PKMnnrTHP4pppY5L22mle96M1yP48=
cloud.google.com/go/compute/metadata v0.2.1/go.mod h1:jgHgmJd2RKBGzXqF5LR2EZMGxBkeanZ9wwa75XHJgOM=
cloud.google.com/go/compute v1.15.1 h1:7UGq3QknM33pw5xATlpzeoomNxsacIVvTqTTvbfajmE=
cloud.google.com/go/compute v1.15.1/go.mod h1:bjjoF/NtFUrkD/urWfdHaKuOPDR5nWIs63rR+SXhcpA=
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE=
cloud.google.com/go/datastore v1.1.0/go.mod h1:umbIZjpQpHh4hmRpGhH4tLFup+FVzqBi1b3c64qFpCk=
cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk=
Expand Down Expand Up @@ -168,8 +168,9 @@ github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/charithe/durationcheck v0.0.6/go.mod h1:SSbRIBVfMjCi/kEB6K65XEA83D6prSM8ap1UCpNKtgg=
github.com/charithe/durationcheck v0.0.9 h1:mPP4ucLrf/rKZiIG/a9IPXHGlh8p4CzgpyTy6EEutYk=
github.com/charithe/durationcheck v0.0.9/go.mod h1:SSbRIBVfMjCi/kEB6K65XEA83D6prSM8ap1UCpNKtgg=
Expand Down Expand Up @@ -1270,8 +1271,8 @@ go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
go.viam.com/api v0.1.84 h1:Ni7ryaa/U7QwTapftQ8i2z0GCH7kOo4fTWdxfCpGJYU=
go.viam.com/api v0.1.84/go.mod h1:/z5Aj0oShMqj7zUqyFz4BVv1HWgkAJ6/pYZ/8bSnKXs=
go.viam.com/api v0.1.86 h1:zLOfkPZL1BEXiqujZqbJAuAZRFfvxsBdq8Lwb2cuvks=
go.viam.com/api v0.1.86/go.mod h1:v7SdHHR8bvAxV9UGuUsp6MWQYcRHssHkI5FC05W+uyg=
go.viam.com/rdk v0.2.23 h1:3Skwz5s7tlnaksl2f2vQmEkT3ynGq3m9KQml6/QByI8=
go.viam.com/rdk v0.2.23/go.mod h1:GU2cSvuQGELdOeyvuQ9Y3OV0Mjd/9JqraAvP7pZfFs8=
go.viam.com/test v1.1.1-0.20220909204145-f61b7c01c33e h1:1tJIJv/zsobFV5feR2ZiG/+VGZkRPVHqJrSTkXbWfqQ=
Expand Down Expand Up @@ -1435,8 +1436,8 @@ golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.3.0 h1:6l90koy8/LaBLmLu8jpHeHexzMwEita0zFfYlggy2F8=
golang.org/x/oauth2 v0.3.0/go.mod h1:rQrIauxkUhJ6CuwEXwymO2/eh4xz2ZWF1nBkcxS+tGk=
golang.org/x/oauth2 v0.4.0 h1:NF0gk8LVPg1Ml7SSbGyySuoxdsXitj7TvgvuRxIMc/M=
golang.org/x/oauth2 v0.4.0/go.mod h1:RznEsdpjGAINPTOF0UH/t+xJ75L18YO3Ho6Pyn+uRec=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -1795,8 +1796,8 @@ google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv
google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8=
google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc v1.52.3 h1:pf7sOysg4LdgBqduXveGKrcEwbStiK2rtfghdzlUYDQ=
google.golang.org/grpc v1.52.3/go.mod h1:pu6fVzoFb+NBYNAvQL08ic+lvB2IojljRYuun5vorUY=
google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc=
google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down

0 comments on commit 51c0d5c

Please sign in to comment.