fix(dockerCompose): add ServiceDeploy.MergeFirstWin()

This commit is contained in:
Markus Pesch 2023-08-27 19:56:49 +02:00
parent 23290fbd5d
commit 4c3c6cd5d2
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 226 additions and 129 deletions

View File

@ -732,6 +732,28 @@ func (sd *ServiceDeploy) Equal(equalable Equalable) bool {
} }
} }
// MergeFirstWin merges adds or overwrite the attributes of the passed
// serviceDeploy with the existing one.
func (sd *ServiceDeploy) MergeFirstWin(serviceDeploy *ServiceDeploy) {
switch {
case sd == nil && serviceDeploy == nil:
fallthrough
case sd != nil && serviceDeploy == nil:
return
// WARN: It's not possible to change the memory pointer sd *ServiceDeploy
// to a new initialized serviceDeploy without returning the ServiceDeploy
// it self.
//
// case sd == nil && serviceDeploy != nil:
// sd = NewServiceDeploy()
// fallthrough
default:
sd.mergeFirstWinDeployResources(serviceDeploy.Resources)
}
}
// MergeLastWin merges adds or overwrite the attributes of the passed // MergeLastWin merges adds or overwrite the attributes of the passed
// serviceDeploy with the existing one. // serviceDeploy with the existing one.
func (sd *ServiceDeploy) MergeLastWin(serviceDeploy *ServiceDeploy) { func (sd *ServiceDeploy) MergeLastWin(serviceDeploy *ServiceDeploy) {
@ -754,6 +776,19 @@ func (sd *ServiceDeploy) MergeLastWin(serviceDeploy *ServiceDeploy) {
} }
} }
func (sd *ServiceDeploy) mergeFirstWinDeployResources(resources *ServiceDeployResources) {
switch {
case sd.Resources == nil && resources != nil:
sd.Resources = resources
case sd.Resources != nil && resources == nil:
fallthrough
case sd.Resources == nil && resources == nil:
return
default:
sd.Resources.MergeFirstWin(resources)
}
}
func (sd *ServiceDeploy) mergeLastWinDeployResources(resources *ServiceDeployResources) { func (sd *ServiceDeploy) mergeLastWinDeployResources(resources *ServiceDeployResources) {
switch { switch {
case sd.Resources == nil && resources != nil: case sd.Resources == nil && resources != nil:

View File

@ -1295,6 +1295,68 @@ func TestSecretDeploy_Equal(t *testing.T) {
} }
} }
func TestServiceDeploy_MergeFirstWin(t *testing.T) {
require := require.New(t)
testCases := []struct {
serviceDeploymentA *dockerCompose.ServiceDeploy
serviceDeploymentB *dockerCompose.ServiceDeploy
expectedServiceDeployment *dockerCompose.ServiceDeploy
}{
{
serviceDeploymentA: nil,
serviceDeploymentB: nil,
expectedServiceDeployment: nil,
},
{
serviceDeploymentA: &dockerCompose.ServiceDeploy{
Resources: nil,
},
serviceDeploymentB: &dockerCompose.ServiceDeploy{
Resources: nil,
},
expectedServiceDeployment: &dockerCompose.ServiceDeploy{
Resources: nil,
},
},
{
serviceDeploymentA: &dockerCompose.ServiceDeploy{
Resources: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
},
serviceDeploymentB: &dockerCompose.ServiceDeploy{
Resources: nil,
},
expectedServiceDeployment: &dockerCompose.ServiceDeploy{
Resources: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
},
},
{
serviceDeploymentA: &dockerCompose.ServiceDeploy{
Resources: nil,
},
serviceDeploymentB: &dockerCompose.ServiceDeploy{
Resources: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
},
expectedServiceDeployment: &dockerCompose.ServiceDeploy{
Resources: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
},
},
}
for i, testCase := range testCases {
testCase.serviceDeploymentA.MergeLastWin(testCase.serviceDeploymentB)
require.True(testCase.expectedServiceDeployment.Equal(testCase.serviceDeploymentA), "Failed test case %v", i)
}
}
func TestServiceDeploy_MergeLastWin(t *testing.T) { func TestServiceDeploy_MergeLastWin(t *testing.T) {
require := require.New(t) require := require.New(t)
@ -1414,135 +1476,6 @@ func TestSecretDeployResources_Equal(t *testing.T) {
} }
} }
func TestServiceDeployResources_MergeLastWin(t *testing.T) {
require := require.New(t)
testCases := []struct {
serviceDeploymentResourcesA *dockerCompose.ServiceDeployResources
serviceDeploymentResourcesB *dockerCompose.ServiceDeployResources
expectedServiceDeploymentResources *dockerCompose.ServiceDeployResources
}{
{
serviceDeploymentResourcesA: nil,
serviceDeploymentResourcesB: nil,
expectedServiceDeploymentResources: nil,
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Reservations: nil,
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Reservations: nil,
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
},
}
for i, testCase := range testCases {
testCase.serviceDeploymentResourcesA.MergeLastWin(testCase.serviceDeploymentResourcesB)
require.True(testCase.expectedServiceDeploymentResources.Equal(testCase.serviceDeploymentResourcesA), "Failed test case %v", i)
}
}
func TestServiceDeployResources_MergeFirstWin(t *testing.T) { func TestServiceDeployResources_MergeFirstWin(t *testing.T) {
require := require.New(t) require := require.New(t)
@ -1692,6 +1625,135 @@ func TestServiceDeployResources_MergeFirstWin(t *testing.T) {
} }
} }
func TestServiceDeployResources_MergeLastWin(t *testing.T) {
require := require.New(t)
testCases := []struct {
serviceDeploymentResourcesA *dockerCompose.ServiceDeployResources
serviceDeploymentResourcesB *dockerCompose.ServiceDeployResources
expectedServiceDeploymentResources *dockerCompose.ServiceDeployResources
}{
{
serviceDeploymentResourcesA: nil,
serviceDeploymentResourcesB: nil,
expectedServiceDeploymentResources: nil,
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: nil,
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Limits: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Reservations: nil,
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Reservations: nil,
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
},
{
serviceDeploymentResourcesA: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "1",
Memory: "500",
},
},
serviceDeploymentResourcesB: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
expectedServiceDeploymentResources: &dockerCompose.ServiceDeployResources{
Reservations: &dockerCompose.ServiceDeployResourcesLimits{
CPUs: "2",
Memory: "1000",
},
},
},
}
for i, testCase := range testCases {
testCase.serviceDeploymentResourcesA.MergeLastWin(testCase.serviceDeploymentResourcesB)
require.True(testCase.expectedServiceDeploymentResources.Equal(testCase.serviceDeploymentResourcesA), "Failed test case %v", i)
}
}
func TestServiceDeployResourcesLimits_Equal(t *testing.T) { func TestServiceDeployResourcesLimits_Equal(t *testing.T) {
require := require.New(t) require := require.New(t)