Skip to content

Commit 273b7c7

Browse files
authored
chore: add unit test
1 parent c84fbb8 commit 273b7c7

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

internal/orchestrator/app/parser_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/arduino/go-paths-helper"
@@ -73,3 +74,37 @@ func TestIsSingleEmoji(t *testing.T) {
7374
})
7475
}
7576
}
77+
78+
func TestArduinoApp_Load(t *testing.T) {
79+
tempDir := t.TempDir()
80+
err := paths.New(tempDir).MkdirAll()
81+
require.NoError(t, err)
82+
83+
// Create minimal setup
84+
err = paths.New(tempDir, "python").MkdirAll()
85+
require.NoError(t, err)
86+
err = os.WriteFile(paths.New(tempDir, "python", "main.py").String(), []byte("print('Hello World')"), 0600)
87+
require.NoError(t, err)
88+
// Create a valid app.yaml file
89+
appYaml := paths.New(tempDir, "app.yaml")
90+
91+
appDescriptor :=
92+
`name: Test App
93+
bricks:
94+
- arduino:object_detection:
95+
model: yolox-object-detection
96+
variables:
97+
"EI_OBJ_DETECTION_MODEL": "/home/arduino/.arduino-bricks/ei-models/face-det.eim"
98+
`
99+
100+
err = os.WriteFile(appYaml.String(), []byte(appDescriptor), 0600)
101+
require.NoError(t, err)
102+
103+
app, err := Load(tempDir)
104+
require.NoError(t, err)
105+
require.Equal(t, "Test App", app.Name)
106+
require.Equal(t, 1, len(app.Descriptor.Bricks))
107+
require.Equal(t, "arduino:object_detection", app.Descriptor.Bricks[0].ID)
108+
require.Equal(t, "yolox-object-detection", app.Descriptor.Bricks[0].Model)
109+
require.Equal(t, "/home/arduino/.arduino-bricks/ei-models/face-det.eim", app.Descriptor.Bricks[0].Variables["EI_OBJ_DETECTION_MODEL"])
110+
}

internal/orchestrator/orchestrator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func StartApp(
207207
}
208208
})
209209

210+
slog.Debug("starting app", slog.String("command", strings.Join(commands, " ")), slog.Any("envs", envs))
210211
process, err := paths.NewProcess(envs.AsList(), commands...)
211212
if err != nil {
212213
yield(StreamMessage{error: err})
@@ -245,6 +246,7 @@ func getAppEnvironmentVariables(app app.ArduinoApp, brickIndex *bricksindex.Bric
245246
maps.Insert(envs, maps.All(m.ModelConfiguration))
246247
}
247248

249+
slog.Debug("adding Brick", slog.String("brickID", brick.ID), slog.String("model", brick.Model), slog.Any("variables", brick.Variables))
248250
maps.Insert(envs, maps.All(brick.Variables))
249251
}
250252

internal/orchestrator/orchestrator_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,91 @@ models:
478478
// we ignore HOST_IP since it's dynamic
479479
}
480480

481+
func TestGetAppEnvironmentVariablesWithCustomModelOverrides(t *testing.T) {
482+
cfg := setTestOrchestratorConfig(t)
483+
idProvider := app.NewAppIDProvider(cfg)
484+
485+
docker, err := dockerClient.NewClientWithOpts(
486+
dockerClient.FromEnv,
487+
dockerClient.WithAPIVersionNegotiation(),
488+
)
489+
require.NoError(t, err)
490+
dockerCli, err := command.NewDockerCli(
491+
command.WithAPIClient(docker),
492+
command.WithBaseContext(t.Context()),
493+
)
494+
require.NoError(t, err)
495+
496+
err = dockerCli.Initialize(&flags.ClientOptions{})
497+
require.NoError(t, err)
498+
499+
appId := createApp(t, "app1", false, idProvider, cfg)
500+
appDesc, err := app.Load(appId.ToPath().String())
501+
require.NoError(t, err)
502+
appDesc.Descriptor.Bricks = []app.Brick{
503+
{
504+
ID: "arduino:object_detection",
505+
Variables: map[string]string{
506+
"EI_OBJ_DETECTION_MODEL": "/home/arduino/.arduino-bricks/ei-models/face-det.eim",
507+
}, // override the default model via ENV variable
508+
},
509+
}
510+
511+
bricksIndexContent := []byte(`
512+
bricks:
513+
- id: arduino:object_detection
514+
name: Object Detection
515+
description: "Brick for object detection using a pre-trained model. It processes\
516+
\ images and returns the predicted class label, bounding-boxes and confidence\
517+
\ score.\nBrick is designed to work with pre-trained models provided by framework\
518+
\ or with custom object detection models trained on Edge Impulse platform. \n"
519+
require_container: true
520+
require_model: true
521+
require_devices: false
522+
category: video
523+
model_name: yolox-object-detection
524+
variables:
525+
- name: CUSTOM_MODEL_PATH
526+
default_value: /home/arduino/.arduino-bricks/ei-models
527+
description: path to the custom model directory
528+
- name: EI_OBJ_DETECTION_MODEL
529+
default_value: /models/ootb/ei/yolo-x-nano.eim
530+
description: path to the model file
531+
`)
532+
err = cfg.AssetsDir().Join("bricks-list.yaml").WriteFile(bricksIndexContent)
533+
require.NoError(t, err)
534+
bricksIndex, err := bricksindex.GenerateBricksIndexFromFile(cfg.AssetsDir())
535+
assert.NoError(t, err)
536+
537+
modelsIndexContent := []byte(`
538+
models:
539+
- yolox-object-detection:
540+
runner: brick
541+
name : "General purpose object detection - YoloX"
542+
description: "General purpose object detection model based on YoloX Nano. This model is trained on the COCO dataset and can detect 80 different object classes."
543+
model_configuration:
544+
"EI_OBJ_DETECTION_MODEL": "/models/ootb/ei/yolo-x-nano.eim"
545+
metadata:
546+
source: "edgeimpulse"
547+
ei-project-id: 717280
548+
source-model-id: "YOLOX-Nano"
549+
source-model-url: "https://github.com/Megvii-BaseDetection/YOLOX"
550+
bricks:
551+
- arduino:object_detection
552+
- arduino:video_object_detection
553+
`)
554+
err = cfg.AssetsDir().Join("models-list.yaml").WriteFile(modelsIndexContent)
555+
require.NoError(t, err)
556+
modelIndex, err := modelsindex.GenerateModelsIndexFromFile(cfg.AssetsDir())
557+
require.NoError(t, err)
558+
559+
env := getAppEnvironmentVariables(appDesc, bricksIndex, modelIndex)
560+
require.Equal(t, cfg.AppsDir().Join("app1").String(), env["APP_HOME"])
561+
require.Equal(t, "/home/arduino/.arduino-bricks/ei-models/face-det.eim", env["EI_OBJ_DETECTION_MODEL"])
562+
require.Equal(t, "/home/arduino/.arduino-bricks/ei-models", env["CUSTOM_MODEL_PATH"])
563+
// we ignore HOST_IP since it's dynamic
564+
}
565+
481566
func TestValidateDevice(t *testing.T) {
482567

483568
t.Run("valid", func(t *testing.T) {

0 commit comments

Comments
 (0)