@@ -1125,37 +1125,88 @@ func TestUnsetOptionsEnv(t *testing.T) {
11251125func TestLifecycleScripts (t * testing.T ) {
11261126 t .Parallel ()
11271127
1128- // Ensures that a Git repository with a devcontainer.json is cloned and built.
1129- srv := gittest .CreateGitServer (t , gittest.Options {
1130- Files : map [string ]string {
1131- ".devcontainer/devcontainer.json" : `{
1132- "name": "Test",
1133- "build": {
1134- "dockerfile": "Dockerfile"
1135- },
1136- "onCreateCommand": "echo create > /tmp/out",
1137- "updateContentCommand": ["sh", "-c", "echo update >> /tmp/out"],
1138- "postCreateCommand": "(echo -n postCreate. ; id -un) >> /tmp/out",
1139- "postStartCommand": {
1140- "parallel1": "echo parallel1 > /tmp/parallel1",
1141- "parallel2": ["sh", "-c", "echo parallel2 > /tmp/parallel2"]
1142- }
1143- }` ,
1144- ".devcontainer/Dockerfile" : "FROM " + testImageAlpine + "\n USER nobody" ,
1128+ for _ , tt := range []struct {
1129+ name string
1130+ files map [string ]string
1131+ outputCmd string
1132+ expectOutput string
1133+ }{
1134+ {
1135+ name : "build" ,
1136+ files : map [string ]string {
1137+ ".devcontainer/devcontainer.json" : `{
1138+ "name": "Test",
1139+ "build": {
1140+ "dockerfile": "Dockerfile"
1141+ },
1142+ "onCreateCommand": "echo create > /tmp/out",
1143+ "updateContentCommand": ["sh", "-c", "echo update >> /tmp/out"],
1144+ "postCreateCommand": "(echo -n postCreate. ; id -un) >> /tmp/out",
1145+ "postStartCommand": {
1146+ "parallel1": "echo parallel1 > /tmp/parallel1",
1147+ "parallel2": ["sh", "-c", "echo parallel2 > /tmp/parallel2"]
1148+ }
1149+ }` ,
1150+ ".devcontainer/Dockerfile" : "FROM " + testImageAlpine + "\n USER nobody" ,
1151+ },
1152+ outputCmd : "cat /tmp/out /tmp/parallel1 /tmp/parallel2" ,
1153+ expectOutput : "create\n update\n postCreate.nobody\n parallel1\n parallel2" ,
11451154 },
1146- })
1147- ctr , err := runEnvbuilder (t , runOpts {env : []string {
1148- envbuilderEnv ("GIT_URL" , srv .URL ),
1149- }})
1150- require .NoError (t , err )
1151-
1152- output := execContainer (t , ctr , "cat /tmp/out /tmp/parallel1 /tmp/parallel2" )
1153- require .Equal (t ,
1154- `create
1155- update
1156- postCreate.nobody
1157- parallel1
1158- parallel2` , strings .TrimSpace (output ))
1155+ {
1156+ name : "image" ,
1157+ files : map [string ]string {
1158+ ".devcontainer/devcontainer.json" : fmt .Sprintf (`{
1159+ "name": "Test",
1160+ "image": %q,
1161+ "containerUser": "nobody",
1162+ "onCreateCommand": "echo create > /tmp/out",
1163+ "updateContentCommand": ["sh", "-c", "echo update >> /tmp/out"],
1164+ "postCreateCommand": "(echo -n postCreate. ; id -un) >> /tmp/out",
1165+ "postStartCommand": {
1166+ "parallel1": "echo parallel1 > /tmp/parallel1",
1167+ "parallel2": ["sh", "-c", "echo parallel2 > /tmp/parallel2"]
1168+ }
1169+ }` , testImageAlpine ),
1170+ },
1171+ outputCmd : "cat /tmp/out /tmp/parallel1 /tmp/parallel2" ,
1172+ expectOutput : "create\n update\n postCreate.nobody\n parallel1\n parallel2" ,
1173+ },
1174+ {
1175+ name : "label" ,
1176+ files : map [string ]string {
1177+ ".devcontainer/Dockerfile" : fmt .Sprintf (`FROM %s
1178+ LABEL devcontainer.metadata='[{ \
1179+ "onCreateCommand": "echo create > /tmp/out", \
1180+ "updateContentCommand": ["sh", "-c", "echo update >> /tmp/out"], \
1181+ "postCreateCommand": "(echo -n postCreate. ; id -un) >> /tmp/out", \
1182+ "postStartCommand": { \
1183+ "parallel1": "echo parallel1 > /tmp/parallel1", \
1184+ "parallel2": ["sh", "-c", "echo parallel2 > /tmp/parallel2"] \
1185+ } \
1186+ }]'
1187+ USER nobody` , testImageAlpine ),
1188+ },
1189+ outputCmd : "cat /tmp/out /tmp/parallel1 /tmp/parallel2" ,
1190+ expectOutput : "create\n update\n postCreate.nobody\n parallel1\n parallel2" ,
1191+ },
1192+ } {
1193+ tt := tt
1194+ t .Run (tt .name , func (t * testing.T ) {
1195+ srv := gittest .CreateGitServer (t , gittest.Options {
1196+ Files : tt .files ,
1197+ })
1198+ env := []string {
1199+ envbuilderEnv ("GIT_URL" , srv .URL ),
1200+ }
1201+ if _ , ok := tt .files [".devcontainer/devcontainer.json" ]; ! ok {
1202+ env = append (env , envbuilderEnv ("DOCKERFILE_PATH" , ".devcontainer/Dockerfile" ))
1203+ }
1204+ ctr , err := runEnvbuilder (t , runOpts {env : env })
1205+ require .NoError (t , err , "failed to run envbuilder" )
1206+ output := execContainer (t , ctr , tt .outputCmd )
1207+ require .Equal (t , tt .expectOutput , strings .TrimSpace (output ))
1208+ })
1209+ }
11591210}
11601211
11611212func TestPostStartScript (t * testing.T ) {
0 commit comments