1
0

test_docker.py 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests.unit.utils.test_docker
  4. ============================
  5. Test the funcs in salt.utils.docker and salt.utils.docker.translate
  6. '''
  7. # Import Python Libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. import copy
  10. import functools
  11. import logging
  12. import os
  13. log = logging.getLogger(__name__)
  14. # Import Salt Testing Libs
  15. from tests.support.unit import TestCase
  16. # Import salt libs
  17. import salt.config
  18. import salt.loader
  19. import salt.utils.platform
  20. import salt.utils.docker.translate.container
  21. import salt.utils.docker.translate.network
  22. from salt.utils.docker.translate import helpers as translate_helpers
  23. from salt.exceptions import CommandExecutionError
  24. # Import 3rd-party libs
  25. from salt.ext import six
  26. class Assert(object):
  27. def __init__(self, translator):
  28. self.translator = translator
  29. def __call__(self, func):
  30. self.func = func
  31. return functools.wraps(func)(
  32. lambda testcase, *args, **kwargs: self.wrap(testcase, *args, **kwargs) # pylint: disable=W0108
  33. )
  34. def wrap(self, *args, **kwargs):
  35. raise NotImplementedError
  36. def test_stringlist(self, testcase, name):
  37. alias = self.translator.ALIASES_REVMAP.get(name)
  38. # Using file paths here because "volumes" must be passed through this
  39. # set of assertions and it requires absolute paths.
  40. if salt.utils.platform.is_windows():
  41. data = [r'c:\foo', r'c:\bar', r'c:\baz']
  42. else:
  43. data = ['/foo', '/bar', '/baz']
  44. for item in (name, alias):
  45. if item is None:
  46. continue
  47. testcase.assertEqual(
  48. salt.utils.docker.translate_input(
  49. self.translator,
  50. **{item: ','.join(data)}
  51. ),
  52. testcase.apply_defaults({name: data})
  53. )
  54. testcase.assertEqual(
  55. salt.utils.docker.translate_input(
  56. self.translator,
  57. **{item: data}
  58. ),
  59. testcase.apply_defaults({name: data})
  60. )
  61. if name != 'volumes':
  62. # Test coercing to string
  63. testcase.assertEqual(
  64. salt.utils.docker.translate_input(
  65. self.translator,
  66. **{item: ['one', 2]}
  67. ),
  68. testcase.apply_defaults({name: ['one', '2']})
  69. )
  70. if alias is not None:
  71. # Test collision
  72. # sorted() used here because we want to confirm that we discard the
  73. # alias' value and go with the unsorted version.
  74. test_kwargs = {name: data, alias: sorted(data)}
  75. testcase.assertEqual(
  76. salt.utils.docker.translate_input(
  77. self.translator,
  78. ignore_collisions=True,
  79. **test_kwargs
  80. ),
  81. testcase.apply_defaults({name: test_kwargs[name]})
  82. )
  83. with testcase.assertRaisesRegex(
  84. CommandExecutionError,
  85. 'is an alias for.+cannot both be used'):
  86. salt.utils.docker.translate_input(
  87. self.translator,
  88. ignore_collisions=False,
  89. **test_kwargs
  90. )
  91. def test_key_value(self, testcase, name, delimiter):
  92. '''
  93. Common logic for key/value pair testing. IP address validation is
  94. turned off here, and must be done separately in the wrapped function.
  95. '''
  96. alias = self.translator.ALIASES_REVMAP.get(name)
  97. expected = {'foo': 'bar', 'baz': 'qux'}
  98. vals = 'foo{0}bar,baz{0}qux'.format(delimiter)
  99. for item in (name, alias):
  100. if item is None:
  101. continue
  102. for val in (vals, vals.split(',')):
  103. testcase.assertEqual(
  104. salt.utils.docker.translate_input(
  105. self.translator,
  106. validate_ip_addrs=False,
  107. **{item: val}
  108. ),
  109. testcase.apply_defaults({name: expected})
  110. )
  111. # Dictionary input
  112. testcase.assertEqual(
  113. salt.utils.docker.translate_input(
  114. self.translator,
  115. validate_ip_addrs=False,
  116. **{item: expected}
  117. ),
  118. testcase.apply_defaults({name: expected})
  119. )
  120. # "Dictlist" input from states
  121. testcase.assertEqual(
  122. salt.utils.docker.translate_input(
  123. self.translator,
  124. validate_ip_addrs=False,
  125. **{item: [{'foo': 'bar'}, {'baz': 'qux'}]}
  126. ),
  127. testcase.apply_defaults({name: expected})
  128. )
  129. if alias is not None:
  130. # Test collision
  131. test_kwargs = {name: vals, alias: 'hello{0}world'.format(delimiter)}
  132. testcase.assertEqual(
  133. salt.utils.docker.translate_input(
  134. self.translator,
  135. validate_ip_addrs=False,
  136. ignore_collisions=True,
  137. **test_kwargs
  138. ),
  139. testcase.apply_defaults({name: expected})
  140. )
  141. with testcase.assertRaisesRegex(
  142. CommandExecutionError,
  143. 'is an alias for.+cannot both be used'):
  144. salt.utils.docker.translate_input(
  145. self.translator,
  146. validate_ip_addrs=False,
  147. ignore_collisions=False,
  148. **test_kwargs
  149. )
  150. class assert_bool(Assert):
  151. '''
  152. Test a boolean value
  153. '''
  154. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  155. # Strip off the "test_" from the function name
  156. name = self.func.__name__[5:]
  157. alias = self.translator.ALIASES_REVMAP.get(name)
  158. for item in (name, alias):
  159. if item is None:
  160. continue
  161. testcase.assertEqual(
  162. salt.utils.docker.translate_input(
  163. self.translator,
  164. **{item: True}
  165. ),
  166. testcase.apply_defaults({name: True})
  167. )
  168. # These two are contrived examples, but they will test bool-ifying
  169. # a non-bool value to ensure proper input format.
  170. testcase.assertEqual(
  171. salt.utils.docker.translate_input(
  172. self.translator,
  173. **{item: 'foo'}
  174. ),
  175. testcase.apply_defaults({name: True})
  176. )
  177. testcase.assertEqual(
  178. salt.utils.docker.translate_input(
  179. self.translator,
  180. **{item: 0}
  181. ),
  182. testcase.apply_defaults({name: False})
  183. )
  184. if alias is not None:
  185. # Test collision
  186. test_kwargs = {name: True, alias: False}
  187. testcase.assertEqual(
  188. salt.utils.docker.translate_input(
  189. self.translator,
  190. ignore_collisions=True,
  191. **test_kwargs
  192. ),
  193. testcase.apply_defaults({name: test_kwargs[name]})
  194. )
  195. with testcase.assertRaisesRegex(
  196. CommandExecutionError,
  197. 'is an alias for.+cannot both be used'):
  198. salt.utils.docker.translate_input(
  199. self.translator,
  200. ignore_collisions=False,
  201. **test_kwargs
  202. )
  203. return self.func(testcase, *args, **kwargs)
  204. class assert_int(Assert):
  205. '''
  206. Test an integer value
  207. '''
  208. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  209. # Strip off the "test_" from the function name
  210. name = self.func.__name__[5:]
  211. alias = self.translator.ALIASES_REVMAP.get(name)
  212. for item in (name, alias):
  213. if item is None:
  214. continue
  215. for val in (100, '100'):
  216. testcase.assertEqual(
  217. salt.utils.docker.translate_input(
  218. self.translator,
  219. **{item: val}
  220. ),
  221. testcase.apply_defaults({name: 100})
  222. )
  223. # Error case: non-numeric value passed
  224. with testcase.assertRaisesRegex(
  225. CommandExecutionError,
  226. "'foo' is not an integer"):
  227. salt.utils.docker.translate_input(
  228. self.translator,
  229. **{item: 'foo'}
  230. )
  231. if alias is not None:
  232. # Test collision
  233. test_kwargs = {name: 100, alias: 200}
  234. testcase.assertEqual(
  235. salt.utils.docker.translate_input(
  236. self.translator,
  237. ignore_collisions=True,
  238. **test_kwargs
  239. ),
  240. testcase.apply_defaults({name: test_kwargs[name]})
  241. )
  242. with testcase.assertRaisesRegex(
  243. CommandExecutionError,
  244. 'is an alias for.+cannot both be used'):
  245. salt.utils.docker.translate_input(
  246. self.translator,
  247. ignore_collisions=False,
  248. **test_kwargs
  249. )
  250. return self.func(testcase, *args, **kwargs)
  251. class assert_string(Assert):
  252. '''
  253. Test that item is a string or is converted to one
  254. '''
  255. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  256. # Strip off the "test_" from the function name
  257. name = self.func.__name__[5:]
  258. alias = self.translator.ALIASES_REVMAP.get(name)
  259. # Using file paths here because "working_dir" must be passed through
  260. # this set of assertions and it requires absolute paths.
  261. if salt.utils.platform.is_windows():
  262. data = r'c:\foo'
  263. else:
  264. data = '/foo'
  265. for item in (name, alias):
  266. if item is None:
  267. continue
  268. testcase.assertEqual(
  269. salt.utils.docker.translate_input(
  270. self.translator,
  271. **{item: data}
  272. ),
  273. testcase.apply_defaults({name: data})
  274. )
  275. if name != 'working_dir':
  276. # Test coercing to string
  277. testcase.assertEqual(
  278. salt.utils.docker.translate_input(
  279. self.translator,
  280. **{item: 123}
  281. ),
  282. testcase.apply_defaults({name: '123'})
  283. )
  284. if alias is not None:
  285. # Test collision
  286. test_kwargs = {name: data, alias: data}
  287. testcase.assertEqual(
  288. salt.utils.docker.translate_input(
  289. self.translator,
  290. ignore_collisions=True,
  291. **test_kwargs
  292. ),
  293. testcase.apply_defaults({name: test_kwargs[name]})
  294. )
  295. with testcase.assertRaisesRegex(
  296. CommandExecutionError,
  297. 'is an alias for.+cannot both be used'):
  298. salt.utils.docker.translate_input(
  299. self.translator,
  300. ignore_collisions=False,
  301. **test_kwargs
  302. )
  303. return self.func(testcase, *args, **kwargs)
  304. class assert_int_or_string(Assert):
  305. '''
  306. Test an integer or string value
  307. '''
  308. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  309. # Strip off the "test_" from the function name
  310. name = self.func.__name__[5:]
  311. alias = self.translator.ALIASES_REVMAP.get(name)
  312. for item in (name, alias):
  313. if item is None:
  314. continue
  315. testcase.assertEqual(
  316. salt.utils.docker.translate_input(
  317. self.translator,
  318. **{item: 100}
  319. ),
  320. testcase.apply_defaults({name: 100})
  321. )
  322. testcase.assertEqual(
  323. salt.utils.docker.translate_input(
  324. self.translator,
  325. **{item: '100M'}
  326. ),
  327. testcase.apply_defaults({name: '100M'})
  328. )
  329. if alias is not None:
  330. # Test collision
  331. test_kwargs = {name: 100, alias: '100M'}
  332. testcase.assertEqual(
  333. salt.utils.docker.translate_input(
  334. self.translator,
  335. ignore_collisions=True,
  336. **test_kwargs
  337. ),
  338. testcase.apply_defaults({name: test_kwargs[name]})
  339. )
  340. with testcase.assertRaisesRegex(
  341. CommandExecutionError,
  342. 'is an alias for.+cannot both be used'):
  343. salt.utils.docker.translate_input(
  344. self.translator,
  345. ignore_collisions=False,
  346. **test_kwargs
  347. )
  348. return self.func(testcase, *args, **kwargs)
  349. class assert_stringlist(Assert):
  350. '''
  351. Test a comma-separated or Python list of strings
  352. '''
  353. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  354. # Strip off the "test_" from the function name
  355. name = self.func.__name__[5:]
  356. self.test_stringlist(testcase, name)
  357. return self.func(testcase, *args, **kwargs)
  358. class assert_dict(Assert):
  359. '''
  360. Dictionaries should be untouched, dictlists should be repacked and end up
  361. as a single dictionary.
  362. '''
  363. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  364. # Strip off the "test_" from the function name
  365. name = self.func.__name__[5:]
  366. alias = self.translator.ALIASES_REVMAP.get(name)
  367. expected = {'foo': 'bar', 'baz': 'qux'}
  368. for item in (name, alias):
  369. if item is None:
  370. continue
  371. testcase.assertEqual(
  372. salt.utils.docker.translate_input(
  373. self.translator,
  374. **{item: expected}
  375. ),
  376. testcase.apply_defaults({name: expected})
  377. )
  378. # "Dictlist" input from states
  379. testcase.assertEqual(
  380. salt.utils.docker.translate_input(
  381. self.translator,
  382. **{item: [{x: y} for x, y in six.iteritems(expected)]}
  383. ),
  384. testcase.apply_defaults({name: expected})
  385. )
  386. # Error case: non-dictionary input
  387. with testcase.assertRaisesRegex(
  388. CommandExecutionError,
  389. "'foo' is not a dictionary"):
  390. salt.utils.docker.translate_input(
  391. self.translator,
  392. **{item: 'foo'}
  393. )
  394. if alias is not None:
  395. # Test collision
  396. test_kwargs = {name: 'foo', alias: 'bar'}
  397. testcase.assertEqual(
  398. salt.utils.docker.translate_input(
  399. self.translator,
  400. ignore_collisions=True,
  401. **test_kwargs
  402. ),
  403. testcase.apply_defaults({name: test_kwargs[name]})
  404. )
  405. with testcase.assertRaisesRegex(
  406. CommandExecutionError,
  407. 'is an alias for.+cannot both be used'):
  408. salt.utils.docker.translate_input(
  409. self.translator,
  410. ignore_collisions=False,
  411. **test_kwargs
  412. )
  413. return self.func(testcase, *args, **kwargs)
  414. class assert_cmd(Assert):
  415. '''
  416. Test for a string, or a comma-separated or Python list of strings. This is
  417. different from a stringlist in that we do not do any splitting. This
  418. decorator is used both by the "command" and "entrypoint" arguments.
  419. '''
  420. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  421. # Strip off the "test_" from the function name
  422. name = self.func.__name__[5:]
  423. alias = self.translator.ALIASES_REVMAP.get(name)
  424. for item in (name, alias):
  425. if item is None:
  426. continue
  427. testcase.assertEqual(
  428. salt.utils.docker.translate_input(
  429. self.translator,
  430. **{item: 'foo bar'}
  431. ),
  432. testcase.apply_defaults({name: 'foo bar'})
  433. )
  434. testcase.assertEqual(
  435. salt.utils.docker.translate_input(
  436. self.translator,
  437. **{item: ['foo', 'bar']}
  438. ),
  439. testcase.apply_defaults({name: ['foo', 'bar']})
  440. )
  441. # Test coercing to string
  442. testcase.assertEqual(
  443. salt.utils.docker.translate_input(
  444. self.translator,
  445. **{item: 123}
  446. ),
  447. testcase.apply_defaults({name: '123'})
  448. )
  449. testcase.assertEqual(
  450. salt.utils.docker.translate_input(
  451. self.translator,
  452. **{item: ['one', 2]}
  453. ),
  454. testcase.apply_defaults({name: ['one', '2']})
  455. )
  456. if alias is not None:
  457. # Test collision
  458. test_kwargs = {name: 'foo', alias: 'bar'}
  459. testcase.assertEqual(
  460. salt.utils.docker.translate_input(
  461. self.translator,
  462. ignore_collisions=True,
  463. **test_kwargs
  464. ),
  465. testcase.apply_defaults({name: test_kwargs[name]})
  466. )
  467. with testcase.assertRaisesRegex(
  468. CommandExecutionError,
  469. 'is an alias for.+cannot both be used'):
  470. salt.utils.docker.translate_input(
  471. self.translator,
  472. ignore_collisions=False,
  473. **test_kwargs
  474. )
  475. return self.func(testcase, *args, **kwargs)
  476. class assert_key_colon_value(Assert):
  477. '''
  478. Test a key/value pair with parameters passed as key:value pairs
  479. '''
  480. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  481. # Strip off the "test_" from the function name
  482. name = self.func.__name__[5:]
  483. self.test_key_value(testcase, name, ':')
  484. return self.func(testcase, *args, **kwargs)
  485. class assert_key_equals_value(Assert):
  486. '''
  487. Test a key/value pair with parameters passed as key=value pairs
  488. '''
  489. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  490. # Strip off the "test_" from the function name
  491. name = self.func.__name__[5:]
  492. self.test_key_value(testcase, name, '=')
  493. if name == 'labels':
  494. self.test_stringlist(testcase, name)
  495. return self.func(testcase, *args, **kwargs)
  496. class assert_labels(Assert):
  497. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  498. # Strip off the "test_" from the function name
  499. name = self.func.__name__[5:]
  500. alias = self.translator.ALIASES_REVMAP.get(name)
  501. labels = ['foo', 'bar=baz', {'hello': 'world'}]
  502. expected = {'foo': '', 'bar': 'baz', 'hello': 'world'}
  503. for item in (name, alias):
  504. if item is None:
  505. continue
  506. testcase.assertEqual(
  507. salt.utils.docker.translate_input(
  508. self.translator,
  509. **{item: labels}
  510. ),
  511. testcase.apply_defaults({name: expected})
  512. )
  513. # Error case: Passed a mutli-element dict in dictlist
  514. bad_labels = copy.deepcopy(labels)
  515. bad_labels[-1]['bad'] = 'input'
  516. with testcase.assertRaisesRegex(
  517. CommandExecutionError, r'Invalid label\(s\)'):
  518. salt.utils.docker.translate_input(
  519. self.translator,
  520. **{item: bad_labels}
  521. )
  522. return self.func(testcase, *args, **kwargs)
  523. class assert_device_rates(Assert):
  524. '''
  525. Tests for device_{read,write}_{bps,iops}. The bps values have a "Rate"
  526. value expressed in bytes/kb/mb/gb, while the iops values have a "Rate"
  527. expressed as a simple integer.
  528. '''
  529. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  530. # Strip off the "test_" from the function name
  531. name = self.func.__name__[5:]
  532. alias = self.translator.ALIASES_REVMAP.get(name)
  533. for item in (name, alias):
  534. if item is None:
  535. continue
  536. # Error case: Not an absolute path
  537. path = os.path.join('foo', 'bar', 'baz')
  538. with testcase.assertRaisesRegex(
  539. CommandExecutionError,
  540. "Path '{0}' is not absolute".format(path.replace('\\', '\\\\'))):
  541. salt.utils.docker.translate_input(
  542. self.translator,
  543. **{item: '{0}:1048576'.format(path)}
  544. )
  545. if name.endswith('_bps'):
  546. # Both integer bytes and a string providing a shorthand for kb,
  547. # mb, or gb can be used, so we need to test for both.
  548. expected = (
  549. {}, []
  550. )
  551. vals = '/dev/sda:1048576,/dev/sdb:1048576'
  552. for val in (vals, vals.split(',')):
  553. testcase.assertEqual(
  554. salt.utils.docker.translate_input(
  555. self.translator,
  556. **{item: val}
  557. ),
  558. testcase.apply_defaults(
  559. {name: [{'Path': '/dev/sda', 'Rate': 1048576},
  560. {'Path': '/dev/sdb', 'Rate': 1048576}]}
  561. )
  562. )
  563. vals = '/dev/sda:1mb,/dev/sdb:5mb'
  564. for val in (vals, vals.split(',')):
  565. testcase.assertEqual(
  566. salt.utils.docker.translate_input(
  567. self.translator,
  568. **{item: val}
  569. ),
  570. testcase.apply_defaults(
  571. {name: [{'Path': '/dev/sda', 'Rate': '1mb'},
  572. {'Path': '/dev/sdb', 'Rate': '5mb'}]}
  573. )
  574. )
  575. if alias is not None:
  576. # Test collision
  577. test_kwargs = {
  578. name: '/dev/sda:1048576,/dev/sdb:1048576',
  579. alias: '/dev/sda:1mb,/dev/sdb:5mb'
  580. }
  581. testcase.assertEqual(
  582. salt.utils.docker.translate_input(
  583. self.translator,
  584. ignore_collisions=True,
  585. **test_kwargs
  586. ),
  587. testcase.apply_defaults(
  588. {name: [{'Path': '/dev/sda', 'Rate': 1048576},
  589. {'Path': '/dev/sdb', 'Rate': 1048576}]}
  590. )
  591. )
  592. with testcase.assertRaisesRegex(
  593. CommandExecutionError,
  594. 'is an alias for.+cannot both be used'):
  595. salt.utils.docker.translate_input(
  596. self.translator,
  597. ignore_collisions=False,
  598. **test_kwargs
  599. )
  600. else:
  601. # The "Rate" value must be an integer
  602. vals = '/dev/sda:1000,/dev/sdb:500'
  603. for val in (vals, vals.split(',')):
  604. testcase.assertEqual(
  605. salt.utils.docker.translate_input(
  606. self.translator,
  607. **{item: val}
  608. ),
  609. testcase.apply_defaults(
  610. {name: [{'Path': '/dev/sda', 'Rate': 1000},
  611. {'Path': '/dev/sdb', 'Rate': 500}]}
  612. )
  613. )
  614. # Test non-integer input
  615. expected = (
  616. {},
  617. {item: 'Rate \'5mb\' for path \'/dev/sdb\' is non-numeric'},
  618. []
  619. )
  620. vals = '/dev/sda:1000,/dev/sdb:5mb'
  621. for val in (vals, vals.split(',')):
  622. with testcase.assertRaisesRegex(
  623. CommandExecutionError,
  624. "Rate '5mb' for path '/dev/sdb' is non-numeric"):
  625. salt.utils.docker.translate_input(
  626. self.translator,
  627. **{item: val}
  628. )
  629. if alias is not None:
  630. # Test collision
  631. test_kwargs = {
  632. name: '/dev/sda:1000,/dev/sdb:500',
  633. alias: '/dev/sda:888,/dev/sdb:999'
  634. }
  635. testcase.assertEqual(
  636. salt.utils.docker.translate_input(
  637. self.translator,
  638. ignore_collisions=True,
  639. **test_kwargs
  640. ),
  641. testcase.apply_defaults(
  642. {name: [{'Path': '/dev/sda', 'Rate': 1000},
  643. {'Path': '/dev/sdb', 'Rate': 500}]}
  644. )
  645. )
  646. with testcase.assertRaisesRegex(
  647. CommandExecutionError,
  648. 'is an alias for.+cannot both be used'):
  649. salt.utils.docker.translate_input(
  650. self.translator,
  651. ignore_collisions=False,
  652. **test_kwargs
  653. )
  654. return self.func(testcase, *args, **kwargs)
  655. class assert_subnet(Assert):
  656. '''
  657. Test an IPv4 or IPv6 subnet
  658. '''
  659. def wrap(self, testcase, *args, **kwargs): # pylint: disable=arguments-differ
  660. # Strip off the "test_" from the function name
  661. name = self.func.__name__[5:]
  662. alias = self.translator.ALIASES_REVMAP.get(name)
  663. for item in (name, alias):
  664. if item is None:
  665. continue
  666. for val in ('127.0.0.1/32', '::1/128'):
  667. log.debug('Verifying \'%s\' is a valid subnet', val)
  668. testcase.assertEqual(
  669. salt.utils.docker.translate_input(
  670. self.translator,
  671. validate_ip_addrs=True,
  672. **{item: val}
  673. ),
  674. testcase.apply_defaults({name: val})
  675. )
  676. # Error case: invalid subnet caught by validation
  677. for val in ('127.0.0.1', '999.999.999.999/24', '10.0.0.0/33',
  678. '::1', 'feaz::1/128', '::1/129'):
  679. log.debug('Verifying \'%s\' is not a valid subnet', val)
  680. with testcase.assertRaisesRegex(
  681. CommandExecutionError,
  682. "'{0}' is not a valid subnet".format(val)):
  683. salt.utils.docker.translate_input(
  684. self.translator,
  685. validate_ip_addrs=True,
  686. **{item: val}
  687. )
  688. # This is not valid input but it will test whether or not subnet
  689. # validation happened
  690. val = 'foo'
  691. testcase.assertEqual(
  692. salt.utils.docker.translate_input(
  693. self.translator,
  694. validate_ip_addrs=False,
  695. **{item: val}
  696. ),
  697. testcase.apply_defaults({name: val})
  698. )
  699. if alias is not None:
  700. # Test collision
  701. test_kwargs = {name: '10.0.0.0/24', alias: '192.168.50.128/25'}
  702. testcase.assertEqual(
  703. salt.utils.docker.translate_input(
  704. self.translator,
  705. ignore_collisions=True,
  706. **test_kwargs
  707. ),
  708. testcase.apply_defaults({name: test_kwargs[name]})
  709. )
  710. with testcase.assertRaisesRegex(
  711. CommandExecutionError,
  712. 'is an alias for.+cannot both be used'):
  713. salt.utils.docker.translate_input(
  714. self.translator,
  715. ignore_collisions=False,
  716. **test_kwargs
  717. )
  718. return self.func(testcase, *args, **kwargs)
  719. class TranslateBase(TestCase):
  720. maxDiff = None
  721. translator = None # Must be overridden in the subclass
  722. def apply_defaults(self, ret, skip_translate=None):
  723. if skip_translate is not True:
  724. defaults = getattr(self.translator, 'DEFAULTS', {})
  725. for key, val in six.iteritems(defaults):
  726. if key not in ret:
  727. ret[key] = val
  728. return ret
  729. @staticmethod
  730. def normalize_ports(ret):
  731. '''
  732. When we translate exposed ports, we can end up with a mixture of ints
  733. (representing TCP ports) and tuples (representing UDP ports). Python 2
  734. will sort an iterable containing these mixed types, but Python 3 will
  735. not. This helper is used to munge the ports in the return data so that
  736. the resulting list is sorted in a way that can reliably be compared to
  737. the expected results in the test.
  738. This helper should only be needed for port_bindings and ports.
  739. '''
  740. if 'ports' in ret[0]:
  741. tcp_ports = []
  742. udp_ports = []
  743. for item in ret[0]['ports']:
  744. if isinstance(item, six.integer_types):
  745. tcp_ports.append(item)
  746. else:
  747. udp_ports.append(item)
  748. ret[0]['ports'] = sorted(tcp_ports) + sorted(udp_ports)
  749. return ret
  750. def tearDown(self):
  751. '''
  752. Test skip_translate kwarg
  753. '''
  754. name = self.id().split('.')[-1][5:]
  755. # The below is not valid input for the Docker API, but these
  756. # assertions confirm that we successfully skipped translation.
  757. for val in (True, name, [name]):
  758. self.assertEqual(
  759. salt.utils.docker.translate_input(
  760. self.translator,
  761. skip_translate=val,
  762. **{name: 'foo'}
  763. ),
  764. self.apply_defaults({name: 'foo'}, skip_translate=val)
  765. )
  766. class TranslateContainerInputTestCase(TranslateBase):
  767. '''
  768. Tests for salt.utils.docker.translate_input(), invoked using
  769. salt.utils.docker.translate.container as the translator module.
  770. '''
  771. translator = salt.utils.docker.translate.container
  772. @staticmethod
  773. def normalize_ports(ret):
  774. '''
  775. When we translate exposed ports, we can end up with a mixture of ints
  776. (representing TCP ports) and tuples (representing UDP ports). Python 2
  777. will sort an iterable containing these mixed types, but Python 3 will
  778. not. This helper is used to munge the ports in the return data so that
  779. the resulting list is sorted in a way that can reliably be compared to
  780. the expected results in the test.
  781. This helper should only be needed for port_bindings and ports.
  782. '''
  783. if 'ports' in ret:
  784. tcp_ports = []
  785. udp_ports = []
  786. for item in ret['ports']:
  787. if isinstance(item, six.integer_types):
  788. tcp_ports.append(item)
  789. else:
  790. udp_ports.append(item)
  791. ret['ports'] = sorted(tcp_ports) + sorted(udp_ports)
  792. return ret
  793. @assert_bool(salt.utils.docker.translate.container)
  794. def test_auto_remove(self):
  795. '''
  796. Should be a bool or converted to one
  797. '''
  798. def test_binds(self):
  799. '''
  800. Test the "binds" kwarg. Any volumes not defined in the "volumes" kwarg
  801. should be added to the results.
  802. '''
  803. self.assertEqual(
  804. salt.utils.docker.translate_input(
  805. self.translator,
  806. binds='/srv/www:/var/www:ro',
  807. volumes='/testing'),
  808. {'binds': ['/srv/www:/var/www:ro'],
  809. 'volumes': ['/testing', '/var/www']}
  810. )
  811. self.assertEqual(
  812. salt.utils.docker.translate_input(
  813. self.translator,
  814. binds=['/srv/www:/var/www:ro'],
  815. volumes='/testing'),
  816. {'binds': ['/srv/www:/var/www:ro'],
  817. 'volumes': ['/testing', '/var/www']}
  818. )
  819. self.assertEqual(
  820. salt.utils.docker.translate_input(
  821. self.translator,
  822. binds={'/srv/www': {'bind': '/var/www', 'mode': 'ro'}},
  823. volumes='/testing'),
  824. {'binds': {'/srv/www': {'bind': '/var/www', 'mode': 'ro'}},
  825. 'volumes': ['/testing', '/var/www']}
  826. )
  827. @assert_int(salt.utils.docker.translate.container)
  828. def test_blkio_weight(self):
  829. '''
  830. Should be an int or converted to one
  831. '''
  832. def test_blkio_weight_device(self):
  833. '''
  834. Should translate a list of PATH:WEIGHT pairs to a list of dictionaries
  835. with the following format: {'Path': PATH, 'Weight': WEIGHT}
  836. '''
  837. for val in ('/dev/sda:100,/dev/sdb:200',
  838. ['/dev/sda:100', '/dev/sdb:200']):
  839. self.assertEqual(
  840. salt.utils.docker.translate_input(
  841. self.translator,
  842. blkio_weight_device='/dev/sda:100,/dev/sdb:200'
  843. ),
  844. {'blkio_weight_device': [{'Path': '/dev/sda', 'Weight': 100},
  845. {'Path': '/dev/sdb', 'Weight': 200}]}
  846. )
  847. # Error cases
  848. with self.assertRaisesRegex(
  849. CommandExecutionError,
  850. r"'foo' contains 1 value\(s\) \(expected 2\)"):
  851. salt.utils.docker.translate_input(
  852. self.translator,
  853. blkio_weight_device='foo'
  854. )
  855. with self.assertRaisesRegex(
  856. CommandExecutionError,
  857. r"'foo:bar:baz' contains 3 value\(s\) \(expected 2\)"):
  858. salt.utils.docker.translate_input(
  859. self.translator,
  860. blkio_weight_device='foo:bar:baz'
  861. )
  862. with self.assertRaisesRegex(
  863. CommandExecutionError,
  864. r"Weight 'foo' for path '/dev/sdb' is not an integer"):
  865. salt.utils.docker.translate_input(
  866. self.translator,
  867. blkio_weight_device=['/dev/sda:100', '/dev/sdb:foo']
  868. )
  869. @assert_stringlist(salt.utils.docker.translate.container)
  870. def test_cap_add(self):
  871. '''
  872. Should be a list of strings or converted to one
  873. '''
  874. @assert_stringlist(salt.utils.docker.translate.container)
  875. def test_cap_drop(self):
  876. '''
  877. Should be a list of strings or converted to one
  878. '''
  879. @assert_cmd(salt.utils.docker.translate.container)
  880. def test_command(self):
  881. '''
  882. Can either be a string or a comma-separated or Python list of strings.
  883. '''
  884. @assert_string(salt.utils.docker.translate.container)
  885. def test_cpuset_cpus(self):
  886. '''
  887. Should be a string or converted to one
  888. '''
  889. @assert_string(salt.utils.docker.translate.container)
  890. def test_cpuset_mems(self):
  891. '''
  892. Should be a string or converted to one
  893. '''
  894. @assert_int(salt.utils.docker.translate.container)
  895. def test_cpu_group(self):
  896. '''
  897. Should be an int or converted to one
  898. '''
  899. @assert_int(salt.utils.docker.translate.container)
  900. def test_cpu_period(self):
  901. '''
  902. Should be an int or converted to one
  903. '''
  904. @assert_int(salt.utils.docker.translate.container)
  905. def test_cpu_shares(self):
  906. '''
  907. Should be an int or converted to one
  908. '''
  909. @assert_bool(salt.utils.docker.translate.container)
  910. def test_detach(self):
  911. '''
  912. Should be a bool or converted to one
  913. '''
  914. @assert_device_rates(salt.utils.docker.translate.container)
  915. def test_device_read_bps(self):
  916. '''
  917. CLI input is a list of PATH:RATE pairs, but the API expects a list of
  918. dictionaries in the format [{'Path': path, 'Rate': rate}]
  919. '''
  920. @assert_device_rates(salt.utils.docker.translate.container)
  921. def test_device_read_iops(self):
  922. '''
  923. CLI input is a list of PATH:RATE pairs, but the API expects a list of
  924. dictionaries in the format [{'Path': path, 'Rate': rate}]
  925. '''
  926. @assert_device_rates(salt.utils.docker.translate.container)
  927. def test_device_write_bps(self):
  928. '''
  929. CLI input is a list of PATH:RATE pairs, but the API expects a list of
  930. dictionaries in the format [{'Path': path, 'Rate': rate}]
  931. '''
  932. @assert_device_rates(salt.utils.docker.translate.container)
  933. def test_device_write_iops(self):
  934. '''
  935. CLI input is a list of PATH:RATE pairs, but the API expects a list of
  936. dictionaries in the format [{'Path': path, 'Rate': rate}]
  937. '''
  938. @assert_stringlist(salt.utils.docker.translate.container)
  939. def test_devices(self):
  940. '''
  941. Should be a list of strings or converted to one
  942. '''
  943. @assert_stringlist(salt.utils.docker.translate.container)
  944. def test_dns_opt(self):
  945. '''
  946. Should be a list of strings or converted to one
  947. '''
  948. @assert_stringlist(salt.utils.docker.translate.container)
  949. def test_dns_search(self):
  950. '''
  951. Should be a list of strings or converted to one
  952. '''
  953. def test_dns(self):
  954. '''
  955. While this is a stringlist, it also supports IP address validation, so
  956. it can't use the test_stringlist decorator because we need to test both
  957. with and without validation, and it isn't necessary to make all other
  958. stringlist tests also do that same kind of testing.
  959. '''
  960. for val in ('8.8.8.8,8.8.4.4', ['8.8.8.8', '8.8.4.4']):
  961. self.assertEqual(
  962. salt.utils.docker.translate_input(
  963. self.translator,
  964. dns=val,
  965. validate_ip_addrs=True,
  966. ),
  967. {'dns': ['8.8.8.8', '8.8.4.4']}
  968. )
  969. # Error case: invalid IP address caught by validation
  970. for val in ('8.8.8.888,8.8.4.4', ['8.8.8.888', '8.8.4.4']):
  971. with self.assertRaisesRegex(
  972. CommandExecutionError,
  973. r"'8.8.8.888' is not a valid IP address"):
  974. salt.utils.docker.translate_input(
  975. self.translator,
  976. dns=val,
  977. validate_ip_addrs=True,
  978. )
  979. # This is not valid input but it will test whether or not IP address
  980. # validation happened.
  981. for val in ('foo,bar', ['foo', 'bar']):
  982. self.assertEqual(
  983. salt.utils.docker.translate_input(
  984. self.translator,
  985. dns=val,
  986. validate_ip_addrs=False,
  987. ),
  988. {'dns': ['foo', 'bar']}
  989. )
  990. @assert_string(salt.utils.docker.translate.container)
  991. def test_domainname(self):
  992. '''
  993. Should be a list of strings or converted to one
  994. '''
  995. @assert_cmd(salt.utils.docker.translate.container)
  996. def test_entrypoint(self):
  997. '''
  998. Can either be a string or a comma-separated or Python list of strings.
  999. '''
  1000. @assert_key_equals_value(salt.utils.docker.translate.container)
  1001. def test_environment(self):
  1002. '''
  1003. Can be passed in several formats but must end up as a dictionary
  1004. mapping keys to values
  1005. '''
  1006. def test_extra_hosts(self):
  1007. '''
  1008. Can be passed as a list of key:value pairs but can't be simply tested
  1009. using @assert_key_colon_value since we need to test both with and without
  1010. IP address validation.
  1011. '''
  1012. for val in ('web1:10.9.8.7,web2:10.9.8.8',
  1013. ['web1:10.9.8.7', 'web2:10.9.8.8']):
  1014. self.assertEqual(
  1015. salt.utils.docker.translate_input(
  1016. self.translator,
  1017. extra_hosts=val,
  1018. validate_ip_addrs=True,
  1019. ),
  1020. {'extra_hosts': {'web1': '10.9.8.7', 'web2': '10.9.8.8'}}
  1021. )
  1022. # Error case: invalid IP address caught by validation
  1023. for val in ('web1:10.9.8.299,web2:10.9.8.8',
  1024. ['web1:10.9.8.299', 'web2:10.9.8.8']):
  1025. with self.assertRaisesRegex(
  1026. CommandExecutionError,
  1027. r"'10.9.8.299' is not a valid IP address"):
  1028. salt.utils.docker.translate_input(
  1029. self.translator,
  1030. extra_hosts=val,
  1031. validate_ip_addrs=True,
  1032. )
  1033. # This is not valid input but it will test whether or not IP address
  1034. # validation happened.
  1035. for val in ('foo:bar,baz:qux', ['foo:bar', 'baz:qux']):
  1036. self.assertEqual(
  1037. salt.utils.docker.translate_input(
  1038. self.translator,
  1039. extra_hosts=val,
  1040. validate_ip_addrs=False,
  1041. ),
  1042. {'extra_hosts': {'foo': 'bar', 'baz': 'qux'}}
  1043. )
  1044. @assert_stringlist(salt.utils.docker.translate.container)
  1045. def test_group_add(self):
  1046. '''
  1047. Should be a list of strings or converted to one
  1048. '''
  1049. @assert_string(salt.utils.docker.translate.container)
  1050. def test_hostname(self):
  1051. '''
  1052. Should be a string or converted to one
  1053. '''
  1054. @assert_string(salt.utils.docker.translate.container)
  1055. def test_ipc_mode(self):
  1056. '''
  1057. Should be a string or converted to one
  1058. '''
  1059. @assert_string(salt.utils.docker.translate.container)
  1060. def test_isolation(self):
  1061. '''
  1062. Should be a string or converted to one
  1063. '''
  1064. @assert_labels(salt.utils.docker.translate.container)
  1065. def test_labels(self):
  1066. '''
  1067. Can be passed as a list of key=value pairs or a dictionary, and must
  1068. ultimately end up as a dictionary.
  1069. '''
  1070. @assert_key_colon_value(salt.utils.docker.translate.container)
  1071. def test_links(self):
  1072. '''
  1073. Can be passed as a list of key:value pairs or a dictionary, and must
  1074. ultimately end up as a dictionary.
  1075. '''
  1076. def test_log_config(self):
  1077. '''
  1078. This is a mixture of log_driver and log_opt, which get combined into a
  1079. dictionary.
  1080. log_driver is a simple string, but log_opt can be passed in several
  1081. ways, so we need to test them all.
  1082. '''
  1083. expected = (
  1084. {'log_config': {'Type': 'foo',
  1085. 'Config': {'foo': 'bar', 'baz': 'qux'}}},
  1086. {}, []
  1087. )
  1088. for val in ('foo=bar,baz=qux',
  1089. ['foo=bar', 'baz=qux'],
  1090. [{'foo': 'bar'}, {'baz': 'qux'}],
  1091. {'foo': 'bar', 'baz': 'qux'}):
  1092. self.assertEqual(
  1093. salt.utils.docker.translate_input(
  1094. self.translator,
  1095. log_driver='foo',
  1096. log_opt='foo=bar,baz=qux'
  1097. ),
  1098. {'log_config': {'Type': 'foo',
  1099. 'Config': {'foo': 'bar', 'baz': 'qux'}}}
  1100. )
  1101. # Ensure passing either `log_driver` or `log_opt` alone works
  1102. self.assertEqual(
  1103. salt.utils.docker.translate_input(
  1104. self.translator,
  1105. log_driver='foo'
  1106. ),
  1107. {'log_config': {'Type': 'foo', 'Config': {}}}
  1108. )
  1109. self.assertEqual(
  1110. salt.utils.docker.translate_input(
  1111. self.translator,
  1112. log_opt={'foo': 'bar', 'baz': 'qux'}
  1113. ),
  1114. {'log_config': {'Type': 'none',
  1115. 'Config': {'foo': 'bar', 'baz': 'qux'}}}
  1116. )
  1117. @assert_key_equals_value(salt.utils.docker.translate.container)
  1118. def test_lxc_conf(self):
  1119. '''
  1120. Can be passed as a list of key=value pairs or a dictionary, and must
  1121. ultimately end up as a dictionary.
  1122. '''
  1123. @assert_string(salt.utils.docker.translate.container)
  1124. def test_mac_address(self):
  1125. '''
  1126. Should be a string or converted to one
  1127. '''
  1128. @assert_int_or_string(salt.utils.docker.translate.container)
  1129. def test_mem_limit(self):
  1130. '''
  1131. Should be a string or converted to one
  1132. '''
  1133. @assert_int(salt.utils.docker.translate.container)
  1134. def test_mem_swappiness(self):
  1135. '''
  1136. Should be an int or converted to one
  1137. '''
  1138. @assert_int_or_string(salt.utils.docker.translate.container)
  1139. def test_memswap_limit(self):
  1140. '''
  1141. Should be a string or converted to one
  1142. '''
  1143. @assert_string(salt.utils.docker.translate.container)
  1144. def test_name(self):
  1145. '''
  1146. Should be a string or converted to one
  1147. '''
  1148. @assert_bool(salt.utils.docker.translate.container)
  1149. def test_network_disabled(self):
  1150. '''
  1151. Should be a bool or converted to one
  1152. '''
  1153. @assert_string(salt.utils.docker.translate.container)
  1154. def test_network_mode(self):
  1155. '''
  1156. Should be a string or converted to one
  1157. '''
  1158. @assert_bool(salt.utils.docker.translate.container)
  1159. def test_oom_kill_disable(self):
  1160. '''
  1161. Should be a bool or converted to one
  1162. '''
  1163. @assert_int(salt.utils.docker.translate.container)
  1164. def test_oom_score_adj(self):
  1165. '''
  1166. Should be an int or converted to one
  1167. '''
  1168. @assert_string(salt.utils.docker.translate.container)
  1169. def test_pid_mode(self):
  1170. '''
  1171. Should be a string or converted to one
  1172. '''
  1173. @assert_int(salt.utils.docker.translate.container)
  1174. def test_pids_limit(self):
  1175. '''
  1176. Should be an int or converted to one
  1177. '''
  1178. def test_port_bindings(self):
  1179. '''
  1180. This has several potential formats and can include port ranges. It
  1181. needs its own test.
  1182. '''
  1183. # ip:hostPort:containerPort - Bind a specific IP and port on the host
  1184. # to a specific port within the container.
  1185. bindings = (
  1186. '10.1.2.3:8080:80,10.1.2.3:8888:80,10.4.5.6:3333:3333,'
  1187. '10.7.8.9:14505-14506:4505-4506,10.1.2.3:8080:81/udp,'
  1188. '10.1.2.3:8888:81/udp,10.4.5.6:3334:3334/udp,'
  1189. '10.7.8.9:15505-15506:5505-5506/udp'
  1190. )
  1191. for val in (bindings, bindings.split(',')):
  1192. self.assertEqual(
  1193. self.normalize_ports(
  1194. salt.utils.docker.translate_input(
  1195. self.translator,
  1196. port_bindings=val,
  1197. )
  1198. ),
  1199. {'port_bindings': {80: [('10.1.2.3', 8080),
  1200. ('10.1.2.3', 8888)],
  1201. 3333: ('10.4.5.6', 3333),
  1202. 4505: ('10.7.8.9', 14505),
  1203. 4506: ('10.7.8.9', 14506),
  1204. '81/udp': [('10.1.2.3', 8080),
  1205. ('10.1.2.3', 8888)],
  1206. '3334/udp': ('10.4.5.6', 3334),
  1207. '5505/udp': ('10.7.8.9', 15505),
  1208. '5506/udp': ('10.7.8.9', 15506)},
  1209. 'ports': [80, 3333, 4505, 4506,
  1210. (81, 'udp'), (3334, 'udp'),
  1211. (5505, 'udp'), (5506, 'udp')]}
  1212. )
  1213. # ip::containerPort - Bind a specific IP and an ephemeral port to a
  1214. # specific port within the container.
  1215. bindings = (
  1216. '10.1.2.3::80,10.1.2.3::80,10.4.5.6::3333,10.7.8.9::4505-4506,'
  1217. '10.1.2.3::81/udp,10.1.2.3::81/udp,10.4.5.6::3334/udp,'
  1218. '10.7.8.9::5505-5506/udp'
  1219. )
  1220. for val in (bindings, bindings.split(',')):
  1221. self.assertEqual(
  1222. self.normalize_ports(
  1223. salt.utils.docker.translate_input(
  1224. self.translator,
  1225. port_bindings=val,
  1226. )
  1227. ),
  1228. {'port_bindings': {80: [('10.1.2.3',), ('10.1.2.3',)],
  1229. 3333: ('10.4.5.6',),
  1230. 4505: ('10.7.8.9',),
  1231. 4506: ('10.7.8.9',),
  1232. '81/udp': [('10.1.2.3',), ('10.1.2.3',)],
  1233. '3334/udp': ('10.4.5.6',),
  1234. '5505/udp': ('10.7.8.9',),
  1235. '5506/udp': ('10.7.8.9',)},
  1236. 'ports': [80, 3333, 4505, 4506,
  1237. (81, 'udp'), (3334, 'udp'),
  1238. (5505, 'udp'), (5506, 'udp')]}
  1239. )
  1240. # hostPort:containerPort - Bind a specific port on all of the host's
  1241. # interfaces to a specific port within the container.
  1242. bindings = (
  1243. '8080:80,8888:80,3333:3333,14505-14506:4505-4506,8080:81/udp,'
  1244. '8888:81/udp,3334:3334/udp,15505-15506:5505-5506/udp'
  1245. )
  1246. for val in (bindings, bindings.split(',')):
  1247. self.assertEqual(
  1248. self.normalize_ports(
  1249. salt.utils.docker.translate_input(
  1250. self.translator,
  1251. port_bindings=val,
  1252. )
  1253. ),
  1254. {'port_bindings': {80: [8080, 8888],
  1255. 3333: 3333,
  1256. 4505: 14505,
  1257. 4506: 14506,
  1258. '81/udp': [8080, 8888],
  1259. '3334/udp': 3334,
  1260. '5505/udp': 15505,
  1261. '5506/udp': 15506},
  1262. 'ports': [80, 3333, 4505, 4506,
  1263. (81, 'udp'), (3334, 'udp'),
  1264. (5505, 'udp'), (5506, 'udp')]}
  1265. )
  1266. # containerPort - Bind an ephemeral port on all of the host's
  1267. # interfaces to a specific port within the container.
  1268. bindings = '80,3333,4505-4506,81/udp,3334/udp,5505-5506/udp'
  1269. for val in (bindings, bindings.split(',')):
  1270. self.assertEqual(
  1271. self.normalize_ports(
  1272. salt.utils.docker.translate_input(
  1273. self.translator,
  1274. port_bindings=val,
  1275. )
  1276. ),
  1277. {'port_bindings': {80: None,
  1278. 3333: None,
  1279. 4505: None,
  1280. 4506: None,
  1281. '81/udp': None,
  1282. '3334/udp': None,
  1283. '5505/udp': None,
  1284. '5506/udp': None},
  1285. 'ports': [80, 3333, 4505, 4506,
  1286. (81, 'udp'), (3334, 'udp'),
  1287. (5505, 'udp'), (5506, 'udp')]}
  1288. )
  1289. # Test a mixture of different types of input
  1290. bindings = (
  1291. '10.1.2.3:8080:80,10.4.5.6::3333,14505-14506:4505-4506,'
  1292. '9999-10001,10.1.2.3:8080:81/udp,10.4.5.6::3334/udp,'
  1293. '15505-15506:5505-5506/udp,19999-20001/udp'
  1294. )
  1295. for val in (bindings, bindings.split(',')):
  1296. self.assertEqual(
  1297. self.normalize_ports(
  1298. salt.utils.docker.translate_input(
  1299. self.translator,
  1300. port_bindings=val,
  1301. )
  1302. ),
  1303. {'port_bindings': {80: ('10.1.2.3', 8080),
  1304. 3333: ('10.4.5.6',),
  1305. 4505: 14505,
  1306. 4506: 14506,
  1307. 9999: None,
  1308. 10000: None,
  1309. 10001: None,
  1310. '81/udp': ('10.1.2.3', 8080),
  1311. '3334/udp': ('10.4.5.6',),
  1312. '5505/udp': 15505,
  1313. '5506/udp': 15506,
  1314. '19999/udp': None,
  1315. '20000/udp': None,
  1316. '20001/udp': None},
  1317. 'ports': [80, 3333, 4505, 4506, 9999, 10000, 10001,
  1318. (81, 'udp'), (3334, 'udp'), (5505, 'udp'),
  1319. (5506, 'udp'), (19999, 'udp'),
  1320. (20000, 'udp'), (20001, 'udp')]}
  1321. )
  1322. # Error case: too many items (max 3)
  1323. with self.assertRaisesRegex(
  1324. CommandExecutionError,
  1325. r"'10.1.2.3:8080:80:123' is an invalid port binding "
  1326. r"definition \(at most 3 components are allowed, found 4\)"):
  1327. salt.utils.docker.translate_input(
  1328. self.translator,
  1329. port_bindings='10.1.2.3:8080:80:123'
  1330. )
  1331. # Error case: port range start is greater than end
  1332. for val in ('10.1.2.3:5555-5554:1111-1112',
  1333. '10.1.2.3:1111-1112:5555-5554',
  1334. '10.1.2.3::5555-5554',
  1335. '5555-5554:1111-1112',
  1336. '1111-1112:5555-5554',
  1337. '5555-5554'):
  1338. with self.assertRaisesRegex(
  1339. CommandExecutionError,
  1340. r"Start of port range \(5555\) cannot be greater than end "
  1341. r"of port range \(5554\)"):
  1342. salt.utils.docker.translate_input(
  1343. self.translator,
  1344. port_bindings=val,
  1345. )
  1346. # Error case: non-numeric port range
  1347. for val in ('10.1.2.3:foo:1111-1112',
  1348. '10.1.2.3:1111-1112:foo',
  1349. '10.1.2.3::foo',
  1350. 'foo:1111-1112',
  1351. '1111-1112:foo',
  1352. 'foo'):
  1353. with self.assertRaisesRegex(
  1354. CommandExecutionError,
  1355. "'foo' is non-numeric or an invalid port range"):
  1356. salt.utils.docker.translate_input(
  1357. self.translator,
  1358. port_bindings=val,
  1359. )
  1360. # Error case: misatched port range
  1361. for val in ('10.1.2.3:1111-1113:1111-1112', '1111-1113:1111-1112'):
  1362. with self.assertRaisesRegex(
  1363. CommandExecutionError,
  1364. r'Host port range \(1111-1113\) does not have the same '
  1365. r'number of ports as the container port range \(1111-1112\)'):
  1366. salt.utils.docker.translate_input(
  1367. self.translator,
  1368. port_bindings=val
  1369. )
  1370. for val in ('10.1.2.3:1111-1112:1111-1113', '1111-1112:1111-1113'):
  1371. with self.assertRaisesRegex(
  1372. CommandExecutionError,
  1373. r'Host port range \(1111-1112\) does not have the same '
  1374. r'number of ports as the container port range \(1111-1113\)'):
  1375. salt.utils.docker.translate_input(
  1376. self.translator,
  1377. port_bindings=val,
  1378. )
  1379. # Error case: empty host port or container port
  1380. with self.assertRaisesRegex(
  1381. CommandExecutionError,
  1382. "Empty host port in port binding definition ':1111'"):
  1383. salt.utils.docker.translate_input(
  1384. self.translator,
  1385. port_bindings=':1111'
  1386. )
  1387. with self.assertRaisesRegex(
  1388. CommandExecutionError,
  1389. "Empty container port in port binding definition '1111:'"):
  1390. salt.utils.docker.translate_input(
  1391. self.translator,
  1392. port_bindings='1111:'
  1393. )
  1394. with self.assertRaisesRegex(
  1395. CommandExecutionError,
  1396. 'Empty port binding definition found'):
  1397. salt.utils.docker.translate_input(
  1398. self.translator,
  1399. port_bindings=''
  1400. )
  1401. def test_ports(self):
  1402. '''
  1403. Ports can be passed as a comma-separated or Python list of port
  1404. numbers, with '/tcp' being optional for TCP ports. They must ultimately
  1405. be a list of port definitions, in which an integer denotes a TCP port,
  1406. and a tuple in the format (port_num, 'udp') denotes a UDP port. Also,
  1407. the port numbers must end up as integers. None of the decorators will
  1408. suffice so this one must be tested specially.
  1409. '''
  1410. for val in ('1111,2222/tcp,3333/udp,4505-4506',
  1411. [1111, '2222/tcp', '3333/udp', '4505-4506'],
  1412. ['1111', '2222/tcp', '3333/udp', '4505-4506']):
  1413. self.assertEqual(
  1414. self.normalize_ports(
  1415. salt.utils.docker.translate_input(
  1416. self.translator,
  1417. ports=val,
  1418. )
  1419. ),
  1420. {'ports': [1111, 2222, 4505, 4506, (3333, 'udp')]}
  1421. )
  1422. # Error case: non-integer and non/string value
  1423. for val in (1.0, [1.0]):
  1424. with self.assertRaisesRegex(
  1425. CommandExecutionError,
  1426. "'1.0' is not a valid port definition"):
  1427. salt.utils.docker.translate_input(
  1428. self.translator,
  1429. ports=val,
  1430. )
  1431. # Error case: port range start is greater than end
  1432. with self.assertRaisesRegex(
  1433. CommandExecutionError,
  1434. r"Start of port range \(5555\) cannot be greater than end of "
  1435. r"port range \(5554\)"):
  1436. salt.utils.docker.translate_input(
  1437. self.translator,
  1438. ports='5555-5554',
  1439. )
  1440. @assert_bool(salt.utils.docker.translate.container)
  1441. def test_privileged(self):
  1442. '''
  1443. Should be a bool or converted to one
  1444. '''
  1445. @assert_bool(salt.utils.docker.translate.container)
  1446. def test_publish_all_ports(self):
  1447. '''
  1448. Should be a bool or converted to one
  1449. '''
  1450. @assert_bool(salt.utils.docker.translate.container)
  1451. def test_read_only(self):
  1452. '''
  1453. Should be a bool or converted to one
  1454. '''
  1455. def test_restart_policy(self):
  1456. '''
  1457. Input is in the format "name[:retry_count]", but the API wants it
  1458. in the format {'Name': name, 'MaximumRetryCount': retry_count}
  1459. '''
  1460. name = 'restart_policy'
  1461. alias = 'restart'
  1462. for item in (name, alias):
  1463. # Test with retry count
  1464. self.assertEqual(
  1465. salt.utils.docker.translate_input(
  1466. self.translator,
  1467. **{item: 'on-failure:5'}
  1468. ),
  1469. {name: {'Name': 'on-failure', 'MaximumRetryCount': 5}}
  1470. )
  1471. # Test without retry count
  1472. self.assertEqual(
  1473. salt.utils.docker.translate_input(
  1474. self.translator,
  1475. **{item: 'on-failure'}
  1476. ),
  1477. {name: {'Name': 'on-failure', 'MaximumRetryCount': 0}}
  1478. )
  1479. # Error case: more than one policy passed
  1480. with self.assertRaisesRegex(
  1481. CommandExecutionError,
  1482. 'Only one policy is permitted'):
  1483. salt.utils.docker.translate_input(
  1484. self.translator,
  1485. **{item: 'on-failure,always'}
  1486. )
  1487. # Test collision
  1488. test_kwargs = {name: 'on-failure:5', alias: 'always'}
  1489. self.assertEqual(
  1490. salt.utils.docker.translate_input(
  1491. self.translator,
  1492. ignore_collisions=True,
  1493. **test_kwargs
  1494. ),
  1495. {name: {'Name': 'on-failure', 'MaximumRetryCount': 5}}
  1496. )
  1497. with self.assertRaisesRegex(
  1498. CommandExecutionError,
  1499. "'restart' is an alias for 'restart_policy'"):
  1500. salt.utils.docker.translate_input(
  1501. self.translator,
  1502. ignore_collisions=False,
  1503. **test_kwargs
  1504. )
  1505. @assert_stringlist(salt.utils.docker.translate.container)
  1506. def test_security_opt(self):
  1507. '''
  1508. Should be a list of strings or converted to one
  1509. '''
  1510. @assert_int_or_string(salt.utils.docker.translate.container)
  1511. def test_shm_size(self):
  1512. '''
  1513. Should be a string or converted to one
  1514. '''
  1515. @assert_bool(salt.utils.docker.translate.container)
  1516. def test_stdin_open(self):
  1517. '''
  1518. Should be a bool or converted to one
  1519. '''
  1520. @assert_string(salt.utils.docker.translate.container)
  1521. def test_stop_signal(self):
  1522. '''
  1523. Should be a string or converted to one
  1524. '''
  1525. @assert_int(salt.utils.docker.translate.container)
  1526. def test_stop_timeout(self):
  1527. '''
  1528. Should be an int or converted to one
  1529. '''
  1530. @assert_key_equals_value(salt.utils.docker.translate.container)
  1531. def test_storage_opt(self):
  1532. '''
  1533. Can be passed in several formats but must end up as a dictionary
  1534. mapping keys to values
  1535. '''
  1536. @assert_key_equals_value(salt.utils.docker.translate.container)
  1537. def test_sysctls(self):
  1538. '''
  1539. Can be passed in several formats but must end up as a dictionary
  1540. mapping keys to values
  1541. '''
  1542. @assert_dict(salt.utils.docker.translate.container)
  1543. def test_tmpfs(self):
  1544. '''
  1545. Can be passed in several formats but must end up as a dictionary
  1546. mapping keys to values
  1547. '''
  1548. @assert_bool(salt.utils.docker.translate.container)
  1549. def test_tty(self):
  1550. '''
  1551. Should be a bool or converted to one
  1552. '''
  1553. def test_ulimits(self):
  1554. '''
  1555. Input is in the format "name=soft_limit[:hard_limit]", but the API
  1556. wants it in the format
  1557. {'Name': name, 'Soft': soft_limit, 'Hard': hard_limit}
  1558. '''
  1559. # Test with and without hard limit
  1560. ulimits = 'nofile=1024:2048,nproc=50'
  1561. for val in (ulimits, ulimits.split(',')):
  1562. self.assertEqual(
  1563. salt.utils.docker.translate_input(
  1564. self.translator,
  1565. ulimits=val,
  1566. ),
  1567. {'ulimits': [{'Name': 'nofile', 'Soft': 1024, 'Hard': 2048},
  1568. {'Name': 'nproc', 'Soft': 50, 'Hard': 50}]}
  1569. )
  1570. # Error case: Invalid format
  1571. with self.assertRaisesRegex(
  1572. CommandExecutionError,
  1573. r"Ulimit definition 'nofile:1024:2048' is not in the format "
  1574. r"type=soft_limit\[:hard_limit\]"):
  1575. salt.utils.docker.translate_input(
  1576. self.translator,
  1577. ulimits='nofile:1024:2048'
  1578. )
  1579. # Error case: Invalid format
  1580. with self.assertRaisesRegex(
  1581. CommandExecutionError,
  1582. r"Limit 'nofile=foo:2048' contains non-numeric value\(s\)"):
  1583. salt.utils.docker.translate_input(
  1584. self.translator,
  1585. ulimits='nofile=foo:2048'
  1586. )
  1587. def test_user(self):
  1588. '''
  1589. Must be either username (string) or uid (int). An int passed as a
  1590. string (e.g. '0') should be converted to an int.
  1591. '''
  1592. # Username passed as string
  1593. self.assertEqual(
  1594. salt.utils.docker.translate_input(
  1595. self.translator,
  1596. user='foo'
  1597. ),
  1598. {'user': 'foo'}
  1599. )
  1600. for val in (0, '0'):
  1601. self.assertEqual(
  1602. salt.utils.docker.translate_input(
  1603. self.translator,
  1604. user=val
  1605. ),
  1606. {'user': 0}
  1607. )
  1608. # Error case: non string/int passed
  1609. with self.assertRaisesRegex(
  1610. CommandExecutionError,
  1611. 'Value must be a username or uid'):
  1612. salt.utils.docker.translate_input(
  1613. self.translator,
  1614. user=['foo']
  1615. )
  1616. # Error case: negative int passed
  1617. with self.assertRaisesRegex(
  1618. CommandExecutionError,
  1619. "'-1' is an invalid uid"):
  1620. salt.utils.docker.translate_input(
  1621. self.translator,
  1622. user=-1
  1623. )
  1624. @assert_string(salt.utils.docker.translate.container)
  1625. def test_userns_mode(self):
  1626. '''
  1627. Should be a bool or converted to one
  1628. '''
  1629. @assert_string(salt.utils.docker.translate.container)
  1630. def test_volume_driver(self):
  1631. '''
  1632. Should be a bool or converted to one
  1633. '''
  1634. @assert_stringlist(salt.utils.docker.translate.container)
  1635. def test_volumes(self):
  1636. '''
  1637. Should be a list of absolute paths
  1638. '''
  1639. # Error case: Not an absolute path
  1640. path = os.path.join('foo', 'bar', 'baz')
  1641. with self.assertRaisesRegex(
  1642. CommandExecutionError,
  1643. "'{0}' is not an absolute path".format(path.replace('\\', '\\\\'))):
  1644. salt.utils.docker.translate_input(
  1645. self.translator,
  1646. volumes=path
  1647. )
  1648. @assert_stringlist(salt.utils.docker.translate.container)
  1649. def test_volumes_from(self):
  1650. '''
  1651. Should be a list of strings or converted to one
  1652. '''
  1653. @assert_string(salt.utils.docker.translate.container)
  1654. def test_working_dir(self):
  1655. '''
  1656. Should be a single absolute path
  1657. '''
  1658. # Error case: Not an absolute path
  1659. path = os.path.join('foo', 'bar', 'baz')
  1660. with self.assertRaisesRegex(
  1661. CommandExecutionError,
  1662. "'{0}' is not an absolute path".format(path.replace('\\', '\\\\'))):
  1663. salt.utils.docker.translate_input(
  1664. self.translator,
  1665. working_dir=path
  1666. )
  1667. class TranslateNetworkInputTestCase(TranslateBase):
  1668. '''
  1669. Tests for salt.utils.docker.translate_input(), invoked using
  1670. salt.utils.docker.translate.network as the translator module.
  1671. '''
  1672. translator = salt.utils.docker.translate.network
  1673. ip_addrs = {
  1674. True: ('10.1.2.3', '::1'),
  1675. False: ('FOO', '0.9.800.1000', 'feaz::1', 'aj01::feac'),
  1676. }
  1677. @assert_string(salt.utils.docker.translate.network)
  1678. def test_driver(self):
  1679. '''
  1680. Should be a string or converted to one
  1681. '''
  1682. @assert_key_equals_value(salt.utils.docker.translate.network)
  1683. def test_options(self):
  1684. '''
  1685. Can be passed in several formats but must end up as a dictionary
  1686. mapping keys to values
  1687. '''
  1688. @assert_dict(salt.utils.docker.translate.network)
  1689. def test_ipam(self):
  1690. '''
  1691. Must be a dict
  1692. '''
  1693. @assert_bool(salt.utils.docker.translate.network)
  1694. def test_check_duplicate(self):
  1695. '''
  1696. Should be a bool or converted to one
  1697. '''
  1698. @assert_bool(salt.utils.docker.translate.network)
  1699. def test_internal(self):
  1700. '''
  1701. Should be a bool or converted to one
  1702. '''
  1703. @assert_labels(salt.utils.docker.translate.network)
  1704. def test_labels(self):
  1705. '''
  1706. Can be passed as a list of key=value pairs or a dictionary, and must
  1707. ultimately end up as a dictionary.
  1708. '''
  1709. @assert_bool(salt.utils.docker.translate.network)
  1710. def test_enable_ipv6(self):
  1711. '''
  1712. Should be a bool or converted to one
  1713. '''
  1714. @assert_bool(salt.utils.docker.translate.network)
  1715. def test_attachable(self):
  1716. '''
  1717. Should be a bool or converted to one
  1718. '''
  1719. @assert_bool(salt.utils.docker.translate.network)
  1720. def test_ingress(self):
  1721. '''
  1722. Should be a bool or converted to one
  1723. '''
  1724. @assert_string(salt.utils.docker.translate.network)
  1725. def test_ipam_driver(self):
  1726. '''
  1727. Should be a bool or converted to one
  1728. '''
  1729. @assert_key_equals_value(salt.utils.docker.translate.network)
  1730. def test_ipam_opts(self):
  1731. '''
  1732. Can be passed in several formats but must end up as a dictionary
  1733. mapping keys to values
  1734. '''
  1735. def ipam_pools(self):
  1736. '''
  1737. Must be a list of dictionaries (not a dictlist)
  1738. '''
  1739. good_pool = {
  1740. 'subnet': '10.0.0.0/24',
  1741. 'iprange': '10.0.0.128/25',
  1742. 'gateway': '10.0.0.254',
  1743. 'aux_addresses': {'foo.bar.tld': '10.0.0.20',
  1744. 'hello.world.tld': '10.0.0.21'},
  1745. }
  1746. bad_pools = [
  1747. {'subnet': '10.0.0.0/33',
  1748. 'iprange': '10.0.0.128/25',
  1749. 'gateway': '10.0.0.254',
  1750. 'aux_addresses': {'foo.bar.tld': '10.0.0.20',
  1751. 'hello.world.tld': '10.0.0.21'}},
  1752. {'subnet': '10.0.0.0/24',
  1753. 'iprange': 'foo/25',
  1754. 'gateway': '10.0.0.254',
  1755. 'aux_addresses': {'foo.bar.tld': '10.0.0.20',
  1756. 'hello.world.tld': '10.0.0.21'}},
  1757. {'subnet': '10.0.0.0/24',
  1758. 'iprange': '10.0.0.128/25',
  1759. 'gateway': '10.0.0.256',
  1760. 'aux_addresses': {'foo.bar.tld': '10.0.0.20',
  1761. 'hello.world.tld': '10.0.0.21'}},
  1762. {'subnet': '10.0.0.0/24',
  1763. 'iprange': '10.0.0.128/25',
  1764. 'gateway': '10.0.0.254',
  1765. 'aux_addresses': {'foo.bar.tld': '10.0.0.20',
  1766. 'hello.world.tld': '999.0.0.21'}},
  1767. ]
  1768. self.assertEqual(
  1769. salt.utils.docker.translate_input(
  1770. self.translator,
  1771. ipam_pools=[good_pool],
  1772. ),
  1773. {'ipam_pools': [good_pool]}
  1774. )
  1775. for bad_pool in bad_pools:
  1776. with self.assertRaisesRegex(CommandExecutionError, 'not a valid'):
  1777. salt.utils.docker.translate_input(
  1778. self.translator,
  1779. ipam_pools=[good_pool, bad_pool]
  1780. )
  1781. @assert_subnet(salt.utils.docker.translate.network)
  1782. def test_subnet(self):
  1783. '''
  1784. Must be an IPv4 or IPv6 subnet
  1785. '''
  1786. @assert_subnet(salt.utils.docker.translate.network)
  1787. def test_iprange(self):
  1788. '''
  1789. Must be an IPv4 or IPv6 subnet
  1790. '''
  1791. def test_gateway(self):
  1792. '''
  1793. Must be an IPv4 or IPv6 address
  1794. '''
  1795. for val in self.ip_addrs[True]:
  1796. self.assertEqual(
  1797. salt.utils.docker.translate_input(
  1798. self.translator,
  1799. validate_ip_addrs=True,
  1800. gateway=val,
  1801. ),
  1802. self.apply_defaults({'gateway': val})
  1803. )
  1804. for val in self.ip_addrs[False]:
  1805. with self.assertRaisesRegex(
  1806. CommandExecutionError,
  1807. "'{0}' is not a valid IP address".format(val)):
  1808. salt.utils.docker.translate_input(
  1809. self.translator,
  1810. validate_ip_addrs=True,
  1811. gateway=val,
  1812. )
  1813. self.assertEqual(
  1814. salt.utils.docker.translate_input(
  1815. self.translator,
  1816. validate_ip_addrs=False,
  1817. gateway=val,
  1818. ),
  1819. self.apply_defaults(
  1820. {'gateway': val if isinstance(val, six.string_types)
  1821. else six.text_type(val)}
  1822. )
  1823. )
  1824. @assert_key_equals_value(salt.utils.docker.translate.network)
  1825. def test_aux_addresses(self):
  1826. '''
  1827. Must be a mapping of hostnames to IP addresses
  1828. '''
  1829. name = 'aux_addresses'
  1830. alias = 'aux_address'
  1831. for item in (name, alias):
  1832. for val in self.ip_addrs[True]:
  1833. addresses = {'foo.bar.tld': val}
  1834. self.assertEqual(
  1835. salt.utils.docker.translate_input(
  1836. self.translator,
  1837. validate_ip_addrs=True,
  1838. **{item: addresses}
  1839. ),
  1840. self.apply_defaults({name: addresses})
  1841. )
  1842. for val in self.ip_addrs[False]:
  1843. addresses = {'foo.bar.tld': val}
  1844. with self.assertRaisesRegex(
  1845. CommandExecutionError,
  1846. "'{0}' is not a valid IP address".format(val)):
  1847. salt.utils.docker.translate_input(
  1848. self.translator,
  1849. validate_ip_addrs=True,
  1850. **{item: addresses}
  1851. )
  1852. self.assertEqual(
  1853. salt.utils.docker.translate_input(
  1854. self.translator,
  1855. validate_ip_addrs=False,
  1856. aux_addresses=addresses,
  1857. ),
  1858. self.apply_defaults({name: addresses})
  1859. )
  1860. class DockerTranslateHelperTestCase(TestCase):
  1861. '''
  1862. Tests for a couple helper functions in salt.utils.docker.translate
  1863. '''
  1864. def test_get_port_def(self):
  1865. '''
  1866. Test translation of port definition (1234, '1234/tcp', '1234/udp',
  1867. etc.) into the format which docker-py uses (integer for TCP ports,
  1868. 'port_num/udp' for UDP ports).
  1869. '''
  1870. # Test TCP port (passed as int, no protocol passed)
  1871. self.assertEqual(translate_helpers.get_port_def(2222), 2222)
  1872. # Test TCP port (passed as str, no protocol passed)
  1873. self.assertEqual(translate_helpers.get_port_def('2222'), 2222)
  1874. # Test TCP port (passed as str, with protocol passed)
  1875. self.assertEqual(translate_helpers.get_port_def('2222', 'tcp'), 2222)
  1876. # Test TCP port (proto passed in port_num, with passed proto ignored).
  1877. # This is a contrived example as we would never invoke the function in
  1878. # this way, but it tests that we are taking the port number from the
  1879. # port_num argument and ignoring the passed protocol.
  1880. self.assertEqual(translate_helpers.get_port_def('2222/tcp', 'udp'), 2222)
  1881. # Test UDP port (passed as int)
  1882. self.assertEqual(translate_helpers.get_port_def(2222, 'udp'), (2222, 'udp'))
  1883. # Test UDP port (passed as string)
  1884. self.assertEqual(translate_helpers.get_port_def('2222', 'udp'), (2222, 'udp'))
  1885. # Test UDP port (proto passed in port_num
  1886. self.assertEqual(translate_helpers.get_port_def('2222/udp'), (2222, 'udp'))
  1887. def test_get_port_range(self):
  1888. '''
  1889. Test extracting the start and end of a port range from a port range
  1890. expression (e.g. 4505-4506)
  1891. '''
  1892. # Passing a single int should return the start and end as the same value
  1893. self.assertEqual(translate_helpers.get_port_range(2222), (2222, 2222))
  1894. # Same as above but with port number passed as a string
  1895. self.assertEqual(translate_helpers.get_port_range('2222'), (2222, 2222))
  1896. # Passing a port range
  1897. self.assertEqual(translate_helpers.get_port_range('2222-2223'), (2222, 2223))
  1898. # Error case: port range start is greater than end
  1899. with self.assertRaisesRegex(
  1900. ValueError,
  1901. r'Start of port range \(2222\) cannot be greater than end of '
  1902. r'port range \(2221\)'):
  1903. translate_helpers.get_port_range('2222-2221')
  1904. # Error case: non-numeric input
  1905. with self.assertRaisesRegex(
  1906. ValueError,
  1907. '\'2222-bar\' is non-numeric or an invalid port range'):
  1908. translate_helpers.get_port_range('2222-bar')