Skip to content

Commit 95728f2

Browse files
committed
refactor: update brick handling and improve test coverage for variable updates
1 parent 18b6daf commit 95728f2

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

internal/orchestrator/bricks/bricks.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,17 +381,18 @@ func (s *Service) BrickUpdate(
381381
}
382382
}
383383

384+
appCurrent.Descriptor.Bricks[brickPosition].Model = brickModel
385+
appCurrent.Descriptor.Bricks[brickPosition].Variables = brickVariables
386+
384387
for _, brickVar := range brickFromIndex.Variables {
385388
if brickVar.IsRequired() {
386-
if _, exist := req.Variables[brickVar.Name]; !exist {
389+
newVariables := appCurrent.Descriptor.Bricks[brickPosition].Variables
390+
if _, exist := newVariables[brickVar.Name]; !exist {
387391
return fmt.Errorf("required variable %q must be set", brickVar.Name)
388392
}
389393
}
390394
}
391395

392-
appCurrent.Descriptor.Bricks[brickPosition].Model = brickModel
393-
appCurrent.Descriptor.Bricks[brickPosition].Variables = brickVariables
394-
395396
err := appCurrent.Save()
396397
if err != nil {
397398
return fmt.Errorf("cannot save brick instance with id %s", req.ID)

internal/orchestrator/bricks/bricks_test.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ func TestUdateBrick(t *testing.T) {
134134
require.Equal(t, "brick \"not-existing-id\" not found into the brick index", err.Error())
135135
})
136136

137-
// TODO: if a brick is updated but it is corrunlty not in the app.yaml ? ithink we must add it to the app.yaml
138137
t.Run("fails if brick is present into the index but not in the app ", func(t *testing.T) {
139138
err = brickService.BrickUpdate(BrickCreateUpdateRequest{ID: "arduino:dbstorage_sqlstore"}, f.Must(app.Load("testdata/dummy-app")))
140139
require.Error(t, err)
@@ -150,6 +149,7 @@ func TestUdateBrick(t *testing.T) {
150149
require.Equal(t, "variable \"NON_EXISTING_VARIABLE\" does not exist on brick \"arduino:arduino_cloud\"", err.Error())
151150
})
152151

152+
// TODO: allow to set an empty "" varaible
153153
t.Run("fails if a required variable is set empty", func(t *testing.T) {
154154
req := BrickCreateUpdateRequest{ID: "arduino:arduino_cloud", Variables: map[string]string{
155155
"ARDUINO_DEVICE_ID": "",
@@ -174,8 +174,8 @@ func TestUdateBrick(t *testing.T) {
174174
require.Equal(t, "required variable \"ARDUINO_DEVICE_ID\" must be set", err.Error())
175175
})
176176

177-
t.Run("update the variables of a brick correctly", func(t *testing.T) {
178-
tempDummyApp := paths.New("testdata/dummy-app.brick-override.temp")
177+
t.Run("update a single variables of a brick correctly", func(t *testing.T) {
178+
tempDummyApp := paths.New("testdata/dummy-app.temp")
179179
require.Nil(t, tempDummyApp.RemoveAll())
180180
require.Nil(t, paths.New("testdata/dummy-app").CopyDirTo(tempDummyApp))
181181
bricksIndex, err := bricksindex.GenerateBricksIndexFromFile(paths.New("testdata"))
@@ -203,6 +203,34 @@ func TestUdateBrick(t *testing.T) {
203203
require.Equal(t, secret, after.Descriptor.Bricks[0].Variables["ARDUINO_SECRET"])
204204
})
205205

206+
t.Run("update a single variable correctly", func(t *testing.T) {
207+
tempDummyApp := paths.New("testdata/dummy-app-for-update.temp")
208+
require.Nil(t, tempDummyApp.RemoveAll())
209+
require.Nil(t, paths.New("testdata/dummy-app-for-update").CopyDirTo(tempDummyApp))
210+
bricksIndex, err := bricksindex.GenerateBricksIndexFromFile(paths.New("testdata"))
211+
require.Nil(t, err)
212+
brickService := NewService(nil, bricksIndex, nil)
213+
214+
secret := "updated-the-secret"
215+
req := BrickCreateUpdateRequest{
216+
ID: "arduino:arduino_cloud",
217+
Variables: map[string]string{
218+
// the ARDUINO_DEVICE_ID is already configured int the app.yaml
219+
"ARDUINO_SECRET": secret,
220+
},
221+
}
222+
223+
err = brickService.BrickUpdate(req, f.Must(app.Load(tempDummyApp.String())))
224+
require.Nil(t, err)
225+
226+
after, err := app.Load(tempDummyApp.String())
227+
require.Nil(t, err)
228+
require.Len(t, after.Descriptor.Bricks, 1)
229+
require.Equal(t, "arduino:arduino_cloud", after.Descriptor.Bricks[0].ID)
230+
require.Equal(t, "i-am-a-device-id", after.Descriptor.Bricks[0].Variables["ARDUINO_DEVICE_ID"])
231+
require.Equal(t, secret, after.Descriptor.Bricks[0].Variables["ARDUINO_SECRET"])
232+
})
233+
206234
}
207235

208236
func TestGetBrickInstanceVariableDetails(t *testing.T) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: An app with only a required variable filled
2+
description: An app with only a required variable filled
3+
bricks:
4+
- arduino:arduino_cloud:
5+
variables:
6+
ARDUINO_DEVICE_ID: "i-am-a-device-id"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def main():
2+
pass

0 commit comments

Comments
 (0)