chore: compare container ports to spec in assertContainerMatchesSpec

This commit is contained in:
Pavel Sviderski
2025-03-24 20:24:40 +10:00
parent 3910101a88
commit 2d923380a3
2 changed files with 39 additions and 2 deletions
+29
View File
@@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/netip"
"slices"
"strconv"
"strings"
)
@@ -261,3 +262,31 @@ func validateHostname(hostname string) error {
}
return nil
}
// PortsEqual returns true if the two port sets are equal. The order of the ports is not important.
func PortsEqual(a, b []PortSpec) bool {
if len(a) != len(b) {
return false
}
var err error
aSerialised := make([]string, len(a))
bSerialised := make([]string, len(b))
for i := range a {
aSerialised[i], err = a[i].String()
if err != nil {
return false
}
bSerialised[i], err = b[i].String()
if err != nil {
return false
}
}
slices.Sort(aSerialised)
slices.Sort(bSerialised)
return slices.Equal(aSerialised, bSerialised)
}