Skip to content

Commit 1c74a70

Browse files
committed
test: update test logic in TestTestParseFlagPWithPlatformSpec
This test does not run in the rootfull Linux environment. Also, there are some cases where the test does not run. Therefore, this commit modifies all tests to perform the evaluation. Note that when the host ports are not specified in the -p option of `nerdctl run`, ports are randomly assigned, but it is not known what port number will be assigned in each test. Therefore, this commit modifies not to compare host ports. Signed-off-by: Hayato Kiwata <[email protected]>
1 parent 93c72b5 commit 1c74a70

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

pkg/portutil/portutil_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func TestTestParseFlagPWithPlatformSpec(t *testing.T) {
32-
if runtime.GOOS != "Linux" || rootlessutil.IsRootless() {
32+
if runtime.GOOS != "linux" || rootlessutil.IsRootless() {
3333
t.Skip("no non-Linux platform or rootless mode in Linux are not supported yet")
3434
}
3535
type args struct {
@@ -48,7 +48,6 @@ func TestTestParseFlagPWithPlatformSpec(t *testing.T) {
4848
},
4949
want: []cni.PortMapping{
5050
{
51-
HostPort: 3000,
5251
ContainerPort: 3000,
5352
Protocol: "tcp",
5453
HostIP: "0.0.0.0",
@@ -63,13 +62,11 @@ func TestTestParseFlagPWithPlatformSpec(t *testing.T) {
6362
},
6463
want: []cni.PortMapping{
6564
{
66-
HostPort: 3000,
6765
ContainerPort: 3000,
6866
Protocol: "tcp",
6967
HostIP: "0.0.0.0",
7068
},
7169
{
72-
HostPort: 3001,
7370
ContainerPort: 3001,
7471
Protocol: "tcp",
7572
HostIP: "0.0.0.0",
@@ -92,13 +89,11 @@ func TestTestParseFlagPWithPlatformSpec(t *testing.T) {
9289
},
9390
want: []cni.PortMapping{
9491
{
95-
HostPort: 3000,
9692
ContainerPort: 3000,
9793
Protocol: "tcp",
9894
HostIP: "0.0.0.0",
9995
},
10096
{
101-
HostPort: 3001,
10297
ContainerPort: 3001,
10398
Protocol: "tcp",
10499
HostIP: "0.0.0.0",
@@ -113,15 +108,13 @@ func TestTestParseFlagPWithPlatformSpec(t *testing.T) {
113108
},
114109
want: []cni.PortMapping{
115110
{
116-
HostPort: 3000,
117111
ContainerPort: 3000,
118-
Protocol: "tcp",
112+
Protocol: "udp",
119113
HostIP: "0.0.0.0",
120114
},
121115
{
122-
HostPort: 3001,
123116
ContainerPort: 3001,
124-
Protocol: "tcp",
117+
Protocol: "udp",
125118
HostIP: "0.0.0.0",
126119
},
127120
},
@@ -138,7 +131,7 @@ func TestTestParseFlagPWithPlatformSpec(t *testing.T) {
138131
}
139132
if !reflect.DeepEqual(got, tt.want) {
140133
if len(got) == len(tt.want) {
141-
if len(got) > 1 {
134+
if len(got) > 0 {
142135
var hostPorts []int32
143136
var containerPorts []int32
144137
for _, value := range got {
@@ -154,6 +147,14 @@ func TestTestParseFlagPWithPlatformSpec(t *testing.T) {
154147
if (hostPorts[len(hostPorts)-1] - hostPorts[0]) != (containerPorts[len(hostPorts)-1] - containerPorts[0]) {
155148
t.Errorf("ParseFlagP() = %v, want %v", got, tt.want)
156149
}
150+
sort.Slice(got, func(i, j int) bool {
151+
return got[i].HostPort < got[j].HostPort
152+
})
153+
for i := 0; i < len(got); i++ {
154+
if got[i].ContainerPort != tt.want[i].ContainerPort || got[i].Protocol != tt.want[i].Protocol || got[i].HostIP != tt.want[i].HostIP {
155+
t.Errorf("ParseFlagP() = %v, want %v", got, tt.want)
156+
}
157+
}
157158
}
158159
} else {
159160
t.Errorf("ParseFlagP() = %v, want %v", got, tt.want)

0 commit comments

Comments
 (0)