github.com/sevki/docker@v1.7.1/nat/sort_test.go (about)

     1  package nat
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestSortUniquePorts(t *testing.T) {
    10  	ports := []Port{
    11  		Port("6379/tcp"),
    12  		Port("22/tcp"),
    13  	}
    14  
    15  	Sort(ports, func(ip, jp Port) bool {
    16  		return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && ip.Proto() == "tcp")
    17  	})
    18  
    19  	first := ports[0]
    20  	if fmt.Sprint(first) != "22/tcp" {
    21  		t.Log(fmt.Sprint(first))
    22  		t.Fail()
    23  	}
    24  }
    25  
    26  func TestSortSamePortWithDifferentProto(t *testing.T) {
    27  	ports := []Port{
    28  		Port("8888/tcp"),
    29  		Port("8888/udp"),
    30  		Port("6379/tcp"),
    31  		Port("6379/udp"),
    32  	}
    33  
    34  	Sort(ports, func(ip, jp Port) bool {
    35  		return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && ip.Proto() == "tcp")
    36  	})
    37  
    38  	first := ports[0]
    39  	if fmt.Sprint(first) != "6379/tcp" {
    40  		t.Fail()
    41  	}
    42  }
    43  
    44  func TestSortPortMap(t *testing.T) {
    45  	ports := []Port{
    46  		Port("22/tcp"),
    47  		Port("22/udp"),
    48  		Port("8000/tcp"),
    49  		Port("6379/tcp"),
    50  		Port("9999/tcp"),
    51  	}
    52  
    53  	portMap := PortMap{
    54  		Port("22/tcp"): []PortBinding{
    55  			{},
    56  		},
    57  		Port("8000/tcp"): []PortBinding{
    58  			{},
    59  		},
    60  		Port("6379/tcp"): []PortBinding{
    61  			{},
    62  			{HostIp: "0.0.0.0", HostPort: "32749"},
    63  		},
    64  		Port("9999/tcp"): []PortBinding{
    65  			{HostIp: "0.0.0.0", HostPort: "40000"},
    66  		},
    67  	}
    68  
    69  	SortPortMap(ports, portMap)
    70  	if !reflect.DeepEqual(ports, []Port{
    71  		Port("9999/tcp"),
    72  		Port("6379/tcp"),
    73  		Port("8000/tcp"),
    74  		Port("22/tcp"),
    75  		Port("22/udp"),
    76  	}) {
    77  		t.Errorf("failed to prioritize port with explicit mappings, got %v", ports)
    78  	}
    79  	if pm := portMap[Port("6379/tcp")]; !reflect.DeepEqual(pm, []PortBinding{
    80  		{HostIp: "0.0.0.0", HostPort: "32749"},
    81  		{},
    82  	}) {
    83  		t.Errorf("failed to prioritize bindings with explicit mappings, got %v", pm)
    84  	}
    85  }