config_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package config
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "runtime"
  6. "strings"
  7. "testing"
  8. "github.com/docker/docker/daemon/discovery"
  9. "github.com/docker/docker/opts"
  10. "github.com/docker/docker/pkg/testutil"
  11. "github.com/spf13/pflag"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestDaemonConfigurationNotFound(t *testing.T) {
  15. _, err := MergeDaemonConfigurations(&Config{}, nil, "/tmp/foo-bar-baz-docker")
  16. if err == nil || !os.IsNotExist(err) {
  17. t.Fatalf("expected does not exist error, got %v", err)
  18. }
  19. }
  20. func TestDaemonBrokenConfiguration(t *testing.T) {
  21. f, err := ioutil.TempFile("", "docker-config-")
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. configFile := f.Name()
  26. f.Write([]byte(`{"Debug": tru`))
  27. f.Close()
  28. _, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
  29. if err == nil {
  30. t.Fatalf("expected error, got %v", err)
  31. }
  32. }
  33. func TestParseClusterAdvertiseSettings(t *testing.T) {
  34. if runtime.GOOS == "solaris" {
  35. t.Skip("ClusterSettings not supported on Solaris\n")
  36. }
  37. _, err := ParseClusterAdvertiseSettings("something", "")
  38. if err != discovery.ErrDiscoveryDisabled {
  39. t.Fatalf("expected discovery disabled error, got %v\n", err)
  40. }
  41. _, err = ParseClusterAdvertiseSettings("", "something")
  42. if err == nil {
  43. t.Fatalf("expected discovery store error, got %v\n", err)
  44. }
  45. _, err = ParseClusterAdvertiseSettings("etcd", "127.0.0.1:8080")
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. }
  50. func TestFindConfigurationConflicts(t *testing.T) {
  51. config := map[string]interface{}{"authorization-plugins": "foobar"}
  52. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  53. flags.String("authorization-plugins", "", "")
  54. assert.NoError(t, flags.Set("authorization-plugins", "asdf"))
  55. testutil.ErrorContains(t,
  56. findConfigurationConflicts(config, flags),
  57. "authorization-plugins: (from flag: asdf, from file: foobar)")
  58. }
  59. func TestFindConfigurationConflictsWithNamedOptions(t *testing.T) {
  60. config := map[string]interface{}{"hosts": []string{"qwer"}}
  61. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  62. var hosts []string
  63. flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, opts.ValidateHost), "host", "H", "Daemon socket(s) to connect to")
  64. assert.NoError(t, flags.Set("host", "tcp://127.0.0.1:4444"))
  65. assert.NoError(t, flags.Set("host", "unix:///var/run/docker.sock"))
  66. testutil.ErrorContains(t, findConfigurationConflicts(config, flags), "hosts")
  67. }
  68. func TestDaemonConfigurationMergeConflicts(t *testing.T) {
  69. f, err := ioutil.TempFile("", "docker-config-")
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. configFile := f.Name()
  74. f.Write([]byte(`{"debug": true}`))
  75. f.Close()
  76. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  77. flags.Bool("debug", false, "")
  78. flags.Set("debug", "false")
  79. _, err = MergeDaemonConfigurations(&Config{}, flags, configFile)
  80. if err == nil {
  81. t.Fatal("expected error, got nil")
  82. }
  83. if !strings.Contains(err.Error(), "debug") {
  84. t.Fatalf("expected debug conflict, got %v", err)
  85. }
  86. }
  87. func TestDaemonConfigurationMergeConcurrent(t *testing.T) {
  88. f, err := ioutil.TempFile("", "docker-config-")
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. configFile := f.Name()
  93. f.Write([]byte(`{"max-concurrent-downloads": 1}`))
  94. f.Close()
  95. _, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
  96. if err != nil {
  97. t.Fatal("expected error, got nil")
  98. }
  99. }
  100. func TestDaemonConfigurationMergeConcurrentError(t *testing.T) {
  101. f, err := ioutil.TempFile("", "docker-config-")
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. configFile := f.Name()
  106. f.Write([]byte(`{"max-concurrent-downloads": -1}`))
  107. f.Close()
  108. _, err = MergeDaemonConfigurations(&Config{}, nil, configFile)
  109. if err == nil {
  110. t.Fatalf("expected no error, got error %v", err)
  111. }
  112. }
  113. func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) {
  114. f, err := ioutil.TempFile("", "docker-config-")
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. configFile := f.Name()
  119. f.Write([]byte(`{"tlscacert": "/etc/certificates/ca.pem"}`))
  120. f.Close()
  121. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  122. flags.String("tlscacert", "", "")
  123. flags.Set("tlscacert", "~/.docker/ca.pem")
  124. _, err = MergeDaemonConfigurations(&Config{}, flags, configFile)
  125. if err == nil {
  126. t.Fatal("expected error, got nil")
  127. }
  128. if !strings.Contains(err.Error(), "tlscacert") {
  129. t.Fatalf("expected tlscacert conflict, got %v", err)
  130. }
  131. }
  132. func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) {
  133. config := map[string]interface{}{"tls-verify": "true"}
  134. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  135. flags.Bool("tlsverify", false, "")
  136. err := findConfigurationConflicts(config, flags)
  137. if err == nil {
  138. t.Fatal("expected error, got nil")
  139. }
  140. if !strings.Contains(err.Error(), "the following directives don't match any configuration option: tls-verify") {
  141. t.Fatalf("expected tls-verify conflict, got %v", err)
  142. }
  143. }
  144. func TestFindConfigurationConflictsWithMergedValues(t *testing.T) {
  145. var hosts []string
  146. config := map[string]interface{}{"hosts": "tcp://127.0.0.1:2345"}
  147. flags := pflag.NewFlagSet("base", pflag.ContinueOnError)
  148. flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, nil), "host", "H", "")
  149. err := findConfigurationConflicts(config, flags)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. flags.Set("host", "unix:///var/run/docker.sock")
  154. err = findConfigurationConflicts(config, flags)
  155. if err == nil {
  156. t.Fatal("expected error, got nil")
  157. }
  158. if !strings.Contains(err.Error(), "hosts: (from flag: [unix:///var/run/docker.sock], from file: tcp://127.0.0.1:2345)") {
  159. t.Fatalf("expected hosts conflict, got %v", err)
  160. }
  161. }
  162. func TestValidateConfigurationErrors(t *testing.T) {
  163. minusNumber := -10
  164. testCases := []struct {
  165. config *Config
  166. }{
  167. {
  168. config: &Config{
  169. CommonConfig: CommonConfig{
  170. Labels: []string{"one"},
  171. },
  172. },
  173. },
  174. {
  175. config: &Config{
  176. CommonConfig: CommonConfig{
  177. Labels: []string{"foo=bar", "one"},
  178. },
  179. },
  180. },
  181. {
  182. config: &Config{
  183. CommonConfig: CommonConfig{
  184. DNS: []string{"1.1.1.1o"},
  185. },
  186. },
  187. },
  188. {
  189. config: &Config{
  190. CommonConfig: CommonConfig{
  191. DNS: []string{"2.2.2.2", "1.1.1.1o"},
  192. },
  193. },
  194. },
  195. {
  196. config: &Config{
  197. CommonConfig: CommonConfig{
  198. DNSSearch: []string{"123456"},
  199. },
  200. },
  201. },
  202. {
  203. config: &Config{
  204. CommonConfig: CommonConfig{
  205. DNSSearch: []string{"a.b.c", "123456"},
  206. },
  207. },
  208. },
  209. {
  210. config: &Config{
  211. CommonConfig: CommonConfig{
  212. MaxConcurrentDownloads: &minusNumber,
  213. // This is weird...
  214. ValuesSet: map[string]interface{}{
  215. "max-concurrent-downloads": -1,
  216. },
  217. },
  218. },
  219. },
  220. {
  221. config: &Config{
  222. CommonConfig: CommonConfig{
  223. MaxConcurrentUploads: &minusNumber,
  224. // This is weird...
  225. ValuesSet: map[string]interface{}{
  226. "max-concurrent-uploads": -1,
  227. },
  228. },
  229. },
  230. },
  231. }
  232. for _, tc := range testCases {
  233. err := Validate(tc.config)
  234. if err == nil {
  235. t.Fatalf("expected error, got nil for config %v", tc.config)
  236. }
  237. }
  238. }
  239. func TestValidateConfiguration(t *testing.T) {
  240. minusNumber := 4
  241. testCases := []struct {
  242. config *Config
  243. }{
  244. {
  245. config: &Config{
  246. CommonConfig: CommonConfig{
  247. Labels: []string{"one=two"},
  248. },
  249. },
  250. },
  251. {
  252. config: &Config{
  253. CommonConfig: CommonConfig{
  254. DNS: []string{"1.1.1.1"},
  255. },
  256. },
  257. },
  258. {
  259. config: &Config{
  260. CommonConfig: CommonConfig{
  261. DNSSearch: []string{"a.b.c"},
  262. },
  263. },
  264. },
  265. {
  266. config: &Config{
  267. CommonConfig: CommonConfig{
  268. MaxConcurrentDownloads: &minusNumber,
  269. // This is weird...
  270. ValuesSet: map[string]interface{}{
  271. "max-concurrent-downloads": -1,
  272. },
  273. },
  274. },
  275. },
  276. {
  277. config: &Config{
  278. CommonConfig: CommonConfig{
  279. MaxConcurrentUploads: &minusNumber,
  280. // This is weird...
  281. ValuesSet: map[string]interface{}{
  282. "max-concurrent-uploads": -1,
  283. },
  284. },
  285. },
  286. },
  287. }
  288. for _, tc := range testCases {
  289. err := Validate(tc.config)
  290. if err != nil {
  291. t.Fatalf("expected no error, got error %v", err)
  292. }
  293. }
  294. }
  295. func TestModifiedDiscoverySettings(t *testing.T) {
  296. cases := []struct {
  297. current *Config
  298. modified *Config
  299. expected bool
  300. }{
  301. {
  302. current: discoveryConfig("foo", "bar", map[string]string{}),
  303. modified: discoveryConfig("foo", "bar", map[string]string{}),
  304. expected: false,
  305. },
  306. {
  307. current: discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}),
  308. modified: discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}),
  309. expected: false,
  310. },
  311. {
  312. current: discoveryConfig("foo", "bar", map[string]string{}),
  313. modified: discoveryConfig("foo", "bar", nil),
  314. expected: false,
  315. },
  316. {
  317. current: discoveryConfig("foo", "bar", nil),
  318. modified: discoveryConfig("foo", "bar", map[string]string{}),
  319. expected: false,
  320. },
  321. {
  322. current: discoveryConfig("foo", "bar", nil),
  323. modified: discoveryConfig("baz", "bar", nil),
  324. expected: true,
  325. },
  326. {
  327. current: discoveryConfig("foo", "bar", nil),
  328. modified: discoveryConfig("foo", "baz", nil),
  329. expected: true,
  330. },
  331. {
  332. current: discoveryConfig("foo", "bar", nil),
  333. modified: discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}),
  334. expected: true,
  335. },
  336. }
  337. for _, c := range cases {
  338. got := ModifiedDiscoverySettings(c.current, c.modified.ClusterStore, c.modified.ClusterAdvertise, c.modified.ClusterOpts)
  339. if c.expected != got {
  340. t.Fatalf("expected %v, got %v: current config %v, new config %v", c.expected, got, c.current, c.modified)
  341. }
  342. }
  343. }
  344. func discoveryConfig(backendAddr, advertiseAddr string, opts map[string]string) *Config {
  345. return &Config{
  346. CommonConfig: CommonConfig{
  347. ClusterStore: backendAddr,
  348. ClusterAdvertise: advertiseAddr,
  349. ClusterOpts: opts,
  350. },
  351. }
  352. }