github.com/tompao/docker@v1.9.1/api/common_test.go (about)

     1  package api
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"os"
    10  )
    11  
    12  type ports struct {
    13  	ports    []types.Port
    14  	expected string
    15  }
    16  
    17  // DisplayablePorts
    18  func TestDisplayablePorts(t *testing.T) {
    19  	cases := []ports{
    20  		{
    21  			[]types.Port{
    22  				{
    23  					PrivatePort: 9988,
    24  					Type:        "tcp",
    25  				},
    26  			},
    27  			"9988/tcp"},
    28  		{
    29  			[]types.Port{
    30  				{
    31  					PrivatePort: 9988,
    32  					Type:        "udp",
    33  				},
    34  			},
    35  			"9988/udp",
    36  		},
    37  		{
    38  			[]types.Port{
    39  				{
    40  					IP:          "0.0.0.0",
    41  					PrivatePort: 9988,
    42  					Type:        "tcp",
    43  				},
    44  			},
    45  			"0.0.0.0:0->9988/tcp",
    46  		},
    47  		{
    48  			[]types.Port{
    49  				{
    50  					PrivatePort: 9988,
    51  					PublicPort:  8899,
    52  					Type:        "tcp",
    53  				},
    54  			},
    55  			"9988/tcp",
    56  		},
    57  		{
    58  			[]types.Port{
    59  				{
    60  					IP:          "4.3.2.1",
    61  					PrivatePort: 9988,
    62  					PublicPort:  8899,
    63  					Type:        "tcp",
    64  				},
    65  			},
    66  			"4.3.2.1:8899->9988/tcp",
    67  		},
    68  		{
    69  			[]types.Port{
    70  				{
    71  					IP:          "4.3.2.1",
    72  					PrivatePort: 9988,
    73  					PublicPort:  9988,
    74  					Type:        "tcp",
    75  				},
    76  			},
    77  			"4.3.2.1:9988->9988/tcp",
    78  		},
    79  		{
    80  			[]types.Port{
    81  				{
    82  					PrivatePort: 9988,
    83  					Type:        "udp",
    84  				}, {
    85  					PrivatePort: 9988,
    86  					Type:        "udp",
    87  				},
    88  			},
    89  			"9988/udp, 9988/udp",
    90  		},
    91  		{
    92  			[]types.Port{
    93  				{
    94  					IP:          "1.2.3.4",
    95  					PublicPort:  9998,
    96  					PrivatePort: 9998,
    97  					Type:        "udp",
    98  				}, {
    99  					IP:          "1.2.3.4",
   100  					PublicPort:  9999,
   101  					PrivatePort: 9999,
   102  					Type:        "udp",
   103  				},
   104  			},
   105  			"1.2.3.4:9998-9999->9998-9999/udp",
   106  		},
   107  		{
   108  			[]types.Port{
   109  				{
   110  					IP:          "1.2.3.4",
   111  					PublicPort:  8887,
   112  					PrivatePort: 9998,
   113  					Type:        "udp",
   114  				}, {
   115  					IP:          "1.2.3.4",
   116  					PublicPort:  8888,
   117  					PrivatePort: 9999,
   118  					Type:        "udp",
   119  				},
   120  			},
   121  			"1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp",
   122  		},
   123  		{
   124  			[]types.Port{
   125  				{
   126  					PrivatePort: 9998,
   127  					Type:        "udp",
   128  				}, {
   129  					PrivatePort: 9999,
   130  					Type:        "udp",
   131  				},
   132  			},
   133  			"9998-9999/udp",
   134  		},
   135  		{
   136  			[]types.Port{
   137  				{
   138  					IP:          "1.2.3.4",
   139  					PrivatePort: 6677,
   140  					PublicPort:  7766,
   141  					Type:        "tcp",
   142  				}, {
   143  					PrivatePort: 9988,
   144  					PublicPort:  8899,
   145  					Type:        "udp",
   146  				},
   147  			},
   148  			"9988/udp, 1.2.3.4:7766->6677/tcp",
   149  		},
   150  		{
   151  			[]types.Port{
   152  				{
   153  					IP:          "1.2.3.4",
   154  					PrivatePort: 9988,
   155  					PublicPort:  8899,
   156  					Type:        "udp",
   157  				}, {
   158  					IP:          "1.2.3.4",
   159  					PrivatePort: 9988,
   160  					PublicPort:  8899,
   161  					Type:        "tcp",
   162  				}, {
   163  					IP:          "4.3.2.1",
   164  					PrivatePort: 2233,
   165  					PublicPort:  3322,
   166  					Type:        "tcp",
   167  				},
   168  			},
   169  			"4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/udp, 1.2.3.4:8899->9988/tcp",
   170  		},
   171  		{
   172  			[]types.Port{
   173  				{
   174  					PrivatePort: 9988,
   175  					PublicPort:  8899,
   176  					Type:        "udp",
   177  				}, {
   178  					IP:          "1.2.3.4",
   179  					PrivatePort: 6677,
   180  					PublicPort:  7766,
   181  					Type:        "tcp",
   182  				}, {
   183  					IP:          "4.3.2.1",
   184  					PrivatePort: 2233,
   185  					PublicPort:  3322,
   186  					Type:        "tcp",
   187  				},
   188  			},
   189  			"9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp",
   190  		},
   191  	}
   192  
   193  	for _, port := range cases {
   194  		actual := DisplayablePorts(port.ports)
   195  		if port.expected != actual {
   196  			t.Fatalf("Expected %s, got %s.", port.expected, actual)
   197  		}
   198  	}
   199  }
   200  
   201  // MatchesContentType
   202  func TestJsonContentType(t *testing.T) {
   203  	if !MatchesContentType("application/json", "application/json") {
   204  		t.Fail()
   205  	}
   206  
   207  	if !MatchesContentType("application/json; charset=utf-8", "application/json") {
   208  		t.Fail()
   209  	}
   210  
   211  	if MatchesContentType("dockerapplication/json", "application/json") {
   212  		t.Fail()
   213  	}
   214  }
   215  
   216  // LoadOrCreateTrustKey
   217  func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) {
   218  	tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
   219  	if err != nil {
   220  		t.Fatal(err)
   221  	}
   222  	defer os.RemoveAll(tmpKeyFolderPath)
   223  
   224  	tmpKeyFile, err := ioutil.TempFile(tmpKeyFolderPath, "keyfile")
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  
   229  	if _, err := LoadOrCreateTrustKey(tmpKeyFile.Name()); err == nil {
   230  		t.Fatalf("expected an error, got nothing.")
   231  	}
   232  
   233  }
   234  
   235  func TestLoadOrCreateTrustKeyCreateKey(t *testing.T) {
   236  	tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  	defer os.RemoveAll(tmpKeyFolderPath)
   241  
   242  	// Without the need to create the folder hierarchy
   243  	tmpKeyFile := filepath.Join(tmpKeyFolderPath, "keyfile")
   244  
   245  	if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
   246  		t.Fatalf("expected a new key file, got : %v and %v", err, key)
   247  	}
   248  
   249  	if _, err := os.Stat(tmpKeyFile); err != nil {
   250  		t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
   251  	}
   252  
   253  	// With the need to create the folder hierarchy as tmpKeyFie is in a path
   254  	// where some folder do not exists.
   255  	tmpKeyFile = filepath.Join(tmpKeyFolderPath, "folder/hierarchy/keyfile")
   256  
   257  	if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
   258  		t.Fatalf("expected a new key file, got : %v and %v", err, key)
   259  	}
   260  
   261  	if _, err := os.Stat(tmpKeyFile); err != nil {
   262  		t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
   263  	}
   264  
   265  	// With no path at all
   266  	defer os.Remove("keyfile")
   267  	if key, err := LoadOrCreateTrustKey("keyfile"); err != nil || key == nil {
   268  		t.Fatalf("expected a new key file, got : %v and %v", err, key)
   269  	}
   270  
   271  	if _, err := os.Stat("keyfile"); err != nil {
   272  		t.Fatalf("Expected to find a file keyfile, got %v", err)
   273  	}
   274  }
   275  
   276  func TestLoadOrCreateTrustKeyLoadValidKey(t *testing.T) {
   277  	tmpKeyFile := filepath.Join("fixtures", "keyfile")
   278  
   279  	if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
   280  		t.Fatalf("expected a key file, got : %v and %v", err, key)
   281  	}
   282  }