From 405a0258f1f85d9b4159e35c0ce5fc054edd1a5d Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sun, 27 Aug 2023 19:12:00 +0200 Subject: [PATCH] fix(dockerCompose): add ServiceNetwork.MergeFirstWin() --- pkg/domain/dockerCompose/config.go | 33 +++++++++++++- pkg/domain/dockerCompose/config_test.go | 59 +++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/pkg/domain/dockerCompose/config.go b/pkg/domain/dockerCompose/config.go index 11dbd7f..719d42f 100644 --- a/pkg/domain/dockerCompose/config.go +++ b/pkg/domain/dockerCompose/config.go @@ -939,6 +939,29 @@ func (sn *ServiceNetwork) Equal(equalable Equalable) bool { } } +// MergeFirstWin adds only attributes of the passed +// serviceNetwork if they are undefined. +func (sn *ServiceNetwork) MergeFirstWin(serviceNetwork *ServiceNetwork) { + switch { + case sn == nil && serviceNetwork == nil: + fallthrough + case sn != nil && serviceNetwork == nil: + return + + // WARN: It's not possible to change the memory pointer sn *ServiceNetwork to a new + // initialized ServiceNetwork without returning the serviceNetwork it self. + // + // case l == nil && serviceULimits != nil: + // l = NewServiceULimits() + // fallthrough + + case sn == nil && serviceNetwork != nil: + sn = serviceNetwork + default: + sn.mergeFirstWinAliases(serviceNetwork.Aliases) + } +} + // MergeLastWin merges adds or overwrite the attributes of the passed // serviceNetwork with the existing one. func (sn *ServiceNetwork) MergeLastWin(serviceNetwork *ServiceNetwork) { @@ -963,9 +986,17 @@ func (sn *ServiceNetwork) MergeLastWin(serviceNetwork *ServiceNetwork) { } } +func (sn *ServiceNetwork) mergeFirstWinAliases(aliases []string) { + for _, alias := range aliases { + if !existsInSlice(sn.Aliases, alias) && len(alias) > 0 { + sn.Aliases = append(sn.Aliases, alias) + } + } +} + func (sn *ServiceNetwork) mergeLastWinAliases(aliases []string) { for _, alias := range aliases { - if !existsInSlice(sn.Aliases, alias) { + if !existsInSlice(sn.Aliases, alias) && len(alias) > 0 { sn.Aliases = append(sn.Aliases, alias) } } diff --git a/pkg/domain/dockerCompose/config_test.go b/pkg/domain/dockerCompose/config_test.go index 84dfdef..54bcfd0 100644 --- a/pkg/domain/dockerCompose/config_test.go +++ b/pkg/domain/dockerCompose/config_test.go @@ -1736,6 +1736,65 @@ func TestServiceNetwork_Equal(t *testing.T) { } } +func TestServiceNetwork_MergeFirstWin(t *testing.T) { + require := require.New(t) + + testCases := []struct { + ServiceNetworkA *dockerCompose.ServiceNetwork + ServiceNetworkB *dockerCompose.ServiceNetwork + expectedServiceNetwork *dockerCompose.ServiceNetwork + }{ + { + ServiceNetworkA: nil, + ServiceNetworkB: nil, + expectedServiceNetwork: nil, + }, + { + ServiceNetworkA: &dockerCompose.ServiceNetwork{}, + ServiceNetworkB: nil, + expectedServiceNetwork: &dockerCompose.ServiceNetwork{}, + }, + { + ServiceNetworkA: &dockerCompose.ServiceNetwork{ + Aliases: []string{"my-app.example.com"}, + }, + ServiceNetworkB: &dockerCompose.ServiceNetwork{ + Aliases: []string{"my-app.example.com"}, + }, + expectedServiceNetwork: &dockerCompose.ServiceNetwork{ + Aliases: []string{"my-app.example.com"}, + }, + }, + { + ServiceNetworkA: &dockerCompose.ServiceNetwork{ + Aliases: []string{"my-app.example.com"}, + }, + ServiceNetworkB: &dockerCompose.ServiceNetwork{ + Aliases: []string{"my-app.example.local"}, + }, + expectedServiceNetwork: &dockerCompose.ServiceNetwork{ + Aliases: []string{"my-app.example.com", "my-app.example.local"}, + }, + }, + { + ServiceNetworkA: &dockerCompose.ServiceNetwork{ + Aliases: []string{"my-app.example.com"}, + }, + ServiceNetworkB: &dockerCompose.ServiceNetwork{ + Aliases: []string{""}, + }, + expectedServiceNetwork: &dockerCompose.ServiceNetwork{ + Aliases: []string{"my-app.example.com"}, + }, + }, + } + + for i, testCase := range testCases { + testCase.ServiceNetworkA.MergeFirstWin(testCase.ServiceNetworkB) + require.True(testCase.expectedServiceNetwork.Equal(testCase.ServiceNetworkA), "Failed test case %v", i) + } +} + func TestServiceNetwork_MergeLastWin(t *testing.T) { require := require.New(t)