|
| 1 | +// This file is part of arduino-app-cli. |
| 2 | +// |
| 3 | +// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-app-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package bricks |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/arduino/go-paths-helper" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "go.bug.st/f" |
| 24 | + |
| 25 | + "github.com/arduino/arduino-app-cli/internal/orchestrator/app" |
| 26 | + "github.com/arduino/arduino-app-cli/internal/orchestrator/bricksindex" |
| 27 | +) |
| 28 | + |
| 29 | +func TestBrickCreate(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", func(t *testing.T) { |
| 35 | + err = brickService.BrickCreate(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", err.Error()) |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("fails if the requestes variable is not present in the brick definition", func(t *testing.T) { |
| 41 | + req := BrickCreateUpdateRequest{ID: "arduino:arduino_cloud", Variables: map[string]string{ |
| 42 | + "NON_EXISTING_VARIABLE": "some-value", |
| 43 | + }} |
| 44 | + err = brickService.BrickCreate(req, f.Must(app.Load("testdata/dummy-app"))) |
| 45 | + require.Error(t, err) |
| 46 | + require.Equal(t, "variable \"NON_EXISTING_VARIABLE\" does not exist on brick \"arduino:arduino_cloud\"", err.Error()) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("fails if a required variable is set empty", func(t *testing.T) { |
| 50 | + req := BrickCreateUpdateRequest{ID: "arduino:arduino_cloud", Variables: map[string]string{ |
| 51 | + "ARDUINO_DEVICE_ID": "", |
| 52 | + "ARDUINO_SECRET": "a-secret-a", |
| 53 | + }} |
| 54 | + err = brickService.BrickCreate(req, f.Must(app.Load("testdata/dummy-app"))) |
| 55 | + require.Error(t, err) |
| 56 | + require.Equal(t, "variable \"ARDUINO_DEVICE_ID\" cannot be empty", err.Error()) |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("fails if a mandatory variable is not present in the request", func(t *testing.T) { |
| 60 | + req := BrickCreateUpdateRequest{ID: "arduino:arduino_cloud", Variables: map[string]string{ |
| 61 | + "ARDUINO_SECRET": "a-secret-a", |
| 62 | + }} |
| 63 | + err = brickService.BrickCreate(req, f.Must(app.Load("testdata/dummy-app"))) |
| 64 | + require.Error(t, err) |
| 65 | + require.Equal(t, "required variable \"ARDUINO_DEVICE_ID\" is mandatory", err.Error()) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("the brick is added if it does not exist in the app", func(t *testing.T) { |
| 69 | + tempDummyApp := paths.New("testdata/dummy-app.temp") |
| 70 | + err := tempDummyApp.RemoveAll() |
| 71 | + require.Nil(t, err) |
| 72 | + require.Nil(t, paths.New("testdata/dummy-app").CopyDirTo(tempDummyApp)) |
| 73 | + |
| 74 | + req := BrickCreateUpdateRequest{ID: "arduino:dbstorage_sqlstore"} |
| 75 | + err = brickService.BrickCreate(req, f.Must(app.Load(tempDummyApp.String()))) |
| 76 | + require.Nil(t, err) |
| 77 | + after, err := app.Load(tempDummyApp.String()) |
| 78 | + require.Nil(t, err) |
| 79 | + require.Len(t, after.Descriptor.Bricks, 2) |
| 80 | + require.Equal(t, "arduino:dbstorage_sqlstore", after.Descriptor.Bricks[1].ID) |
| 81 | + }) |
| 82 | + t.Run("the variables of a brick are updated", func(t *testing.T) { |
| 83 | + tempDummyApp := paths.New("testdata/dummy-app.brick-override.temp") |
| 84 | + err := tempDummyApp.RemoveAll() |
| 85 | + require.Nil(t, err) |
| 86 | + err = paths.New("testdata/dummy-app").CopyDirTo(tempDummyApp) |
| 87 | + require.Nil(t, err) |
| 88 | + bricksIndex, err := bricksindex.GenerateBricksIndexFromFile(paths.New("testdata")) |
| 89 | + require.Nil(t, err) |
| 90 | + brickService := NewService(nil, bricksIndex, nil) |
| 91 | + |
| 92 | + deviceID := "this-is-a-device-id" |
| 93 | + secret := "this-is-a-secret" |
| 94 | + req := BrickCreateUpdateRequest{ |
| 95 | + ID: "arduino:arduino_cloud", |
| 96 | + Variables: map[string]string{ |
| 97 | + "ARDUINO_DEVICE_ID": deviceID, |
| 98 | + "ARDUINO_SECRET": secret, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + err = brickService.BrickCreate(req, f.Must(app.Load(tempDummyApp.String()))) |
| 103 | + require.Nil(t, err) |
| 104 | + |
| 105 | + after, err := app.Load(tempDummyApp.String()) |
| 106 | + require.Nil(t, err) |
| 107 | + require.Len(t, after.Descriptor.Bricks, 1) |
| 108 | + require.Equal(t, "arduino:arduino_cloud", after.Descriptor.Bricks[0].ID) |
| 109 | + require.Equal(t, deviceID, after.Descriptor.Bricks[0].Variables["ARDUINO_DEVICE_ID"]) |
| 110 | + require.Equal(t, secret, after.Descriptor.Bricks[0].Variables["ARDUINO_SECRET"]) |
| 111 | + }) |
| 112 | +} |
0 commit comments