fix(dockerCompose): add ServiceNetwork.MergeFirstWin()

This commit is contained in:
Markus Pesch 2023-08-27 19:12:00 +02:00
parent 462299d5cd
commit 405a0258f1
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 91 additions and 1 deletions

View File

@ -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)
}
}

View File

@ -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)