File: sort_test.go

package info (click to toggle)
golang-github-docker-go-connections 0.3.0-3%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 300 kB
  • sloc: makefile: 3
file content (85 lines) | stat: -rw-r--r-- 1,636 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package nat

import (
	"fmt"
	"reflect"
	"testing"
)

func TestSortUniquePorts(t *testing.T) {
	ports := []Port{
		Port("6379/tcp"),
		Port("22/tcp"),
	}

	Sort(ports, func(ip, jp Port) bool {
		return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && ip.Proto() == "tcp")
	})

	first := ports[0]
	if fmt.Sprint(first) != "22/tcp" {
		t.Log(fmt.Sprint(first))
		t.Fail()
	}
}

func TestSortSamePortWithDifferentProto(t *testing.T) {
	ports := []Port{
		Port("8888/tcp"),
		Port("8888/udp"),
		Port("6379/tcp"),
		Port("6379/udp"),
	}

	Sort(ports, func(ip, jp Port) bool {
		return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && ip.Proto() == "tcp")
	})

	first := ports[0]
	if fmt.Sprint(first) != "6379/tcp" {
		t.Fail()
	}
}

func TestSortPortMap(t *testing.T) {
	ports := []Port{
		Port("22/tcp"),
		Port("22/udp"),
		Port("8000/tcp"),
		Port("6379/tcp"),
		Port("9999/tcp"),
	}

	portMap := PortMap{
		Port("22/tcp"): []PortBinding{
			{},
		},
		Port("8000/tcp"): []PortBinding{
			{},
		},
		Port("6379/tcp"): []PortBinding{
			{},
			{HostIP: "0.0.0.0", HostPort: "32749"},
		},
		Port("9999/tcp"): []PortBinding{
			{HostIP: "0.0.0.0", HostPort: "40000"},
		},
	}

	SortPortMap(ports, portMap)
	if !reflect.DeepEqual(ports, []Port{
		Port("9999/tcp"),
		Port("6379/tcp"),
		Port("8000/tcp"),
		Port("22/tcp"),
		Port("22/udp"),
	}) {
		t.Errorf("failed to prioritize port with explicit mappings, got %v", ports)
	}
	if pm := portMap[Port("6379/tcp")]; !reflect.DeepEqual(pm, []PortBinding{
		{HostIP: "0.0.0.0", HostPort: "32749"},
		{},
	}) {
		t.Errorf("failed to prioritize bindings with explicit mappings, got %v", pm)
	}
}