fix(dockerCompose): add ServiceUlimits.MergeFirstWin()

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

View File

@ -1002,6 +1002,28 @@ func (l *ServiceULimits) Equal(equalable Equalable) bool {
} }
} }
// MergeFirstWin adds only the attributes of the passed ServiceULimits they are
// undefined.
func (l *ServiceULimits) MergeFirstWin(serviceULimits *ServiceULimits) {
switch {
case l == nil && serviceULimits == nil:
fallthrough
case l != nil && serviceULimits == nil:
return
// WARN: It's not possible to change the memory pointer l *ServiceULimits to a new
// initialized ServiceULimits without returning the serviceULimits it self.
//
// case l == nil && serviceULimits != nil:
// l = NewServiceULimits()
// fallthrough
default:
l.mergeFirstWinNProc(serviceULimits.NProc)
l.mergeFirstWinNoFile(serviceULimits.NoFile)
}
}
// MergeLastWin merges adds or overwrite the attributes of the passed // MergeLastWin merges adds or overwrite the attributes of the passed
// ServiceULimits with the existing one. // ServiceULimits with the existing one.
func (l *ServiceULimits) MergeLastWin(serviceULimits *ServiceULimits) { func (l *ServiceULimits) MergeLastWin(serviceULimits *ServiceULimits) {
@ -1024,6 +1046,19 @@ func (l *ServiceULimits) MergeLastWin(serviceULimits *ServiceULimits) {
} }
} }
func (l *ServiceULimits) mergeFirstWinNProc(nproc uint) {
if l.NProc != nproc {
return
}
l.NProc = nproc
}
func (l *ServiceULimits) mergeFirstWinNoFile(noFile *ServiceULimitsNoFile) {
if !l.NoFile.Equal(noFile) {
l.NoFile.MergeFirstWin(noFile)
}
}
func (l *ServiceULimits) mergeLastWinNProc(nproc uint) { func (l *ServiceULimits) mergeLastWinNProc(nproc uint) {
if l.NProc != nproc { if l.NProc != nproc {
l.NProc = nproc l.NProc = nproc
@ -1067,6 +1102,8 @@ func (nf *ServiceULimitsNoFile) Equal(equalable Equalable) bool {
} }
} }
// MergeFirstWin adds only the attributes of the passed ServiceULimits they are
// undefined.
func (nf *ServiceULimitsNoFile) MergeFirstWin(serviceULimitsNoFile *ServiceULimitsNoFile) { func (nf *ServiceULimitsNoFile) MergeFirstWin(serviceULimitsNoFile *ServiceULimitsNoFile) {
switch { switch {
case nf == nil && serviceULimitsNoFile == nil: case nf == nil && serviceULimitsNoFile == nil:
@ -1089,7 +1126,7 @@ func (nf *ServiceULimitsNoFile) MergeFirstWin(serviceULimitsNoFile *ServiceULimi
} }
// MergeLastWin merges adds or overwrite the attributes of the passed // MergeLastWin merges adds or overwrite the attributes of the passed
// ServiceULimits with the existing one. // ServiceULimitsNoFile with the existing one.
func (nf *ServiceULimitsNoFile) MergeLastWin(serviceULimitsNoFile *ServiceULimitsNoFile) { func (nf *ServiceULimitsNoFile) MergeLastWin(serviceULimitsNoFile *ServiceULimitsNoFile) {
switch { switch {
case nf == nil && serviceULimitsNoFile == nil: case nf == nil && serviceULimitsNoFile == nil:
@ -1164,6 +1201,8 @@ func (v *Volume) Equal(equalable Equalable) bool {
} }
} }
// MergeFirstWin adds only the attributes of the passed Volume they are
// undefined.
func (v *Volume) MergeFirstWin(volume *Volume) { func (v *Volume) MergeFirstWin(volume *Volume) {
switch { switch {
case v == nil && volume == nil: case v == nil && volume == nil:

View File

@ -1899,6 +1899,54 @@ func TestServiceULimits_MergeLastWin(t *testing.T) {
} }
} }
func TestServiceULimits_MergeFirstWin(t *testing.T) {
require := require.New(t)
testCases := []struct {
ServiceULimitsA *dockerCompose.ServiceULimits
ServiceULimitsB *dockerCompose.ServiceULimits
expectedServiceULimits *dockerCompose.ServiceULimits
}{
{
ServiceULimitsA: nil,
ServiceULimitsB: nil,
expectedServiceULimits: nil,
},
{
ServiceULimitsA: &dockerCompose.ServiceULimits{},
ServiceULimitsB: nil,
expectedServiceULimits: &dockerCompose.ServiceULimits{},
},
{
ServiceULimitsA: &dockerCompose.ServiceULimits{
NProc: 10,
},
ServiceULimitsB: &dockerCompose.ServiceULimits{
NProc: 10,
},
expectedServiceULimits: &dockerCompose.ServiceULimits{
NProc: 10,
},
},
{
ServiceULimitsA: &dockerCompose.ServiceULimits{
NProc: 10,
},
ServiceULimitsB: &dockerCompose.ServiceULimits{
NProc: 20,
},
expectedServiceULimits: &dockerCompose.ServiceULimits{
NProc: 10,
},
},
}
for i, testCase := range testCases {
testCase.ServiceULimitsA.MergeFirstWin(testCase.ServiceULimitsB)
require.True(testCase.expectedServiceULimits.Equal(testCase.ServiceULimitsA), "Failed test case %v", i)
}
}
func TestServiceULimitsNoFile_Equal(t *testing.T) { func TestServiceULimitsNoFile_Equal(t *testing.T) {
require := require.New(t) require := require.New(t)