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