Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/compose/pre_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (s *composeService) createPreStartContainer(
AutoRemove: true,
Privileged: hook.Privileged,
VolumesFrom: []string{ctr.ID},
ExtraHosts: service.ExtraHosts.AsList(":"),
}

apiVersion, err := s.RuntimeAPIVersion(ctx)
Expand Down
34 changes: 34 additions & 0 deletions pkg/compose/pre_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,40 @@ func TestPreStart_VolumesFromServiceContainer(t *testing.T) {
assert.Assert(t, gotAutoRemove)
}

func TestPreStart_ExtraHostsPassedToContainer(t *testing.T) {
tested, apiClient := newPreStartTestService(t)

project := &types.Project{Name: "demo"}
service := types.ServiceConfig{
Name: "web",
Image: "alpine",
ExtraHosts: types.HostsList{
"somehost": {"162.242.195.82"},
},
PreStart: []types.ServiceHook{
{Image: "alpine", Command: types.ShellCommand{"true"}},
},
}
ctr := container.Summary{ID: "service-ctr-id"}

var gotExtraHosts []string
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ any, opts client.ContainerCreateOptions) (client.ContainerCreateResult, error) {
gotExtraHosts = opts.HostConfig.ExtraHosts
return client.ContainerCreateResult{ID: "hook-1"}, nil
})
apiClient.EXPECT().ContainerStart(gomock.Any(), "hook-1", gomock.Any()).
Return(client.ContainerStartResult{}, nil)
apiClient.EXPECT().ContainerLogs(gomock.Any(), "hook-1", gomock.Any()).
Return(emptyLogs(), nil)
apiClient.EXPECT().ContainerWait(gomock.Any(), "hook-1", gomock.Any()).
Return(waitResultExit(0))

err := tested.runPreStart(t.Context(), project, service, ctr, func(api.ContainerEvent) {})
assert.NilError(t, err)
assert.DeepEqual(t, gotExtraHosts, []string{"somehost:162.242.195.82"})
}

func TestPreStart_ContainerCreateFailurePropagates(t *testing.T) {
tested, apiClient := newPreStartTestService(t)

Expand Down