opts_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package opts // import "github.com/docker/docker/opts"
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "gotest.tools/v3/assert"
  7. is "gotest.tools/v3/assert/cmp"
  8. )
  9. func TestValidateIPAddress(t *testing.T) {
  10. tests := []struct {
  11. doc string
  12. input string
  13. expectedOut string
  14. expectedErr string
  15. }{
  16. {
  17. doc: "IPv4 loopback",
  18. input: `127.0.0.1`,
  19. expectedOut: `127.0.0.1`,
  20. },
  21. {
  22. doc: "IPv4 loopback with whitespace",
  23. input: ` 127.0.0.1 `,
  24. expectedOut: `127.0.0.1`,
  25. },
  26. {
  27. doc: "IPv6 loopback long form",
  28. input: `0:0:0:0:0:0:0:1`,
  29. expectedOut: `::1`,
  30. },
  31. {
  32. doc: "IPv6 loopback",
  33. input: `::1`,
  34. expectedOut: `::1`,
  35. },
  36. {
  37. doc: "IPv6 loopback with whitespace",
  38. input: ` ::1 `,
  39. expectedOut: `::1`,
  40. },
  41. {
  42. doc: "IPv6 lowercase",
  43. input: `2001:db8::68`,
  44. expectedOut: `2001:db8::68`,
  45. },
  46. {
  47. doc: "IPv6 uppercase",
  48. input: `2001:DB8::68`,
  49. expectedOut: `2001:db8::68`,
  50. },
  51. {
  52. doc: "IPv6 with brackets",
  53. input: `[::1]`,
  54. expectedErr: `IP address is not correctly formatted: [::1]`,
  55. },
  56. {
  57. doc: "IPv4 partial",
  58. input: `127`,
  59. expectedErr: `IP address is not correctly formatted: 127`,
  60. },
  61. {
  62. doc: "random invalid string",
  63. input: `random invalid string`,
  64. expectedErr: `IP address is not correctly formatted: random invalid string`,
  65. },
  66. }
  67. for _, tc := range tests {
  68. tc := tc
  69. t.Run(tc.input, func(t *testing.T) {
  70. actualOut, actualErr := ValidateIPAddress(tc.input)
  71. assert.Check(t, is.Equal(tc.expectedOut, actualOut))
  72. if tc.expectedErr == "" {
  73. assert.Check(t, actualErr)
  74. } else {
  75. assert.Check(t, is.Error(actualErr, tc.expectedErr))
  76. }
  77. })
  78. }
  79. }
  80. func TestMapOpts(t *testing.T) {
  81. tmpMap := make(map[string]string)
  82. o := NewMapOpts(tmpMap, logOptsValidator)
  83. o.Set("max-size=1")
  84. if o.String() != "map[max-size:1]" {
  85. t.Errorf("%s != [map[max-size:1]", o.String())
  86. }
  87. o.Set("max-file=2")
  88. if len(tmpMap) != 2 {
  89. t.Errorf("map length %d != 2", len(tmpMap))
  90. }
  91. if tmpMap["max-file"] != "2" {
  92. t.Errorf("max-file = %s != 2", tmpMap["max-file"])
  93. }
  94. if tmpMap["max-size"] != "1" {
  95. t.Errorf("max-size = %s != 1", tmpMap["max-size"])
  96. }
  97. if o.Set("dummy-val=3") == nil {
  98. t.Error("validator is not being called")
  99. }
  100. }
  101. func TestListOptsWithoutValidator(t *testing.T) {
  102. o := NewListOpts(nil)
  103. o.Set("foo")
  104. if o.String() != "[foo]" {
  105. t.Errorf("%s != [foo]", o.String())
  106. }
  107. o.Set("bar")
  108. if o.Len() != 2 {
  109. t.Errorf("%d != 2", o.Len())
  110. }
  111. o.Set("bar")
  112. if o.Len() != 3 {
  113. t.Errorf("%d != 3", o.Len())
  114. }
  115. if !o.Get("bar") {
  116. t.Error(`o.Get("bar") == false`)
  117. }
  118. if o.Get("baz") {
  119. t.Error(`o.Get("baz") == true`)
  120. }
  121. o.Delete("foo")
  122. if o.String() != "[bar bar]" {
  123. t.Errorf("%s != [bar bar]", o.String())
  124. }
  125. listOpts := o.GetAll()
  126. if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
  127. t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
  128. }
  129. mapListOpts := o.GetMap()
  130. if len(mapListOpts) != 1 {
  131. t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
  132. }
  133. }
  134. func TestListOptsWithValidator(t *testing.T) {
  135. // Re-using logOptsvalidator (used by MapOpts)
  136. o := NewListOpts(logOptsValidator)
  137. o.Set("foo")
  138. if o.String() != "" {
  139. t.Errorf(`%s != ""`, o.String())
  140. }
  141. o.Set("foo=bar")
  142. if o.String() != "" {
  143. t.Errorf(`%s != ""`, o.String())
  144. }
  145. o.Set("max-file=2")
  146. if o.Len() != 1 {
  147. t.Errorf("%d != 1", o.Len())
  148. }
  149. if !o.Get("max-file=2") {
  150. t.Error(`o.Get("max-file=2") == false`)
  151. }
  152. if o.Get("baz") {
  153. t.Error(`o.Get("baz") == true`)
  154. }
  155. o.Delete("max-file=2")
  156. if o.String() != "" {
  157. t.Errorf(`%s != ""`, o.String())
  158. }
  159. }
  160. func TestValidateDNSSearch(t *testing.T) {
  161. valid := []string{
  162. `.`,
  163. `a`,
  164. `a.`,
  165. `1.foo`,
  166. `17.foo`,
  167. `foo.bar`,
  168. `foo.bar.baz`,
  169. `foo.bar.`,
  170. `foo.bar.baz`,
  171. `foo1.bar2`,
  172. `foo1.bar2.baz`,
  173. `1foo.2bar.`,
  174. `1foo.2bar.baz`,
  175. `foo-1.bar-2`,
  176. `foo-1.bar-2.baz`,
  177. `foo-1.bar-2.`,
  178. `foo-1.bar-2.baz`,
  179. `1-foo.2-bar`,
  180. `1-foo.2-bar.baz`,
  181. `1-foo.2-bar.`,
  182. `1-foo.2-bar.baz`,
  183. }
  184. invalid := []string{
  185. ``,
  186. ` `,
  187. ` `,
  188. `17`,
  189. `17.`,
  190. `.17`,
  191. `17-.`,
  192. `17-.foo`,
  193. `.foo`,
  194. `foo-.bar`,
  195. `-foo.bar`,
  196. `foo.bar-`,
  197. `foo.bar-.baz`,
  198. `foo.-bar`,
  199. `foo.-bar.baz`,
  200. `foo.bar.baz.this.should.fail.on.long.name.because.it.is.longer.thanitshouldbethis.should.fail.on.long.name.because.it.is.longer.thanitshouldbethis.should.fail.on.long.name.because.it.is.longer.thanitshouldbethis.should.fail.on.long.name.because.it.is.longer.thanitshouldbe`,
  201. }
  202. for _, domain := range valid {
  203. if ret, err := ValidateDNSSearch(domain); err != nil || ret == "" {
  204. t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
  205. }
  206. }
  207. for _, domain := range invalid {
  208. if ret, err := ValidateDNSSearch(domain); err == nil || ret != "" {
  209. t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
  210. }
  211. }
  212. }
  213. func TestValidateLabel(t *testing.T) {
  214. testCases := []struct {
  215. name string
  216. label string
  217. expectedResult string
  218. expectedErr string
  219. }{
  220. {
  221. name: "lable with bad attribute format",
  222. label: "label",
  223. expectedErr: "bad attribute format: label",
  224. },
  225. {
  226. name: "label with general format",
  227. label: "key1=value1",
  228. expectedResult: "key1=value1",
  229. },
  230. {
  231. name: "label with more than one =",
  232. label: "key1=value1=value2",
  233. expectedResult: "key1=value1=value2",
  234. },
  235. {
  236. name: "label with one more",
  237. label: "key1=value1=value2=value3",
  238. expectedResult: "key1=value1=value2=value3",
  239. },
  240. {
  241. name: "label with no reserved com.docker.*",
  242. label: "com.dockerpsychnotreserved.label=value",
  243. expectedResult: "com.dockerpsychnotreserved.label=value",
  244. },
  245. {
  246. name: "label with no reserved io.docker.*",
  247. label: "io.dockerproject.not=reserved",
  248. expectedResult: "io.dockerproject.not=reserved",
  249. },
  250. {
  251. name: "label with no reserved org.dockerproject.*",
  252. label: "org.docker.not=reserved",
  253. expectedResult: "org.docker.not=reserved",
  254. },
  255. {
  256. name: "label with reserved com.docker.*",
  257. label: "com.docker.feature=enabled",
  258. expectedErr: "label com.docker.feature=enabled is not allowed: the namespaces com.docker.*, io.docker.*, and org.dockerproject.* are reserved for internal use",
  259. },
  260. {
  261. name: "label with reserved upcase com.docker.* ",
  262. label: "COM.docker.feature=enabled",
  263. expectedErr: "label COM.docker.feature=enabled is not allowed: the namespaces com.docker.*, io.docker.*, and org.dockerproject.* are reserved for internal use",
  264. },
  265. {
  266. name: "label with reserved io.docker.*",
  267. label: "io.docker.configuration=0",
  268. expectedErr: "label io.docker.configuration=0 is not allowed: the namespaces com.docker.*, io.docker.*, and org.dockerproject.* are reserved for internal use",
  269. },
  270. {
  271. name: "label with reserved upcase io.docker.*",
  272. label: "io.DOCKER.CONFIGURATion=0",
  273. expectedErr: "label io.DOCKER.CONFIGURATion=0 is not allowed: the namespaces com.docker.*, io.docker.*, and org.dockerproject.* are reserved for internal use",
  274. },
  275. {
  276. name: "label with reserved org.dockerproject.*",
  277. label: "org.dockerproject.setting=on",
  278. expectedErr: "label org.dockerproject.setting=on is not allowed: the namespaces com.docker.*, io.docker.*, and org.dockerproject.* are reserved for internal use",
  279. },
  280. {
  281. name: "label with reserved upcase org.dockerproject.*",
  282. label: "Org.Dockerproject.Setting=on",
  283. expectedErr: "label Org.Dockerproject.Setting=on is not allowed: the namespaces com.docker.*, io.docker.*, and org.dockerproject.* are reserved for internal use",
  284. },
  285. }
  286. for _, testCase := range testCases {
  287. testCase := testCase
  288. t.Run(testCase.name, func(t *testing.T) {
  289. result, err := ValidateLabel(testCase.label)
  290. if testCase.expectedErr != "" {
  291. assert.Error(t, err, testCase.expectedErr)
  292. } else {
  293. assert.NilError(t, err)
  294. }
  295. if testCase.expectedResult != "" {
  296. assert.Check(t, is.Equal(result, testCase.expectedResult))
  297. }
  298. })
  299. }
  300. }
  301. func logOptsValidator(val string) (string, error) {
  302. allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
  303. vals := strings.Split(val, "=")
  304. if allowedKeys[vals[0]] != "" {
  305. return val, nil
  306. }
  307. return "", fmt.Errorf("invalid key %s", vals[0])
  308. }
  309. func TestNamedListOpts(t *testing.T) {
  310. var v []string
  311. o := NewNamedListOptsRef("foo-name", &v, nil)
  312. o.Set("foo")
  313. if o.String() != "[foo]" {
  314. t.Errorf("%s != [foo]", o.String())
  315. }
  316. if o.Name() != "foo-name" {
  317. t.Errorf("%s != foo-name", o.Name())
  318. }
  319. if len(v) != 1 {
  320. t.Errorf("expected foo to be in the values, got %v", v)
  321. }
  322. }
  323. func TestNamedMapOpts(t *testing.T) {
  324. tmpMap := make(map[string]string)
  325. o := NewNamedMapOpts("max-name", tmpMap, nil)
  326. o.Set("max-size=1")
  327. if o.String() != "map[max-size:1]" {
  328. t.Errorf("%s != [map[max-size:1]", o.String())
  329. }
  330. if o.Name() != "max-name" {
  331. t.Errorf("%s != max-name", o.Name())
  332. }
  333. if _, exist := tmpMap["max-size"]; !exist {
  334. t.Errorf("expected map-size to be in the values, got %v", tmpMap)
  335. }
  336. }
  337. func TestParseLink(t *testing.T) {
  338. t.Run("name and alias", func(t *testing.T) {
  339. name, alias, err := ParseLink("name:alias")
  340. assert.Check(t, err)
  341. assert.Check(t, is.Equal(name, "name"))
  342. assert.Check(t, is.Equal(alias, "alias"))
  343. })
  344. t.Run("short format", func(t *testing.T) {
  345. name, alias, err := ParseLink("name")
  346. assert.Check(t, err)
  347. assert.Check(t, is.Equal(name, "name"))
  348. assert.Check(t, is.Equal(alias, "name"))
  349. })
  350. t.Run("empty string", func(t *testing.T) {
  351. _, _, err := ParseLink("")
  352. assert.Check(t, is.Error(err, "empty string specified for links"))
  353. })
  354. t.Run("more than two colons", func(t *testing.T) {
  355. _, _, err := ParseLink("link:alias:wrong")
  356. assert.Check(t, is.Error(err, "bad format for links: link:alias:wrong"))
  357. })
  358. t.Run("legacy format", func(t *testing.T) {
  359. name, alias, err := ParseLink("/foo:/c1/bar")
  360. assert.Check(t, err)
  361. assert.Check(t, is.Equal(name, "foo"))
  362. assert.Check(t, is.Equal(alias, "bar"))
  363. })
  364. }
  365. func TestMapMapOpts(t *testing.T) {
  366. tmpMap := make(map[string]map[string]string)
  367. validator := func(val string) (string, error) {
  368. if strings.HasPrefix(val, "invalid-key=") {
  369. return "", fmt.Errorf("invalid key %s", val)
  370. }
  371. return val, nil
  372. }
  373. o := NewMapMapOpts(tmpMap, validator)
  374. o.Set("r1=k11=v11")
  375. assert.Check(t, is.DeepEqual(tmpMap, map[string]map[string]string{"r1": {"k11": "v11"}}))
  376. o.Set("r2=k21=v21")
  377. assert.Check(t, is.Len(tmpMap, 2))
  378. if err := o.Set("invalid-syntax"); err == nil {
  379. t.Error("invalid mapping syntax is not being caught")
  380. }
  381. if err := o.Set("k=invalid-syntax"); err == nil {
  382. t.Error("invalid value syntax is not being caught")
  383. }
  384. o.Set("r1=k12=v12")
  385. assert.Check(t, is.DeepEqual(tmpMap["r1"], map[string]string{"k11": "v11", "k12": "v12"}))
  386. if o.Set(`invalid-key={"k":"v"}`) == nil {
  387. t.Error("validator is not being called")
  388. }
  389. }