1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/runconfig/compare_test.go
Doug Davis 12b6083c8f Remove panic in nat package on invalid hostport
Closes #14621

This one grew to be much more than I expected so here's the story... :-)
- when a bad port string (e.g. xxx80) is passed into container.create()
  via the API it wasn't being checked until we tried to start the container.
- While starting the container we trid to parse 'xxx80' in nat.Int()
  and would panic on the strconv.ParseUint().  We should (almost) never panic.
- In trying to remove the panic I decided to make it so that we, instead,
  checked the string during the NewPort() constructor.  This means that
  I had to change all casts from 'string' to 'Port' to use NewPort() instead.
  Which is a good thing anyway, people shouldn't assume they know the
  internal format of types like that, in general.
- This meant I had to go and add error checks on all calls to NewPort().
  To avoid changing the testcases too much I create newPortNoError() **JUST**
  for the testcase uses where we know the port string is ok.
- After all of that I then went back and added a check during container.create()
  to check the port string so we'll report the error as soon as we get the
  data.
- If, somehow, the bad string does get into the metadata we will generate
  an error during container.start() but I can't test for that because
  the container.create() catches it now.  But I did add a testcase for that.

Signed-off-by: Doug Davis <dug@us.ibm.com>
2015-07-17 13:02:54 -07:00

124 lines
3.9 KiB
Go

package runconfig
import (
"testing"
"github.com/docker/docker/pkg/nat"
)
// Just to make life easier
func newPortNoError(proto, port string) nat.Port {
p, _ := nat.NewPort(proto, port)
return p
}
func TestCompare(t *testing.T) {
ports1 := make(nat.PortSet)
ports1[newPortNoError("tcp", "1111")] = struct{}{}
ports1[newPortNoError("tcp", "2222")] = struct{}{}
ports2 := make(nat.PortSet)
ports2[newPortNoError("tcp", "3333")] = struct{}{}
ports2[newPortNoError("tcp", "4444")] = struct{}{}
ports3 := make(nat.PortSet)
ports3[newPortNoError("tcp", "1111")] = struct{}{}
ports3[newPortNoError("tcp", "2222")] = struct{}{}
ports3[newPortNoError("tcp", "5555")] = struct{}{}
volumes1 := make(map[string]struct{})
volumes1["/test1"] = struct{}{}
volumes2 := make(map[string]struct{})
volumes2["/test2"] = struct{}{}
volumes3 := make(map[string]struct{})
volumes3["/test1"] = struct{}{}
volumes3["/test3"] = struct{}{}
envs1 := []string{"ENV1=value1", "ENV2=value2"}
envs2 := []string{"ENV1=value1", "ENV3=value3"}
entrypoint1 := &Entrypoint{parts: []string{"/bin/sh", "-c"}}
entrypoint2 := &Entrypoint{parts: []string{"/bin/sh", "-d"}}
entrypoint3 := &Entrypoint{parts: []string{"/bin/sh", "-c", "echo"}}
cmd1 := &Command{parts: []string{"/bin/sh", "-c"}}
cmd2 := &Command{parts: []string{"/bin/sh", "-d"}}
cmd3 := &Command{parts: []string{"/bin/sh", "-c", "echo"}}
labels1 := map[string]string{"LABEL1": "value1", "LABEL2": "value2"}
labels2 := map[string]string{"LABEL1": "value1", "LABEL2": "value3"}
labels3 := map[string]string{"LABEL1": "value1", "LABEL2": "value2", "LABEL3": "value3"}
sameConfigs := map[*Config]*Config{
// Empty config
&Config{}: {},
// Does not compare hostname, domainname & image
&Config{
Hostname: "host1",
Domainname: "domain1",
Image: "image1",
User: "user",
}: {
Hostname: "host2",
Domainname: "domain2",
Image: "image2",
User: "user",
},
// only OpenStdin
&Config{OpenStdin: false}: {OpenStdin: false},
// only env
&Config{Env: envs1}: {Env: envs1},
// only cmd
&Config{Cmd: cmd1}: {Cmd: cmd1},
// only labels
&Config{Labels: labels1}: {Labels: labels1},
// only exposedPorts
&Config{ExposedPorts: ports1}: {ExposedPorts: ports1},
// only entrypoints
&Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint1},
// only volumes
&Config{Volumes: volumes1}: {Volumes: volumes1},
}
differentConfigs := map[*Config]*Config{
nil: nil,
&Config{
Hostname: "host1",
Domainname: "domain1",
Image: "image1",
User: "user1",
}: {
Hostname: "host1",
Domainname: "domain1",
Image: "image1",
User: "user2",
},
// only OpenStdin
&Config{OpenStdin: false}: {OpenStdin: true},
&Config{OpenStdin: true}: {OpenStdin: false},
// only env
&Config{Env: envs1}: {Env: envs2},
// only cmd
&Config{Cmd: cmd1}: {Cmd: cmd2},
// not the same number of parts
&Config{Cmd: cmd1}: {Cmd: cmd3},
// only labels
&Config{Labels: labels1}: {Labels: labels2},
// not the same number of labels
&Config{Labels: labels1}: {Labels: labels3},
// only exposedPorts
&Config{ExposedPorts: ports1}: {ExposedPorts: ports2},
// not the same number of ports
&Config{ExposedPorts: ports1}: {ExposedPorts: ports3},
// only entrypoints
&Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint2},
// not the same number of parts
&Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint3},
// only volumes
&Config{Volumes: volumes1}: {Volumes: volumes2},
// not the same number of labels
&Config{Volumes: volumes1}: {Volumes: volumes3},
}
for config1, config2 := range sameConfigs {
if !Compare(config1, config2) {
t.Fatalf("Compare should be true for [%v] and [%v]", config1, config2)
}
}
for config1, config2 := range differentConfigs {
if Compare(config1, config2) {
t.Fatalf("Compare should be false for [%v] and [%v]", config1, config2)
}
}
}