2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2017-01-07 14:30:25 +00:00
|
|
|
|
|
|
|
import (
|
2018-05-22 20:12:29 +00:00
|
|
|
"os"
|
2017-05-09 21:00:31 +00:00
|
|
|
"sort"
|
2017-01-07 14:30:25 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/config"
|
2018-02-07 20:52:47 +00:00
|
|
|
"github.com/docker/docker/daemon/images"
|
2021-05-28 00:15:56 +00:00
|
|
|
"github.com/docker/docker/libnetwork"
|
2017-01-07 14:30:25 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2019-10-17 00:47:37 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-01-07 14:30:25 +00:00
|
|
|
)
|
|
|
|
|
2019-10-17 00:47:37 +00:00
|
|
|
// muteLogs suppresses logs that are generated during the test
|
|
|
|
func muteLogs() {
|
|
|
|
logrus.SetLevel(logrus.ErrorLevel)
|
|
|
|
}
|
|
|
|
|
2017-01-07 14:30:25 +00:00
|
|
|
func TestDaemonReloadLabels(t *testing.T) {
|
2018-02-02 22:18:46 +00:00
|
|
|
daemon := &Daemon{
|
|
|
|
configStore: &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
|
|
|
Labels: []string{"foo:bar"},
|
|
|
|
},
|
2017-01-07 14:30:25 +00:00
|
|
|
},
|
2018-02-07 20:52:47 +00:00
|
|
|
imageService: images.NewImageService(images.ImageServiceConfig{}),
|
2017-01-07 14:30:25 +00:00
|
|
|
}
|
2019-10-17 00:47:37 +00:00
|
|
|
muteLogs()
|
2017-01-07 14:30:25 +00:00
|
|
|
|
|
|
|
valuesSets := make(map[string]interface{})
|
|
|
|
valuesSets["labels"] = "foo:baz"
|
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
|
|
|
Labels: []string{"foo:baz"},
|
|
|
|
ValuesSet: valuesSets,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
label := daemon.configStore.Labels[0]
|
|
|
|
if label != "foo:baz" {
|
|
|
|
t.Fatalf("Expected daemon label `foo:baz`, got %s", label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 21:00:31 +00:00
|
|
|
func TestDaemonReloadAllowNondistributableArtifacts(t *testing.T) {
|
|
|
|
daemon := &Daemon{
|
2018-02-02 22:18:46 +00:00
|
|
|
configStore: &config.Config{},
|
2018-02-07 20:52:47 +00:00
|
|
|
imageService: images.NewImageService(images.ImageServiceConfig{}),
|
2017-05-09 21:00:31 +00:00
|
|
|
}
|
2019-10-17 00:47:37 +00:00
|
|
|
muteLogs()
|
2017-05-09 21:00:31 +00:00
|
|
|
|
2017-09-01 14:35:04 +00:00
|
|
|
var err error
|
2017-05-09 21:00:31 +00:00
|
|
|
// Initialize daemon with some registries.
|
2022-03-02 10:43:33 +00:00
|
|
|
daemon.registryService, err = registry.NewService(registry.ServiceOptions{
|
2017-05-09 21:00:31 +00:00
|
|
|
AllowNondistributableArtifacts: []string{
|
|
|
|
"127.0.0.0/8",
|
|
|
|
"10.10.1.11:5000",
|
|
|
|
"10.10.1.22:5000", // This will be removed during reload.
|
|
|
|
"docker1.com",
|
|
|
|
"docker2.com", // This will be removed during reload.
|
|
|
|
},
|
|
|
|
})
|
2017-09-01 14:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-09 21:00:31 +00:00
|
|
|
|
|
|
|
registries := []string{
|
|
|
|
"127.0.0.0/8",
|
|
|
|
"10.10.1.11:5000",
|
|
|
|
"10.10.1.33:5000", // This will be added during reload.
|
|
|
|
"docker1.com",
|
|
|
|
"docker3.com", // This will be added during reload.
|
|
|
|
}
|
|
|
|
|
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
|
|
|
ServiceOptions: registry.ServiceOptions{
|
|
|
|
AllowNondistributableArtifacts: registries,
|
|
|
|
},
|
|
|
|
ValuesSet: map[string]interface{}{
|
|
|
|
"allow-nondistributable-artifacts": registries,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-05-19 11:38:54 +00:00
|
|
|
var actual []string
|
2022-03-02 10:43:33 +00:00
|
|
|
serviceConfig := daemon.registryService.ServiceConfig()
|
2017-05-09 21:00:31 +00:00
|
|
|
for _, value := range serviceConfig.AllowNondistributableArtifactsCIDRs {
|
|
|
|
actual = append(actual, value.String())
|
|
|
|
}
|
2017-09-11 18:55:05 +00:00
|
|
|
actual = append(actual, serviceConfig.AllowNondistributableArtifactsHostnames...)
|
2017-05-09 21:00:31 +00:00
|
|
|
|
|
|
|
sort.Strings(registries)
|
|
|
|
sort.Strings(actual)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.DeepEqual(registries, actual))
|
2017-05-09 21:00:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 14:30:25 +00:00
|
|
|
func TestDaemonReloadMirrors(t *testing.T) {
|
2018-02-02 22:18:46 +00:00
|
|
|
daemon := &Daemon{
|
2018-02-07 20:52:47 +00:00
|
|
|
imageService: images.NewImageService(images.ImageServiceConfig{}),
|
2018-02-02 22:18:46 +00:00
|
|
|
}
|
2019-10-17 00:47:37 +00:00
|
|
|
muteLogs()
|
|
|
|
|
2017-09-01 14:35:04 +00:00
|
|
|
var err error
|
2022-03-02 10:43:33 +00:00
|
|
|
daemon.registryService, err = registry.NewService(registry.ServiceOptions{
|
2017-01-07 14:30:25 +00:00
|
|
|
InsecureRegistries: []string{},
|
|
|
|
Mirrors: []string{
|
2021-04-02 12:06:27 +00:00
|
|
|
"https://mirror.test1.example.com",
|
|
|
|
"https://mirror.test2.example.com", // this will be removed when reloading
|
|
|
|
"https://mirror.test3.example.com", // this will be removed when reloading
|
2017-01-07 14:30:25 +00:00
|
|
|
},
|
|
|
|
})
|
2017-09-01 14:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-07 14:30:25 +00:00
|
|
|
|
|
|
|
daemon.configStore = &config.Config{}
|
|
|
|
|
|
|
|
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,
|
2021-04-02 12:06:27 +00:00
|
|
|
mirrors: []string{"10.10.1.11:5000", "mirror.test1.example.com"}, // mirrors are invalid
|
2017-01-07 14:30:25 +00:00
|
|
|
after: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
valid: true,
|
2021-04-02 12:06:27 +00:00
|
|
|
mirrors: []string{"https://mirror.test1.example.com", "https://mirror.test4.example.com"},
|
|
|
|
after: []string{"https://mirror.test1.example.com/", "https://mirror.test4.example.com/"},
|
2017-01-07 14:30:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range loadMirrors {
|
|
|
|
valuesSets := make(map[string]interface{})
|
|
|
|
valuesSets["registry-mirrors"] = value.mirrors
|
|
|
|
|
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
|
|
|
ServiceOptions: registry.ServiceOptions{
|
|
|
|
Mirrors: value.mirrors,
|
|
|
|
},
|
|
|
|
ValuesSet: valuesSets,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2022-03-02 10:43:33 +00:00
|
|
|
registryService := daemon.registryService.ServiceConfig()
|
2017-01-07 14:30:25 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonReloadInsecureRegistries(t *testing.T) {
|
2018-02-02 22:18:46 +00:00
|
|
|
daemon := &Daemon{
|
2018-02-07 20:52:47 +00:00
|
|
|
imageService: images.NewImageService(images.ImageServiceConfig{}),
|
2018-02-02 22:18:46 +00:00
|
|
|
}
|
2019-10-17 00:47:37 +00:00
|
|
|
muteLogs()
|
|
|
|
|
2017-09-01 14:35:04 +00:00
|
|
|
var err error
|
2017-01-07 14:30:25 +00:00
|
|
|
// initialize daemon with existing insecure registries: "127.0.0.0/8", "10.10.1.11:5000", "10.10.1.22:5000"
|
2022-03-02 10:43:33 +00:00
|
|
|
daemon.registryService, err = registry.NewService(registry.ServiceOptions{
|
2017-01-07 14:30:25 +00:00
|
|
|
InsecureRegistries: []string{
|
|
|
|
"127.0.0.0/8",
|
|
|
|
"10.10.1.11:5000",
|
|
|
|
"10.10.1.22:5000", // this will be removed when reloading
|
2021-04-02 12:06:27 +00:00
|
|
|
"docker1.example.com",
|
|
|
|
"docker2.example.com", // this will be removed when reloading
|
2017-01-07 14:30:25 +00:00
|
|
|
},
|
|
|
|
})
|
2017-09-01 14:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-07 14:30:25 +00:00
|
|
|
|
|
|
|
daemon.configStore = &config.Config{}
|
|
|
|
|
|
|
|
insecureRegistries := []string{
|
2021-04-02 12:06:27 +00:00
|
|
|
"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.example.com", // this will be kept
|
|
|
|
"docker3.example.com", // this will be newly added
|
2017-01-07 14:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valuesSets := make(map[string]interface{})
|
|
|
|
valuesSets["insecure-registries"] = insecureRegistries
|
|
|
|
|
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
|
|
|
ServiceOptions: registry.ServiceOptions{
|
|
|
|
InsecureRegistries: insecureRegistries,
|
|
|
|
},
|
|
|
|
ValuesSet: valuesSets,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-03-02 10:43:33 +00:00
|
|
|
registries := daemon.registryService.ServiceConfig()
|
2017-01-07 14:30:25 +00:00
|
|
|
|
|
|
|
// 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
|
2021-04-02 12:06:27 +00:00
|
|
|
if value, ok := dataMap["docker2.example.com"]; ok {
|
2017-01-07 14:30:25 +00:00
|
|
|
t.Fatalf("Expected no insecure registry of docker2.com, got %d", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonReloadNotAffectOthers(t *testing.T) {
|
2018-02-02 22:18:46 +00:00
|
|
|
daemon := &Daemon{
|
2018-02-07 20:52:47 +00:00
|
|
|
imageService: images.NewImageService(images.ImageServiceConfig{}),
|
2018-02-02 22:18:46 +00:00
|
|
|
}
|
2019-10-17 00:47:37 +00:00
|
|
|
muteLogs()
|
|
|
|
|
2017-01-07 14:30:25 +00:00
|
|
|
daemon.configStore = &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
|
|
|
Labels: []string{"foo:bar"},
|
|
|
|
Debug: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
valuesSets := make(map[string]interface{})
|
|
|
|
valuesSets["labels"] = "foo:baz"
|
|
|
|
newConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
|
|
|
Labels: []string{"foo:baz"},
|
|
|
|
ValuesSet: valuesSets,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.Reload(newConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-02-21 08:53:29 +00:00
|
|
|
t.Fatal("Expected debug 'enabled', got 'disabled'")
|
2017-01-07 14:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 19:24:14 +00:00
|
|
|
func TestDaemonReloadNetworkDiagnosticPort(t *testing.T) {
|
2018-05-22 20:12:29 +00:00
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("root required")
|
|
|
|
}
|
2018-02-02 22:18:46 +00:00
|
|
|
daemon := &Daemon{
|
2018-02-07 20:52:47 +00:00
|
|
|
imageService: images.NewImageService(images.ImageServiceConfig{}),
|
2022-04-23 21:12:55 +00:00
|
|
|
configStore: &config.Config{},
|
2018-02-02 22:18:46 +00:00
|
|
|
}
|
2017-12-01 19:24:14 +00:00
|
|
|
|
|
|
|
enableConfig := &config.Config{
|
|
|
|
CommonConfig: config.CommonConfig{
|
|
|
|
NetworkDiagnosticPort: 2000,
|
2022-04-23 21:12:55 +00:00
|
|
|
ValuesSet: map[string]interface{}{
|
|
|
|
"network-diagnostic-port": 2000,
|
|
|
|
},
|
2017-12-01 19:24:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-23 21:12:55 +00:00
|
|
|
netOptions, err := daemon.networkOptions(nil, nil)
|
2017-12-01 19:24:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
controller, err := libnetwork.New(netOptions...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
daemon.netController = controller
|
|
|
|
|
|
|
|
// Enable/Disable the server for some iterations
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
enableConfig.CommonConfig.NetworkDiagnosticPort++
|
|
|
|
if err := daemon.Reload(enableConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-29 19:19:37 +00:00
|
|
|
// Check that the diagnostic is enabled
|
|
|
|
if !daemon.netController.IsDiagnosticEnabled() {
|
2022-04-23 21:12:55 +00:00
|
|
|
t.Fatalf("diagnostic should be enabled")
|
2017-12-01 19:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reload
|
2022-04-23 21:12:55 +00:00
|
|
|
if err := daemon.Reload(&config.Config{}); err != nil {
|
2017-12-01 19:24:14 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-29 19:19:37 +00:00
|
|
|
// Check that the diagnostic is disabled
|
|
|
|
if daemon.netController.IsDiagnosticEnabled() {
|
2022-04-23 21:12:55 +00:00
|
|
|
t.Fatalf("diagnostic should be disabled")
|
2017-12-01 19:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enableConfig.CommonConfig.NetworkDiagnosticPort++
|
|
|
|
// 2 times the enable should not create problems
|
|
|
|
if err := daemon.Reload(enableConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-29 19:19:37 +00:00
|
|
|
// Check that the diagnostic is enabled
|
|
|
|
if !daemon.netController.IsDiagnosticEnabled() {
|
|
|
|
t.Fatalf("diagnostic should be enable")
|
2017-12-01 19:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that another reload does not cause issues
|
|
|
|
if err := daemon.Reload(enableConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-29 19:19:37 +00:00
|
|
|
// Check that the diagnostic is enable
|
|
|
|
if !daemon.netController.IsDiagnosticEnabled() {
|
|
|
|
t.Fatalf("diagnostic should be enable")
|
2017-12-01 19:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|