...

Source file src/github.com/docker/go-connections/nat/parse_test.go

Documentation: github.com/docker/go-connections/nat

     1  package nat
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestParsePortRange(t *testing.T) {
     9  	if start, end, err := ParsePortRange("8000-8080"); err != nil || start != 8000 || end != 8080 {
    10  		t.Fatalf("Error: %s or Expecting {start,end} values {8000,8080} but found {%d,%d}.", err, start, end)
    11  	}
    12  }
    13  
    14  func TestParsePortRangeEmpty(t *testing.T) {
    15  	if _, _, err := ParsePortRange(""); err == nil || err.Error() != "empty string specified for ports" {
    16  		t.Fatalf("Expected error 'empty string specified for ports', got %v", err)
    17  	}
    18  }
    19  
    20  func TestParsePortRangeWithNoRange(t *testing.T) {
    21  	start, end, err := ParsePortRange("8080")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if start != 8080 || end != 8080 {
    26  		t.Fatalf("Expected start and end to be the same and equal to 8080, but were %v and %v", start, end)
    27  	}
    28  }
    29  
    30  func TestParsePortRangeIncorrectRange(t *testing.T) {
    31  	if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "invalid range specified for port") {
    32  		t.Fatalf("Expecting error 'invalid range specified for port' but received %s.", err)
    33  	}
    34  }
    35  
    36  func TestParsePortRangeIncorrectEndRange(t *testing.T) {
    37  	if _, _, err := ParsePortRange("8000-a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
    38  		t.Fatalf("Expecting error 'invalid syntax' but received %s.", err)
    39  	}
    40  
    41  	if _, _, err := ParsePortRange("8000-30a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
    42  		t.Fatalf("Expecting error 'invalid syntax' but received %s.", err)
    43  	}
    44  }
    45  
    46  func TestParsePortRangeIncorrectStartRange(t *testing.T) {
    47  	if _, _, err := ParsePortRange("a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
    48  		t.Fatalf("Expecting error 'invalid syntax' but received %s.", err)
    49  	}
    50  
    51  	if _, _, err := ParsePortRange("30a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
    52  		t.Fatalf("Expecting error 'invalid syntax' but received %s.", err)
    53  	}
    54  }
    55  

View as plain text