mirror of https://github.com/docker/compose.git
Remove duplicate compute of image name & add unit test
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
parent
c1405aeaa3
commit
7dd3a5a1eb
|
@ -36,7 +36,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project) erro
|
|||
opts := map[string]build.Options{}
|
||||
for _, service := range project.Services {
|
||||
if service.Build != nil {
|
||||
imageName := getImageName(service, project)
|
||||
imageName := getImageName(service, project.Name)
|
||||
opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName)
|
||||
}
|
||||
}
|
||||
|
@ -44,14 +44,6 @@ func (s *composeService) Build(ctx context.Context, project *types.Project) erro
|
|||
return s.build(ctx, project, opts)
|
||||
}
|
||||
|
||||
func getImageName(service types.ServiceConfig, project *types.Project) string {
|
||||
imageName := service.Image
|
||||
if imageName == "" {
|
||||
imageName = project.Name + "_" + service.Name
|
||||
}
|
||||
return imageName
|
||||
}
|
||||
|
||||
func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project) error {
|
||||
opts := map[string]build.Options{}
|
||||
for _, service := range project.Services {
|
||||
|
@ -59,7 +51,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
|
|||
return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
|
||||
}
|
||||
|
||||
imageName := getImageName(service, project)
|
||||
imageName := getImageName(service, project.Name)
|
||||
localImagePresent, err := s.localImagePresent(ctx, imageName)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -123,6 +123,14 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
|
|||
return nil
|
||||
}
|
||||
|
||||
func getImageName(service types.ServiceConfig, projectName string) string {
|
||||
imageName := service.Image
|
||||
if imageName == "" {
|
||||
imageName = projectName + "_" + service.Name
|
||||
}
|
||||
return imageName
|
||||
}
|
||||
|
||||
func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
|
||||
hash, err := jsonHash(s)
|
||||
if err != nil {
|
||||
|
@ -155,10 +163,6 @@ func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inher
|
|||
if len(s.Entrypoint) > 0 {
|
||||
entrypoint = strslice.StrSlice(s.Entrypoint)
|
||||
}
|
||||
image := s.Image
|
||||
if s.Image == "" {
|
||||
image = fmt.Sprintf("%s_%s", p.Name, s.Name)
|
||||
}
|
||||
|
||||
var (
|
||||
tty = s.Tty
|
||||
|
@ -178,7 +182,7 @@ func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inher
|
|||
AttachStderr: true,
|
||||
AttachStdout: true,
|
||||
Cmd: runCmd,
|
||||
Image: image,
|
||||
Image: getImageName(s, p.Name),
|
||||
WorkingDir: s.WorkingDir,
|
||||
Entrypoint: entrypoint,
|
||||
NetworkDisabled: s.NetworkMode == "disabled",
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/compose-spec/compose-go/types"
|
||||
composetypes "github.com/compose-spec/compose-go/types"
|
||||
mountTypes "github.com/docker/docker/api/types/mount"
|
||||
"gotest.tools/v3/assert"
|
||||
|
@ -60,3 +61,8 @@ func TestBuildVolumeMount(t *testing.T) {
|
|||
assert.Equal(t, mount.Source, "myProject_myVolume")
|
||||
assert.Equal(t, mount.Type, mountTypes.TypeVolume)
|
||||
}
|
||||
|
||||
func TestServiceImageName(t *testing.T) {
|
||||
assert.Equal(t, getImageName(types.ServiceConfig{Image: "myImage"}, "myProject"), "myImage")
|
||||
assert.Equal(t, getImageName(types.ServiceConfig{Name: "aService"}, "myProject"), "myProject_aService")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue