2013-06-25 02:30:48 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-10-18 02:47:08 -04:00
|
|
|
"fmt"
|
2013-10-26 22:24:01 -04:00
|
|
|
"github.com/dotcloud/docker/engine"
|
2013-10-18 17:15:24 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-06-25 02:30:48 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2013-10-18 17:15:24 -04:00
|
|
|
"runtime"
|
2013-06-25 02:30:48 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains utility functions for docker's unit test suite.
|
|
|
|
// It has to be named XXX_test.go, apparently, in other to access private functions
|
|
|
|
// from other XXX_test.go functions.
|
|
|
|
|
2013-10-18 02:47:08 -04:00
|
|
|
var globalTestID string
|
|
|
|
|
2013-06-25 02:30:48 -04:00
|
|
|
// Create a temporary runtime suitable for unit testing.
|
|
|
|
// Call t.Fatal() at the first error.
|
2013-10-27 03:13:45 -04:00
|
|
|
func mkRuntime(f utils.Fataler) *Runtime {
|
|
|
|
root, err := newTestDirectory(unitTestStoreBase)
|
|
|
|
if err != nil {
|
|
|
|
f.Fatal(err)
|
2013-10-18 02:47:08 -04:00
|
|
|
}
|
2013-10-27 03:13:45 -04:00
|
|
|
config := &DaemonConfig{
|
2013-11-13 14:25:55 -05:00
|
|
|
Root: root,
|
2013-10-27 03:13:45 -04:00
|
|
|
AutoRestart: false,
|
|
|
|
}
|
|
|
|
r, err := NewRuntimeFromDirectory(config)
|
2013-06-25 02:30:48 -04:00
|
|
|
if err != nil {
|
2013-07-11 20:59:25 -04:00
|
|
|
f.Fatal(err)
|
2013-06-25 02:30:48 -04:00
|
|
|
}
|
2013-10-27 03:13:45 -04:00
|
|
|
r.UpdateCapabilities(true)
|
|
|
|
return r
|
2013-06-25 02:30:48 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 22:20:00 -04:00
|
|
|
func createNamedTestContainer(eng *engine.Engine, config *Config, f utils.Fataler, name string) (shortId string) {
|
|
|
|
job := eng.Job("create", name)
|
|
|
|
if err := job.ImportEnv(config); err != nil {
|
|
|
|
f.Fatal(err)
|
|
|
|
}
|
|
|
|
job.StdoutParseString(&shortId)
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
f.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
2013-07-11 20:59:25 -04:00
|
|
|
}
|
|
|
|
|
2013-10-27 22:20:00 -04:00
|
|
|
func createTestContainer(eng *engine.Engine, config *Config, f utils.Fataler) (shortId string) {
|
|
|
|
return createNamedTestContainer(eng, config, f, "")
|
|
|
|
}
|
|
|
|
|
2013-10-27 03:13:45 -04:00
|
|
|
func mkServerFromEngine(eng *engine.Engine, t utils.Fataler) *Server {
|
2013-10-26 22:24:01 -04:00
|
|
|
iSrv := eng.Hack_GetGlobalVar("httpapi.server")
|
|
|
|
if iSrv == nil {
|
2013-10-27 03:16:32 -04:00
|
|
|
panic("Legacy server field not set in engine")
|
2013-10-18 02:47:08 -04:00
|
|
|
}
|
2013-10-26 22:24:01 -04:00
|
|
|
srv, ok := iSrv.(*Server)
|
|
|
|
if !ok {
|
2013-10-27 03:16:32 -04:00
|
|
|
panic("Legacy server field in engine does not cast to *Server")
|
2013-10-26 22:24:01 -04:00
|
|
|
}
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2013-10-27 03:13:45 -04:00
|
|
|
func NewTestEngine(t utils.Fataler) *engine.Engine {
|
|
|
|
root, err := newTestDirectory(unitTestStoreBase)
|
2013-07-11 20:59:25 -04:00
|
|
|
if err != nil {
|
2013-10-27 03:13:45 -04:00
|
|
|
t.Fatal(err)
|
2013-07-11 20:59:25 -04:00
|
|
|
}
|
2013-10-27 03:13:45 -04:00
|
|
|
eng, err := engine.New(root)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-11 20:59:25 -04:00
|
|
|
}
|
2013-10-27 03:13:45 -04:00
|
|
|
// Load default plugins
|
|
|
|
// (This is manually copied and modified from main() until we have a more generic plugin system)
|
|
|
|
job := eng.Job("initapi")
|
|
|
|
job.Setenv("Root", root)
|
|
|
|
job.SetenvBool("AutoRestart", false)
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-11 20:59:25 -04:00
|
|
|
}
|
2013-10-27 03:13:45 -04:00
|
|
|
return eng
|
|
|
|
}
|
2013-07-11 20:59:25 -04:00
|
|
|
|
2013-10-27 03:13:45 -04:00
|
|
|
func newTestDirectory(templateDir string) (dir string, err error) {
|
|
|
|
if globalTestID == "" {
|
|
|
|
globalTestID = GenerateID()[:4]
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2013-10-27 03:13:45 -04:00
|
|
|
prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, getCallerName(2))
|
|
|
|
if prefix == "" {
|
|
|
|
prefix = "docker-test-"
|
2013-07-11 20:59:25 -04:00
|
|
|
}
|
2013-10-27 03:13:45 -04:00
|
|
|
dir, err = ioutil.TempDir("", prefix)
|
|
|
|
if err = os.Remove(dir); err != nil {
|
|
|
|
return
|
2013-07-11 20:59:25 -04:00
|
|
|
}
|
2013-10-27 03:13:45 -04:00
|
|
|
if err = utils.CopyDirectory(templateDir, dir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCallerName(depth int) string {
|
|
|
|
// Use the caller function name as a prefix.
|
|
|
|
// This helps trace temp directories back to their test.
|
|
|
|
pc, _, _, _ := runtime.Caller(depth + 1)
|
|
|
|
callerLongName := runtime.FuncForPC(pc).Name()
|
|
|
|
parts := strings.Split(callerLongName, ".")
|
|
|
|
callerShortName := parts[len(parts)-1]
|
|
|
|
return callerShortName
|
2013-07-11 20:59:25 -04:00
|
|
|
}
|
|
|
|
|
2013-06-25 02:30:48 -04:00
|
|
|
// Write `content` to the file at path `dst`, creating it if necessary,
|
|
|
|
// as well as any missing directories.
|
|
|
|
// The file is truncated if it already exists.
|
|
|
|
// Call t.Fatal() at the first error.
|
|
|
|
func writeFile(dst, content string, t *testing.T) {
|
|
|
|
// Create subdirectories if necessary
|
|
|
|
if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-26 15:04:55 -04:00
|
|
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
|
2013-06-25 02:30:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Write content (truncate if it exists)
|
|
|
|
if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the contents of file at path `src`.
|
|
|
|
// Call t.Fatal() at the first error (including if the file doesn't exist)
|
|
|
|
func readFile(src string, t *testing.T) (content string) {
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test container from the given runtime `r` and run arguments `args`.
|
2013-07-12 20:56:55 -04:00
|
|
|
// If the image name is "_", (eg. []string{"-i", "-t", "_", "bash"}, it is
|
|
|
|
// dynamically replaced by the current test image.
|
2013-06-25 02:30:48 -04:00
|
|
|
// The caller is responsible for destroying the container.
|
|
|
|
// Call t.Fatal() at the first error.
|
2013-10-31 17:58:43 -04:00
|
|
|
func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, error) {
|
2013-06-25 02:30:48 -04:00
|
|
|
config, hostConfig, _, err := ParseRun(args, nil)
|
2013-07-12 20:56:55 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil && t != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2013-06-25 02:30:48 -04:00
|
|
|
if err != nil {
|
2013-10-31 17:58:43 -04:00
|
|
|
return nil, err
|
2013-07-12 20:56:55 -04:00
|
|
|
}
|
|
|
|
if config.Image == "_" {
|
|
|
|
config.Image = GetTestImage(r).ID
|
2013-06-25 02:30:48 -04:00
|
|
|
}
|
2013-10-28 19:58:59 -04:00
|
|
|
c, _, err := r.Create(config, "")
|
2013-06-25 02:30:48 -04:00
|
|
|
if err != nil {
|
2013-10-31 17:58:43 -04:00
|
|
|
return nil, err
|
2013-06-25 02:30:48 -04:00
|
|
|
}
|
2013-10-31 17:58:43 -04:00
|
|
|
c.hostConfig = hostConfig
|
|
|
|
return c, nil
|
2013-06-25 02:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test container, start it, wait for it to complete, destroy it,
|
|
|
|
// and return its standard output as a string.
|
2013-06-26 15:04:55 -04:00
|
|
|
// The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
|
2013-06-25 02:30:48 -04:00
|
|
|
// If t is not nil, call t.Fatal() at the first error. Otherwise return errors normally.
|
|
|
|
func runContainer(r *Runtime, args []string, t *testing.T) (output string, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil && t != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2013-10-31 17:58:43 -04:00
|
|
|
container, err := mkContainer(r, args, t)
|
2013-07-12 20:56:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-06-25 02:30:48 -04:00
|
|
|
defer r.Destroy(container)
|
|
|
|
stdout, err := container.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer stdout.Close()
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-06-25 02:30:48 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
container.Wait()
|
|
|
|
data, err := ioutil.ReadAll(stdout)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
output = string(data)
|
|
|
|
return
|
|
|
|
}
|
2013-07-15 09:12:33 -04:00
|
|
|
|
2013-07-16 11:58:23 -04:00
|
|
|
func TestCompareConfig(t *testing.T) {
|
2013-07-17 16:51:25 -04:00
|
|
|
volumes1 := make(map[string]struct{})
|
|
|
|
volumes1["/test1"] = struct{}{}
|
2013-07-16 11:58:23 -04:00
|
|
|
config1 := Config{
|
2013-07-17 16:51:25 -04:00
|
|
|
Dns: []string{"1.1.1.1", "2.2.2.2"},
|
|
|
|
PortSpecs: []string{"1111:1111", "2222:2222"},
|
|
|
|
Env: []string{"VAR1=1", "VAR2=2"},
|
|
|
|
VolumesFrom: "11111111",
|
|
|
|
Volumes: volumes1,
|
2013-07-16 11:58:23 -04:00
|
|
|
}
|
|
|
|
config2 := Config{
|
2013-07-17 16:51:25 -04:00
|
|
|
Dns: []string{"0.0.0.0", "2.2.2.2"},
|
|
|
|
PortSpecs: []string{"1111:1111", "2222:2222"},
|
|
|
|
Env: []string{"VAR1=1", "VAR2=2"},
|
|
|
|
VolumesFrom: "11111111",
|
|
|
|
Volumes: volumes1,
|
2013-07-16 11:58:23 -04:00
|
|
|
}
|
|
|
|
config3 := Config{
|
2013-07-17 16:51:25 -04:00
|
|
|
Dns: []string{"1.1.1.1", "2.2.2.2"},
|
|
|
|
PortSpecs: []string{"0000:0000", "2222:2222"},
|
|
|
|
Env: []string{"VAR1=1", "VAR2=2"},
|
|
|
|
VolumesFrom: "11111111",
|
|
|
|
Volumes: volumes1,
|
|
|
|
}
|
|
|
|
config4 := Config{
|
|
|
|
Dns: []string{"1.1.1.1", "2.2.2.2"},
|
|
|
|
PortSpecs: []string{"0000:0000", "2222:2222"},
|
|
|
|
Env: []string{"VAR1=1", "VAR2=2"},
|
|
|
|
VolumesFrom: "22222222",
|
|
|
|
Volumes: volumes1,
|
|
|
|
}
|
|
|
|
volumes2 := make(map[string]struct{})
|
|
|
|
volumes2["/test2"] = struct{}{}
|
|
|
|
config5 := Config{
|
|
|
|
Dns: []string{"1.1.1.1", "2.2.2.2"},
|
|
|
|
PortSpecs: []string{"0000:0000", "2222:2222"},
|
|
|
|
Env: []string{"VAR1=1", "VAR2=2"},
|
|
|
|
VolumesFrom: "11111111",
|
|
|
|
Volumes: volumes2,
|
2013-07-16 11:58:23 -04:00
|
|
|
}
|
|
|
|
if CompareConfig(&config1, &config2) {
|
|
|
|
t.Fatalf("CompareConfig should return false, Dns are different")
|
|
|
|
}
|
|
|
|
if CompareConfig(&config1, &config3) {
|
|
|
|
t.Fatalf("CompareConfig should return false, PortSpecs are different")
|
|
|
|
}
|
2013-07-17 16:51:25 -04:00
|
|
|
if CompareConfig(&config1, &config4) {
|
|
|
|
t.Fatalf("CompareConfig should return false, VolumesFrom are different")
|
|
|
|
}
|
|
|
|
if CompareConfig(&config1, &config5) {
|
|
|
|
t.Fatalf("CompareConfig should return false, Volumes are different")
|
|
|
|
}
|
|
|
|
if !CompareConfig(&config1, &config1) {
|
|
|
|
t.Fatalf("CompareConfig should return true")
|
|
|
|
}
|
2013-07-16 11:58:23 -04:00
|
|
|
}
|
|
|
|
|
2013-07-15 09:12:33 -04:00
|
|
|
func TestMergeConfig(t *testing.T) {
|
|
|
|
volumesImage := make(map[string]struct{})
|
|
|
|
volumesImage["/test1"] = struct{}{}
|
|
|
|
volumesImage["/test2"] = struct{}{}
|
|
|
|
configImage := &Config{
|
2013-07-17 17:06:46 -04:00
|
|
|
Dns: []string{"1.1.1.1", "2.2.2.2"},
|
|
|
|
PortSpecs: []string{"1111:1111", "2222:2222"},
|
|
|
|
Env: []string{"VAR1=1", "VAR2=2"},
|
|
|
|
VolumesFrom: "1111",
|
|
|
|
Volumes: volumesImage,
|
2013-07-15 09:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
volumesUser := make(map[string]struct{})
|
|
|
|
volumesUser["/test3"] = struct{}{}
|
|
|
|
configUser := &Config{
|
|
|
|
Dns: []string{"3.3.3.3"},
|
2013-07-18 22:47:35 -04:00
|
|
|
PortSpecs: []string{"3333:2222", "3333:3333"},
|
2013-07-15 09:12:33 -04:00
|
|
|
Env: []string{"VAR2=3", "VAR3=3"},
|
|
|
|
Volumes: volumesUser,
|
|
|
|
}
|
|
|
|
|
2013-11-07 17:31:25 -05:00
|
|
|
if err := MergeConfig(configUser, configImage); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-07-15 09:12:33 -04:00
|
|
|
|
|
|
|
if len(configUser.Dns) != 3 {
|
|
|
|
t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
|
|
|
|
}
|
|
|
|
for _, dns := range configUser.Dns {
|
|
|
|
if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
|
|
|
|
t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
if len(configUser.ExposedPorts) != 3 {
|
2013-11-07 17:31:25 -05:00
|
|
|
t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
|
2013-07-15 09:12:33 -04:00
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
for portSpecs := range configUser.ExposedPorts {
|
|
|
|
if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
|
|
|
|
t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
|
2013-07-15 09:12:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(configUser.Env) != 3 {
|
|
|
|
t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
|
|
|
|
}
|
|
|
|
for _, env := range configUser.Env {
|
|
|
|
if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
|
|
|
|
t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configUser.Volumes) != 3 {
|
|
|
|
t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
|
|
|
|
}
|
2013-07-30 18:48:20 -04:00
|
|
|
for v := range configUser.Volumes {
|
2013-07-15 09:12:33 -04:00
|
|
|
if v != "/test1" && v != "/test2" && v != "/test3" {
|
|
|
|
t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 17:06:46 -04:00
|
|
|
|
|
|
|
if configUser.VolumesFrom != "1111" {
|
|
|
|
t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom)
|
|
|
|
}
|
2013-11-07 17:31:25 -05:00
|
|
|
|
|
|
|
ports, _, err := parsePortSpecs([]string{"0000"})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
configImage2 := &Config{
|
|
|
|
ExposedPorts: ports,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := MergeConfig(configUser, configImage2); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configUser.ExposedPorts) != 4 {
|
|
|
|
t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
|
|
|
|
}
|
|
|
|
for portSpecs := range configUser.ExposedPorts {
|
|
|
|
if portSpecs.Port() != "0000" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
|
|
|
|
t.Fatalf("Expected 0000 or 1111 or 2222 or 3333, found %s", portSpecs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 09:12:33 -04:00
|
|
|
}
|
2013-07-18 23:01:39 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
func TestParseLxcConfOpt(t *testing.T) {
|
|
|
|
opts := []string{"lxc.utsname=docker", "lxc.utsname = docker "}
|
2013-07-18 23:01:39 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
for _, o := range opts {
|
|
|
|
k, v, err := parseLxcOpt(o)
|
|
|
|
if err != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if k != "lxc.utsname" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if v != "docker" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2013-07-18 23:01:39 -04:00
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2013-07-18 23:01:39 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
func TestParseNetworkOptsPrivateOnly(t *testing.T) {
|
|
|
|
ports, bindings, err := parsePortSpecs([]string{"192.168.1.100::80"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(ports) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(ports))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(bindings) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(bindings))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
for k := range ports {
|
|
|
|
if k.Proto() != "tcp" {
|
|
|
|
t.Logf("Expected tcp got %s", k.Proto())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if k.Port() != "80" {
|
|
|
|
t.Logf("Expected 80 got %s", k.Port())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
b, exists := bindings[k]
|
|
|
|
if !exists {
|
|
|
|
t.Log("Binding does not exist")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(b) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(b))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
s := b[0]
|
|
|
|
if s.HostPort != "" {
|
|
|
|
t.Logf("Expected \"\" got %s", s.HostPort)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if s.HostIp != "192.168.1.100" {
|
|
|
|
t.Fail()
|
2013-07-18 23:01:39 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2013-07-18 23:01:39 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
func TestParseNetworkOptsPublic(t *testing.T) {
|
|
|
|
ports, bindings, err := parsePortSpecs([]string{"192.168.1.100:8080:80"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-18 23:01:39 -04:00
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
if len(ports) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(ports))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(bindings) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(bindings))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
for k := range ports {
|
|
|
|
if k.Proto() != "tcp" {
|
|
|
|
t.Logf("Expected tcp got %s", k.Proto())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if k.Port() != "80" {
|
|
|
|
t.Logf("Expected 80 got %s", k.Port())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
b, exists := bindings[k]
|
|
|
|
if !exists {
|
|
|
|
t.Log("Binding does not exist")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(b) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(b))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
s := b[0]
|
|
|
|
if s.HostPort != "8080" {
|
|
|
|
t.Logf("Expected 8080 got %s", s.HostPort)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if s.HostIp != "192.168.1.100" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2013-07-18 23:01:39 -04:00
|
|
|
}
|
|
|
|
}
|
2013-08-15 19:35:03 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
func TestParseNetworkOptsUdp(t *testing.T) {
|
|
|
|
ports, bindings, err := parsePortSpecs([]string{"192.168.1.100::6000/udp"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(ports) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(ports))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(bindings) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(bindings))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
for k := range ports {
|
|
|
|
if k.Proto() != "udp" {
|
|
|
|
t.Logf("Expected udp got %s", k.Proto())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if k.Port() != "6000" {
|
|
|
|
t.Logf("Expected 6000 got %s", k.Port())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
b, exists := bindings[k]
|
|
|
|
if !exists {
|
|
|
|
t.Log("Binding does not exist")
|
2013-08-15 19:35:03 -04:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
if len(b) != 1 {
|
|
|
|
t.Logf("Expected 1 got %d", len(b))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
s := b[0]
|
|
|
|
if s.HostPort != "" {
|
|
|
|
t.Logf("Expected \"\" got %s", s.HostPort)
|
2013-08-15 19:35:03 -04:00
|
|
|
t.Fail()
|
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
if s.HostIp != "192.168.1.100" {
|
2013-08-15 19:35:03 -04:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|