@@ -26,6 +26,86 @@ import (
2626 "github.com/arduino/arduino-app-cli/internal/orchestrator/bricksindex"
2727)
2828
29+ func TestUdateBrick (t * testing.T ) {
30+ bricksIndex , err := bricksindex .GenerateBricksIndexFromFile (paths .New ("testdata" ))
31+ require .Nil (t , err )
32+ brickService := NewService (nil , bricksIndex , nil )
33+
34+ t .Run ("fails if brick id does not exist into brick index" , func (t * testing.T ) {
35+ err = brickService .BrickUpdate (BrickCreateUpdateRequest {ID : "not-existing-id" }, f .Must (app .Load ("testdata/dummy-app" )))
36+ require .Error (t , err )
37+ require .Equal (t , "brick \" not-existing-id\" not found into the brick index" , err .Error ())
38+ })
39+
40+ t .Run ("fails if brick id is present into the index but not in the app " , func (t * testing.T ) {
41+ err = brickService .BrickUpdate (BrickCreateUpdateRequest {ID : "arduino:dbstorage_sqlstore" }, f .Must (app .Load ("testdata/dummy-app" )))
42+ require .Error (t , err )
43+ require .Equal (t , "brick \" not-existing-id\" not found into app descriptor" , err .Error ())
44+ })
45+
46+ t .Run ("fails if the updated variable is not present in the brick definition" , func (t * testing.T ) {
47+ req := BrickCreateUpdateRequest {ID : "arduino:arduino_cloud" , Variables : map [string ]string {
48+ "NON_EXISTING_VARIABLE" : "some-value" ,
49+ }}
50+ err = brickService .BrickUpdate (req , f .Must (app .Load ("testdata/dummy-app" )))
51+ require .Error (t , err )
52+ require .Equal (t , "variable \" NON_EXISTING_VARIABLE\" does not exist on brick \" arduino:arduino_cloud\" " , err .Error ())
53+ })
54+
55+ t .Run ("fails if a required variable is set empty" , func (t * testing.T ) {
56+ req := BrickCreateUpdateRequest {ID : "arduino:arduino_cloud" , Variables : map [string ]string {
57+ "ARDUINO_DEVICE_ID" : "" ,
58+ "ARDUINO_SECRET" : "a-secret-a" ,
59+ }}
60+ err = brickService .BrickUpdate (req , f .Must (app .Load ("testdata/dummy-app" )))
61+ require .Error (t , err )
62+ require .Equal (t , "required variable \" ARDUINO_DEVICE_ID\" cannot be empty" , err .Error ())
63+ })
64+
65+ t .Run ("fails if a mandatory variable is not present" , func (t * testing.T ) {
66+ tempDummyApp := paths .New ("testdata/dummy-app.temp" )
67+ err := tempDummyApp .RemoveAll ()
68+ require .Nil (t , err )
69+ require .Nil (t , paths .New ("testdata/dummy-app" ).CopyDirTo (tempDummyApp ))
70+
71+ req := BrickCreateUpdateRequest {ID : "arduino:arduino_cloud" , Variables : map [string ]string {
72+ "ARDUINO_SECRET" : "a-secret-a" ,
73+ }}
74+ err = brickService .BrickUpdate (req , f .Must (app .Load (tempDummyApp .String ())))
75+ require .Error (t , err )
76+ require .Equal (t , "required variable \" ARDUINO_DEVICE_ID\" must be set" , err .Error ())
77+ })
78+
79+ t .Run ("update the variables of a brick correctly" , func (t * testing.T ) {
80+ tempDummyApp := paths .New ("testdata/dummy-app.brick-override.temp" )
81+ require .Nil (t , tempDummyApp .RemoveAll ())
82+ require .Nil (t , paths .New ("testdata/dummy-app" ).CopyDirTo (tempDummyApp ))
83+ bricksIndex , err := bricksindex .GenerateBricksIndexFromFile (paths .New ("testdata" ))
84+ require .Nil (t , err )
85+ brickService := NewService (nil , bricksIndex , nil )
86+
87+ deviceID := "updated-device-id"
88+ secret := "updated-secret"
89+ req := BrickCreateUpdateRequest {
90+ ID : "arduino:arduino_cloud" ,
91+ Variables : map [string ]string {
92+ "ARDUINO_DEVICE_ID" : deviceID ,
93+ "ARDUINO_SECRET" : secret ,
94+ },
95+ }
96+
97+ err = brickService .BrickUpdate (req , f .Must (app .Load (tempDummyApp .String ())))
98+ require .Nil (t , err )
99+
100+ after , err := app .Load (tempDummyApp .String ())
101+ require .Nil (t , err )
102+ require .Len (t , after .Descriptor .Bricks , 1 )
103+ require .Equal (t , "arduino:arduino_cloud" , after .Descriptor .Bricks [0 ].ID )
104+ require .Equal (t , deviceID , after .Descriptor .Bricks [0 ].Variables ["ARDUINO_DEVICE_ID" ])
105+ require .Equal (t , secret , after .Descriptor .Bricks [0 ].Variables ["ARDUINO_SECRET" ])
106+ })
107+
108+ }
29109func TestBrickCreate (t * testing.T ) {
30110 bricksIndex , err := bricksindex .GenerateBricksIndexFromFile (paths .New ("testdata" ))
31111 require .Nil (t , err )
@@ -90,6 +170,7 @@ func TestBrickCreate(t *testing.T) {
90170 require .Len (t , after .Descriptor .Bricks , 2 )
91171 require .Equal (t , "arduino:dbstorage_sqlstore" , after .Descriptor .Bricks [1 ].ID )
92172 })
173+
93174 t .Run ("the variables of a brick are updated" , func (t * testing.T ) {
94175 tempDummyApp := paths .New ("testdata/dummy-app.brick-override.temp" )
95176 err := tempDummyApp .RemoveAll ()
0 commit comments