Skip to content

Commit 4b03f55

Browse files
Support new files|volumes spec with score-go v1.10.0 (#302)
Signed-off-by: Mathieu Benoit <mathieu-benoit@hotmail.fr> Co-authored-by: Ben Meier <1651305+astromechza@users.noreply.github.com>
1 parent bc18611 commit 4b03f55

9 files changed

Lines changed: 255 additions & 87 deletions

File tree

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Disable all the default make stuff
2+
MAKEFLAGS += --no-builtin-rules
3+
.SUFFIXES:
4+
5+
## Display a list of the documented make targets
6+
.PHONY: help
7+
help:
8+
@echo Documented Make targets:
9+
@perl -e 'undef $$/; while (<>) { while ($$_ =~ /## (.*?)(?:\n# .*)*\n.PHONY:\s+(\S+).*/mg) { printf "\033[36m%-30s\033[0m %s\n", $$2, $$1 } }' $(MAKEFILE_LIST) | sort
10+
11+
.PHONY: .FORCE
12+
.FORCE:
13+
14+
build:
15+
go build ./cmd/score-compose/
16+
17+
test:
18+
go vet ./...
19+
go test ./... -cover -race

examples/03-files/score.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ containers:
99
command: ["/bin/sh"]
1010
args: ["-c", "while true; do cat /fileA.txt; cat /fileB.txt; cat /fileC.bin; sleep 5; done"]
1111
files:
12-
- target: /fileA.txt
12+
"/fileA.txt":
1313
source: fileA.txt
14-
- target: /fileB.txt
14+
"/fileB.txt":
1515
content: |
1616
I am ${metadata.name}
17-
- target: /fileC.bin
17+
"/fileC.bin":
1818
binaryContent: aGVsbG8gd29ybGQ=

examples/05-volume-mounts/score.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ containers:
77
first:
88
image: "nginx:latest"
99
volumes:
10-
- target: /data
10+
"/data":
1111
source: ${resources.data}
1212

1313
resources:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/Masterminds/sprig/v3 v3.3.0
88
github.com/compose-spec/compose-go/v2 v2.6.0
99
github.com/mitchellh/mapstructure v1.5.0
10-
github.com/score-spec/score-go v1.9.7
10+
github.com/score-spec/score-go v1.10.0
1111
github.com/spf13/cobra v1.9.1
1212
github.com/spf13/pflag v1.0.6
1313
github.com/stretchr/testify v1.10.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
6161
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
6262
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
6363
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
64-
github.com/score-spec/score-go v1.9.7 h1:2afe1vSHIWafRcEc3484U9nGWPo6Fmr0T97ypjvMSLs=
65-
github.com/score-spec/score-go v1.9.7/go.mod h1:uvlaGT8wvBujAqU0aU2Kx3gFrq8owBGjjeUjWNI7/zY=
64+
github.com/score-spec/score-go v1.10.0 h1:ukWV2Df41uAYvLbVdRS1njmbedMCtp80zd6Pio3Wy60=
65+
github.com/score-spec/score-go v1.10.0/go.mod h1:bEtNhlGekZaNuWI11n15V5ibcX+E0Inry+TC6NWTuFI=
6666
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
6767
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
6868
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=

internal/command/generate_examples_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ services:
9494
image: busybox
9595
volumes:
9696
- type: bind
97-
source: .score-compose/mounts/files/hello-world-files-0-fileA.txt
97+
source: .score-compose/mounts/files/hello-world-files-fileA.txt
9898
target: /fileA.txt
9999
- type: bind
100-
source: .score-compose/mounts/files/hello-world-files-1-fileB.txt
100+
source: .score-compose/mounts/files/hello-world-files-fileB.txt
101101
target: /fileB.txt
102102
- type: bind
103-
source: .score-compose/mounts/files/hello-world-files-2-fileC.bin
103+
source: .score-compose/mounts/files/hello-world-files-fileC.bin
104104
target: /fileC.bin
105105
`,
106106
},

internal/command/generate_test.go

Lines changed: 153 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
11181266
func TestGenerateMongodbResource(t *testing.T) {
11191267
td := changeToTempDir(t)
11201268
stdout, _, err := executeAndResetCommand(context.Background(), rootCmd, []string{"init"})

0 commit comments

Comments
 (0)