Skip to content

Commit

Permalink
Merge pull request #16 from viam-modules/RSDK-8848
Browse files Browse the repository at this point in the history
[RSDK-8848] - Test improvements bucket
  • Loading branch information
seanavery authored Sep 30, 2024
2 parents b9d2829 + 33199da commit 8d5c497
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 119 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ lint: tool-install $(FFMPEG_BUILD)
CGO_CFLAGS=$(CGO_CFLAGS) GOFLAGS=$(GOFLAGS) $(TOOL_BIN)/golangci-lint run -v --fix --config=./etc/.golangci.yaml

test: $(BIN_OUTPUT_PATH)/video-store
ifeq ($(shell which ffmpeg > /dev/null 2>&1; echo $$?), 1)
ifeq ($(SOURCE_OS),linux)
sudo apt update && sudo apt install -y ffmpeg
endif
ifeq ($(SOURCE_OS),darwin)
brew update && brew install ffmpeg
endif
endif
git lfs pull
cp $(BIN_OUTPUT_PATH)/video-store bin/video-store
go test -v ./tests/
Expand Down
43 changes: 35 additions & 8 deletions tests/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
Expand All @@ -27,6 +28,7 @@ const (
)

func setupViamServer(ctx context.Context, configStr string) (robot.Robot, error) {
cleanVideoStoreDir()
logger := logging.NewLogger("video-store-module")
cfg, err := config.FromReader(ctx, "default.json", bytes.NewReader([]byte(configStr)), logger)
if err != nil {
Expand All @@ -50,6 +52,31 @@ func getModuleBinPath() (string, error) {
return fullModuleBinPath, nil
}

func cleanVideoStoreDir() error {
currentDir, err := os.Getwd()
if err != nil {
return err
}
videoStoreDir := filepath.Join(currentDir, "video-storage")
err = os.Chdir(videoStoreDir)
if err != nil {
return err
}
defer os.Chdir(currentDir) // Ensure we change back to the original directory
cmd := exec.Command("git", "clean", "-fdx")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func testVideoPlayback(t *testing.T, videoPath string) {
_, err := os.Stat(videoPath)
test.That(t, err, test.ShouldBeNil)
cmd := exec.Command("ffmpeg", "-v", "error", "-i", videoPath, "-f", "null", "-")
err = cmd.Run()
test.That(t, err, test.ShouldBeNil)
}

func TestModuleConfiguration(t *testing.T) {
fullModuleBinPath, err := getModuleBinPath()
if err != nil {
Expand All @@ -75,8 +102,8 @@ func TestModuleConfiguration(t *testing.T) {
"storage_path": "%s"
},
"cam_props": {
"width": 1920,
"height": 1080,
"width": 1280,
"height": 720,
"framerate": 30
},
"video": {
Expand Down Expand Up @@ -181,8 +208,8 @@ func TestModuleConfiguration(t *testing.T) {
"camera": "fake-cam-1",
"sync": "data_manager-1",
"cam_props": {
"width": 1920,
"height": 1080,
"width": 1280,
"height": 720,
"framerate": 30
},
"video": {
Expand Down Expand Up @@ -247,8 +274,8 @@ func TestModuleConfiguration(t *testing.T) {
"storage_path": "/tmp/video-storage"
},
"cam_props": {
"width": 1920,
"height": 1080,
"width": 1280,
"height": 720,
"framerate": 30
},
"video": {
Expand Down Expand Up @@ -375,8 +402,8 @@ func TestModuleConfiguration(t *testing.T) {
"storage_path": "/tmp"
},
"cam_props": {
"width": 1920,
"height": 1080,
"width": 1280,
"height": 720,
"framerate": 30
},
"video": {
Expand Down
64 changes: 20 additions & 44 deletions tests/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"go.viam.com/rdk/components/camera"
"go.viam.com/test"
)

func TestFetchDoCommand(t *testing.T) {
Expand Down Expand Up @@ -39,8 +40,8 @@ func TestFetchDoCommand(t *testing.T) {
"storage_path": "%s"
},
"cam_props": {
"width": 1920,
"height": 1080,
"width": 1280,
"height": 720,
"framerate": 30
},
"video": {
Expand Down Expand Up @@ -119,78 +120,53 @@ func TestFetchDoCommand(t *testing.T) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
r, err := setupViamServer(timeoutCtx, config1)
if err != nil {
t.Fatalf("failed to setup viam server: %v", err)
}
test.That(t, err, test.ShouldBeNil)
defer r.Close(timeoutCtx)
vs, err := camera.FromRobot(r, videoStoreComponentName)
if err != nil {
t.Fatalf("failed to get video store component: %v", err)
}
test.That(t, err, test.ShouldBeNil)
res, err := vs.DoCommand(timeoutCtx, fetchCmd1)
if err != nil {
t.Fatalf("failed to execute fetch command: %v", err)
}
test.That(t, err, test.ShouldBeNil)
video, ok := res["video"].(string)
if !ok {
t.Fatalf("failed to parse video from response: %v", res)
}
if video == "" {
t.Fatalf("video not found in response: %v", res)
}
test.That(t, ok, test.ShouldBeTrue)
test.That(t, video, test.ShouldNotBeEmpty)
})

t.Run("Test Fetch DoCommand Valid Time Range Over GRPC Limit.", func(t *testing.T) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
r, err := setupViamServer(timeoutCtx, config1)
if err != nil {
t.Fatalf("failed to setup viam server: %v", err)
}
test.That(t, err, test.ShouldBeNil)
defer r.Close(timeoutCtx)
vs, err := camera.FromRobot(r, videoStoreComponentName)
if err != nil {
t.Fatalf("failed to get video store component: %v", err)
}
test.That(t, err, test.ShouldBeNil)
_, err = vs.DoCommand(timeoutCtx, fetchCmd2)
if err == nil {
t.Fatalf("expected error but got nil")
}
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "grpc")
})

t.Run("Test Fetch DoCommand Invalid Time Range.", func(t *testing.T) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
r, err := setupViamServer(timeoutCtx, config1)
if err != nil {
t.Fatalf("failed to setup viam server: %v", err)
}
test.That(t, err, test.ShouldBeNil)
defer r.Close(timeoutCtx)
vs, err := camera.FromRobot(r, videoStoreComponentName)
if err != nil {
t.Fatalf("failed to get video store component: %v", err)
}
test.That(t, err, test.ShouldBeNil)
_, err = vs.DoCommand(timeoutCtx, fetchCmd3)
if err == nil {
t.Fatalf("expected error for invalid time range")
}
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "range")
})

t.Run("Test Fetch DoCommand Invalid Datetime Format.", func(t *testing.T) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
r, err := setupViamServer(timeoutCtx, config1)
if err != nil {
t.Fatalf("failed to setup viam server: %v", err)
}
test.That(t, err, test.ShouldBeNil)
defer r.Close(timeoutCtx)
vs, err := camera.FromRobot(r, videoStoreComponentName)
if err != nil {
t.Fatalf("failed to get video store component: %v", err)
}
test.That(t, err, test.ShouldBeNil)
_, err = vs.DoCommand(timeoutCtx, fetchCmd4)
if err == nil {
t.Fatalf("expected error for invalid datetime format")
}
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "parsing time")
})
}
Loading

0 comments on commit 8d5c497

Please sign in to comment.