fix(dockerCompose): add ServiceDeployResourcesLimits.MergeFirstWin()

This commit is contained in:
Markus Pesch 2023-08-27 19:35:54 +02:00
parent 405a0258f1
commit 07673bfd53
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 128 additions and 0 deletions

View File

@ -877,6 +877,29 @@ func (sdrl *ServiceDeployResourcesLimits) Equal(equalable Equalable) bool {
}
}
// MergeFirstWin adds only attributes of the passed serviceDeployResourcesLimits
// if they are not already exists.
func (sdrl *ServiceDeployResourcesLimits) MergeFirstWin(serviceDeployResourcesLimits *ServiceDeployResourcesLimits) {
switch {
case sdrl == nil && serviceDeployResourcesLimits == nil:
fallthrough
case sdrl != nil && serviceDeployResourcesLimits == nil:
return
// WARN: It's not possible to change the memory pointer sdrl *ServiceDeployResourcesLimits
// to a new initialized serviceDeployResourcesLimits without returning the
// serviceDeployResourcesLimits it self.
//
// case sdrl == nil && serviceDeployResourcesLimits != nil:
// sdrl = NewServiceDeployResourcesLimits()
// fallthrough
default:
sdrl.mergeFirstWinCPUs(serviceDeployResourcesLimits.CPUs)
sdrl.mergeFirstWinMemory(serviceDeployResourcesLimits.Memory)
}
}
// MergeLastWin merges adds or overwrite the attributes of the passed
// serviceDeployResourcesLimits with the existing one.
func (sdrl *ServiceDeployResourcesLimits) MergeLastWin(serviceDeployResourcesLimits *ServiceDeployResourcesLimits) {
@ -900,6 +923,18 @@ func (sdrl *ServiceDeployResourcesLimits) MergeLastWin(serviceDeployResourcesLim
}
}
func (sdrl *ServiceDeployResourcesLimits) mergeFirstWinCPUs(cpus string) {
if len(sdrl.CPUs) <= 0 {
sdrl.CPUs = cpus
}
}
func (sdrl *ServiceDeployResourcesLimits) mergeFirstWinMemory(memory string) {
if len(sdrl.Memory) <= 0 {
sdrl.Memory = memory
}
}
func (sdrl *ServiceDeployResourcesLimits) mergeLastWinCPUs(cpus string) {
if sdrl.CPUs != cpus {
sdrl.CPUs = cpus

View File

@ -1603,6 +1603,99 @@ func TestServiceDeployResourcesLimits_Equal(t *testing.T) {
}
}
func TestServiceDeployResourcesLimits_MergeFirstWin(t *testing.T) {
require := require.New(t)
testCases := []struct {
serviceDeploymentResourcesLimitsA *dockerCompose.ServiceDeployResourcesLimits
serviceDeploymentResourcesLimitsB *dockerCompose.ServiceDeployResourcesLimits
expectedServiceDeploymentResourcesLimits *dockerCompose.ServiceDeployResourcesLimits
}{
{
serviceDeploymentResourcesLimitsA: nil,
serviceDeploymentResourcesLimitsB: nil,
expectedServiceDeploymentResourcesLimits: nil,
},
{
serviceDeploymentResourcesLimitsA: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
serviceDeploymentResourcesLimitsB: nil,
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
{
serviceDeploymentResourcesLimitsA: &dockerCompose.ServiceDeployResourcesLimits{},
serviceDeploymentResourcesLimitsB: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
{
serviceDeploymentResourcesLimitsA: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "",
Memory: "",
},
serviceDeploymentResourcesLimitsB: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
{
serviceDeploymentResourcesLimitsA: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
serviceDeploymentResourcesLimitsB: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
{
serviceDeploymentResourcesLimitsA: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
},
serviceDeploymentResourcesLimitsB: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
},
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
},
},
{
serviceDeploymentResourcesLimitsA: &dockerCompose.ServiceDeployResourcesLimits{
Memory: "500",
},
serviceDeploymentResourcesLimitsB: &dockerCompose.ServiceDeployResourcesLimits{
Memory: "1000",
},
expectedServiceDeploymentResourcesLimits: &dockerCompose.ServiceDeployResourcesLimits{
Memory: "500",
},
},
}
for i, testCase := range testCases {
testCase.serviceDeploymentResourcesLimitsA.MergeFirstWin(testCase.serviceDeploymentResourcesLimitsB)
require.True(testCase.expectedServiceDeploymentResourcesLimits.Equal(testCase.serviceDeploymentResourcesLimitsA), "Failed test case %v", i)
}
}
func TestServiceDeployResourcesLimits_MergeLastWin(t *testing.T) {
require := require.New(t)