@@ -11,7 +11,7 @@ import (
1111 "github.com/bcmi-labs/orchestrator/internal/orchestrator/bricksindex"
1212 "github.com/bcmi-labs/orchestrator/internal/store"
1313
14- yaml "github.com/goccy/go-yaml"
14+ "github.com/goccy/go-yaml"
1515
1616 "github.com/stretchr/testify/require"
1717)
@@ -251,3 +251,156 @@ services:
251251 })
252252
253253}
254+
255+ func TestProvisionAppWithDependsOn (t * testing.T ) {
256+ cfg := setTestOrchestratorConfig (t )
257+ staticStore := store .NewStaticStore (cfg .AssetsDir ().String ())
258+ tempDirectory := t .TempDir ()
259+ var env = map [string ]string {}
260+ type services struct {
261+ Services map [string ]struct {
262+ Image string `yaml:"image"`
263+ DependsOn map [string ]struct {
264+ Condition string `yaml:"condition"`
265+ } `yaml:"depends_on"`
266+ } `yaml:"services"`
267+ }
268+
269+ bricksIndexContent := []byte (`
270+ bricks:
271+ - id: arduino:dbstorage_tsstore
272+ name: Database Storage - Time Series Store
273+ description: Simplified time series database storage layer for Arduino sensor samples
274+ built on top of InfluxDB.
275+ require_container: true
276+ require_model: false
277+ ports: []
278+ category: storage
279+ variables:
280+ - name: APP_HOME
281+ default_value: .` )
282+ err := cfg .AssetsDir ().Join ("bricks-list.yaml" ).WriteFile (bricksIndexContent )
283+ require .NoError (t , err )
284+
285+ bricksIndex , err := bricksindex .GenerateBricksIndexFromFile (cfg .AssetsDir ())
286+ require .Nil (t , err , "Failed to load bricks index with custom content" )
287+ br , ok := bricksIndex .FindBrickByID ("arduino:dbstorage_tsstore" )
288+ require .True (t , ok , "Brick arduino:dbstorage_tsstore should exist in the index" )
289+ require .NotNil (t , br , "Brick arduino:dbstorage_tsstore should not be nil" )
290+ require .Equal (t , "Database Storage - Time Series Store" , br .Name , "Brick name should match" )
291+
292+ app := app.ArduinoApp {
293+ Name : "TestApp" ,
294+ Descriptor : app.AppDescriptor {
295+ Bricks : []app.Brick {
296+ {
297+ ID : "arduino:dbstorage_tsstore" ,
298+ },
299+ },
300+ },
301+ FullPath : paths .New (tempDirectory ),
302+ }
303+ require .NoError (t , app .ProvisioningStateDir ().MkdirAll ())
304+
305+ t .Run ("services with healthcheck" , func (t * testing.T ) {
306+ fileComposePath := cfg .AssetsDir ().Join ("compose" , "arduino" , "dbstorage_tsstore" )
307+ require .NoError (t , fileComposePath .MkdirAll ())
308+ dependsOnFromStrings := `
309+ services:
310+ dbstorage-influx:
311+ image: influxdb:2.7
312+ ports:
313+ - "${BIND_ADDRESS:-127.0.0.1}:${BIND_PORT:-8086}:8086"
314+ volumes:
315+ - "${APP_HOME:-.}/data/influx-data:/var/lib/influxdb2"
316+ environment:
317+ DOCKER_INFLUXDB_INIT_MODE: setup
318+ healthcheck:
319+ test: ["CMD", "curl", "-f", "http://localhost:8086/health"]`
320+ err := fileComposePath .Join ("brick_compose.yaml" ).WriteFile ([]byte (dependsOnFromStrings ))
321+ require .NoError (t , err )
322+
323+ // Run the provision function to generate the main compose file
324+ err = generateMainComposeFile (& app , bricksIndex , "app-bricks:python-apps-base:dev-latest" , cfg , env , staticStore )
325+ require .NoError (t , err , "Failed to generate main compose file" )
326+ composeFilePath := paths .New (tempDirectory ).Join (".cache" ).Join ("app-compose.yaml" )
327+ require .True (t , composeFilePath .Exist (), "Main compose file should exist" )
328+
329+ // Open main compose file and check for the expected depends_on with service_healthy
330+ mainComposeFileContent , err := composeFilePath .ReadFile ()
331+ require .Nil (t , err , "Failed to read compose file" )
332+ var content services
333+ err = yaml .Unmarshal (mainComposeFileContent , & content )
334+ require .Nil (t , err , "Failed to unmarshal overrides content" )
335+ exp := services {
336+ Services : map [string ]struct {
337+ Image string `yaml:"image"`
338+ DependsOn map [string ]struct {
339+ Condition string `yaml:"condition"`
340+ } `yaml:"depends_on"`
341+ }{
342+ "main" : {
343+ Image : "app-bricks:python-apps-base:dev-latest" ,
344+ DependsOn : map [string ]struct {
345+ Condition string `yaml:"condition"`
346+ }{
347+ "dbstorage-influx" : {
348+ Condition : "service_healthy" ,
349+ },
350+ },
351+ },
352+ },
353+ }
354+ require .Equal (t , exp , content , "Main compose content should match the expected structure" )
355+ })
356+
357+ t .Run ("services without healthcheck" , func (t * testing.T ) {
358+ fileComposePath := cfg .AssetsDir ().Join ("compose" , "arduino" , "dbstorage_tsstore" )
359+ require .NoError (t , fileComposePath .MkdirAll ())
360+ dependsOnFromStrings := `
361+ services:
362+ dbstorage-influx:
363+ image: influxdb:2.7
364+ ports:
365+ - "${BIND_ADDRESS:-127.0.0.1}:${BIND_PORT:-8086}:8086"
366+ volumes:
367+ - "${APP_HOME:-.}/data/influx-data:/var/lib/influxdb2"
368+ environment:
369+ DOCKER_INFLUXDB_INIT_MODE: setup`
370+ err = fileComposePath .Join ("brick_compose.yaml" ).WriteFile ([]byte (dependsOnFromStrings ))
371+ require .NoError (t , err )
372+
373+ // Run the provision function to generate the main compose file
374+ err = generateMainComposeFile (& app , bricksIndex , "app-bricks:python-apps-base:dev-latest" , cfg , env , staticStore )
375+ require .NoError (t , err , "Failed to generate main compose file" )
376+ composeFilePath := paths .New (tempDirectory ).Join (".cache" ).Join ("app-compose.yaml" )
377+ require .True (t , composeFilePath .Exist (), "Main compose file should exist" )
378+
379+ // Open main compose file and check for the expected depends_on with service_started
380+ mainComposeFileContent , err := composeFilePath .ReadFile ()
381+ require .Nil (t , err , "Failed to read compose file" )
382+ var content services
383+ err = yaml .Unmarshal (mainComposeFileContent , & content )
384+ require .Nil (t , err , "Failed to unmarshal overrides content" )
385+ exp := services {
386+ Services : map [string ]struct {
387+ Image string `yaml:"image"`
388+ DependsOn map [string ]struct {
389+ Condition string `yaml:"condition"`
390+ } `yaml:"depends_on"`
391+ }{
392+ "main" : {
393+ Image : "app-bricks:python-apps-base:dev-latest" ,
394+ DependsOn : map [string ]struct {
395+ Condition string `yaml:"condition"`
396+ }{
397+ "dbstorage-influx" : {
398+ Condition : "service_started" ,
399+ },
400+ },
401+ },
402+ },
403+ }
404+ require .Equal (t , exp , content , "Main compose content should match the expected structure" )
405+ })
406+ }
0 commit comments