1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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)
This commit is contained in:
Daniel, Dao Quang Minh 2014-11-12 22:14:15 -05:00
parent 87d0562c61
commit 29be7b439e

View file

@ -2,6 +2,7 @@ package main
import ( import (
"archive/tar" "archive/tar"
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -10,9 +11,11 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv"
"strings" "strings"
"syscall" "syscall"
"testing" "testing"
"text/template"
"time" "time"
"github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/archive"
@ -1732,6 +1735,62 @@ func TestBuildExpose(t *testing.T) {
logDone("build - expose") logDone("build - expose")
} }
func TestBuildExposeMorePorts(t *testing.T) {
// start building docker file with a large number of ports
portList := make([]string, 50)
line := make([]string, 100)
expectedPorts := make([]int, len(portList)*len(line))
for i := 0; i < len(portList); i++ {
for j := 0; j < len(line); j++ {
p := i*len(line) + j + 1
line[j] = strconv.Itoa(p)
expectedPorts[p-1] = p
}
if i == len(portList)-1 {
portList[i] = strings.Join(line, " ")
} else {
portList[i] = strings.Join(line, " ") + ` \`
}
}
dockerfile := `FROM scratch
EXPOSE {{range .}} {{.}}
{{end}}`
tmpl := template.Must(template.New("dockerfile").Parse(dockerfile))
buf := bytes.NewBuffer(nil)
tmpl.Execute(buf, portList)
name := "testbuildexpose"
defer deleteImages(name)
_, err := buildImage(name, buf.String(), true)
if err != nil {
t.Fatal(err)
}
// check if all the ports are saved inside Config.ExposedPorts
res, err := inspectFieldJSON(name, "Config.ExposedPorts")
if err != nil {
t.Fatal(err)
}
var exposedPorts map[string]interface{}
if err := json.Unmarshal([]byte(res), &exposedPorts); err != nil {
t.Fatal(err)
}
for _, p := range expectedPorts {
ep := fmt.Sprintf("%d/tcp", p)
if _, ok := exposedPorts[ep]; !ok {
t.Errorf("Port(%s) is not exposed", ep)
} else {
delete(exposedPorts, ep)
}
}
if len(exposedPorts) != 0 {
t.Errorf("Unexpected extra exposed ports %v", exposedPorts)
}
logDone("build - expose large number of ports")
}
func TestBuildExposeOrder(t *testing.T) { func TestBuildExposeOrder(t *testing.T) {
buildID := func(name, exposed string) string { buildID := func(name, exposed string) string {
_, err := buildImage(name, fmt.Sprintf(`FROM scratch _, err := buildImage(name, fmt.Sprintf(`FROM scratch