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

[RSDK-8848] - Test improvements bucket #16

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding separate FFmpeg installation here to get binary for playable checks.

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
Loading