2016-06-07 03:45:21 -04:00
|
|
|
// +build !solaris
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-11-12 14:55:17 -05:00
|
|
|
"io/ioutil"
|
2014-12-16 18:06:35 -05:00
|
|
|
"os"
|
2015-11-12 14:55:17 -05:00
|
|
|
"path/filepath"
|
2015-12-10 18:35:10 -05:00
|
|
|
"reflect"
|
2014-12-16 18:06:35 -05:00
|
|
|
"testing"
|
2015-12-10 18:35:10 -05:00
|
|
|
"time"
|
2015-05-26 15:01:15 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2017-01-23 06:23:07 -05:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2015-12-10 18:35:10 -05:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
|
|
|
_ "github.com/docker/docker/pkg/discovery/memory"
|
2015-09-03 20:51:04 -04:00
|
|
|
"github.com/docker/docker/pkg/registrar"
|
2015-05-26 15:01:15 -04:00
|
|
|
"github.com/docker/docker/pkg/truncindex"
|
2016-04-24 22:51:28 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/volume"
|
2015-06-12 09:25:32 -04:00
|
|
|
volumedrivers "github.com/docker/docker/volume/drivers"
|
2015-06-03 12:26:41 -04:00
|
|
|
"github.com/docker/docker/volume/local"
|
2015-09-18 19:58:05 -04:00
|
|
|
"github.com/docker/docker/volume/store"
|
2016-01-05 11:48:09 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2014-12-16 18:06:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
// https://github.com/docker/docker/issues/8069
|
|
|
|
//
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
func TestGetContainer(t *testing.T) {
|
2015-11-12 14:55:17 -05:00
|
|
|
c1 := &container.Container{
|
|
|
|
CommonContainer: container.CommonContainer{
|
2015-04-29 18:53:35 -04:00
|
|
|
ID: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57",
|
|
|
|
Name: "tender_bardeen",
|
|
|
|
},
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
c2 := &container.Container{
|
|
|
|
CommonContainer: container.CommonContainer{
|
2015-04-29 18:53:35 -04:00
|
|
|
ID: "3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de",
|
|
|
|
Name: "drunk_hawking",
|
|
|
|
},
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
c3 := &container.Container{
|
|
|
|
CommonContainer: container.CommonContainer{
|
2015-04-29 18:53:35 -04:00
|
|
|
ID: "3cdbd1aa394fd68559fd1441d6eff2abfafdcba06e72d2febdba229008b0bf57",
|
|
|
|
Name: "3cdbd1aa",
|
|
|
|
},
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
c4 := &container.Container{
|
|
|
|
CommonContainer: container.CommonContainer{
|
2015-04-29 18:53:35 -04:00
|
|
|
ID: "75fb0b800922abdbef2d27e60abcdfaf7fb0698b2a96d22d3354da361a6ff4a5",
|
|
|
|
Name: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57",
|
|
|
|
},
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
c5 := &container.Container{
|
|
|
|
CommonContainer: container.CommonContainer{
|
2015-04-29 18:53:35 -04:00
|
|
|
ID: "d22d69a2b8960bf7fafdcba06e72d2febdba960bf7fafdcba06e72d2f9008b060b",
|
|
|
|
Name: "d22d69a2b896",
|
|
|
|
},
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
|
|
|
|
2016-01-15 18:55:46 -05:00
|
|
|
store := container.NewMemoryStore()
|
|
|
|
store.Add(c1.ID, c1)
|
|
|
|
store.Add(c2.ID, c2)
|
|
|
|
store.Add(c3.ID, c3)
|
|
|
|
store.Add(c4.ID, c4)
|
|
|
|
store.Add(c5.ID, c5)
|
2014-12-16 18:06:35 -05:00
|
|
|
|
|
|
|
index := truncindex.NewTruncIndex([]string{})
|
|
|
|
index.Add(c1.ID)
|
|
|
|
index.Add(c2.ID)
|
|
|
|
index.Add(c3.ID)
|
|
|
|
index.Add(c4.ID)
|
|
|
|
index.Add(c5.ID)
|
|
|
|
|
|
|
|
daemon := &Daemon{
|
2015-09-03 20:51:04 -04:00
|
|
|
containers: store,
|
|
|
|
idIndex: index,
|
|
|
|
nameIndex: registrar.NewRegistrar(),
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
daemon.reserveName(c1.ID, c1.Name)
|
|
|
|
daemon.reserveName(c2.ID, c2.Name)
|
|
|
|
daemon.reserveName(c3.ID, c3.Name)
|
|
|
|
daemon.reserveName(c4.ID, c4.Name)
|
|
|
|
daemon.reserveName(c5.ID, c5.Name)
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
if container, _ := daemon.GetContainer("3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de"); container != c2 {
|
2014-12-16 18:06:35 -05:00
|
|
|
t.Fatal("Should explicitly match full container IDs")
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
if container, _ := daemon.GetContainer("75fb0b8009"); container != c4 {
|
2014-12-16 18:06:35 -05:00
|
|
|
t.Fatal("Should match a partial ID")
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
if container, _ := daemon.GetContainer("drunk_hawking"); container != c2 {
|
2014-12-16 18:06:35 -05:00
|
|
|
t.Fatal("Should match a full name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// c3.Name is a partial match for both c3.ID and c2.ID
|
2015-12-11 12:39:28 -05:00
|
|
|
if c, _ := daemon.GetContainer("3cdbd1aa"); c != c3 {
|
2014-12-16 18:06:35 -05:00
|
|
|
t.Fatal("Should match a full name even though it collides with another container's ID")
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
if container, _ := daemon.GetContainer("d22d69a2b896"); container != c5 {
|
2016-07-21 06:03:37 -04:00
|
|
|
t.Fatal("Should match a container where the provided prefix is an exact match to the its name, and is also a prefix for its ID")
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
if _, err := daemon.GetContainer("3cdbd1"); err == nil {
|
2014-12-16 18:06:35 -05:00
|
|
|
t.Fatal("Should return an error when provided a prefix that partially matches multiple container ID's")
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
if _, err := daemon.GetContainer("nothing"); err == nil {
|
2014-12-16 18:06:35 -05:00
|
|
|
t.Fatal("Should return an error when provided a prefix that is neither a name or a partial match to an ID")
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 15:01:15 -04:00
|
|
|
|
2015-11-06 17:22:48 -05:00
|
|
|
func initDaemonWithVolumeStore(tmp string) (*Daemon, error) {
|
2016-03-16 17:52:34 -04:00
|
|
|
var err error
|
2015-06-03 12:26:41 -04:00
|
|
|
daemon := &Daemon{
|
|
|
|
repository: tmp,
|
|
|
|
root: tmp,
|
2016-03-16 17:52:34 -04:00
|
|
|
}
|
|
|
|
daemon.volumes, err = store.New(tmp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-06-03 12:26:41 -04:00
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
volumesDriver, err := local.New(tmp, 0, 0)
|
2015-06-03 12:26:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
volumedrivers.Register(volumesDriver, volumesDriver.Name())
|
|
|
|
|
|
|
|
return daemon, nil
|
|
|
|
}
|
2015-07-28 03:36:29 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
func TestValidContainerNames(t *testing.T) {
|
|
|
|
invalidNames := []string{"-rm", "&sdfsfd", "safd%sd"}
|
|
|
|
validNames := []string{"word-word", "word_word", "1weoid"}
|
|
|
|
|
|
|
|
for _, name := range invalidNames {
|
|
|
|
if validContainerNamePattern.MatchString(name) {
|
|
|
|
t.Fatalf("%q is not a valid container name and was returned as valid.", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range validNames {
|
|
|
|
if !validContainerNamePattern.MatchString(name) {
|
|
|
|
t.Fatalf("%q is a valid container name and was returned as invalid.", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerInitDNS(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempDir("", "docker-container-test-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
containerID := "d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e"
|
|
|
|
containerPath := filepath.Join(tmp, containerID)
|
|
|
|
if err := os.MkdirAll(containerPath, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := `{"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":2464,"ExitCode":0,
|
|
|
|
"Error":"","StartedAt":"2015-05-26T16:48:53.869308965Z","FinishedAt":"0001-01-01T00:00:00Z"},
|
|
|
|
"ID":"d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e","Created":"2015-05-26T16:48:53.7987917Z","Path":"top",
|
|
|
|
"Args":[],"Config":{"Hostname":"d59df5276e7b","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"Cpuset":"",
|
|
|
|
"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":true,"OpenStdin":true,
|
|
|
|
"StdinOnce":false,"Env":null,"Cmd":["top"],"Image":"ubuntu:latest","Volumes":null,"WorkingDir":"","Entrypoint":null,
|
|
|
|
"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":{}},"Image":"07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95",
|
|
|
|
"NetworkSettings":{"IPAddress":"172.17.0.1","IPPrefixLen":16,"MacAddress":"02:42:ac:11:00:01","LinkLocalIPv6Address":"fe80::42:acff:fe11:1",
|
|
|
|
"LinkLocalIPv6PrefixLen":64,"GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"Gateway":"172.17.42.1","IPv6Gateway":"","Bridge":"docker0","Ports":{}},
|
|
|
|
"ResolvConfPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/resolv.conf",
|
|
|
|
"HostnamePath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hostname",
|
|
|
|
"HostsPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hosts",
|
|
|
|
"LogPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e-json.log",
|
|
|
|
"Name":"/ubuntu","Driver":"aufs","MountLabel":"","ProcessLabel":"","AppArmorProfile":"","RestartCount":0,
|
|
|
|
"UpdateDns":false,"Volumes":{},"VolumesRW":{},"AppliedVolumesFrom":null}`
|
|
|
|
|
|
|
|
// Container struct only used to retrieve path to config file
|
|
|
|
container := &container.Container{CommonContainer: container.CommonContainer{Root: containerPath}}
|
|
|
|
configPath, err := container.ConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(configPath, []byte(config), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostConfig := `{"Binds":[],"ContainerIDFile":"","Memory":0,"MemorySwap":0,"CpuShares":0,"CpusetCpus":"",
|
|
|
|
"Privileged":false,"PortBindings":{},"Links":null,"PublishAllPorts":false,"Dns":null,"DnsOptions":null,"DnsSearch":null,"ExtraHosts":null,"VolumesFrom":null,
|
|
|
|
"Devices":[],"NetworkMode":"bridge","IpcMode":"","PidMode":"","CapAdd":null,"CapDrop":null,"RestartPolicy":{"Name":"no","MaximumRetryCount":0},
|
|
|
|
"SecurityOpt":null,"ReadonlyRootfs":false,"Ulimits":null,"LogConfig":{"Type":"","Config":null},"CgroupParent":""}`
|
|
|
|
|
|
|
|
hostConfigPath, err := container.HostConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(hostConfigPath, []byte(hostConfig), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon, err := initDaemonWithVolumeStore(tmp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer volumedrivers.Unregister(volume.DefaultDriverName)
|
|
|
|
|
|
|
|
c, err := daemon.load(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.DNS == nil {
|
|
|
|
t.Fatal("Expected container DNS to not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.DNSSearch == nil {
|
|
|
|
t.Fatal("Expected container DNSSearch to not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.DNSOptions == nil {
|
|
|
|
t.Fatal("Expected container DNSOptions to not be nil")
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 11:48:09 -05:00
|
|
|
|
|
|
|
func newPortNoError(proto, port string) nat.Port {
|
|
|
|
p, _ := nat.NewPort(proto, port)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMerge(t *testing.T) {
|
|
|
|
volumesImage := make(map[string]struct{})
|
|
|
|
volumesImage["/test1"] = struct{}{}
|
|
|
|
volumesImage["/test2"] = struct{}{}
|
|
|
|
portsImage := make(nat.PortSet)
|
|
|
|
portsImage[newPortNoError("tcp", "1111")] = struct{}{}
|
|
|
|
portsImage[newPortNoError("tcp", "2222")] = struct{}{}
|
|
|
|
configImage := &containertypes.Config{
|
|
|
|
ExposedPorts: portsImage,
|
|
|
|
Env: []string{"VAR1=1", "VAR2=2"},
|
|
|
|
Volumes: volumesImage,
|
|
|
|
}
|
|
|
|
|
|
|
|
portsUser := make(nat.PortSet)
|
|
|
|
portsUser[newPortNoError("tcp", "2222")] = struct{}{}
|
|
|
|
portsUser[newPortNoError("tcp", "3333")] = struct{}{}
|
|
|
|
volumesUser := make(map[string]struct{})
|
|
|
|
volumesUser["/test3"] = struct{}{}
|
|
|
|
configUser := &containertypes.Config{
|
|
|
|
ExposedPorts: portsUser,
|
|
|
|
Env: []string{"VAR2=3", "VAR3=3"},
|
|
|
|
Volumes: volumesUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := merge(configUser, configImage); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configUser.ExposedPorts) != 3 {
|
|
|
|
t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
for v := range configUser.Volumes {
|
|
|
|
if v != "/test1" && v != "/test2" && v != "/test3" {
|
|
|
|
t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ports, _, err := nat.ParsePortSpecs([]string{"0000"})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
configImage2 := &containertypes.Config{
|
|
|
|
ExposedPorts: ports,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := merge(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() != "0" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
|
|
|
|
t.Fatalf("Expected %q or %q or %q or %q, found %s", 0, 1111, 2222, 3333, portSpecs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
func TestDaemonReloadLabels(t *testing.T) {
|
|
|
|
daemon := &Daemon{}
|
2017-01-23 06:23:07 -05:00
|
|
|
daemon.configStore = &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2015-12-10 18:35:10 -05:00
|
|
|
Labels: []string{"foo:bar"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-24 03:13:44 -05:00
|
|
|
valuesSets := make(map[string]interface{})
|
2016-04-25 09:57:28 -04:00
|
|
|
valuesSets["labels"] = "foo:baz"
|
2017-01-23 06:23:07 -05:00
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2016-02-24 03:13:44 -05:00
|
|
|
Labels: []string{"foo:baz"},
|
2017-01-23 06:23:07 -05:00
|
|
|
ValuesSet: valuesSets,
|
2015-12-10 18:35:10 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-24 22:51:28 -04:00
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
label := daemon.configStore.Labels[0]
|
|
|
|
if label != "foo:baz" {
|
|
|
|
t.Fatalf("Expected daemon label `foo:baz`, got %s", label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 05:31:05 -05:00
|
|
|
func TestDaemonReloadMirrors(t *testing.T) {
|
|
|
|
daemon := &Daemon{}
|
|
|
|
|
|
|
|
daemon.RegistryService = registry.NewService(registry.ServiceOptions{
|
|
|
|
InsecureRegistries: []string{},
|
|
|
|
Mirrors: []string{
|
|
|
|
"https://mirror.test1.com",
|
|
|
|
"https://mirror.test2.com", // this will be removed when reloading
|
|
|
|
"https://mirror.test3.com", // this will be removed when reloading
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
daemon.configStore = &config.Config{}
|
2016-12-21 05:31:05 -05:00
|
|
|
|
|
|
|
type pair struct {
|
|
|
|
valid bool
|
|
|
|
mirrors []string
|
|
|
|
after []string
|
|
|
|
}
|
|
|
|
|
|
|
|
loadMirrors := []pair{
|
|
|
|
{
|
|
|
|
valid: false,
|
|
|
|
mirrors: []string{"10.10.1.11:5000"}, // this mirror is invalid
|
|
|
|
after: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
valid: false,
|
|
|
|
mirrors: []string{"mirror.test1.com"}, // this mirror is invalid
|
|
|
|
after: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
valid: false,
|
|
|
|
mirrors: []string{"10.10.1.11:5000", "mirror.test1.com"}, // mirrors are invalid
|
|
|
|
after: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
valid: true,
|
|
|
|
mirrors: []string{"https://mirror.test1.com", "https://mirror.test4.com"},
|
|
|
|
after: []string{"https://mirror.test1.com/", "https://mirror.test4.com/"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range loadMirrors {
|
|
|
|
valuesSets := make(map[string]interface{})
|
|
|
|
valuesSets["registry-mirrors"] = value.mirrors
|
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2016-12-21 05:31:05 -05:00
|
|
|
ServiceOptions: registry.ServiceOptions{
|
|
|
|
Mirrors: value.mirrors,
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
ValuesSet: valuesSets,
|
2016-12-21 05:31:05 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := daemon.Reload(newConfig)
|
|
|
|
if !value.valid && err == nil {
|
|
|
|
// mirrors should be invalid, should be a non-nil error
|
|
|
|
t.Fatalf("Expected daemon reload error with invalid mirrors: %s, while get nil", value.mirrors)
|
|
|
|
}
|
|
|
|
|
|
|
|
if value.valid {
|
|
|
|
if err != nil {
|
|
|
|
// mirrors should be valid, should be no error
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
registryService := daemon.RegistryService.ServiceConfig()
|
|
|
|
|
|
|
|
if len(registryService.Mirrors) != len(value.after) {
|
|
|
|
t.Fatalf("Expected %d daemon mirrors %s while get %d with %s",
|
|
|
|
len(value.after),
|
|
|
|
value.after,
|
|
|
|
len(registryService.Mirrors),
|
|
|
|
registryService.Mirrors)
|
|
|
|
}
|
|
|
|
|
|
|
|
dataMap := map[string]struct{}{}
|
|
|
|
|
|
|
|
for _, mirror := range registryService.Mirrors {
|
|
|
|
if _, exist := dataMap[mirror]; !exist {
|
|
|
|
dataMap[mirror] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range value.after {
|
|
|
|
if _, exist := dataMap[address]; !exist {
|
|
|
|
t.Fatalf("Expected %s in daemon mirrors, while get none", address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-24 22:51:28 -04:00
|
|
|
func TestDaemonReloadInsecureRegistries(t *testing.T) {
|
|
|
|
daemon := &Daemon{}
|
|
|
|
// initialize daemon with existing insecure registries: "127.0.0.0/8", "10.10.1.11:5000", "10.10.1.22:5000"
|
|
|
|
daemon.RegistryService = registry.NewService(registry.ServiceOptions{
|
|
|
|
InsecureRegistries: []string{
|
|
|
|
"127.0.0.0/8",
|
|
|
|
"10.10.1.11:5000",
|
|
|
|
"10.10.1.22:5000", // this will be removed when reloading
|
|
|
|
"docker1.com",
|
|
|
|
"docker2.com", // this will be removed when reloading
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
daemon.configStore = &config.Config{}
|
2016-04-24 22:51:28 -04:00
|
|
|
|
|
|
|
insecureRegistries := []string{
|
|
|
|
"127.0.0.0/8", // this will be kept
|
|
|
|
"10.10.1.11:5000", // this will be kept
|
|
|
|
"10.10.1.33:5000", // this will be newly added
|
|
|
|
"docker1.com", // this will be kept
|
|
|
|
"docker3.com", // this will be newly added
|
|
|
|
}
|
|
|
|
|
|
|
|
valuesSets := make(map[string]interface{})
|
|
|
|
valuesSets["insecure-registries"] = insecureRegistries
|
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2016-04-24 22:51:28 -04:00
|
|
|
ServiceOptions: registry.ServiceOptions{
|
|
|
|
InsecureRegistries: insecureRegistries,
|
|
|
|
},
|
2017-01-23 06:23:07 -05:00
|
|
|
ValuesSet: valuesSets,
|
2016-04-24 22:51:28 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After Reload, daemon.RegistryService will be changed which is useful
|
|
|
|
// for registry communication in daemon.
|
|
|
|
registries := daemon.RegistryService.ServiceConfig()
|
|
|
|
|
|
|
|
// After Reload(), newConfig has come to registries.InsecureRegistryCIDRs and registries.IndexConfigs in daemon.
|
|
|
|
// Then collect registries.InsecureRegistryCIDRs in dataMap.
|
|
|
|
// When collecting, we need to convert CIDRS into string as a key,
|
|
|
|
// while the times of key appears as value.
|
|
|
|
dataMap := map[string]int{}
|
|
|
|
for _, value := range registries.InsecureRegistryCIDRs {
|
|
|
|
if _, ok := dataMap[value.String()]; !ok {
|
|
|
|
dataMap[value.String()] = 1
|
|
|
|
} else {
|
|
|
|
dataMap[value.String()]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range registries.IndexConfigs {
|
|
|
|
if _, ok := dataMap[value.Name]; !ok {
|
|
|
|
dataMap[value.Name] = 1
|
|
|
|
} else {
|
|
|
|
dataMap[value.Name]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally compare dataMap with the original insecureRegistries.
|
|
|
|
// Each value in insecureRegistries should appear in daemon's insecure registries,
|
|
|
|
// and each can only appear exactly ONCE.
|
|
|
|
for _, r := range insecureRegistries {
|
|
|
|
if value, ok := dataMap[r]; !ok {
|
|
|
|
t.Fatalf("Expected daemon insecure registry %s, got none", r)
|
|
|
|
} else if value != 1 {
|
|
|
|
t.Fatalf("Expected only 1 daemon insecure registry %s, got %d", r, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert if "10.10.1.22:5000" is removed when reloading
|
|
|
|
if value, ok := dataMap["10.10.1.22:5000"]; ok {
|
|
|
|
t.Fatalf("Expected no insecure registry of 10.10.1.22:5000, got %d", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert if "docker2.com" is removed when reloading
|
|
|
|
if value, ok := dataMap["docker2.com"]; ok {
|
|
|
|
t.Fatalf("Expected no insecure registry of docker2.com, got %d", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 03:13:44 -05:00
|
|
|
func TestDaemonReloadNotAffectOthers(t *testing.T) {
|
|
|
|
daemon := &Daemon{}
|
2017-01-23 06:23:07 -05:00
|
|
|
daemon.configStore = &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2016-02-24 03:13:44 -05:00
|
|
|
Labels: []string{"foo:bar"},
|
|
|
|
Debug: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
valuesSets := make(map[string]interface{})
|
2016-04-25 09:57:28 -04:00
|
|
|
valuesSets["labels"] = "foo:baz"
|
2017-01-23 06:23:07 -05:00
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2016-02-24 03:13:44 -05:00
|
|
|
Labels: []string{"foo:baz"},
|
2017-01-23 06:23:07 -05:00
|
|
|
ValuesSet: valuesSets,
|
2016-02-24 03:13:44 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-24 22:51:28 -04:00
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-02-24 03:13:44 -05:00
|
|
|
label := daemon.configStore.Labels[0]
|
|
|
|
if label != "foo:baz" {
|
|
|
|
t.Fatalf("Expected daemon label `foo:baz`, got %s", label)
|
|
|
|
}
|
|
|
|
debug := daemon.configStore.Debug
|
|
|
|
if !debug {
|
|
|
|
t.Fatalf("Expected debug 'enabled', got 'disabled'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
func TestDaemonDiscoveryReload(t *testing.T) {
|
|
|
|
daemon := &Daemon{}
|
2017-01-23 06:23:07 -05:00
|
|
|
daemon.configStore = &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2015-12-10 18:35:10 -05:00
|
|
|
ClusterStore: "memory://127.0.0.1",
|
|
|
|
ClusterAdvertise: "127.0.0.1:3333",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.initDiscovery(daemon.configStore); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := discovery.Entries{
|
|
|
|
&discovery.Entry{Host: "127.0.0.1", Port: "3333"},
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:13:23 -04:00
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for discovery")
|
|
|
|
case <-daemon.discoveryWatcher.ReadyCh():
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
|
|
|
ch, errCh := daemon.discoveryWatcher.Watch(stopCh)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("failed to get discovery advertisements in time")
|
|
|
|
case e := <-ch:
|
|
|
|
if !reflect.DeepEqual(e, expected) {
|
|
|
|
t.Fatalf("expected %v, got %v\n", expected, e)
|
|
|
|
}
|
|
|
|
case e := <-errCh:
|
|
|
|
t.Fatal(e)
|
|
|
|
}
|
|
|
|
|
2016-02-24 03:13:44 -05:00
|
|
|
valuesSets := make(map[string]interface{})
|
|
|
|
valuesSets["cluster-store"] = "memory://127.0.0.1:2222"
|
|
|
|
valuesSets["cluster-advertise"] = "127.0.0.1:5555"
|
2017-01-23 06:23:07 -05:00
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2015-12-10 18:35:10 -05:00
|
|
|
ClusterStore: "memory://127.0.0.1:2222",
|
|
|
|
ClusterAdvertise: "127.0.0.1:5555",
|
2017-01-23 06:23:07 -05:00
|
|
|
ValuesSet: valuesSets,
|
2015-12-10 18:35:10 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = discovery.Entries{
|
|
|
|
&discovery.Entry{Host: "127.0.0.1", Port: "5555"},
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:08:11 -05:00
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
2015-12-10 18:35:10 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-04 10:13:23 -04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for discovery")
|
|
|
|
case <-daemon.discoveryWatcher.ReadyCh():
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
ch, errCh = daemon.discoveryWatcher.Watch(stopCh)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("failed to get discovery advertisements in time")
|
|
|
|
case e := <-ch:
|
|
|
|
if !reflect.DeepEqual(e, expected) {
|
|
|
|
t.Fatalf("expected %v, got %v\n", expected, e)
|
|
|
|
}
|
|
|
|
case e := <-errCh:
|
|
|
|
t.Fatal(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonDiscoveryReloadFromEmptyDiscovery(t *testing.T) {
|
|
|
|
daemon := &Daemon{}
|
2017-01-23 06:23:07 -05:00
|
|
|
daemon.configStore = &config.Config{}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2016-02-24 03:13:44 -05:00
|
|
|
valuesSet := make(map[string]interface{})
|
|
|
|
valuesSet["cluster-store"] = "memory://127.0.0.1:2222"
|
|
|
|
valuesSet["cluster-advertise"] = "127.0.0.1:5555"
|
2017-01-23 06:23:07 -05:00
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2015-12-10 18:35:10 -05:00
|
|
|
ClusterStore: "memory://127.0.0.1:2222",
|
|
|
|
ClusterAdvertise: "127.0.0.1:5555",
|
2017-01-23 06:23:07 -05:00
|
|
|
ValuesSet: valuesSet,
|
2015-12-10 18:35:10 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := discovery.Entries{
|
|
|
|
&discovery.Entry{Host: "127.0.0.1", Port: "5555"},
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:08:11 -05:00
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
2015-12-10 18:35:10 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-04 10:13:23 -04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for discovery")
|
|
|
|
case <-daemon.discoveryWatcher.ReadyCh():
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
|
|
|
ch, errCh := daemon.discoveryWatcher.Watch(stopCh)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("failed to get discovery advertisements in time")
|
|
|
|
case e := <-ch:
|
|
|
|
if !reflect.DeepEqual(e, expected) {
|
|
|
|
t.Fatalf("expected %v, got %v\n", expected, e)
|
|
|
|
}
|
|
|
|
case e := <-errCh:
|
|
|
|
t.Fatal(e)
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 03:13:44 -05:00
|
|
|
|
|
|
|
func TestDaemonDiscoveryReloadOnlyClusterAdvertise(t *testing.T) {
|
|
|
|
daemon := &Daemon{}
|
2017-01-23 06:23:07 -05:00
|
|
|
daemon.configStore = &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2016-02-24 03:13:44 -05:00
|
|
|
ClusterStore: "memory://127.0.0.1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
valuesSets := make(map[string]interface{})
|
|
|
|
valuesSets["cluster-advertise"] = "127.0.0.1:5555"
|
2017-01-23 06:23:07 -05:00
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
2016-02-24 03:13:44 -05:00
|
|
|
ClusterAdvertise: "127.0.0.1:5555",
|
2017-01-23 06:23:07 -05:00
|
|
|
ValuesSet: valuesSets,
|
2016-02-24 03:13:44 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
expected := discovery.Entries{
|
|
|
|
&discovery.Entry{Host: "127.0.0.1", Port: "5555"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-04 10:13:23 -04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-daemon.discoveryWatcher.ReadyCh():
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("Timeout waiting for discovery")
|
|
|
|
}
|
2016-02-24 03:13:44 -05:00
|
|
|
stopCh := make(chan struct{})
|
|
|
|
defer close(stopCh)
|
|
|
|
ch, errCh := daemon.discoveryWatcher.Watch(stopCh)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("failed to get discovery advertisements in time")
|
|
|
|
case e := <-ch:
|
|
|
|
if !reflect.DeepEqual(e, expected) {
|
|
|
|
t.Fatalf("expected %v, got %v\n", expected, e)
|
|
|
|
}
|
|
|
|
case e := <-errCh:
|
|
|
|
t.Fatal(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|