2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-12-16 23:06:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-11-12 19:55:17 +00:00
|
|
|
"path/filepath"
|
2017-11-14 13:36:25 +00:00
|
|
|
"runtime"
|
2014-12-16 23:06:35 +00:00
|
|
|
"testing"
|
2015-05-26 19:01:15 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2021-05-28 00:15:56 +00:00
|
|
|
"github.com/docker/docker/libnetwork"
|
2017-05-19 22:06:46 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2018-03-22 21:11:03 +00:00
|
|
|
volumesservice "github.com/docker/docker/volume/service"
|
2016-01-05 16:48:09 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-11-29 01:06:26 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2014-12-16 23:06:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
// https://github.com/docker/docker/issues/8069
|
|
|
|
//
|
|
|
|
|
2015-12-11 17:39:28 +00:00
|
|
|
func TestGetContainer(t *testing.T) {
|
2015-11-12 19:55:17 +00:00
|
|
|
c1 := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57",
|
|
|
|
Name: "tender_bardeen",
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
c2 := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de",
|
|
|
|
Name: "drunk_hawking",
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
c3 := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "3cdbd1aa394fd68559fd1441d6eff2abfafdcba06e72d2febdba229008b0bf57",
|
|
|
|
Name: "3cdbd1aa",
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
c4 := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "75fb0b800922abdbef2d27e60abcdfaf7fb0698b2a96d22d3354da361a6ff4a5",
|
|
|
|
Name: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57",
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
c5 := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "d22d69a2b8960bf7fafdcba06e72d2febdba960bf7fafdcba06e72d2f9008b060b",
|
|
|
|
Name: "d22d69a2b896",
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 23:55:46 +00: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 23:06:35 +00:00
|
|
|
|
2017-06-30 01:56:22 +00:00
|
|
|
containersReplica, err := container.NewViewDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create ViewDB: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:12:02 +00:00
|
|
|
containersReplica.Save(c1)
|
|
|
|
containersReplica.Save(c2)
|
|
|
|
containersReplica.Save(c3)
|
|
|
|
containersReplica.Save(c4)
|
|
|
|
containersReplica.Save(c5)
|
|
|
|
|
2014-12-16 23:06:35 +00:00
|
|
|
daemon := &Daemon{
|
2017-06-30 01:56:22 +00:00
|
|
|
containers: store,
|
|
|
|
containersReplica: containersReplica,
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 00:51:04 +00: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)
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
if ctr, _ := daemon.GetContainer("3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de"); ctr != c2 {
|
2014-12-16 23:06:35 +00:00
|
|
|
t.Fatal("Should explicitly match full container IDs")
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
if ctr, _ := daemon.GetContainer("75fb0b8009"); ctr != c4 {
|
2014-12-16 23:06:35 +00:00
|
|
|
t.Fatal("Should match a partial ID")
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
if ctr, _ := daemon.GetContainer("drunk_hawking"); ctr != c2 {
|
2014-12-16 23:06:35 +00:00
|
|
|
t.Fatal("Should match a full name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// c3.Name is a partial match for both c3.ID and c2.ID
|
2015-12-11 17:39:28 +00:00
|
|
|
if c, _ := daemon.GetContainer("3cdbd1aa"); c != c3 {
|
2014-12-16 23:06:35 +00:00
|
|
|
t.Fatal("Should match a full name even though it collides with another container's ID")
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
if ctr, _ := daemon.GetContainer("d22d69a2b896"); ctr != c5 {
|
2016-07-21 10:03:37 +00: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 23:06:35 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 17:39:28 +00:00
|
|
|
if _, err := daemon.GetContainer("3cdbd1"); err == nil {
|
2014-12-16 23:06:35 +00:00
|
|
|
t.Fatal("Should return an error when provided a prefix that partially matches multiple container ID's")
|
|
|
|
}
|
|
|
|
|
2015-12-11 17:39:28 +00:00
|
|
|
if _, err := daemon.GetContainer("nothing"); err == nil {
|
2014-12-16 23:06:35 +00: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 19:01:15 +00:00
|
|
|
|
2015-11-06 22:22:48 +00:00
|
|
|
func initDaemonWithVolumeStore(tmp string) (*Daemon, error) {
|
2016-03-16 21:52:34 +00:00
|
|
|
var err error
|
2015-06-03 16:26:41 +00:00
|
|
|
daemon := &Daemon{
|
|
|
|
repository: tmp,
|
|
|
|
root: tmp,
|
2016-03-16 21:52:34 +00:00
|
|
|
}
|
2017-11-16 06:20:33 +00:00
|
|
|
daemon.volumes, err = volumesservice.NewVolumeService(tmp, nil, idtools.Identity{UID: 0, GID: 0}, daemon)
|
2016-03-16 21:52:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-06-03 16:26:41 +00:00
|
|
|
}
|
|
|
|
return daemon, nil
|
|
|
|
}
|
2015-07-28 07:36:29 +00:00
|
|
|
|
2015-11-12 19:55:17 +00: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) {
|
2018-05-22 20:12:29 +00:00
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("root required") // for chown
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
tmp, err := os.MkdirTemp("", "docker-container-test-")
|
2015-11-12 19:55:17 +00:00
|
|
|
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
|
2019-08-09 12:10:07 +00:00
|
|
|
ctr := &container.Container{Root: containerPath}
|
|
|
|
configPath, err := ctr.ConfigPath()
|
2015-11-12 19:55:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-24 10:10:50 +00:00
|
|
|
if err = os.WriteFile(configPath, []byte(config), 0644); err != nil {
|
2015-11-12 19:55:17 +00:00
|
|
|
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":""}`
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
hostConfigPath, err := ctr.HostConfigPath()
|
2015-11-12 19:55:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-24 10:10:50 +00:00
|
|
|
if err = os.WriteFile(hostConfigPath, []byte(hostConfig), 0644); err != nil {
|
2015-11-12 19:55:17 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon, err := initDaemonWithVolumeStore(tmp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:48:09 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 13:36:25 +00:00
|
|
|
|
|
|
|
func TestValidateContainerIsolation(t *testing.T) {
|
|
|
|
d := Daemon{}
|
|
|
|
|
2021-06-11 19:01:18 +00:00
|
|
|
_, err := d.verifyContainerSettings(&containertypes.HostConfig{Isolation: containertypes.Isolation("invalid")}, nil, false)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Error(err, "invalid isolation 'invalid' on "+runtime.GOOS))
|
2017-11-14 13:36:25 +00:00
|
|
|
}
|
2017-11-29 01:06:26 +00:00
|
|
|
|
|
|
|
func TestFindNetworkErrorType(t *testing.T) {
|
|
|
|
d := Daemon{}
|
2018-01-15 17:26:43 +00:00
|
|
|
_, err := d.FindNetwork("fakeNet")
|
2020-04-17 10:01:01 +00:00
|
|
|
var nsn libnetwork.ErrNoSuchNetwork
|
|
|
|
ok := errors.As(err, &nsn)
|
2017-11-29 01:06:26 +00:00
|
|
|
if !errdefs.IsNotFound(err) || !ok {
|
2018-03-13 19:28:34 +00:00
|
|
|
t.Error("The FindNetwork method MUST always return an error that implements the NotFound interface and is ErrNoSuchNetwork")
|
2017-11-29 01:06:26 +00:00
|
|
|
}
|
|
|
|
}
|