Browse code

add test for exposing large number of ports

this test checks if exposing a large number of ports in Dockerfile properly
saves the port in configs. We dont actually expose a VERY large number of ports
here because the result is the same and it increases the test time by a few
seconds

Docker-DCO-1.1-Signed-off-by: Daniel, Dao Quang Minh <dqminh89@gmail.com> (github: dqminh)

Daniel, Dao Quang Minh authored on 2014/11/13 12:14:15
Showing 1 changed files
... ...
@@ -2,6 +2,7 @@ package main
2 2
 
3 3
 import (
4 4
 	"archive/tar"
5
+	"bytes"
5 6
 	"encoding/json"
6 7
 	"fmt"
7 8
 	"io/ioutil"
... ...
@@ -10,9 +11,11 @@ import (
10 10
 	"path"
11 11
 	"path/filepath"
12 12
 	"regexp"
13
+	"strconv"
13 14
 	"strings"
14 15
 	"syscall"
15 16
 	"testing"
17
+	"text/template"
16 18
 	"time"
17 19
 
18 20
 	"github.com/docker/docker/pkg/archive"
... ...
@@ -1732,6 +1735,62 @@ func TestBuildExpose(t *testing.T) {
1732 1732
 	logDone("build - expose")
1733 1733
 }
1734 1734
 
1735
+func TestBuildExposeMorePorts(t *testing.T) {
1736
+	// start building docker file with a large number of ports
1737
+	portList := make([]string, 50)
1738
+	line := make([]string, 100)
1739
+	expectedPorts := make([]int, len(portList)*len(line))
1740
+	for i := 0; i < len(portList); i++ {
1741
+		for j := 0; j < len(line); j++ {
1742
+			p := i*len(line) + j + 1
1743
+			line[j] = strconv.Itoa(p)
1744
+			expectedPorts[p-1] = p
1745
+		}
1746
+		if i == len(portList)-1 {
1747
+			portList[i] = strings.Join(line, " ")
1748
+		} else {
1749
+			portList[i] = strings.Join(line, " ") + ` \`
1750
+		}
1751
+	}
1752
+
1753
+	dockerfile := `FROM scratch
1754
+	EXPOSE {{range .}} {{.}}
1755
+	{{end}}`
1756
+	tmpl := template.Must(template.New("dockerfile").Parse(dockerfile))
1757
+	buf := bytes.NewBuffer(nil)
1758
+	tmpl.Execute(buf, portList)
1759
+
1760
+	name := "testbuildexpose"
1761
+	defer deleteImages(name)
1762
+	_, err := buildImage(name, buf.String(), true)
1763
+	if err != nil {
1764
+		t.Fatal(err)
1765
+	}
1766
+
1767
+	// check if all the ports are saved inside Config.ExposedPorts
1768
+	res, err := inspectFieldJSON(name, "Config.ExposedPorts")
1769
+	if err != nil {
1770
+		t.Fatal(err)
1771
+	}
1772
+	var exposedPorts map[string]interface{}
1773
+	if err := json.Unmarshal([]byte(res), &exposedPorts); err != nil {
1774
+		t.Fatal(err)
1775
+	}
1776
+
1777
+	for _, p := range expectedPorts {
1778
+		ep := fmt.Sprintf("%d/tcp", p)
1779
+		if _, ok := exposedPorts[ep]; !ok {
1780
+			t.Errorf("Port(%s) is not exposed", ep)
1781
+		} else {
1782
+			delete(exposedPorts, ep)
1783
+		}
1784
+	}
1785
+	if len(exposedPorts) != 0 {
1786
+		t.Errorf("Unexpected extra exposed ports %v", exposedPorts)
1787
+	}
1788
+	logDone("build - expose large number of ports")
1789
+}
1790
+
1735 1791
 func TestBuildExposeOrder(t *testing.T) {
1736 1792
 	buildID := func(name, exposed string) string {
1737 1793
 		_, err := buildImage(name, fmt.Sprintf(`FROM scratch