mirror of
https://github.com/moby/moby.git
synced 2026-06-30 19:58:03 +00:00
Merge pull request #52047 from thaJeztah/swarm_portspec_sorting
api/types/swarm: PortConfig: add Compare method
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package swarm
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"net/netip"
|
||||
|
||||
"github.com/moby/moby/api/types/network"
|
||||
@@ -41,6 +42,27 @@ type PortConfig struct {
|
||||
PublishMode PortConfigPublishMode `json:",omitempty"`
|
||||
}
|
||||
|
||||
// Compare returns the lexical ordering of p and other, and can be used
|
||||
// with [slices.SortFunc].
|
||||
//
|
||||
// The comparison is performed in the following priority order:
|
||||
// 1. PublishedPort (host port)
|
||||
// 2. TargetPort (container port)
|
||||
// 3. Protocol
|
||||
// 4. PublishMode
|
||||
func (p PortConfig) Compare(other PortConfig) int {
|
||||
if n := cmp.Compare(p.PublishedPort, other.PublishedPort); n != 0 {
|
||||
return n
|
||||
}
|
||||
if n := cmp.Compare(p.TargetPort, other.TargetPort); n != 0 {
|
||||
return n
|
||||
}
|
||||
if n := cmp.Compare(p.Protocol, other.Protocol); n != 0 {
|
||||
return n
|
||||
}
|
||||
return cmp.Compare(p.PublishMode, other.PublishMode)
|
||||
}
|
||||
|
||||
// PortConfigPublishMode represents the mode in which the port is to
|
||||
// be published.
|
||||
type PortConfigPublishMode string
|
||||
|
||||
94
api/types/swarm/network_test.go
Normal file
94
api/types/swarm/network_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package swarm_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/api/types/network"
|
||||
"github.com/moby/moby/api/types/swarm"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestPortConfigCompareSort(t *testing.T) {
|
||||
tests := []struct {
|
||||
doc string
|
||||
ports []swarm.PortConfig
|
||||
expected []swarm.PortConfig
|
||||
}{
|
||||
{
|
||||
doc: "sort ports lexicographically",
|
||||
ports: []swarm.PortConfig{
|
||||
{
|
||||
PublishedPort: 443,
|
||||
TargetPort: 443,
|
||||
Protocol: network.TCP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
{
|
||||
PublishedPort: 80,
|
||||
TargetPort: 8080,
|
||||
Protocol: network.UDP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
{
|
||||
PublishedPort: 80,
|
||||
TargetPort: 9090,
|
||||
Protocol: network.TCP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
{
|
||||
PublishedPort: 80,
|
||||
TargetPort: 8080,
|
||||
Protocol: network.TCP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
{
|
||||
PublishedPort: 80,
|
||||
TargetPort: 8080,
|
||||
Protocol: network.TCP,
|
||||
PublishMode: swarm.PortConfigPublishModeHost,
|
||||
},
|
||||
},
|
||||
expected: []swarm.PortConfig{
|
||||
{
|
||||
PublishedPort: 80,
|
||||
TargetPort: 8080,
|
||||
Protocol: network.TCP,
|
||||
PublishMode: swarm.PortConfigPublishModeHost,
|
||||
},
|
||||
{
|
||||
PublishedPort: 80,
|
||||
TargetPort: 8080,
|
||||
Protocol: network.TCP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
{
|
||||
PublishedPort: 80,
|
||||
TargetPort: 8080,
|
||||
Protocol: network.UDP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
{
|
||||
PublishedPort: 80,
|
||||
TargetPort: 9090,
|
||||
Protocol: network.TCP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
{
|
||||
PublishedPort: 443,
|
||||
TargetPort: 443,
|
||||
Protocol: network.TCP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.doc, func(t *testing.T) {
|
||||
got := slices.Clone(tc.ports)
|
||||
slices.SortFunc(got, swarm.PortConfig.Compare)
|
||||
assert.DeepEqual(t, tc.expected, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user