This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4e009ba
commit 51c0d5c
Showing
4 changed files
with
152 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters