@@ -329,7 +329,7 @@ services:
329329
330330}
331331
332- func TestInitAndGenerate_with_files (t * testing.T ) {
332+ func TestInitAndGenerate_with_files_old_files_spec (t * testing.T ) {
333333 td := changeToTempDir (t )
334334 stdout , _ , err := executeAndResetCommand (context .Background (), rootCmd , []string {"init" })
335335 assert .NoError (t , err )
@@ -360,7 +360,43 @@ services:
360360 image: foo
361361 volumes:
362362 - type: bind
363- source: .score-compose/mounts/files/example-files-0-blah.txt
363+ source: .score-compose/mounts/files/example-files-blah.txt
364+ target: /blah.txt
365+ ` , string (raw ))
366+ }
367+
368+ func TestInitAndGenerate_with_files_new_files_spec (t * testing.T ) {
369+ td := changeToTempDir (t )
370+ stdout , _ , err := executeAndResetCommand (context .Background (), rootCmd , []string {"init" })
371+ assert .NoError (t , err )
372+ assert .Equal (t , "" , stdout )
373+ assert .NoError (t , os .WriteFile (filepath .Join (td , "score.yaml" ), []byte (`
374+ apiVersion: score.dev/v1b1
375+ metadata:
376+ name: example
377+ containers:
378+ example:
379+ image: foo
380+ files:
381+ "/blah.txt":
382+ source: ./original.txt
383+ ` ), 0644 ))
384+ assert .NoError (t , os .WriteFile (filepath .Join (td , "original.txt" ), []byte (`first ${metadata.name} second` ), 0644 ))
385+ stdout , _ , err = executeAndResetCommand (context .Background (), rootCmd , []string {"generate" , "score.yaml" })
386+ assert .NoError (t , err )
387+ assert .Equal (t , "" , stdout )
388+ raw , err := os .ReadFile (filepath .Join (td , "compose.yaml" ))
389+ assert .NoError (t , err )
390+ assert .Equal (t , `name: "001"
391+ services:
392+ example-example:
393+ annotations:
394+ compose.score.dev/workload-name: example
395+ hostname: example
396+ image: foo
397+ volumes:
398+ - type: bind
399+ source: .score-compose/mounts/files/example-files-blah.txt
364400 target: /blah.txt
365401` , string (raw ))
366402}
@@ -976,7 +1012,7 @@ services:
9761012` , string (raw ))
9771013}
9781014
979- func TestEnvVarsMustResolveInsideFiles (t * testing.T ) {
1015+ func TestEnvVarsMustResolveInsideFiles_old_files_spec (t * testing.T ) {
9801016 td := changeToTempDir (t )
9811017 stdout , _ , err := executeAndResetCommand (context .Background (), rootCmd , []string {"init" })
9821018 assert .NoError (t , err )
@@ -996,7 +1032,33 @@ resources:
9961032 type: environment
9971033` ), 0644 ))
9981034 _ , _ , err = executeAndResetCommand (context .Background (), rootCmd , []string {"generate" , "score.yaml" })
999- assert .EqualError (t , err , "failed to convert workload 'example' to Docker compose: containers.example.files[0]: " +
1035+ assert .EqualError (t , err , "failed to convert workload 'example' to Docker compose: containers.example.files[/some/file]: " +
1036+ "failed to substitute in content: invalid ref 'resources.env.UNKNOWN_SCORE_VARIABLE': " +
1037+ "environment variable 'UNKNOWN_SCORE_VARIABLE' must be resolved" ,
1038+ )
1039+ }
1040+
1041+ func TestEnvVarsMustResolveInsideFiles_new_files_spec (t * testing.T ) {
1042+ td := changeToTempDir (t )
1043+ stdout , _ , err := executeAndResetCommand (context .Background (), rootCmd , []string {"init" })
1044+ assert .NoError (t , err )
1045+ assert .Equal (t , "" , stdout )
1046+ assert .NoError (t , os .WriteFile (filepath .Join (td , "score.yaml" ), []byte (`
1047+ apiVersion: score.dev/v1b1
1048+ metadata:
1049+ name: example
1050+ containers:
1051+ example:
1052+ image: foo
1053+ files:
1054+ "/some/file":
1055+ content: ${resources.env.UNKNOWN_SCORE_VARIABLE}
1056+ resources:
1057+ env:
1058+ type: environment
1059+ ` ), 0644 ))
1060+ _ , _ , err = executeAndResetCommand (context .Background (), rootCmd , []string {"generate" , "score.yaml" })
1061+ assert .EqualError (t , err , "failed to convert workload 'example' to Docker compose: containers.example.files[/some/file]: " +
10001062 "failed to substitute in content: invalid ref 'resources.env.UNKNOWN_SCORE_VARIABLE': " +
10011063 "environment variable 'UNKNOWN_SCORE_VARIABLE' must be resolved" ,
10021064 )
@@ -1029,7 +1091,7 @@ resources:
10291091 )
10301092}
10311093
1032- func TestInitAndGenerate_with_volume_types (t * testing.T ) {
1094+ func TestInitAndGenerate_with_volume_types_with_old_volumes_spec (t * testing.T ) {
10331095 td := changeToTempDir (t )
10341096 stdout , _ , err := executeAndResetCommand (context .Background (), rootCmd , []string {"init" })
10351097 assert .NoError (t , err )
@@ -1115,6 +1177,92 @@ services:
11151177` , string (raw ))
11161178}
11171179
1180+ func TestInitAndGenerate_with_volume_types_with_new_volumes_spec (t * testing.T ) {
1181+ td := changeToTempDir (t )
1182+ stdout , _ , err := executeAndResetCommand (context .Background (), rootCmd , []string {"init" })
1183+ assert .NoError (t , err )
1184+ assert .Equal (t , "" , stdout )
1185+
1186+ // write custom providers
1187+ assert .NoError (t , os .WriteFile (filepath .Join (td , ".score-compose" , "00-custom.provisioners.yaml" ), []byte (`
1188+ - uri: template://docker-volume
1189+ type: volume
1190+ outputs: |
1191+ type: volume
1192+ source: named-volume
1193+ - uri: template://tmpfs-volume
1194+ type: tmp-volume
1195+ outputs: |
1196+ type: tmpfs
1197+ tmpfs:
1198+ size: 10000000
1199+ - uri: template://bind-volume
1200+ type: bind-volume
1201+ outputs: |
1202+ type: bind
1203+ source: /dev/something
1204+ bind:
1205+ create_host_path: true
1206+ ` ), 0644 ))
1207+
1208+ // write custom score file
1209+ assert .NoError (t , os .WriteFile (filepath .Join (td , "score.yaml" ), []byte (`
1210+ apiVersion: score.dev/v1b1
1211+ metadata:
1212+ name: example
1213+ containers:
1214+ example:
1215+ image: busybox
1216+ volumes:
1217+ "/mnt/v1":
1218+ source: ${resources.v1}
1219+ "/mnt/v2":
1220+ source: ${resources.v2}
1221+ path: thing
1222+ "/mnt/v3":
1223+ source: ${resources.v3}
1224+ path: other/thing
1225+ resources:
1226+ v1:
1227+ type: tmp-volume
1228+ v2:
1229+ type: bind-volume
1230+ v3:
1231+ type: volume
1232+ ` ), 0644 ))
1233+
1234+ // generate
1235+ stdout , _ , err = executeAndResetCommand (context .Background (), rootCmd , []string {"generate" , "score.yaml" })
1236+ assert .NoError (t , err )
1237+ assert .Equal (t , "" , stdout )
1238+ raw , err := os .ReadFile (filepath .Join (td , "compose.yaml" ))
1239+ assert .NoError (t , err )
1240+ assert .Equal (t , `name: "001"
1241+ services:
1242+ example-example:
1243+ annotations:
1244+ compose.score.dev/workload-name: example
1245+ hostname: example
1246+ image: busybox
1247+ volumes:
1248+ - type: bind
1249+ source: /dev/something/thing
1250+ target: /mnt/v2
1251+ bind:
1252+ create_host_path: true
1253+ - type: volume
1254+ source: named-volume
1255+ target: /mnt/v3
1256+ volume:
1257+ subpath: other/thing
1258+ - type: tmpfs
1259+ source: tmp-volume.default#example.v1
1260+ target: /mnt/v1
1261+ tmpfs:
1262+ size: "10000000"
1263+ ` , string (raw ))
1264+ }
1265+
11181266func TestGenerateMongodbResource (t * testing.T ) {
11191267 td := changeToTempDir (t )
11201268 stdout , _ , err := executeAndResetCommand (context .Background (), rootCmd , []string {"init" })
0 commit comments