Skip to content

Commit 5742d45

Browse files
committed
test: add dummy app configuration and main.py for brick creation tests
1 parent c7a2e55 commit 5742d45

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

internal/orchestrator/bricks/bricks_test.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package bricks
1717

1818
import (
19-
"fmt"
2019
"testing"
21-
"time"
2220

2321
"github.com/arduino/go-paths-helper"
2422
"github.com/stretchr/testify/require"
@@ -34,7 +32,7 @@ func TestBrickCreate(t *testing.T) {
3432
brickService := NewService(nil, bricksIndex, nil)
3533

3634
t.Run("fails if brick id does not exist", func(t *testing.T) {
37-
err = brickService.BrickCreate(BrickCreateUpdateRequest{ID: "not-existing-id"}, f.Must(app.Load("testdata/AppFromExample")))
35+
err = brickService.BrickCreate(BrickCreateUpdateRequest{ID: "not-existing-id"}, f.Must(app.Load("testdata/dummy-app")))
3836
require.Error(t, err)
3937
require.Equal(t, "brick \"not-existing-id\" not found", err.Error())
4038
})
@@ -43,59 +41,56 @@ func TestBrickCreate(t *testing.T) {
4341
req := BrickCreateUpdateRequest{ID: "arduino:arduino_cloud", Variables: map[string]string{
4442
"NON_EXISTING_VARIABLE": "some-value",
4543
}}
46-
err = brickService.BrickCreate(req, f.Must(app.Load("testdata/AppFromExample")))
44+
err = brickService.BrickCreate(req, f.Must(app.Load("testdata/dummy-app")))
4745
require.Error(t, err)
4846
require.Equal(t, "variable \"NON_EXISTING_VARIABLE\" does not exist on brick \"arduino:arduino_cloud\"", err.Error())
4947
})
5048

51-
//TODO: currently we do not accept an empty string as a valid value for a variable
5249
t.Run("fails if a required variable is set empty", func(t *testing.T) {
5350
req := BrickCreateUpdateRequest{ID: "arduino:arduino_cloud", Variables: map[string]string{
5451
"ARDUINO_DEVICE_ID": "",
5552
"ARDUINO_SECRET": "a-secret-a",
5653
}}
57-
err = brickService.BrickCreate(req, f.Must(app.Load("testdata/AppFromExample")))
54+
err = brickService.BrickCreate(req, f.Must(app.Load("testdata/dummy-app")))
5855
require.Error(t, err)
5956
require.Equal(t, "variable \"ARDUINO_DEVICE_ID\" cannot be empty", err.Error())
6057
})
6158

62-
t.Run("omit a mandatory variable is not present in the request", func(t *testing.T) {
63-
tempApp, cleanUp := copyToTempApp(t, paths.New("testdata/AppFromExample"))
64-
defer cleanUp()
65-
59+
t.Run("do not fail if a mandatory variable is not present", func(t *testing.T) {
6660
req := BrickCreateUpdateRequest{ID: "arduino:arduino_cloud", Variables: map[string]string{
6761
"ARDUINO_SECRET": "a-secret-a",
6862
}}
69-
err = brickService.BrickCreate(req, f.Must(app.Load(tempApp.String())))
70-
require.Nil(t, err)
63+
err = brickService.BrickCreate(req, f.Must(app.Load("testdata/dummy-app")))
64+
require.NoError(t, err)
7165

72-
after, err := app.Load(tempApp.String())
66+
after, err := app.Load("testdata/dummy-app")
7367
require.Nil(t, err)
7468
require.Len(t, after.Descriptor.Bricks, 1)
7569
require.Equal(t, "arduino:arduino_cloud", after.Descriptor.Bricks[0].ID)
76-
// NOTE: currently it is not possible to distinguish a field with empty string or missing field into the yaml.
77-
// The 'ARDUINO_DEVICE_ID' is missing from the app.yaml but here we check the empty string.
78-
// A better aproach is to use golden files
7970
require.Equal(t, "", after.Descriptor.Bricks[0].Variables["ARDUINO_DEVICE_ID"])
8071
require.Equal(t, "a-secret-a", after.Descriptor.Bricks[0].Variables["ARDUINO_SECRET"])
8172
})
8273

8374
t.Run("the brick is added if it does not exist in the app", func(t *testing.T) {
84-
tempApp, cleanUp := copyToTempApp(t, paths.New("testdata/AppFromExample"))
85-
defer cleanUp()
75+
tempDummyApp := paths.New("testdata/dummy-app.temp")
76+
err := tempDummyApp.RemoveAll()
77+
require.Nil(t, err)
78+
require.Nil(t, paths.New("testdata/dummy-app").CopyDirTo(tempDummyApp))
8679

8780
req := BrickCreateUpdateRequest{ID: "arduino:dbstorage_sqlstore"}
88-
err = brickService.BrickCreate(req, f.Must(app.Load(tempApp.String())))
81+
err = brickService.BrickCreate(req, f.Must(app.Load(tempDummyApp.String())))
8982
require.Nil(t, err)
90-
after, err := app.Load(tempApp.String())
83+
after, err := app.Load(tempDummyApp.String())
9184
require.Nil(t, err)
9285
require.Len(t, after.Descriptor.Bricks, 2)
9386
require.Equal(t, "arduino:dbstorage_sqlstore", after.Descriptor.Bricks[1].ID)
9487
})
9588
t.Run("the variables of a brick are updated", func(t *testing.T) {
96-
tempApp, cleanUp := copyToTempApp(t, paths.New("testdata/AppFromExample"))
97-
defer cleanUp()
98-
89+
tempDummyApp := paths.New("testdata/dummy-app.brick-override.temp")
90+
err := tempDummyApp.RemoveAll()
91+
require.Nil(t, err)
92+
err = paths.New("testdata/dummy-app").CopyDirTo(tempDummyApp)
93+
require.Nil(t, err)
9994
bricksIndex, err := bricksindex.GenerateBricksIndexFromFile(paths.New("testdata"))
10095
require.Nil(t, err)
10196
brickService := NewService(nil, bricksIndex, nil)
@@ -110,10 +105,10 @@ func TestBrickCreate(t *testing.T) {
110105
},
111106
}
112107

113-
err = brickService.BrickCreate(req, f.Must(app.Load(tempApp.String())))
108+
err = brickService.BrickCreate(req, f.Must(app.Load(tempDummyApp.String())))
114109
require.Nil(t, err)
115110

116-
after, err := app.Load(tempApp.String())
111+
after, err := app.Load(tempDummyApp.String())
117112
require.Nil(t, err)
118113
require.Len(t, after.Descriptor.Bricks, 1)
119114
require.Equal(t, "arduino:arduino_cloud", after.Descriptor.Bricks[0].ID)
@@ -122,14 +117,6 @@ func TestBrickCreate(t *testing.T) {
122117
})
123118
}
124119

125-
func copyToTempApp(t *testing.T, srcApp *paths.Path) (tmpApp *paths.Path, cleanUp func()) {
126-
tmpAppPath := paths.New(srcApp.String() + "-" + fmt.Sprint(time.Now().UnixMicro()) + ".temp")
127-
require.Nil(t, srcApp.CopyDirTo(tmpAppPath))
128-
return tmpAppPath, func() {
129-
require.Nil(t, tmpAppPath.RemoveAll())
130-
}
131-
}
132-
133120
func TestGetBrickInstanceVariableDetails(t *testing.T) {
134121
tests := []struct {
135122
name string

internal/orchestrator/bricks/testdata/AppFromExample/app.yaml renamed to internal/orchestrator/bricks/testdata/dummy-app/app.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Copy of Blinking LED from Arduino Cloud
22
description: Control the LED from the Arduino IoT Cloud using RPC calls
3-
icon: ☁️
43
ports: []
54
bricks:
6-
- arduino:arduino_cloud:
5+
- arduino:arduino_cloud:
6+
variables:
7+
ARDUINO_SECRET: a-secret-a
8+
icon: ☁️

internal/orchestrator/bricks/testdata/AppFromExample/python/main.py renamed to internal/orchestrator/bricks/testdata/dummy-app/python/main.py

File renamed without changes.

0 commit comments

Comments
 (0)