2018-02-05 21:05:59 +00:00
|
|
|
package config // import "github.com/docker/docker/daemon/config"
|
2015-12-10 23:35:10 +00:00
|
|
|
|
|
|
|
import (
|
2023-11-13 11:22:04 +00:00
|
|
|
"encoding/json"
|
2015-12-10 23:35:10 +00:00
|
|
|
"os"
|
2023-01-09 16:32:36 +00:00
|
|
|
"path/filepath"
|
2022-06-06 10:29:15 +00:00
|
|
|
"reflect"
|
2015-12-10 23:35:10 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-12-21 17:41:00 +00:00
|
|
|
"dario.cat/mergo"
|
daemon: raise default minimum API version to v1.24
The daemon currently provides support for API versions all the way back
to v1.12, which is the version of the API that shipped with docker 1.0. On
Windows, the minimum supported version is v1.24.
Such old versions of the client are rare, and supporting older API versions
has accumulated significant amounts of code to remain backward-compatible
(which is largely untested, and a "best-effort" at most).
This patch updates the minimum API version to v1.24, which is the fallback
API version used when API-version negotiation fails. The intent is to start
deprecating older API versions, but no code is removed yet as part of this
patch, and a DOCKER_MIN_API_VERSION environment variable is added, which
allows overriding the minimum version (to allow restoring the behavior from
before this patch).
With this patch the daemon defaults to API v1.24 as minimum:
docker version
Client:
Version: 24.0.2
API version: 1.43
Go version: go1.20.4
Git commit: cb74dfc
Built: Thu May 25 21:50:49 2023
OS/Arch: linux/arm64
Context: default
Server:
Engine:
Version: dev
API version: 1.44 (minimum version 1.24)
Go version: go1.21.3
Git commit: 0322a29b9ef8806aaa4b45dc9d9a2ebcf0244bf4
Built: Mon Dec 4 15:22:17 2023
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: v1.7.9
GitCommit: 4f03e100cb967922bec7459a78d16ccbac9bb81d
runc:
Version: 1.1.10
GitCommit: v1.1.10-0-g18a0cb0
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Trying to use an older version of the API produces an error:
DOCKER_API_VERSION=1.23 docker version
Client:
Version: 24.0.2
API version: 1.23 (downgraded from 1.43)
Go version: go1.20.4
Git commit: cb74dfc
Built: Thu May 25 21:50:49 2023
OS/Arch: linux/arm64
Context: default
Error response from daemon: client version 1.23 is too old. Minimum supported API version is 1.24, please upgrade your client to a newer version
To restore the previous minimum, users can start the daemon with the
DOCKER_MIN_API_VERSION environment variable set:
DOCKER_MIN_API_VERSION=1.12 dockerd
API 1.12 is the oldest supported API version on Linux;
docker version
Client:
Version: 24.0.2
API version: 1.43
Go version: go1.20.4
Git commit: cb74dfc
Built: Thu May 25 21:50:49 2023
OS/Arch: linux/arm64
Context: default
Server:
Engine:
Version: dev
API version: 1.44 (minimum version 1.12)
Go version: go1.21.3
Git commit: 0322a29b9ef8806aaa4b45dc9d9a2ebcf0244bf4
Built: Mon Dec 4 15:22:17 2023
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: v1.7.9
GitCommit: 4f03e100cb967922bec7459a78d16ccbac9bb81d
runc:
Version: 1.1.10
GitCommit: v1.1.10-0-g18a0cb0
docker-init:
Version: 0.19.0
GitCommit: de40ad0
When using the `DOCKER_MIN_API_VERSION` with a version of the API that
is not supported, an error is produced when starting the daemon;
DOCKER_MIN_API_VERSION=1.11 dockerd --validate
invalid DOCKER_MIN_API_VERSION: minimum supported API version is 1.12: 1.11
DOCKER_MIN_API_VERSION=1.45 dockerd --validate
invalid DOCKER_MIN_API_VERSION: maximum supported API version is 1.44: 1.45
Specifying a malformed API version also produces the same error;
DOCKER_MIN_API_VERSION=hello dockerd --validate
invalid DOCKER_MIN_API_VERSION: minimum supported API version is 1.12: hello
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-04 12:44:21 +00:00
|
|
|
"github.com/docker/docker/api"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/ipamutils"
|
2021-05-28 00:15:56 +00:00
|
|
|
"github.com/docker/docker/opts"
|
2022-06-06 10:29:15 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2016-06-22 22:36:51 +00:00
|
|
|
"github.com/spf13/pflag"
|
2023-01-10 20:35:46 +00:00
|
|
|
"golang.org/x/text/encoding"
|
|
|
|
"golang.org/x/text/encoding/unicode"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/skip"
|
2015-12-10 23:35:10 +00:00
|
|
|
)
|
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
func makeConfigFile(t *testing.T, content string) string {
|
|
|
|
t.Helper()
|
|
|
|
name := filepath.Join(t.TempDir(), "daemon.json")
|
2023-08-24 15:59:22 +00:00
|
|
|
err := os.WriteFile(name, []byte(content), 0o666)
|
2023-01-09 23:42:13 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
func TestDaemonConfigurationNotFound(t *testing.T) {
|
|
|
|
_, err := MergeDaemonConfigurations(&Config{}, nil, "/tmp/foo-bar-baz-docker")
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.Check(t, os.IsNotExist(err), "got: %[1]T: %[1]v", err)
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonBrokenConfiguration(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{"Debug": tru`)
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
_, err := MergeDaemonConfigurations(&Config{}, nil, configFile)
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.ErrorContains(t, err, `invalid character ' ' in literal true`)
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 20:35:46 +00:00
|
|
|
// TestDaemonConfigurationUnicodeVariations feeds various variations of Unicode into the JSON parser, ensuring that we
|
|
|
|
// respect a BOM and otherwise default to UTF-8.
|
|
|
|
func TestDaemonConfigurationUnicodeVariations(t *testing.T) {
|
|
|
|
jsonData := `{"debug": true}`
|
2023-01-09 16:32:36 +00:00
|
|
|
|
2023-01-10 20:35:46 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
encoding encoding.Encoding
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "UTF-8",
|
|
|
|
encoding: unicode.UTF8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "UTF-8 (with BOM)",
|
|
|
|
encoding: unicode.UTF8BOM,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "UTF-16 (BE with BOM)",
|
|
|
|
encoding: unicode.UTF16(unicode.BigEndian, unicode.UseBOM),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "UTF-16 (LE with BOM)",
|
|
|
|
encoding: unicode.UTF16(unicode.LittleEndian, unicode.UseBOM),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
encodedJson, err := tc.encoding.NewEncoder().String(jsonData)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
configFile := makeConfigFile(t, encodedJson)
|
|
|
|
_, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
})
|
|
|
|
}
|
2023-01-09 16:32:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 23:32:31 +00:00
|
|
|
// TestDaemonConfigurationInvalidUnicode ensures that the JSON parser returns a useful error message if malformed UTF-8
|
|
|
|
// is provided.
|
|
|
|
func TestDaemonConfigurationInvalidUnicode(t *testing.T) {
|
|
|
|
configFileBOM := makeConfigFile(t, "\xef\xbb\xbf{\"debug\": true}\xff")
|
|
|
|
_, err := MergeDaemonConfigurations(&Config{}, nil, configFileBOM)
|
|
|
|
assert.ErrorIs(t, err, encoding.ErrInvalidUTF8)
|
|
|
|
|
|
|
|
configFileNoBOM := makeConfigFile(t, "{\"debug\": true}\xff")
|
|
|
|
_, err = MergeDaemonConfigurations(&Config{}, nil, configFileNoBOM)
|
|
|
|
assert.ErrorIs(t, err, encoding.ErrInvalidUTF8)
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
func TestFindConfigurationConflicts(t *testing.T) {
|
|
|
|
config := map[string]interface{}{"authorization-plugins": "foobar"}
|
2016-06-22 22:36:51 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
flags.String("authorization-plugins", "", "")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, flags.Set("authorization-plugins", "asdf"))
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(findConfigurationConflicts(config, flags), "authorization-plugins: (from flag: asdf, from file: foobar)"))
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindConfigurationConflictsWithNamedOptions(t *testing.T) {
|
|
|
|
config := map[string]interface{}{"hosts": []string{"qwer"}}
|
2016-06-22 22:36:51 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2015-12-10 23:35:10 +00:00
|
|
|
|
|
|
|
var hosts []string
|
2016-06-22 22:36:51 +00:00
|
|
|
flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, opts.ValidateHost), "host", "H", "Daemon socket(s) to connect to")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, flags.Set("host", "tcp://127.0.0.1:4444"))
|
|
|
|
assert.Check(t, flags.Set("host", "unix:///var/run/docker.sock"))
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(findConfigurationConflicts(config, flags), "hosts"))
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonConfigurationMergeConflicts(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{"debug": true}`)
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.Bool("debug", false, "")
|
2022-06-22 12:35:12 +00:00
|
|
|
assert.Check(t, flags.Set("debug", "false"))
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
_, err := MergeDaemonConfigurations(&Config{}, flags, configFile)
|
2015-12-10 23:35:10 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "debug") {
|
|
|
|
t.Fatalf("expected debug conflict, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 00:58:06 +00:00
|
|
|
func TestDaemonConfigurationMergeConcurrent(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{"max-concurrent-downloads": 1}`)
|
2017-03-02 00:58:06 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
_, err := MergeDaemonConfigurations(&Config{}, nil, configFile)
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.NilError(t, err)
|
2017-03-02 00:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDaemonConfigurationMergeConcurrentError(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{"max-concurrent-downloads": -1}`)
|
2017-03-02 00:58:06 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
_, err := MergeDaemonConfigurations(&Config{}, nil, configFile)
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.ErrorContains(t, err, `invalid max concurrent downloads: -1`)
|
2017-03-02 00:58:06 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{"tlscacert": "/etc/certificates/ca.pem"}`)
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("tlscacert", "", "")
|
2022-06-22 12:35:12 +00:00
|
|
|
assert.Check(t, flags.Set("tlscacert", "~/.docker/ca.pem"))
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
_, err := MergeDaemonConfigurations(&Config{}, flags, configFile)
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.ErrorContains(t, err, `the following directives are specified both as a flag and in the configuration file: tlscacert`)
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
2016-01-20 22:16:49 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
// TestDaemonConfigurationMergeDefaultAddressPools is a regression test for #40711.
|
2020-07-21 00:54:51 +00:00
|
|
|
func TestDaemonConfigurationMergeDefaultAddressPools(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
emptyConfigFile := makeConfigFile(t, `{}`)
|
|
|
|
configFile := makeConfigFile(t, `{"default-address-pools":[{"base": "10.123.0.0/16", "size": 24 }]}`)
|
2020-07-21 00:54:51 +00:00
|
|
|
|
|
|
|
expected := []*ipamutils.NetworkToSplit{{Base: "10.123.0.0/16", Size: 24}}
|
|
|
|
|
|
|
|
t.Run("empty config file", func(t *testing.T) {
|
2022-01-20 12:58:35 +00:00
|
|
|
conf := Config{}
|
2020-07-21 00:54:51 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
2022-06-22 12:35:12 +00:00
|
|
|
assert.Check(t, flags.Set("default-address-pool", "base=10.123.0.0/16,size=24"))
|
2020-07-21 00:54:51 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
config, err := MergeDaemonConfigurations(&conf, flags, emptyConfigFile)
|
2020-07-21 00:54:51 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("config file", func(t *testing.T) {
|
2022-01-20 12:58:35 +00:00
|
|
|
conf := Config{}
|
2020-07-21 00:54:51 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
config, err := MergeDaemonConfigurations(&conf, flags, configFile)
|
2020-07-21 00:54:51 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, config.DefaultAddressPools.Value(), expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with conflicting options", func(t *testing.T) {
|
2022-01-20 12:58:35 +00:00
|
|
|
conf := Config{}
|
2020-07-21 00:54:51 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "")
|
2022-06-22 12:35:12 +00:00
|
|
|
assert.Check(t, flags.Set("default-address-pool", "base=10.123.0.0/16,size=24"))
|
2020-07-21 00:54:51 +00:00
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
_, err := MergeDaemonConfigurations(&conf, flags, configFile)
|
2020-07-21 00:54:51 +00:00
|
|
|
assert.ErrorContains(t, err, "the following directives are specified both as a flag and in the configuration file")
|
|
|
|
assert.ErrorContains(t, err, "default-address-pools")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-20 22:16:49 +00:00
|
|
|
func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) {
|
|
|
|
config := map[string]interface{}{"tls-verify": "true"}
|
2016-06-22 22:36:51 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2016-01-20 22:16:49 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
flags.Bool("tlsverify", false, "")
|
2016-01-20 22:16:49 +00:00
|
|
|
err := findConfigurationConflicts(config, flags)
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.ErrorContains(t, err, "the following directives don't match any configuration option: tls-verify")
|
2016-01-20 22:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindConfigurationConflictsWithMergedValues(t *testing.T) {
|
|
|
|
var hosts []string
|
|
|
|
config := map[string]interface{}{"hosts": "tcp://127.0.0.1:2345"}
|
2016-06-22 22:36:51 +00:00
|
|
|
flags := pflag.NewFlagSet("base", pflag.ContinueOnError)
|
|
|
|
flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, nil), "host", "H", "")
|
2016-01-20 22:16:49 +00:00
|
|
|
|
|
|
|
err := findConfigurationConflicts(config, flags)
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.NilError(t, err)
|
2016-01-20 22:16:49 +00:00
|
|
|
|
2022-06-22 12:35:12 +00:00
|
|
|
assert.Check(t, flags.Set("host", "unix:///var/run/docker.sock"))
|
2016-01-20 22:16:49 +00:00
|
|
|
err = findConfigurationConflicts(config, flags)
|
2022-06-22 13:03:06 +00:00
|
|
|
assert.ErrorContains(t, err, "hosts: (from flag: [unix:///var/run/docker.sock], from file: tcp://127.0.0.1:2345)")
|
2016-01-20 22:16:49 +00:00
|
|
|
}
|
2016-03-11 08:50:49 +00:00
|
|
|
|
2017-01-23 11:23:07 +00:00
|
|
|
func TestValidateConfigurationErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2019-06-25 13:26:36 +00:00
|
|
|
name string
|
2022-06-06 10:29:15 +00:00
|
|
|
field string
|
2019-06-25 13:26:36 +00:00
|
|
|
config *Config
|
|
|
|
expectedErr string
|
2017-01-23 11:23:07 +00:00
|
|
|
}{
|
|
|
|
{
|
2019-06-25 13:26:36 +00:00
|
|
|
name: "single label without value",
|
2017-01-23 11:23:07 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Labels: []string{"one"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
expectedErr: "bad attribute format: one",
|
2016-03-11 08:50:49 +00:00
|
|
|
},
|
2017-01-23 11:23:07 +00:00
|
|
|
{
|
2019-06-25 13:26:36 +00:00
|
|
|
name: "multiple label without value",
|
2017-01-23 11:23:07 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Labels: []string{"foo=bar", "one"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
expectedErr: "bad attribute format: one",
|
2016-03-11 08:50:49 +00:00
|
|
|
},
|
2017-01-23 11:23:07 +00:00
|
|
|
{
|
2019-06-25 13:26:36 +00:00
|
|
|
name: "single DNSSearch",
|
2017-01-23 11:23:07 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-06 01:36:33 +00:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNSSearch: []string{"123456"},
|
|
|
|
},
|
2017-01-23 11:23:07 +00:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
expectedErr: "123456 is not a valid domain",
|
2017-01-23 11:23:07 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 13:26:36 +00:00
|
|
|
name: "multiple DNSSearch",
|
2017-01-23 11:23:07 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-06 01:36:33 +00:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNSSearch: []string{"a.b.c", "123456"},
|
|
|
|
},
|
2017-01-23 11:23:07 +00:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
expectedErr: "123456 is not a valid domain",
|
2017-01-23 11:23:07 +00:00
|
|
|
},
|
2022-06-06 14:53:25 +00:00
|
|
|
{
|
|
|
|
name: "negative MTU",
|
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2023-11-03 10:08:22 +00:00
|
|
|
BridgeConfig: BridgeConfig{
|
|
|
|
DefaultBridgeConfig: DefaultBridgeConfig{
|
|
|
|
MTU: -10,
|
|
|
|
},
|
|
|
|
},
|
2022-06-06 14:53:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: "invalid default MTU: -10",
|
|
|
|
},
|
2017-01-23 11:23:07 +00:00
|
|
|
{
|
2019-06-25 13:26:36 +00:00
|
|
|
name: "negative max-concurrent-downloads",
|
2017-01-23 11:23:07 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 20:59:54 +00:00
|
|
|
MaxConcurrentDownloads: -10,
|
2017-01-23 11:23:07 +00:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
expectedErr: "invalid max concurrent downloads: -10",
|
2017-01-23 11:23:07 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 13:26:36 +00:00
|
|
|
name: "negative max-concurrent-uploads",
|
2017-01-23 11:23:07 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 20:59:54 +00:00
|
|
|
MaxConcurrentUploads: -10,
|
2019-06-25 13:26:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: "invalid max concurrent uploads: -10",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "negative max-download-attempts",
|
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 20:59:54 +00:00
|
|
|
MaxDownloadAttempts: -10,
|
2017-01-23 11:23:07 +00:00
|
|
|
},
|
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
expectedErr: "invalid max download attempts: -10",
|
2016-03-11 08:50:49 +00:00
|
|
|
},
|
2022-04-24 20:42:05 +00:00
|
|
|
// TODO(thaJeztah) temporarily excluding this test as it assumes defaults are set before validating and applying updated configs
|
|
|
|
/*
|
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "zero max-download-attempts",
|
|
|
|
field: "MaxDownloadAttempts",
|
2022-04-24 20:42:05 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 20:59:54 +00:00
|
|
|
MaxDownloadAttempts: 0,
|
2022-04-24 20:42:05 +00:00
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
},
|
2022-04-24 20:42:05 +00:00
|
|
|
expectedErr: "invalid max download attempts: 0",
|
2019-06-25 13:26:36 +00:00
|
|
|
},
|
2022-04-24 20:42:05 +00:00
|
|
|
*/
|
2019-06-25 13:26:36 +00:00
|
|
|
{
|
|
|
|
name: "generic resource without =",
|
2017-10-30 22:23:43 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
NodeGenericResources: []string{"foo"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
expectedErr: "could not parse GenericResource: incorrect term foo, missing '=' or malformed expression",
|
2017-10-30 22:23:43 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-25 13:26:36 +00:00
|
|
|
name: "generic resource mixed named and discrete",
|
2017-10-30 22:23:43 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
NodeGenericResources: []string{"foo=bar", "foo=1"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-25 13:26:36 +00:00
|
|
|
expectedErr: "could not parse GenericResource: mixed discrete and named resources in expression 'foo=[bar 1]'",
|
2017-10-30 22:23:43 +00:00
|
|
|
},
|
2022-04-04 13:18:01 +00:00
|
|
|
{
|
|
|
|
name: "with invalid hosts",
|
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Hosts: []string{"127.0.0.1:2375/path"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: "invalid bind address (127.0.0.1:2375/path): should not contain a path element",
|
|
|
|
},
|
2022-04-22 13:59:23 +00:00
|
|
|
{
|
|
|
|
name: "with invalid log-level",
|
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
LogLevel: "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: "invalid logging level: foobar",
|
|
|
|
},
|
2016-03-11 08:50:49 +00:00
|
|
|
}
|
2017-01-23 11:23:07 +00:00
|
|
|
for _, tc := range testCases {
|
2019-06-25 13:26:36 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-06-06 19:11:08 +00:00
|
|
|
cfg, err := New()
|
|
|
|
assert.NilError(t, err)
|
2022-06-06 10:29:15 +00:00
|
|
|
if tc.field != "" {
|
|
|
|
assert.Check(t, mergo.Merge(cfg, tc.config, mergo.WithOverride, withForceOverwrite(tc.field)))
|
|
|
|
} else {
|
|
|
|
assert.Check(t, mergo.Merge(cfg, tc.config, mergo.WithOverride))
|
|
|
|
}
|
2022-06-06 19:11:08 +00:00
|
|
|
err = Validate(cfg)
|
2019-06-25 13:26:36 +00:00
|
|
|
assert.Error(t, err, tc.expectedErr)
|
|
|
|
})
|
2016-03-11 08:50:49 +00:00
|
|
|
}
|
2017-01-23 11:23:07 +00:00
|
|
|
}
|
2016-03-11 08:50:49 +00:00
|
|
|
|
2022-06-06 10:29:15 +00:00
|
|
|
func withForceOverwrite(fieldName string) func(config *mergo.Config) {
|
|
|
|
return mergo.WithTransformers(overwriteTransformer{fieldName: fieldName})
|
|
|
|
}
|
|
|
|
|
|
|
|
type overwriteTransformer struct {
|
|
|
|
fieldName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tf overwriteTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
|
|
|
|
if typ == reflect.TypeOf(CommonConfig{}) {
|
|
|
|
return func(dst, src reflect.Value) error {
|
|
|
|
dst.FieldByName(tf.fieldName).Set(src.FieldByName(tf.fieldName))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-23 11:23:07 +00:00
|
|
|
func TestValidateConfiguration(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2019-06-25 13:26:36 +00:00
|
|
|
name string
|
2022-06-06 10:29:15 +00:00
|
|
|
field string
|
2017-01-23 11:23:07 +00:00
|
|
|
config *Config
|
|
|
|
}{
|
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with label",
|
|
|
|
field: "Labels",
|
2017-01-23 11:23:07 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Labels: []string{"one=two"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with dns-search",
|
|
|
|
field: "DNSConfig",
|
2017-01-23 11:23:07 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2019-06-06 01:36:33 +00:00
|
|
|
DNSConfig: DNSConfig{
|
|
|
|
DNSSearch: []string{"a.b.c"},
|
|
|
|
},
|
2017-01-23 11:23:07 +00:00
|
|
|
},
|
|
|
|
},
|
2016-03-11 08:50:49 +00:00
|
|
|
},
|
2022-06-06 14:53:25 +00:00
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with mtu",
|
2023-07-05 12:21:04 +00:00
|
|
|
field: "MTU",
|
2022-06-06 14:53:25 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2023-11-03 10:08:22 +00:00
|
|
|
BridgeConfig: BridgeConfig{
|
|
|
|
DefaultBridgeConfig: DefaultBridgeConfig{
|
|
|
|
MTU: 1234,
|
|
|
|
},
|
|
|
|
},
|
2022-06-06 14:53:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-02 00:58:06 +00:00
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with max-concurrent-downloads",
|
|
|
|
field: "MaxConcurrentDownloads",
|
2017-03-02 00:58:06 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 20:59:54 +00:00
|
|
|
MaxConcurrentDownloads: 4,
|
2017-03-02 00:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with max-concurrent-uploads",
|
|
|
|
field: "MaxConcurrentUploads",
|
2017-03-02 00:58:06 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 20:59:54 +00:00
|
|
|
MaxConcurrentUploads: 4,
|
2017-03-02 00:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 22:23:43 +00:00
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with max-download-attempts",
|
|
|
|
field: "MaxDownloadAttempts",
|
2019-06-25 13:26:36 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
2022-04-24 20:59:54 +00:00
|
|
|
MaxDownloadAttempts: 4,
|
2019-06-25 13:26:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with multiple node generic resources",
|
|
|
|
field: "NodeGenericResources",
|
2017-10-30 22:23:43 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
NodeGenericResources: []string{"foo=bar", "foo=baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with node generic resources",
|
|
|
|
field: "NodeGenericResources",
|
2017-10-30 22:23:43 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
NodeGenericResources: []string{"foo=1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-04 13:18:01 +00:00
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with hosts",
|
|
|
|
field: "Hosts",
|
2022-04-04 13:18:01 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
Hosts: []string{"tcp://127.0.0.1:2375"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-22 13:59:23 +00:00
|
|
|
{
|
2022-06-06 10:29:15 +00:00
|
|
|
name: "with log-level warn",
|
|
|
|
field: "LogLevel",
|
2022-04-22 13:59:23 +00:00
|
|
|
config: &Config{
|
|
|
|
CommonConfig: CommonConfig{
|
|
|
|
LogLevel: "warn",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-03-11 08:50:49 +00:00
|
|
|
}
|
2017-01-23 11:23:07 +00:00
|
|
|
for _, tc := range testCases {
|
2019-06-25 13:26:36 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-06-06 10:29:15 +00:00
|
|
|
// Start with a config with all defaults set, so that we only
|
2022-06-06 19:11:08 +00:00
|
|
|
cfg, err := New()
|
|
|
|
assert.NilError(t, err)
|
2022-06-06 10:29:15 +00:00
|
|
|
assert.Check(t, mergo.Merge(cfg, tc.config, mergo.WithOverride))
|
|
|
|
|
|
|
|
// Check that the override happened :)
|
|
|
|
assert.Check(t, is.DeepEqual(cfg, tc.config, field(tc.field)))
|
2022-06-06 19:11:08 +00:00
|
|
|
err = Validate(cfg)
|
2019-06-25 13:26:36 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
})
|
2016-03-11 08:50:49 +00:00
|
|
|
}
|
2017-01-23 11:23:07 +00:00
|
|
|
}
|
2016-03-11 08:50:49 +00:00
|
|
|
|
daemon: raise default minimum API version to v1.24
The daemon currently provides support for API versions all the way back
to v1.12, which is the version of the API that shipped with docker 1.0. On
Windows, the minimum supported version is v1.24.
Such old versions of the client are rare, and supporting older API versions
has accumulated significant amounts of code to remain backward-compatible
(which is largely untested, and a "best-effort" at most).
This patch updates the minimum API version to v1.24, which is the fallback
API version used when API-version negotiation fails. The intent is to start
deprecating older API versions, but no code is removed yet as part of this
patch, and a DOCKER_MIN_API_VERSION environment variable is added, which
allows overriding the minimum version (to allow restoring the behavior from
before this patch).
With this patch the daemon defaults to API v1.24 as minimum:
docker version
Client:
Version: 24.0.2
API version: 1.43
Go version: go1.20.4
Git commit: cb74dfc
Built: Thu May 25 21:50:49 2023
OS/Arch: linux/arm64
Context: default
Server:
Engine:
Version: dev
API version: 1.44 (minimum version 1.24)
Go version: go1.21.3
Git commit: 0322a29b9ef8806aaa4b45dc9d9a2ebcf0244bf4
Built: Mon Dec 4 15:22:17 2023
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: v1.7.9
GitCommit: 4f03e100cb967922bec7459a78d16ccbac9bb81d
runc:
Version: 1.1.10
GitCommit: v1.1.10-0-g18a0cb0
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Trying to use an older version of the API produces an error:
DOCKER_API_VERSION=1.23 docker version
Client:
Version: 24.0.2
API version: 1.23 (downgraded from 1.43)
Go version: go1.20.4
Git commit: cb74dfc
Built: Thu May 25 21:50:49 2023
OS/Arch: linux/arm64
Context: default
Error response from daemon: client version 1.23 is too old. Minimum supported API version is 1.24, please upgrade your client to a newer version
To restore the previous minimum, users can start the daemon with the
DOCKER_MIN_API_VERSION environment variable set:
DOCKER_MIN_API_VERSION=1.12 dockerd
API 1.12 is the oldest supported API version on Linux;
docker version
Client:
Version: 24.0.2
API version: 1.43
Go version: go1.20.4
Git commit: cb74dfc
Built: Thu May 25 21:50:49 2023
OS/Arch: linux/arm64
Context: default
Server:
Engine:
Version: dev
API version: 1.44 (minimum version 1.12)
Go version: go1.21.3
Git commit: 0322a29b9ef8806aaa4b45dc9d9a2ebcf0244bf4
Built: Mon Dec 4 15:22:17 2023
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: v1.7.9
GitCommit: 4f03e100cb967922bec7459a78d16ccbac9bb81d
runc:
Version: 1.1.10
GitCommit: v1.1.10-0-g18a0cb0
docker-init:
Version: 0.19.0
GitCommit: de40ad0
When using the `DOCKER_MIN_API_VERSION` with a version of the API that
is not supported, an error is produced when starting the daemon;
DOCKER_MIN_API_VERSION=1.11 dockerd --validate
invalid DOCKER_MIN_API_VERSION: minimum supported API version is 1.12: 1.11
DOCKER_MIN_API_VERSION=1.45 dockerd --validate
invalid DOCKER_MIN_API_VERSION: maximum supported API version is 1.44: 1.45
Specifying a malformed API version also produces the same error;
DOCKER_MIN_API_VERSION=hello dockerd --validate
invalid DOCKER_MIN_API_VERSION: minimum supported API version is 1.12: hello
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-04 12:44:21 +00:00
|
|
|
func TestValidateMinAPIVersion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
doc string
|
|
|
|
input string
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "empty",
|
|
|
|
expectedErr: "value is empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "with prefix",
|
|
|
|
input: "v1.43",
|
|
|
|
expectedErr: `API version must be provided without "v" prefix`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "major only",
|
|
|
|
input: "1",
|
|
|
|
expectedErr: `minimum supported API version is`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "too low",
|
|
|
|
input: "1.0",
|
|
|
|
expectedErr: `minimum supported API version is`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "minor too high",
|
|
|
|
input: "1.99",
|
|
|
|
expectedErr: `maximum supported API version is`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "major too high",
|
|
|
|
input: "9.0",
|
|
|
|
expectedErr: `maximum supported API version is`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "current version",
|
|
|
|
input: api.DefaultVersion,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
err := ValidateMinAPIVersion(tc.input)
|
|
|
|
if tc.expectedErr != "" {
|
|
|
|
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
|
|
|
|
} else {
|
|
|
|
assert.Check(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:22:04 +00:00
|
|
|
func TestConfigInvalidDNS(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
doc string
|
|
|
|
input string
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "single DNS, invalid IP-address",
|
|
|
|
input: `{"dns": ["1.1.1.1o"]}`,
|
|
|
|
expectedErr: `invalid IP address: 1.1.1.1o`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "multiple DNS, invalid IP-address",
|
|
|
|
input: `{"dns": ["2.2.2.2", "1.1.1.1o"]}`,
|
|
|
|
expectedErr: `invalid IP address: 1.1.1.1o`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
var cfg Config
|
|
|
|
err := json.Unmarshal([]byte(tc.input), &cfg)
|
|
|
|
assert.Check(t, is.Error(err, tc.expectedErr))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:29:15 +00:00
|
|
|
func field(field string) cmp.Option {
|
|
|
|
tmp := reflect.TypeOf(Config{})
|
|
|
|
ignoreFields := make([]string, 0, tmp.NumField())
|
|
|
|
for i := 0; i < tmp.NumField(); i++ {
|
|
|
|
if tmp.Field(i).Name != field {
|
|
|
|
ignoreFields = append(ignoreFields, tmp.Field(i).Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cmpopts.IgnoreFields(Config{}, ignoreFields...)
|
|
|
|
}
|
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
// TestReloadSetConfigFileNotExist tests that when `--config-file` is set, and it doesn't exist the `Reload` function
|
|
|
|
// returns an error.
|
2017-10-07 21:26:50 +00:00
|
|
|
func TestReloadSetConfigFileNotExist(t *testing.T) {
|
|
|
|
configFile := "/tmp/blabla/not/exists/config.json"
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("config-file", "", "")
|
2022-06-22 12:35:12 +00:00
|
|
|
assert.Check(t, flags.Set("config-file", configFile))
|
2017-10-07 21:26:50 +00:00
|
|
|
|
|
|
|
err := Reload(configFile, flags, func(c *Config) {})
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "unable to configure the Docker daemon with file"))
|
2017-10-07 21:26:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
// TestReloadDefaultConfigNotExist tests that if the default configuration file doesn't exist the daemon still will
|
|
|
|
// still be reloaded.
|
2017-10-07 21:26:50 +00:00
|
|
|
func TestReloadDefaultConfigNotExist(t *testing.T) {
|
2018-06-11 13:32:11 +00:00
|
|
|
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
2022-04-04 12:56:48 +00:00
|
|
|
defaultConfigFile := "/tmp/blabla/not/exists/daemon.json"
|
2017-10-07 21:26:50 +00:00
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
2022-04-04 12:56:48 +00:00
|
|
|
flags.String("config-file", defaultConfigFile, "")
|
2022-06-06 14:54:21 +00:00
|
|
|
reloaded := false
|
2022-04-04 12:56:48 +00:00
|
|
|
err := Reload(defaultConfigFile, flags, func(c *Config) {
|
2017-10-07 21:26:50 +00:00
|
|
|
reloaded = true
|
|
|
|
})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, reloaded)
|
2017-10-07 21:26:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 23:42:13 +00:00
|
|
|
// TestReloadBadDefaultConfig tests that when `--config-file` is not set and the default configuration file exists and
|
|
|
|
// is bad, an error is returned.
|
2017-10-07 21:26:50 +00:00
|
|
|
func TestReloadBadDefaultConfig(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{wrong: "configuration"}`)
|
2017-10-07 21:26:50 +00:00
|
|
|
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("config-file", configFile, "")
|
2022-06-06 14:54:21 +00:00
|
|
|
reloaded := false
|
2023-01-09 23:42:13 +00:00
|
|
|
err := Reload(configFile, flags, func(c *Config) {
|
2022-06-06 14:54:21 +00:00
|
|
|
reloaded = true
|
|
|
|
})
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "unable to configure the Docker daemon with file"))
|
2022-06-06 14:54:21 +00:00
|
|
|
assert.Check(t, reloaded == false)
|
2017-10-07 21:26:50 +00:00
|
|
|
}
|
2017-11-12 02:09:28 +00:00
|
|
|
|
|
|
|
func TestReloadWithConflictingLabels(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{"labels": ["foo=bar", "foo=baz"]}`)
|
2017-11-12 02:09:28 +00:00
|
|
|
|
|
|
|
var lbls []string
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("config-file", configFile, "")
|
|
|
|
flags.StringSlice("labels", lbls, "")
|
2022-06-06 14:54:21 +00:00
|
|
|
reloaded := false
|
|
|
|
err := Reload(configFile, flags, func(c *Config) {
|
|
|
|
reloaded = true
|
|
|
|
})
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "conflict labels for foo=baz and foo=bar"))
|
2022-06-06 14:54:21 +00:00
|
|
|
assert.Check(t, reloaded == false)
|
2017-11-12 02:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReloadWithDuplicateLabels(t *testing.T) {
|
2023-01-09 23:42:13 +00:00
|
|
|
configFile := makeConfigFile(t, `{"labels": ["foo=the-same", "foo=the-same"]}`)
|
2017-11-12 02:09:28 +00:00
|
|
|
|
|
|
|
var lbls []string
|
|
|
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
flags.String("config-file", configFile, "")
|
|
|
|
flags.StringSlice("labels", lbls, "")
|
2022-06-06 14:54:21 +00:00
|
|
|
reloaded := false
|
|
|
|
err := Reload(configFile, flags, func(c *Config) {
|
|
|
|
reloaded = true
|
|
|
|
assert.Check(t, is.DeepEqual(c.Labels, []string{"foo=the-same"}))
|
|
|
|
})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2022-06-06 14:54:21 +00:00
|
|
|
assert.Check(t, reloaded)
|
2017-11-12 02:09:28 +00:00
|
|
|
}
|
2021-08-31 12:05:49 +00:00
|
|
|
|
|
|
|
func TestMaskURLCredentials(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
rawURL string
|
|
|
|
maskedURL string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
rawURL: "",
|
|
|
|
maskedURL: "",
|
|
|
|
}, {
|
|
|
|
rawURL: "invalidURL",
|
|
|
|
maskedURL: "invalidURL",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://proxy.example.com:80/",
|
|
|
|
maskedURL: "http://proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER:PASSWORD@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://PASSWORD:PASSWORD@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER:@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://:PASSWORD@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER@docker:password@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER%40docker:password@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER%40docker:pa%3Fsword@proxy.example.com:80/",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/",
|
|
|
|
}, {
|
|
|
|
rawURL: "http://USER%40docker:pa%3Fsword@proxy.example.com:80/hello%20world",
|
|
|
|
maskedURL: "http://xxxxx:xxxxx@proxy.example.com:80/hello%20world",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
maskedURL := MaskCredentials(test.rawURL)
|
|
|
|
assert.Equal(t, maskedURL, test.maskedURL)
|
|
|
|
}
|
|
|
|
}
|