project.Services is a map

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof
2023-11-27 10:14:31 +01:00
committed by Nicolas De loof
parent cda04f288e
commit 138facea62
25 changed files with 120 additions and 124 deletions

View File

@@ -23,7 +23,6 @@ import (
"strings"
"github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/loader"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/command"
cliopts "github.com/docker/cli/opts"

View File

@@ -26,20 +26,20 @@ import (
func TestFilterServices(t *testing.T) {
p := &types.Project{
Services: types.Services{
{
"foo": {
Name: "foo",
Links: []string{"bar"},
},
{
"bar": {
Name: "bar",
DependsOn: map[string]types.ServiceDependency{
"zot": {},
},
},
{
"zot": {
Name: "zot",
},
{
"qix": {
Name: "qix",
},
},

View File

@@ -86,7 +86,7 @@ func sampleProject() *types.Project {
return &types.Project{
Name: "test",
Services: types.Services{
{
"svc": {
Name: "svc",
Build: &types.BuildConfig{
Context: ".",

View File

@@ -25,10 +25,7 @@ import (
func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
defaultPlatform := project.Environment["DOCKER_DEFAULT_PLATFORM"]
for i := range project.Services {
// mutable reference so platform fields can be updated
service := &project.Services[i]
for _, service := range project.Services {
if service.Build == nil {
continue
}

View File

@@ -27,7 +27,7 @@ func TestApplyPlatforms_InferFromRuntime(t *testing.T) {
makeProject := func() *types.Project {
return &types.Project{
Services: types.Services{
{
"test": {
Name: "test",
Image: "foo",
Build: &types.BuildConfig{
@@ -47,14 +47,14 @@ func TestApplyPlatforms_InferFromRuntime(t *testing.T) {
t.Run("SinglePlatform", func(t *testing.T) {
project := makeProject()
require.NoError(t, applyPlatforms(project, true))
require.EqualValues(t, []string{"alice/32"}, project.Services[0].Build.Platforms)
require.EqualValues(t, []string{"alice/32"}, project.Services["test"].Build.Platforms)
})
t.Run("MultiPlatform", func(t *testing.T) {
project := makeProject()
require.NoError(t, applyPlatforms(project, false))
require.EqualValues(t, []string{"linux/amd64", "linux/arm64", "alice/32"},
project.Services[0].Build.Platforms)
project.Services["test"].Build.Platforms)
})
}
@@ -65,7 +65,7 @@ func TestApplyPlatforms_DockerDefaultPlatform(t *testing.T) {
"DOCKER_DEFAULT_PLATFORM": "linux/amd64",
},
Services: types.Services{
{
"test": {
Name: "test",
Image: "foo",
Build: &types.BuildConfig{
@@ -83,14 +83,14 @@ func TestApplyPlatforms_DockerDefaultPlatform(t *testing.T) {
t.Run("SinglePlatform", func(t *testing.T) {
project := makeProject()
require.NoError(t, applyPlatforms(project, true))
require.EqualValues(t, []string{"linux/amd64"}, project.Services[0].Build.Platforms)
require.EqualValues(t, []string{"linux/amd64"}, project.Services["test"].Build.Platforms)
})
t.Run("MultiPlatform", func(t *testing.T) {
project := makeProject()
require.NoError(t, applyPlatforms(project, false))
require.EqualValues(t, []string{"linux/amd64", "linux/arm64"},
project.Services[0].Build.Platforms)
project.Services["test"].Build.Platforms)
})
}
@@ -101,7 +101,7 @@ func TestApplyPlatforms_UnsupportedPlatform(t *testing.T) {
"DOCKER_DEFAULT_PLATFORM": "commodore/64",
},
Services: types.Services{
{
"foo": {
Name: "test",
Image: "foo",
Build: &types.BuildConfig{

View File

@@ -26,21 +26,21 @@ import (
func TestApplyPullOptions(t *testing.T) {
project := &types.Project{
Services: types.Services{
{
"must-build": {
Name: "must-build",
// No image, local build only
Build: &types.BuildConfig{
Context: ".",
},
},
{
"has-build": {
Name: "has-build",
Image: "registry.example.com/myservice",
Build: &types.BuildConfig{
Context: ".",
},
},
{
"must-pull": {
Name: "must-pull",
Image: "registry.example.com/another-service",
},
@@ -51,7 +51,7 @@ func TestApplyPullOptions(t *testing.T) {
}.apply(project, nil)
assert.NilError(t, err)
assert.Equal(t, project.Services[0].PullPolicy, "") // still default
assert.Equal(t, project.Services[1].PullPolicy, types.PullPolicyMissing)
assert.Equal(t, project.Services[2].PullPolicy, types.PullPolicyMissing)
assert.Equal(t, project.Services["must-build"].PullPolicy, "") // still default
assert.Equal(t, project.Services["has-build"].PullPolicy, types.PullPolicyMissing)
assert.Equal(t, project.Services["must-pull"].PullPolicy, types.PullPolicyMissing)
}

View File

@@ -300,16 +300,16 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
func startDependencies(ctx context.Context, backend api.Service, project types.Project, buildOpts *api.BuildOptions, requestedServiceName string, ignoreOrphans bool) error {
dependencies := types.Services{}
var requestedService types.ServiceConfig
for _, service := range project.Services {
for name, service := range project.Services {
if service.Name != requestedServiceName {
dependencies = append(dependencies, service)
dependencies[name] = service
} else {
requestedService = service
}
}
project.Services = dependencies
project.DisabledServices = append(project.DisabledServices, requestedService)
project.DisabledServices[requestedServiceName] = requestedService
err := backend.Create(ctx, &project, api.CreateOptions{
Build: buildOpts,
IgnoreOrphans: ignoreOrphans,

View File

@@ -26,10 +26,10 @@ import (
func TestApplyScaleOpt(t *testing.T) {
p := types.Project{
Services: types.Services{
{
"foo": {
Name: "foo",
},
{
"bar": {
Name: "bar",
Deploy: &types.DeployConfig{
Mode: "test",