test_config.py 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Unit tests for salt.config
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. import os
  9. import textwrap
  10. # Import Salt Testing libs
  11. from tests.support.helpers import with_tempdir, with_tempfile, patched_environ
  12. from tests.support.mixins import AdaptedConfigurationTestCaseMixin
  13. from tests.support.unit import skipIf, TestCase
  14. from tests.support.runtests import RUNTIME_VARS
  15. from tests.support.mock import (
  16. Mock,
  17. MagicMock,
  18. patch
  19. )
  20. # Import Salt libs
  21. import salt.config
  22. import salt.minion
  23. import salt.syspaths
  24. import salt.utils.files
  25. import salt.utils.network
  26. import salt.utils.platform
  27. import salt.utils.yaml
  28. from salt.ext import six
  29. from salt.syspaths import CONFIG_DIR
  30. from salt.exceptions import (
  31. CommandExecutionError,
  32. SaltConfigurationError,
  33. SaltCloudConfigError
  34. )
  35. log = logging.getLogger(__name__)
  36. SAMPLE_CONF_DIR = os.path.join(RUNTIME_VARS.CODE_DIR, 'conf') + os.sep
  37. # mock hostname should be more complex than the systems FQDN
  38. MOCK_HOSTNAME = 'very.long.complex.fqdn.that.is.crazy.extra.long.example.com'
  39. MOCK_ETC_HOSTS = textwrap.dedent('''\
  40. ##
  41. # Host Database
  42. #
  43. # localhost is used to configure the loopback interface
  44. # when the system is booting. Do not change this entry.
  45. ## The empty line below must remain, it factors into the tests.
  46. 127.0.0.1 localhost {hostname}
  47. 10.0.0.100 {hostname}
  48. 200.200.200.2 other.host.alias.com
  49. ::1 ip6-localhost ip6-loopback
  50. fe00::0 ip6-localnet
  51. ff00::0 ip6-mcastprefix
  52. '''.format(hostname=MOCK_HOSTNAME))
  53. MOCK_ETC_HOSTNAME = '{}\n'.format(MOCK_HOSTNAME)
  54. PATH = 'path/to/some/cloud/conf/file'
  55. DEFAULT = {'default_include': PATH}
  56. class DefaultConfigsBase(object):
  57. @classmethod
  58. def setUpClass(cls):
  59. cls.mock_master_default_opts = dict(
  60. root_dir=RUNTIME_VARS.TMP_ROOT_DIR,
  61. log_file=os.path.join(RUNTIME_VARS.TMP_ROOT_DIR, 'var', 'log', 'salt', 'master'),
  62. pid_file=os.path.join(RUNTIME_VARS.TMP_ROOT_DIR, 'var', 'run', 'salt-master.pid')
  63. )
  64. class SampleConfTest(DefaultConfigsBase, TestCase):
  65. '''
  66. Validate files in the salt/conf directory.
  67. '''
  68. def test_conf_master_sample_is_commented(self):
  69. '''
  70. The sample config file located in salt/conf/master must be completely
  71. commented out. This test checks for any lines that are not commented or blank.
  72. '''
  73. master_config = SAMPLE_CONF_DIR + 'master'
  74. ret = salt.config._read_conf_file(master_config)
  75. self.assertEqual(
  76. ret,
  77. {},
  78. 'Sample config file \'{}\' must be commented out.'.format(
  79. master_config
  80. )
  81. )
  82. def test_conf_minion_sample_is_commented(self):
  83. '''
  84. The sample config file located in salt/conf/minion must be completely
  85. commented out. This test checks for any lines that are not commented or blank.
  86. '''
  87. minion_config = SAMPLE_CONF_DIR + 'minion'
  88. ret = salt.config._read_conf_file(minion_config)
  89. self.assertEqual(
  90. ret,
  91. {},
  92. 'Sample config file \'{}\' must be commented out.'.format(
  93. minion_config
  94. )
  95. )
  96. def test_conf_cloud_sample_is_commented(self):
  97. '''
  98. The sample config file located in salt/conf/cloud must be completely
  99. commented out. This test checks for any lines that are not commented or blank.
  100. '''
  101. cloud_config = SAMPLE_CONF_DIR + 'cloud'
  102. ret = salt.config._read_conf_file(cloud_config)
  103. self.assertEqual(
  104. ret,
  105. {},
  106. 'Sample config file \'{}\' must be commented out.'.format(
  107. cloud_config
  108. )
  109. )
  110. def test_conf_cloud_profiles_sample_is_commented(self):
  111. '''
  112. The sample config file located in salt/conf/cloud.profiles must be completely
  113. commented out. This test checks for any lines that are not commented or blank.
  114. '''
  115. cloud_profiles_config = SAMPLE_CONF_DIR + 'cloud.profiles'
  116. ret = salt.config._read_conf_file(cloud_profiles_config)
  117. self.assertEqual(
  118. ret,
  119. {},
  120. 'Sample config file \'{}\' must be commented out.'.format(
  121. cloud_profiles_config
  122. )
  123. )
  124. def test_conf_cloud_providers_sample_is_commented(self):
  125. '''
  126. The sample config file located in salt/conf/cloud.providers must be completely
  127. commented out. This test checks for any lines that are not commented or blank.
  128. '''
  129. cloud_providers_config = SAMPLE_CONF_DIR + 'cloud.providers'
  130. ret = salt.config._read_conf_file(cloud_providers_config)
  131. self.assertEqual(
  132. ret,
  133. {},
  134. 'Sample config file \'{}\' must be commented out.'.format(
  135. cloud_providers_config
  136. )
  137. )
  138. def test_conf_proxy_sample_is_commented(self):
  139. '''
  140. The sample config file located in salt/conf/proxy must be completely
  141. commented out. This test checks for any lines that are not commented or blank.
  142. '''
  143. proxy_config = SAMPLE_CONF_DIR + 'proxy'
  144. ret = salt.config._read_conf_file(proxy_config)
  145. self.assertEqual(
  146. ret,
  147. {},
  148. 'Sample config file \'{}\' must be commented out.'.format(
  149. proxy_config
  150. )
  151. )
  152. def test_conf_roster_sample_is_commented(self):
  153. '''
  154. The sample config file located in salt/conf/roster must be completely
  155. commented out. This test checks for any lines that are not commented or blank.
  156. '''
  157. roster_config = SAMPLE_CONF_DIR + 'roster'
  158. ret = salt.config._read_conf_file(roster_config)
  159. self.assertEqual(
  160. ret,
  161. {},
  162. 'Sample config file \'{}\' must be commented out.'.format(
  163. roster_config
  164. )
  165. )
  166. def test_conf_cloud_profiles_d_files_are_commented(self):
  167. '''
  168. All cloud profile sample configs in salt/conf/cloud.profiles.d/* must be completely
  169. commented out. This test loops through all of the files in that directory to check
  170. for any lines that are not commented or blank.
  171. '''
  172. cloud_sample_dir = SAMPLE_CONF_DIR + 'cloud.profiles.d/'
  173. if not os.path.exists(cloud_sample_dir):
  174. self.skipTest("Sample config directory '{}' is missing.".format(cloud_sample_dir))
  175. cloud_sample_files = os.listdir(cloud_sample_dir)
  176. for conf_file in cloud_sample_files:
  177. profile_conf = cloud_sample_dir + conf_file
  178. ret = salt.config._read_conf_file(profile_conf)
  179. self.assertEqual(
  180. ret,
  181. {},
  182. 'Sample config file \'{}\' must be commented out.'.format(
  183. conf_file
  184. )
  185. )
  186. def test_conf_cloud_providers_d_files_are_commented(self):
  187. '''
  188. All cloud profile sample configs in salt/conf/cloud.providers.d/* must be completely
  189. commented out. This test loops through all of the files in that directory to check
  190. for any lines that are not commented or blank.
  191. '''
  192. cloud_sample_dir = SAMPLE_CONF_DIR + 'cloud.providers.d/'
  193. if not os.path.exists(cloud_sample_dir):
  194. self.skipTest("Sample config directory '{}' is missing.".format(cloud_sample_dir))
  195. cloud_sample_files = os.listdir(cloud_sample_dir)
  196. for conf_file in cloud_sample_files:
  197. provider_conf = cloud_sample_dir + conf_file
  198. ret = salt.config._read_conf_file(provider_conf)
  199. self.assertEqual(
  200. ret,
  201. {},
  202. 'Sample config file \'{}\' must be commented out.'.format(
  203. conf_file
  204. )
  205. )
  206. def test_conf_cloud_maps_d_files_are_commented(self):
  207. '''
  208. All cloud profile sample configs in salt/conf/cloud.maps.d/* must be completely
  209. commented out. This test loops through all of the files in that directory to check
  210. for any lines that are not commented or blank.
  211. '''
  212. cloud_sample_dir = SAMPLE_CONF_DIR + 'cloud.maps.d/'
  213. if not os.path.exists(cloud_sample_dir):
  214. self.skipTest("Sample config directory '{}' is missing.".format(cloud_sample_dir))
  215. cloud_sample_files = os.listdir(cloud_sample_dir)
  216. for conf_file in cloud_sample_files:
  217. map_conf = cloud_sample_dir + conf_file
  218. ret = salt.config._read_conf_file(map_conf)
  219. self.assertEqual(
  220. ret,
  221. {},
  222. 'Sample config file \'{}\' must be commented out.'.format(
  223. conf_file
  224. )
  225. )
  226. def _unhandled_mock_read(filename):
  227. '''
  228. Raise an error because we should not be calling salt.utils.files.fopen()
  229. '''
  230. raise CommandExecutionError('Unhandled mock read for {}'.format(filename))
  231. def _salt_configuration_error(filename):
  232. '''
  233. Raise an error to indicate error in the Salt configuration file
  234. '''
  235. raise SaltConfigurationError('Configuration error in {}'.format(filename))
  236. class ConfigTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
  237. @with_tempfile()
  238. def test_sha256_is_default_for_master(self, fpath):
  239. with salt.utils.files.fopen(fpath, 'w') as wfh:
  240. wfh.write(
  241. "root_dir: /\n"
  242. "key_logfile: key\n"
  243. )
  244. config = salt.config.master_config(fpath)
  245. self.assertEqual(config['hash_type'], 'sha256')
  246. @with_tempfile()
  247. def test_sha256_is_default_for_minion(self, fpath):
  248. with salt.utils.files.fopen(fpath, 'w') as wfh:
  249. wfh.write(
  250. "root_dir: /\n"
  251. "key_logfile: key\n"
  252. )
  253. config = salt.config.minion_config(fpath)
  254. self.assertEqual(config['hash_type'], 'sha256')
  255. @with_tempfile()
  256. def test_proper_path_joining(self, fpath):
  257. temp_config = 'root_dir: /\n' \
  258. 'key_logfile: key\n'
  259. if salt.utils.platform.is_windows():
  260. temp_config = 'root_dir: c:\\\n' \
  261. 'key_logfile: key\n'
  262. with salt.utils.files.fopen(fpath, 'w') as fp_:
  263. fp_.write(temp_config)
  264. config = salt.config.master_config(fpath)
  265. expect_path_join = os.path.join('/', 'key')
  266. expect_sep_join = '//key'
  267. if salt.utils.platform.is_windows():
  268. expect_path_join = os.path.join('c:\\', 'key')
  269. expect_sep_join = 'c:\\\\key'
  270. # os.path.join behavior
  271. self.assertEqual(config['key_logfile'], expect_path_join)
  272. # os.sep.join behavior
  273. self.assertNotEqual(config['key_logfile'], expect_sep_join)
  274. @with_tempdir()
  275. def test_common_prefix_stripping(self, tempdir):
  276. root_dir = os.path.join(tempdir, 'foo', 'bar')
  277. os.makedirs(root_dir)
  278. fpath = os.path.join(root_dir, 'config')
  279. with salt.utils.files.fopen(fpath, 'w') as fp_:
  280. fp_.write(
  281. 'root_dir: {}\n'
  282. 'log_file: {}\n'.format(root_dir, fpath)
  283. )
  284. config = salt.config.master_config(fpath)
  285. self.assertEqual(config['log_file'], fpath)
  286. @with_tempdir()
  287. def test_default_root_dir_included_in_config_root_dir(self, tempdir):
  288. root_dir = os.path.join(tempdir, 'foo', 'bar')
  289. os.makedirs(root_dir)
  290. fpath = os.path.join(root_dir, 'config')
  291. with salt.utils.files.fopen(fpath, 'w') as fp_:
  292. fp_.write(
  293. 'root_dir: {}\n'
  294. 'log_file: {}\n'.format(root_dir, fpath)
  295. )
  296. config = salt.config.master_config(fpath)
  297. self.assertEqual(config['log_file'], fpath)
  298. @skipIf(
  299. salt.utils.platform.is_windows(),
  300. 'You can\'t set an environment dynamically in Windows')
  301. @with_tempdir()
  302. def test_load_master_config_from_environ_var(self, tempdir):
  303. env_root_dir = os.path.join(tempdir, 'foo', 'env')
  304. os.makedirs(env_root_dir)
  305. env_fpath = os.path.join(env_root_dir, 'config-env')
  306. with salt.utils.files.fopen(env_fpath, 'w') as fp_:
  307. fp_.write(
  308. 'root_dir: {}\n'
  309. 'log_file: {}\n'.format(env_root_dir, env_fpath)
  310. )
  311. with patched_environ(SALT_MASTER_CONFIG=env_fpath):
  312. # Should load from env variable, not the default configuration file.
  313. config = salt.config.master_config('{}/master'.format(CONFIG_DIR))
  314. self.assertEqual(config['log_file'], env_fpath)
  315. root_dir = os.path.join(tempdir, 'foo', 'bar')
  316. os.makedirs(root_dir)
  317. fpath = os.path.join(root_dir, 'config')
  318. with salt.utils.files.fopen(fpath, 'w') as fp_:
  319. fp_.write(
  320. 'root_dir: {}\n'
  321. 'log_file: {}\n'.format(root_dir, fpath)
  322. )
  323. # Let's set the environment variable, yet, since the configuration
  324. # file path is not the default one, i.e., the user has passed an
  325. # alternative configuration file form the CLI parser, the
  326. # environment variable will be ignored.
  327. with patched_environ(SALT_MASTER_CONFIG=env_fpath):
  328. config = salt.config.master_config(fpath)
  329. self.assertEqual(config['log_file'], fpath)
  330. @skipIf(
  331. salt.utils.platform.is_windows(),
  332. 'You can\'t set an environment dynamically in Windows')
  333. @with_tempdir()
  334. def test_load_minion_config_from_environ_var(self, tempdir):
  335. env_root_dir = os.path.join(tempdir, 'foo', 'env')
  336. os.makedirs(env_root_dir)
  337. env_fpath = os.path.join(env_root_dir, 'config-env')
  338. with salt.utils.files.fopen(env_fpath, 'w') as fp_:
  339. fp_.write(
  340. 'root_dir: {}\n'
  341. 'log_file: {}\n'.format(env_root_dir, env_fpath)
  342. )
  343. with patched_environ(SALT_MINION_CONFIG=env_fpath):
  344. # Should load from env variable, not the default configuration file
  345. config = salt.config.minion_config('{}/minion'.format(CONFIG_DIR))
  346. self.assertEqual(config['log_file'], env_fpath)
  347. root_dir = os.path.join(tempdir, 'foo', 'bar')
  348. os.makedirs(root_dir)
  349. fpath = os.path.join(root_dir, 'config')
  350. with salt.utils.files.fopen(fpath, 'w') as fp_:
  351. fp_.write(
  352. 'root_dir: {}\n'
  353. 'log_file: {}\n'.format(root_dir, fpath)
  354. )
  355. # Let's set the environment variable, yet, since the configuration
  356. # file path is not the default one, i.e., the user has passed an
  357. # alternative configuration file form the CLI parser, the
  358. # environment variable will be ignored.
  359. with patched_environ(SALT_MINION_CONFIG=env_fpath):
  360. config = salt.config.minion_config(fpath)
  361. self.assertEqual(config['log_file'], fpath)
  362. @with_tempdir()
  363. def test_load_client_config_from_environ_var(self, tempdir):
  364. env_root_dir = os.path.join(tempdir, 'foo', 'env')
  365. os.makedirs(env_root_dir)
  366. # Let's populate a master configuration file which should not get
  367. # picked up since the client configuration tries to load the master
  368. # configuration settings using the provided client configuration
  369. # file
  370. master_config = os.path.join(env_root_dir, 'master')
  371. with salt.utils.files.fopen(master_config, 'w') as fp_:
  372. fp_.write(
  373. 'blah: true\n'
  374. 'root_dir: {}\n'
  375. 'log_file: {}\n'.format(env_root_dir, master_config)
  376. )
  377. # Now the client configuration file
  378. env_fpath = os.path.join(env_root_dir, 'config-env')
  379. with salt.utils.files.fopen(env_fpath, 'w') as fp_:
  380. fp_.write(
  381. 'root_dir: {}\n'
  382. 'log_file: {}\n'.format(env_root_dir, env_fpath)
  383. )
  384. with patched_environ(SALT_MASTER_CONFIG=master_config,
  385. SALT_CLIENT_CONFIG=env_fpath):
  386. # Should load from env variable, not the default configuration file
  387. config = salt.config.client_config(os.path.expanduser('~/.salt'))
  388. self.assertEqual(config['log_file'], env_fpath)
  389. self.assertTrue('blah' not in config)
  390. root_dir = os.path.join(tempdir, 'foo', 'bar')
  391. os.makedirs(root_dir)
  392. fpath = os.path.join(root_dir, 'config')
  393. with salt.utils.files.fopen(fpath, 'w') as fp_:
  394. fp_.write(
  395. 'root_dir: {}\n'
  396. 'log_file: {}\n'.format(root_dir, fpath)
  397. )
  398. # Let's set the environment variable, yet, since the configuration
  399. # file path is not the default one, i.e., the user has passed an
  400. # alternative configuration file form the CLI parser, the
  401. # environment variable will be ignored.
  402. with patched_environ(SALT_MASTER_CONFIG=env_fpath,
  403. SALT_CLIENT_CONFIG=env_fpath):
  404. config = salt.config.master_config(fpath)
  405. self.assertEqual(config['log_file'], fpath)
  406. @with_tempdir()
  407. def test_issue_5970_minion_confd_inclusion(self, tempdir):
  408. minion_config = os.path.join(tempdir, 'minion')
  409. minion_confd = os.path.join(tempdir, 'minion.d')
  410. os.makedirs(minion_confd)
  411. # Let's populate a minion configuration file with some basic
  412. # settings
  413. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  414. fp_.write(
  415. 'blah: false\n'
  416. 'root_dir: {}\n'
  417. 'log_file: {}\n'.format(tempdir, minion_config)
  418. )
  419. # Now, let's populate an extra configuration file under minion.d
  420. # Notice that above we've set blah as False and below as True.
  421. # Since the minion.d files are loaded after the main configuration
  422. # file so overrides can happen, the final value of blah should be
  423. # True.
  424. extra_config = os.path.join(minion_confd, 'extra.conf')
  425. with salt.utils.files.fopen(extra_config, 'w') as fp_:
  426. fp_.write('blah: true\n')
  427. # Let's load the configuration
  428. config = salt.config.minion_config(minion_config)
  429. self.assertEqual(config['log_file'], minion_config)
  430. # As proven by the assertion below, blah is True
  431. self.assertTrue(config['blah'])
  432. @with_tempdir()
  433. def test_master_confd_inclusion(self, tempdir):
  434. master_config = os.path.join(tempdir, 'master')
  435. master_confd = os.path.join(tempdir, 'master.d')
  436. os.makedirs(master_confd)
  437. # Let's populate a master configuration file with some basic
  438. # settings
  439. with salt.utils.files.fopen(master_config, 'w') as fp_:
  440. fp_.write(
  441. 'blah: false\n'
  442. 'root_dir: {}\n'
  443. 'log_file: {}\n'.format(tempdir, master_config)
  444. )
  445. # Now, let's populate an extra configuration file under master.d
  446. # Notice that above we've set blah as False and below as True.
  447. # Since the master.d files are loaded after the main configuration
  448. # file so overrides can happen, the final value of blah should be
  449. # True.
  450. extra_config = os.path.join(master_confd, 'extra.conf')
  451. with salt.utils.files.fopen(extra_config, 'w') as fp_:
  452. fp_.write('blah: true\n')
  453. # Let's load the configuration
  454. config = salt.config.master_config(master_config)
  455. self.assertEqual(config['log_file'], master_config)
  456. # As proven by the assertion below, blah is True
  457. self.assertTrue(config['blah'])
  458. @with_tempfile()
  459. @with_tempdir()
  460. def test_master_file_roots_glob(self, tempdir, fpath):
  461. # Create some files
  462. for f in 'abc':
  463. fpath = os.path.join(tempdir, f)
  464. with salt.utils.files.fopen(fpath, 'w') as wfh:
  465. wfh.write(f)
  466. with salt.utils.files.fopen(fpath, 'w') as wfh:
  467. wfh.write(
  468. 'file_roots:\n'
  469. ' base:\n'
  470. ' - {}'.format(os.path.join(tempdir, '*'))
  471. )
  472. config = salt.config.master_config(fpath)
  473. base = config['file_roots']['base']
  474. self.assertEqual(set(base),
  475. {os.path.join(tempdir, 'a'), os.path.join(tempdir, 'b'), os.path.join(tempdir, 'c')})
  476. def test_validate_bad_file_roots(self):
  477. expected = salt.config._expand_glob_path(
  478. [salt.syspaths.BASE_FILE_ROOTS_DIR]
  479. )
  480. with patch('salt.config._normalize_roots') as mk:
  481. ret = salt.config._validate_file_roots(None)
  482. assert not mk.called
  483. assert ret == {'base': expected}
  484. @with_tempfile()
  485. @with_tempdir()
  486. def test_master_pillar_roots_glob(self, tempdir, fpath):
  487. # Create some files.
  488. for f in 'abc':
  489. fpath = os.path.join(tempdir, f)
  490. with salt.utils.files.fopen(fpath, 'w') as wfh:
  491. wfh.write(f)
  492. with salt.utils.files.fopen(fpath, 'w') as wfh:
  493. wfh.write(
  494. 'pillar_roots:\n'
  495. ' base:\n'
  496. ' - {}'.format(os.path.join(tempdir, '*'))
  497. )
  498. config = salt.config.master_config(fpath)
  499. base = config['pillar_roots']['base']
  500. self.assertEqual(set(base),
  501. {os.path.join(tempdir, 'a'), os.path.join(tempdir, 'b'), os.path.join(tempdir, 'c')})
  502. def test_validate_bad_pillar_roots(self):
  503. expected = salt.config._expand_glob_path(
  504. [salt.syspaths.BASE_PILLAR_ROOTS_DIR]
  505. )
  506. with patch('salt.config._normalize_roots') as mk:
  507. ret = salt.config._validate_pillar_roots(None)
  508. assert not mk.called
  509. assert ret == {'base': expected}
  510. @with_tempdir()
  511. def test_master_id_function(self, tempdir):
  512. master_config = os.path.join(tempdir, 'master')
  513. with salt.utils.files.fopen(master_config, 'w') as fp_:
  514. fp_.write(
  515. 'id_function:\n'
  516. ' test.echo:\n'
  517. ' text: hello_world\n'
  518. 'root_dir: {}\n'
  519. 'log_file: {}\n'.format(tempdir, master_config)
  520. )
  521. # Let's load the configuration
  522. config = salt.config.master_config(master_config)
  523. self.assertEqual(config['log_file'], master_config)
  524. # 'master_config' appends '_master' to the ID
  525. self.assertEqual(config['id'], 'hello_world_master')
  526. @with_tempfile()
  527. @with_tempdir()
  528. def test_minion_file_roots_glob(self, tempdir, fpath):
  529. # Create some files.
  530. for f in 'abc':
  531. fpath = os.path.join(tempdir, f)
  532. with salt.utils.files.fopen(fpath, 'w') as wfh:
  533. wfh.write(f)
  534. with salt.utils.files.fopen(fpath, 'w') as wfh:
  535. wfh.write(
  536. 'file_roots:\n'
  537. ' base:\n'
  538. ' - {}'.format(os.path.join(tempdir, '*'))
  539. )
  540. config = salt.config.minion_config(fpath)
  541. base = config['file_roots']['base']
  542. self.assertEqual(set(base),
  543. {os.path.join(tempdir, 'a'), os.path.join(tempdir, 'b'), os.path.join(tempdir, 'c')})
  544. @with_tempfile()
  545. @with_tempdir()
  546. def test_minion_pillar_roots_glob(self, tempdir, fpath):
  547. # Create some files.
  548. for f in 'abc':
  549. fpath = os.path.join(tempdir, f)
  550. with salt.utils.files.fopen(fpath, 'w') as wfh:
  551. wfh.write(f)
  552. with salt.utils.files.fopen(fpath, 'w') as wfh:
  553. wfh.write(
  554. 'pillar_roots:\n'
  555. ' base:\n'
  556. ' - {}'.format(os.path.join(tempdir, '*'))
  557. )
  558. config = salt.config.minion_config(fpath)
  559. base = config['pillar_roots']['base']
  560. self.assertEqual(set(base),
  561. {os.path.join(tempdir, 'a'), os.path.join(tempdir, 'b'), os.path.join(tempdir, 'c')})
  562. @with_tempdir()
  563. def test_minion_id_function(self, tempdir):
  564. minion_config = os.path.join(tempdir, 'minion')
  565. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  566. fp_.write(
  567. 'id_function:\n'
  568. ' test.echo:\n'
  569. ' text: hello_world\n'
  570. 'root_dir: {}\n'
  571. 'log_file: {}\n'.format(tempdir, minion_config)
  572. )
  573. # Let's load the configuration
  574. config = salt.config.minion_config(minion_config)
  575. self.assertEqual(config['log_file'], minion_config)
  576. self.assertEqual(config['id'], 'hello_world')
  577. @with_tempdir()
  578. def test_minion_id_lowercase(self, tempdir):
  579. '''
  580. This tests that setting `minion_id_lowercase: True` does lower case
  581. the minion id. Lowercase does not operate on a static `id: KING_BOB`
  582. setting, or a cached id.
  583. '''
  584. minion_config = os.path.join(tempdir, 'minion')
  585. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  586. fp_.write(textwrap.dedent('''\
  587. id_function:
  588. test.echo:
  589. text: KING_BOB
  590. minion_id_caching: False
  591. minion_id_lowercase: True
  592. '''))
  593. config = salt.config.minion_config(minion_config) # Load the configuration
  594. self.assertEqual(config['minion_id_caching'], False) # Check the configuration
  595. self.assertEqual(config['minion_id_lowercase'], True) # Check the configuration
  596. self.assertEqual(config['id'], 'king_bob')
  597. @with_tempdir()
  598. def test_minion_id_remove_domain_string_positive(self, tempdir):
  599. '''
  600. This tests that the values of `minion_id_remove_domain` is suppressed from a generated minion id,
  601. effectivly generating a hostname minion_id.
  602. '''
  603. minion_config = os.path.join(tempdir, 'minion')
  604. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  605. fp_.write(textwrap.dedent('''\
  606. id_function:
  607. test.echo:
  608. text: king_bob.foo.org
  609. minion_id_remove_domain: foo.org
  610. minion_id_caching: False
  611. '''))
  612. # Let's load the configuration
  613. config = salt.config.minion_config(minion_config)
  614. self.assertEqual(config['minion_id_remove_domain'], 'foo.org')
  615. self.assertEqual(config['id'], 'king_bob')
  616. @with_tempdir()
  617. def test_minion_id_remove_domain_string_negative(self, tempdir):
  618. '''
  619. See above
  620. '''
  621. minion_config = os.path.join(tempdir, 'minion')
  622. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  623. fp_.write(textwrap.dedent('''\
  624. id_function:
  625. test.echo:
  626. text: king_bob.foo.org
  627. minion_id_remove_domain: bar.org
  628. minion_id_caching: False
  629. '''))
  630. config = salt.config.minion_config(minion_config)
  631. self.assertEqual(config['id'], 'king_bob.foo.org')
  632. @with_tempdir()
  633. def test_minion_id_remove_domain_bool_true(self, tempdir):
  634. '''
  635. See above
  636. '''
  637. minion_config = os.path.join(tempdir, 'minion')
  638. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  639. fp_.write(textwrap.dedent('''\
  640. id_function:
  641. test.echo:
  642. text: king_bob.foo.org
  643. minion_id_remove_domain: True
  644. minion_id_caching: False
  645. '''))
  646. config = salt.config.minion_config(minion_config)
  647. self.assertEqual(config['id'], 'king_bob')
  648. @with_tempdir()
  649. def test_minion_id_remove_domain_bool_false(self, tempdir):
  650. '''
  651. See above
  652. '''
  653. minion_config = os.path.join(tempdir, 'minion')
  654. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  655. fp_.write(textwrap.dedent('''\
  656. id_function:
  657. test.echo:
  658. text: king_bob.foo.org
  659. minion_id_remove_domain: False
  660. minion_id_caching: False
  661. '''))
  662. config = salt.config.minion_config(minion_config)
  663. self.assertEqual(config['id'], 'king_bob.foo.org')
  664. @with_tempdir()
  665. def test_backend_rename(self, tempdir):
  666. '''
  667. This tests that we successfully rename git, hg, svn, and minion to
  668. gitfs, hgfs, svnfs, and minionfs in the master and minion opts.
  669. '''
  670. fpath = salt.utils.files.mkstemp(dir=tempdir)
  671. with salt.utils.files.fopen(fpath, 'w') as fp_:
  672. fp_.write(textwrap.dedent('''\
  673. fileserver_backend:
  674. - roots
  675. - git
  676. - hg
  677. - svn
  678. - minion
  679. '''))
  680. master_config = salt.config.master_config(fpath)
  681. minion_config = salt.config.minion_config(fpath)
  682. expected = ['roots', 'gitfs', 'hgfs', 'svnfs', 'minionfs']
  683. self.assertEqual(master_config['fileserver_backend'], expected)
  684. self.assertEqual(minion_config['fileserver_backend'], expected)
  685. def test_syndic_config(self):
  686. minion_conf_path = self.get_config_file_path('syndic')
  687. master_conf_path = os.path.join(os.path.dirname(minion_conf_path), 'master')
  688. syndic_opts = salt.config.syndic_config(master_conf_path, minion_conf_path)
  689. root_dir = syndic_opts['root_dir']
  690. # id & pki dir are shared & so configured on the minion side
  691. self.assertEqual(syndic_opts['id'], 'syndic')
  692. self.assertEqual(syndic_opts['pki_dir'], os.path.join(root_dir, 'pki'))
  693. # the rest is configured master side
  694. if RUNTIME_VARS.PYTEST_SESSION is False:
  695. # Pytest assigns ports dynamically
  696. self.assertEqual(syndic_opts['master_port'], 54506)
  697. self.assertEqual(syndic_opts['master'], 'localhost')
  698. self.assertEqual(syndic_opts['sock_dir'], os.path.join(root_dir, 'syndic_sock'))
  699. self.assertEqual(syndic_opts['cachedir'], os.path.join(root_dir, 'cache'))
  700. self.assertEqual(syndic_opts['log_file'], os.path.join(root_dir, 'logs', 'syndic.log'))
  701. self.assertEqual(syndic_opts['pidfile'], os.path.join(root_dir, 'run', 'syndic.pid'))
  702. # Show that the options of localclient that repub to local master
  703. # are not merged with syndic ones
  704. self.assertEqual(syndic_opts['_master_conf_file'], minion_conf_path)
  705. self.assertEqual(syndic_opts['_minion_conf_file'], master_conf_path)
  706. @with_tempfile()
  707. def _get_tally(self, fpath, conf_func):
  708. '''
  709. This ensures that any strings which are loaded are unicode strings
  710. '''
  711. tally = {}
  712. def _count_strings(config):
  713. if isinstance(config, dict):
  714. for key, val in six.iteritems(config):
  715. log.debug('counting strings in dict key: %s', key)
  716. log.debug('counting strings in dict val: %s', val)
  717. _count_strings(key)
  718. _count_strings(val)
  719. elif isinstance(config, list):
  720. log.debug('counting strings in list: %s', config)
  721. for item in config:
  722. _count_strings(item)
  723. else:
  724. if isinstance(config, six.string_types):
  725. if isinstance(config, six.text_type):
  726. tally['unicode'] = tally.get('unicode', 0) + 1
  727. else:
  728. # We will never reach this on PY3
  729. tally.setdefault('non_unicode', []).append(config)
  730. with salt.utils.files.fopen(fpath, 'w') as wfh:
  731. wfh.write(textwrap.dedent('''
  732. foo: bar
  733. mylist:
  734. - somestring
  735. - 9
  736. - 123.456
  737. - True
  738. - nested:
  739. - key: val
  740. - nestedlist:
  741. - foo
  742. - bar
  743. - baz
  744. mydict:
  745. - somestring: 9
  746. - 123.456: 789
  747. - True: False
  748. - nested:
  749. - key: val
  750. - nestedlist:
  751. - foo
  752. - bar
  753. - baz'''))
  754. if conf_func is salt.config.master_config:
  755. wfh.write('\n\n')
  756. wfh.write(textwrap.dedent('''
  757. rest_cherrypy:
  758. port: 8000
  759. disable_ssl: True
  760. app_path: /beacon_demo
  761. app: /srv/web/html/index.html
  762. static: /srv/web/static'''))
  763. config = conf_func(fpath)
  764. _count_strings(config)
  765. return tally
  766. def test_conf_file_strings_are_unicode_for_master(self):
  767. '''
  768. This ensures that any strings which are loaded are unicode strings
  769. '''
  770. tally = self._get_tally(salt.config.master_config) # pylint: disable=no-value-for-parameter
  771. non_unicode = tally.get('non_unicode', [])
  772. self.assertEqual(len(non_unicode), 8 if six.PY2 else 0, non_unicode)
  773. self.assertTrue(tally['unicode'] > 0)
  774. def test_conf_file_strings_are_unicode_for_minion(self):
  775. '''
  776. This ensures that any strings which are loaded are unicode strings
  777. '''
  778. tally = self._get_tally(salt.config.minion_config) # pylint: disable=no-value-for-parameter
  779. non_unicode = tally.get('non_unicode', [])
  780. self.assertEqual(len(non_unicode), 0, non_unicode)
  781. self.assertTrue(tally['unicode'] > 0)
  782. # <---- Salt Cloud Configuration Tests ---------------------------------------------
  783. # cloud_config tests
  784. def test_cloud_config_double_master_path(self):
  785. '''
  786. Tests passing in master_config_path and master_config kwargs.
  787. '''
  788. with patch('salt.config.load_config', MagicMock(return_value={})):
  789. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  790. master_config_path='foo', master_config='bar')
  791. def test_cloud_config_double_providers_path(self):
  792. '''
  793. Tests passing in providers_config_path and providers_config kwargs.
  794. '''
  795. with patch('salt.config.load_config', MagicMock(return_value={})):
  796. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  797. providers_config_path='foo', providers_config='bar')
  798. def test_cloud_config_double_profiles_path(self):
  799. '''
  800. Tests passing in profiles_config_path and profiles_config kwargs.
  801. '''
  802. with patch('salt.config.load_config', MagicMock(return_value={})):
  803. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  804. profiles_config_path='foo', profiles_config='bar')
  805. def test_cloud_config_providers_in_opts(self):
  806. '''
  807. Tests mixing old cloud providers with pre-configured providers configurations
  808. using the providers_config kwarg
  809. '''
  810. with patch('salt.config.load_config', MagicMock(return_value={})):
  811. with patch('salt.config.apply_cloud_config',
  812. MagicMock(return_value={'providers': 'foo'})):
  813. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  814. providers_config='bar')
  815. def test_cloud_config_providers_in_opts_path(self):
  816. '''
  817. Tests mixing old cloud providers with pre-configured providers configurations
  818. using the providers_config_path kwarg
  819. '''
  820. with patch('salt.config.load_config', MagicMock(return_value={})):
  821. with patch('salt.config.apply_cloud_config',
  822. MagicMock(return_value={'providers': 'foo'})):
  823. with patch('os.path.isfile', MagicMock(return_value=True)):
  824. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  825. providers_config_path='bar')
  826. def test_cloud_config_deploy_scripts_search_path(self):
  827. '''
  828. Tests the contents of the 'deploy_scripts_search_path' tuple to ensure that
  829. the correct deploy search paths are present.
  830. There should be two search paths reported in the tuple: ``/etc/salt/cloud.deploy.d``
  831. and ``<path-to-salt-install>/salt/cloud/deploy``. The first element is usually
  832. ``/etc/salt/cloud.deploy.d``, but sometimes is can be something like
  833. ``/etc/local/salt/cloud.deploy.d``, so we'll only test against the last part of
  834. the path.
  835. '''
  836. with patch('os.path.isdir', MagicMock(return_value=True)):
  837. search_paths = salt.config.cloud_config('/etc/salt/cloud').get('deploy_scripts_search_path')
  838. etc_deploy_path = '/salt/cloud.deploy.d'
  839. deploy_path = '/salt/cloud/deploy'
  840. if salt.utils.platform.is_windows():
  841. etc_deploy_path = '/salt\\cloud.deploy.d'
  842. deploy_path = '\\salt\\cloud\\deploy'
  843. # Check cloud.deploy.d path is the first element in the search_paths tuple
  844. self.assertTrue(search_paths[0].endswith(etc_deploy_path))
  845. # Check the second element in the search_paths tuple
  846. self.assertTrue(search_paths[1].endswith(deploy_path))
  847. # apply_cloud_config tests
  848. def test_apply_cloud_config_no_provider_detail_list(self):
  849. '''
  850. Tests when the provider is not contained in a list of details
  851. '''
  852. overrides = {'providers': {'foo': [{'bar': 'baz'}]}}
  853. self.assertRaises(SaltCloudConfigError, salt.config.apply_cloud_config,
  854. overrides, defaults=DEFAULT)
  855. def test_apply_cloud_config_no_provider_detail_dict(self):
  856. '''
  857. Tests when the provider is not contained in the details dictionary
  858. '''
  859. overrides = {'providers': {'foo': {'bar': 'baz'}}}
  860. self.assertRaises(SaltCloudConfigError, salt.config.apply_cloud_config,
  861. overrides, defaults=DEFAULT)
  862. def test_apply_cloud_config_success_list(self):
  863. '''
  864. Tests success when valid data is passed into the function as a list
  865. '''
  866. with patch('salt.config.old_to_new',
  867. MagicMock(return_value={
  868. 'default_include': 'path/to/some/cloud/conf/file',
  869. 'providers': {'foo': {'bar': {'driver': 'foo:bar'}}}
  870. })):
  871. overrides = {
  872. 'providers': {'foo': [{'driver': 'bar'}]}
  873. }
  874. ret = {
  875. 'default_include': 'path/to/some/cloud/conf/file',
  876. 'providers': {'foo': {'bar': {'driver': 'foo:bar'}}}
  877. }
  878. self.assertEqual(salt.config.apply_cloud_config(overrides, defaults=DEFAULT), ret)
  879. def test_apply_cloud_config_success_dict(self):
  880. '''
  881. Tests success when valid data is passed into function as a dictionary
  882. '''
  883. with patch('salt.config.old_to_new',
  884. MagicMock(return_value={
  885. 'default_include': 'path/to/some/cloud/conf/file',
  886. 'providers': {'foo': {'bar': {'driver': 'foo:bar'}}}
  887. })):
  888. overrides = {
  889. 'providers': {'foo': {'driver': 'bar'}}}
  890. ret = {
  891. 'default_include': 'path/to/some/cloud/conf/file',
  892. 'providers': {'foo': {'bar': {'driver': 'foo:bar'}}}
  893. }
  894. self.assertEqual(salt.config.apply_cloud_config(overrides, defaults=DEFAULT), ret)
  895. # apply_vm_profiles_config tests
  896. def test_apply_vm_profiles_config_bad_profile_format(self):
  897. '''
  898. Tests passing in a bad profile format in overrides
  899. '''
  900. overrides = {'foo': 'bar', 'conf_file': PATH}
  901. self.assertRaises(SaltCloudConfigError, salt.config.apply_vm_profiles_config,
  902. PATH, overrides, defaults=DEFAULT)
  903. def test_apply_vm_profiles_config_success(self):
  904. '''
  905. Tests passing in valid provider and profile config files successfully
  906. '''
  907. providers = {
  908. 'test-provider': {
  909. 'digitalocean': {
  910. 'driver': 'digitalocean', 'profiles': {}
  911. }
  912. }
  913. }
  914. overrides = {
  915. 'test-profile': {
  916. 'provider': 'test-provider',
  917. 'image': 'Ubuntu 12.10 x64',
  918. 'size': '512MB'
  919. },
  920. 'conf_file': PATH
  921. }
  922. ret = {
  923. 'test-profile': {
  924. 'profile': 'test-profile',
  925. 'provider': 'test-provider:digitalocean',
  926. 'image': 'Ubuntu 12.10 x64',
  927. 'size': '512MB'
  928. }
  929. }
  930. self.assertEqual(salt.config.apply_vm_profiles_config(providers,
  931. overrides,
  932. defaults=DEFAULT), ret)
  933. def test_apply_vm_profiles_config_extend_success(self):
  934. '''
  935. Tests profile extends functionality with valid provider and profile configs
  936. '''
  937. providers = {
  938. 'test-config': {
  939. 'ec2': {
  940. 'profiles': {},
  941. 'driver': 'ec2'
  942. }
  943. }
  944. }
  945. overrides = {
  946. 'Amazon': {
  947. 'image': 'test-image-1',
  948. 'extends': 'dev-instances'
  949. },
  950. 'Fedora': {
  951. 'image': 'test-image-2',
  952. 'extends': 'dev-instances'
  953. },
  954. 'conf_file': PATH,
  955. 'dev-instances': {
  956. 'ssh_username': 'test_user',
  957. 'provider': 'test-config'
  958. }
  959. }
  960. ret = {
  961. 'Amazon': {
  962. 'profile': 'Amazon',
  963. 'ssh_username': 'test_user',
  964. 'image': 'test-image-1',
  965. 'provider': 'test-config:ec2'
  966. },
  967. 'Fedora': {
  968. 'profile': 'Fedora',
  969. 'ssh_username': 'test_user',
  970. 'image': 'test-image-2',
  971. 'provider': 'test-config:ec2'
  972. },
  973. 'dev-instances': {
  974. 'profile': 'dev-instances',
  975. 'ssh_username': 'test_user',
  976. 'provider': 'test-config:ec2'
  977. }
  978. }
  979. self.assertEqual(salt.config.apply_vm_profiles_config(providers,
  980. overrides,
  981. defaults=DEFAULT), ret)
  982. def test_apply_vm_profiles_config_extend_override_success(self):
  983. '''
  984. Tests profile extends and recursively merges data elements
  985. '''
  986. self.maxDiff = None
  987. providers = {
  988. 'test-config': {
  989. 'ec2': {
  990. 'profiles': {},
  991. 'driver': 'ec2'
  992. }
  993. }
  994. }
  995. overrides = {
  996. 'Fedora': {
  997. 'image': 'test-image-2',
  998. 'extends': 'dev-instances',
  999. 'minion': {
  1000. 'grains': {
  1001. 'stage': 'experimental'
  1002. }
  1003. }
  1004. },
  1005. 'conf_file': PATH,
  1006. 'dev-instances': {
  1007. 'ssh_username': 'test_user',
  1008. 'provider': 'test-config',
  1009. 'minion': {
  1010. 'grains': {
  1011. 'role': 'webserver'}
  1012. }
  1013. }
  1014. }
  1015. ret = {
  1016. 'Fedora': {
  1017. 'profile': 'Fedora',
  1018. 'ssh_username': 'test_user',
  1019. 'image': 'test-image-2',
  1020. 'minion': {
  1021. 'grains': {
  1022. 'role': 'webserver',
  1023. 'stage': 'experimental'
  1024. }
  1025. },
  1026. 'provider': 'test-config:ec2'
  1027. },
  1028. 'dev-instances': {
  1029. 'profile': 'dev-instances',
  1030. 'ssh_username': 'test_user',
  1031. 'minion': {
  1032. 'grains': {
  1033. 'role': 'webserver'
  1034. }
  1035. },
  1036. 'provider': 'test-config:ec2'
  1037. }
  1038. }
  1039. self.assertEqual(salt.config.apply_vm_profiles_config(providers,
  1040. overrides,
  1041. defaults=DEFAULT), ret)
  1042. # apply_cloud_providers_config tests
  1043. def test_apply_cloud_providers_config_same_providers(self):
  1044. '''
  1045. Tests when two providers are given with the same provider name
  1046. '''
  1047. overrides = {
  1048. 'my-dev-envs': [{
  1049. 'id': 'ABCDEFGHIJKLMNOP',
  1050. 'key': 'supersecretkeysupersecretkey',
  1051. 'driver': 'ec2'
  1052. }, {
  1053. 'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1054. 'password': 'supersecret',
  1055. 'driver': 'ec2'
  1056. }],
  1057. 'conf_file': PATH}
  1058. self.assertRaises(SaltCloudConfigError,
  1059. salt.config.apply_cloud_providers_config,
  1060. overrides,
  1061. DEFAULT)
  1062. def test_apply_cloud_providers_config_extend(self):
  1063. '''
  1064. Tests the successful extension of a cloud provider
  1065. '''
  1066. overrides = {
  1067. 'my-production-envs': [{
  1068. 'extends': 'my-dev-envs:ec2',
  1069. 'location': 'us-east-1',
  1070. 'user': 'ec2-user@mycorp.com'
  1071. }],
  1072. 'my-dev-envs': [{
  1073. 'id': 'ABCDEFGHIJKLMNOP',
  1074. 'user': 'user@mycorp.com',
  1075. 'location': 'ap-southeast-1',
  1076. 'key': 'supersecretkeysupersecretkey',
  1077. 'driver': 'ec2'
  1078. }, {
  1079. 'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1080. 'password': 'supersecret',
  1081. 'driver': 'linode'
  1082. }, {
  1083. 'id': 'a-tencentcloud-id',
  1084. 'key': 'a-tencentcloud-key',
  1085. 'location': 'ap-guangzhou',
  1086. 'driver': 'tencentcloud',
  1087. },
  1088. ],
  1089. 'conf_file': PATH
  1090. }
  1091. ret = {
  1092. 'my-production-envs': {
  1093. 'ec2': {
  1094. 'profiles': {},
  1095. 'location': 'us-east-1',
  1096. 'key': 'supersecretkeysupersecretkey',
  1097. 'driver': 'ec2',
  1098. 'id': 'ABCDEFGHIJKLMNOP',
  1099. 'user': 'ec2-user@mycorp.com'
  1100. }
  1101. },
  1102. 'my-dev-envs': {
  1103. 'linode': {
  1104. 'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1105. 'password': 'supersecret',
  1106. 'profiles': {},
  1107. 'driver': 'linode'
  1108. },
  1109. 'tencentcloud': {
  1110. 'id': 'a-tencentcloud-id',
  1111. 'key': 'a-tencentcloud-key',
  1112. 'location': 'ap-guangzhou',
  1113. 'profiles': {},
  1114. 'driver': 'tencentcloud',
  1115. },
  1116. 'ec2': {
  1117. 'profiles': {},
  1118. 'location': 'ap-southeast-1',
  1119. 'key': 'supersecretkeysupersecretkey',
  1120. 'driver': 'ec2',
  1121. 'id': 'ABCDEFGHIJKLMNOP',
  1122. 'user': 'user@mycorp.com',
  1123. },
  1124. },
  1125. }
  1126. self.assertEqual(ret,
  1127. salt.config.apply_cloud_providers_config(
  1128. overrides,
  1129. defaults=DEFAULT))
  1130. def test_apply_cloud_providers_config_extend_multiple(self):
  1131. '''
  1132. Tests the successful extension of two cloud providers
  1133. '''
  1134. overrides = {
  1135. 'my-production-envs': [{
  1136. 'extends': 'my-dev-envs:ec2',
  1137. 'location': 'us-east-1',
  1138. 'user': 'ec2-user@mycorp.com'
  1139. }, {
  1140. 'password': 'new-password',
  1141. 'extends': 'my-dev-envs:linode',
  1142. 'location': 'Salt Lake City'
  1143. }, {
  1144. 'extends': 'my-dev-envs:tencentcloud',
  1145. 'id': 'new-id',
  1146. 'key': 'new-key',
  1147. 'location': 'ap-beijing',
  1148. },
  1149. ],
  1150. 'my-dev-envs': [{
  1151. 'id': 'ABCDEFGHIJKLMNOP',
  1152. 'user': 'user@mycorp.com',
  1153. 'location': 'ap-southeast-1',
  1154. 'key': 'supersecretkeysupersecretkey',
  1155. 'driver': 'ec2'
  1156. }, {
  1157. 'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1158. 'password': 'supersecret',
  1159. 'driver': 'linode'
  1160. }, {
  1161. 'id': 'the-tencentcloud-id',
  1162. 'location': 'ap-beijing',
  1163. 'key': 'the-tencentcloud-key',
  1164. 'driver': 'tencentcloud',
  1165. },
  1166. ],
  1167. 'conf_file': PATH}
  1168. ret = {
  1169. 'my-production-envs': {
  1170. 'linode': {
  1171. 'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1172. 'profiles': {},
  1173. 'location': 'Salt Lake City',
  1174. 'driver': 'linode',
  1175. 'password': 'new-password'
  1176. },
  1177. 'ec2': {
  1178. 'user': 'ec2-user@mycorp.com',
  1179. 'key': 'supersecretkeysupersecretkey',
  1180. 'driver': 'ec2',
  1181. 'id': 'ABCDEFGHIJKLMNOP',
  1182. 'profiles': {},
  1183. 'location': 'us-east-1'
  1184. },
  1185. 'tencentcloud': {
  1186. 'id': 'new-id',
  1187. 'key': 'new-key',
  1188. 'location': 'ap-beijing',
  1189. 'profiles': {},
  1190. 'driver': 'tencentcloud',
  1191. },
  1192. },
  1193. 'my-dev-envs': {
  1194. 'linode': {
  1195. 'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1196. 'password': 'supersecret',
  1197. 'profiles': {},
  1198. 'driver': 'linode'
  1199. },
  1200. 'ec2': {
  1201. 'profiles': {},
  1202. 'user': 'user@mycorp.com',
  1203. 'key': 'supersecretkeysupersecretkey',
  1204. 'driver': 'ec2',
  1205. 'id': 'ABCDEFGHIJKLMNOP',
  1206. 'location': 'ap-southeast-1'
  1207. },
  1208. 'tencentcloud': {
  1209. 'id': 'the-tencentcloud-id',
  1210. 'key': 'the-tencentcloud-key',
  1211. 'location': 'ap-beijing',
  1212. 'profiles': {},
  1213. 'driver': 'tencentcloud',
  1214. },
  1215. },
  1216. }
  1217. self.assertEqual(ret, salt.config.apply_cloud_providers_config(overrides, defaults=DEFAULT))
  1218. def test_apply_cloud_providers_config_extends_bad_alias(self):
  1219. '''
  1220. Tests when the extension contains an alias not found in providers list
  1221. '''
  1222. overrides = {
  1223. 'my-production-envs': [{
  1224. 'extends': 'test-alias:ec2',
  1225. 'location': 'us-east-1',
  1226. 'user': 'ec2-user@mycorp.com'
  1227. }],
  1228. 'my-dev-envs': [{
  1229. 'id': 'ABCDEFGHIJKLMNOP',
  1230. 'user': 'user@mycorp.com',
  1231. 'location': 'ap-southeast-1',
  1232. 'key': 'supersecretkeysupersecretkey',
  1233. 'driver': 'ec2'
  1234. }],
  1235. 'conf_file': PATH}
  1236. self.assertRaises(SaltCloudConfigError,
  1237. salt.config.apply_cloud_providers_config,
  1238. overrides,
  1239. DEFAULT)
  1240. def test_apply_cloud_providers_config_extends_bad_provider(self):
  1241. '''
  1242. Tests when the extension contains a provider not found in providers list
  1243. '''
  1244. overrides = {
  1245. 'my-production-envs': [{
  1246. 'extends': 'my-dev-envs:linode',
  1247. 'location': 'us-east-1',
  1248. 'user': 'ec2-user@mycorp.com'
  1249. }, {
  1250. 'extends': 'my-dev-envs:tencentcloud',
  1251. 'location': 'ap-shanghai',
  1252. 'id': 'the-tencentcloud-id',
  1253. },
  1254. ],
  1255. 'my-dev-envs': [{
  1256. 'id': 'ABCDEFGHIJKLMNOP',
  1257. 'user': 'user@mycorp.com',
  1258. 'location': 'ap-southeast-1',
  1259. 'key': 'supersecretkeysupersecretkey',
  1260. 'driver': 'ec2'
  1261. }],
  1262. 'conf_file': PATH}
  1263. self.assertRaises(SaltCloudConfigError,
  1264. salt.config.apply_cloud_providers_config,
  1265. overrides,
  1266. DEFAULT)
  1267. def test_apply_cloud_providers_config_extends_no_provider(self):
  1268. '''
  1269. Tests when no provider is supplied in the extends statement
  1270. '''
  1271. overrides = {
  1272. 'my-production-envs': [{
  1273. 'extends': 'my-dev-envs',
  1274. 'location': 'us-east-1',
  1275. 'user': 'ec2-user@mycorp.com'
  1276. }, {
  1277. 'extends': 'my-dev-envs:tencentcloud',
  1278. 'location': 'ap-shanghai',
  1279. 'id': 'the-tencentcloud-id',
  1280. },
  1281. ],
  1282. 'my-dev-envs': [{
  1283. 'id': 'ABCDEFGHIJKLMNOP',
  1284. 'user': 'user@mycorp.com',
  1285. 'location': 'ap-southeast-1',
  1286. 'key': 'supersecretkeysupersecretkey',
  1287. 'driver': 'linode'
  1288. }],
  1289. 'conf_file': PATH}
  1290. self.assertRaises(SaltCloudConfigError,
  1291. salt.config.apply_cloud_providers_config,
  1292. overrides,
  1293. DEFAULT)
  1294. def test_apply_cloud_providers_extends_not_in_providers(self):
  1295. '''
  1296. Tests when extends is not in the list of providers
  1297. '''
  1298. overrides = {
  1299. 'my-production-envs': [{
  1300. 'extends': 'my-dev-envs ec2',
  1301. 'location': 'us-east-1',
  1302. 'user': 'ec2-user@mycorp.com'
  1303. }],
  1304. 'my-dev-envs': [{
  1305. 'id': 'ABCDEFGHIJKLMNOP',
  1306. 'user': 'user@mycorp.com',
  1307. 'location': 'ap-southeast-1',
  1308. 'key': 'supersecretkeysupersecretkey',
  1309. 'driver': 'linode'
  1310. }, {
  1311. 'id': 'a-tencentcloud-id',
  1312. 'key': 'a-tencentcloud-key',
  1313. 'location': 'ap-guangzhou',
  1314. 'driver': 'tencentcloud',
  1315. },
  1316. ],
  1317. 'conf_file': PATH}
  1318. self.assertRaises(SaltCloudConfigError,
  1319. salt.config.apply_cloud_providers_config,
  1320. overrides,
  1321. DEFAULT)
  1322. # is_provider_configured tests
  1323. def test_is_provider_configured_no_alias(self):
  1324. '''
  1325. Tests when provider alias is not in opts
  1326. '''
  1327. opts = {'providers': 'test'}
  1328. provider = 'foo:bar'
  1329. self.assertFalse(salt.config.is_provider_configured(opts, provider))
  1330. def test_is_provider_configured_no_driver(self):
  1331. '''
  1332. Tests when provider driver is not in opts
  1333. '''
  1334. opts = {'providers': {'foo': 'baz'}}
  1335. provider = 'foo:bar'
  1336. self.assertFalse(salt.config.is_provider_configured(opts, provider))
  1337. def test_is_provider_configured_key_is_none(self):
  1338. '''
  1339. Tests when a required configuration key is not set
  1340. '''
  1341. opts = {'providers': {'foo': {'bar': {'api_key': None}}}}
  1342. provider = 'foo:bar'
  1343. self.assertFalse(
  1344. salt.config.is_provider_configured(opts,
  1345. provider,
  1346. required_keys=('api_key',)))
  1347. def test_is_provider_configured_success(self):
  1348. '''
  1349. Tests successful cloud provider configuration
  1350. '''
  1351. opts = {'providers': {'foo': {'bar': {'api_key': 'baz'}}}}
  1352. provider = 'foo:bar'
  1353. ret = {'api_key': 'baz'}
  1354. self.assertEqual(
  1355. salt.config.is_provider_configured(opts,
  1356. provider,
  1357. required_keys=('api_key',)), ret)
  1358. def test_is_provider_configured_multiple_driver_not_provider(self):
  1359. '''
  1360. Tests when the drive is not the same as the provider when
  1361. searching through multiple providers
  1362. '''
  1363. opts = {'providers': {'foo': {'bar': {'api_key': 'baz'}}}}
  1364. provider = 'foo'
  1365. self.assertFalse(salt.config.is_provider_configured(opts, provider))
  1366. def test_is_provider_configured_multiple_key_is_none(self):
  1367. '''
  1368. Tests when a required configuration key is not set when
  1369. searching through multiple providers
  1370. '''
  1371. opts = {'providers': {'foo': {'bar': {'api_key': None}}}}
  1372. provider = 'bar'
  1373. self.assertFalse(
  1374. salt.config.is_provider_configured(opts,
  1375. provider,
  1376. required_keys=('api_key',)))
  1377. def test_is_provider_configured_multiple_success(self):
  1378. '''
  1379. Tests successful cloud provider configuration when searching
  1380. through multiple providers
  1381. '''
  1382. opts = {'providers': {'foo': {'bar': {'api_key': 'baz'}}}}
  1383. provider = 'bar'
  1384. ret = {'api_key': 'baz'}
  1385. self.assertEqual(
  1386. salt.config.is_provider_configured(opts,
  1387. provider,
  1388. required_keys=('api_key',)), ret)
  1389. # other cloud configuration tests
  1390. @skipIf(
  1391. salt.utils.platform.is_windows(),
  1392. 'You can\'t set an environment dynamically in Windows')
  1393. @with_tempdir()
  1394. def test_load_cloud_config_from_environ_var(self, tempdir):
  1395. env_root_dir = os.path.join(tempdir, 'foo', 'env')
  1396. os.makedirs(env_root_dir)
  1397. env_fpath = os.path.join(env_root_dir, 'config-env')
  1398. with salt.utils.files.fopen(env_fpath, 'w') as fp_:
  1399. fp_.write(
  1400. 'root_dir: {}\n'
  1401. 'log_file: {}\n'.format(env_root_dir, env_fpath)
  1402. )
  1403. with patched_environ(SALT_CLOUD_CONFIG=env_fpath):
  1404. # Should load from env variable, not the default configuration file
  1405. config = salt.config.cloud_config('/etc/salt/cloud')
  1406. self.assertEqual(config['log_file'], env_fpath)
  1407. root_dir = os.path.join(tempdir, 'foo', 'bar')
  1408. os.makedirs(root_dir)
  1409. fpath = os.path.join(root_dir, 'config')
  1410. with salt.utils.files.fopen(fpath, 'w') as fp_:
  1411. fp_.write(
  1412. 'root_dir: {}\n'
  1413. 'log_file: {}\n'.format(root_dir, fpath)
  1414. )
  1415. # Let's set the environment variable, yet, since the configuration
  1416. # file path is not the default one, i.e., the user has passed an
  1417. # alternative configuration file form the CLI parser, the
  1418. # environment variable will be ignored.
  1419. with patched_environ(SALT_CLOUD_CONFIG=env_fpath):
  1420. config = salt.config.cloud_config(fpath)
  1421. self.assertEqual(config['log_file'], fpath)
  1422. @with_tempdir()
  1423. def test_deploy_search_path_as_string(self, temp_conf_dir):
  1424. config_file_path = os.path.join(temp_conf_dir, 'cloud')
  1425. deploy_dir_path = os.path.join(temp_conf_dir, 'test-deploy.d')
  1426. for directory in (temp_conf_dir, deploy_dir_path):
  1427. if not os.path.isdir(directory):
  1428. os.makedirs(directory)
  1429. default_config = salt.config.cloud_config(config_file_path)
  1430. default_config['deploy_scripts_search_path'] = deploy_dir_path
  1431. with salt.utils.files.fopen(config_file_path, 'w') as cfd:
  1432. salt.utils.yaml.safe_dump(default_config, cfd, default_flow_style=False)
  1433. default_config = salt.config.cloud_config(config_file_path)
  1434. # Our custom deploy scripts path was correctly added to the list
  1435. self.assertIn(
  1436. deploy_dir_path,
  1437. default_config['deploy_scripts_search_path']
  1438. )
  1439. # And it's even the first occurrence as it should
  1440. self.assertEqual(
  1441. deploy_dir_path,
  1442. default_config['deploy_scripts_search_path'][0]
  1443. )
  1444. def test_includes_load(self):
  1445. '''
  1446. Tests that cloud.{providers,profiles}.d directories are loaded, even if not
  1447. directly passed in through path
  1448. '''
  1449. log.warning('Clound config file path: %s', self.get_config_file_path('cloud'))
  1450. config = salt.config.cloud_config(self.get_config_file_path('cloud'))
  1451. self.assertIn('ec2-config', config['providers'])
  1452. self.assertIn('ec2-test', config['profiles'])
  1453. # <---- Salt Cloud Configuration Tests ---------------------------------------------
  1454. def test_include_config_without_errors(self):
  1455. '''
  1456. Tests that include_config function returns valid configuration
  1457. '''
  1458. include_file = 'minion.d/my.conf'
  1459. config_path = '/etc/salt/minion'
  1460. config_opts = {'id': 'myminion.example.com'}
  1461. with patch('glob.glob', MagicMock(return_value=include_file)):
  1462. with patch('salt.config._read_conf_file', MagicMock(return_value=config_opts)):
  1463. configuration = salt.config.include_config(include_file, config_path, verbose=False)
  1464. self.assertEqual(config_opts, configuration)
  1465. def test_include_config_with_errors(self):
  1466. '''
  1467. Tests that include_config function returns valid configuration even on errors
  1468. '''
  1469. include_file = 'minion.d/my.conf'
  1470. config_path = '/etc/salt/minion'
  1471. config_opts = {}
  1472. with patch('glob.glob', MagicMock(return_value=include_file)):
  1473. with patch('salt.config._read_conf_file', _salt_configuration_error):
  1474. configuration = salt.config.include_config(include_file, config_path, verbose=False)
  1475. self.assertEqual(config_opts, configuration)
  1476. def test_include_config_with_errors_exit(self):
  1477. '''
  1478. Tests that include_config exits on errors
  1479. '''
  1480. include_file = 'minion.d/my.conf'
  1481. config_path = '/etc/salt/minion'
  1482. with patch('glob.glob', MagicMock(return_value=include_file)):
  1483. with patch('salt.config._read_conf_file', _salt_configuration_error):
  1484. with self.assertRaises(SystemExit):
  1485. salt.config.include_config(include_file,
  1486. config_path,
  1487. verbose=False,
  1488. exit_on_config_errors=True)
  1489. @staticmethod
  1490. def _get_defaults(**kwargs):
  1491. ret = {
  1492. 'saltenv': kwargs.pop('saltenv', None),
  1493. 'id': 'test',
  1494. 'cachedir': '/A',
  1495. 'sock_dir': '/B',
  1496. 'root_dir': '/C',
  1497. 'fileserver_backend': 'roots',
  1498. 'open_mode': False,
  1499. 'auto_accept': False,
  1500. 'file_roots': {},
  1501. 'pillar_roots': {},
  1502. 'file_ignore_glob': [],
  1503. 'file_ignore_regex': [],
  1504. 'worker_threads': 5,
  1505. 'hash_type': 'sha256',
  1506. 'log_file': 'foo.log',
  1507. }
  1508. ret.update(kwargs)
  1509. return ret
  1510. def test_apply_config(self):
  1511. '''
  1512. Ensure that the environment and saltenv options work properly
  1513. '''
  1514. with patch.object(salt.config, '_adjust_log_file_override', Mock()), \
  1515. patch.object(salt.config, '_update_ssl_config', Mock()), \
  1516. patch.object(salt.config, '_update_discovery_config', Mock()):
  1517. # MASTER CONFIG
  1518. # Ensure that environment overrides saltenv when saltenv not
  1519. # explicitly passed.
  1520. defaults = self._get_defaults(environment='foo')
  1521. ret = salt.config.apply_master_config(defaults=defaults)
  1522. self.assertEqual(ret['environment'], 'foo')
  1523. self.assertEqual(ret['saltenv'], 'foo')
  1524. # Ensure that environment overrides saltenv when saltenv not
  1525. # explicitly passed.
  1526. defaults = self._get_defaults(environment='foo', saltenv='bar')
  1527. ret = salt.config.apply_master_config(defaults=defaults)
  1528. self.assertEqual(ret['environment'], 'bar')
  1529. self.assertEqual(ret['saltenv'], 'bar')
  1530. # If environment was not explicitly set, it should not be in the
  1531. # opts at all.
  1532. defaults = self._get_defaults()
  1533. ret = salt.config.apply_master_config(defaults=defaults)
  1534. self.assertNotIn('environment', ret)
  1535. self.assertEqual(ret['saltenv'], None)
  1536. # Same test as above but with saltenv explicitly set
  1537. defaults = self._get_defaults(saltenv='foo')
  1538. ret = salt.config.apply_master_config(defaults=defaults)
  1539. self.assertNotIn('environment', ret)
  1540. self.assertEqual(ret['saltenv'], 'foo')
  1541. # MINION CONFIG
  1542. # Ensure that environment overrides saltenv when saltenv not
  1543. # explicitly passed.
  1544. defaults = self._get_defaults(environment='foo')
  1545. ret = salt.config.apply_minion_config(defaults=defaults)
  1546. self.assertEqual(ret['environment'], 'foo')
  1547. self.assertEqual(ret['saltenv'], 'foo')
  1548. # Ensure that environment overrides saltenv when saltenv not
  1549. # explicitly passed.
  1550. defaults = self._get_defaults(environment='foo', saltenv='bar')
  1551. ret = salt.config.apply_minion_config(defaults=defaults)
  1552. self.assertEqual(ret['environment'], 'bar')
  1553. self.assertEqual(ret['saltenv'], 'bar')
  1554. # If environment was not explicitly set, it should not be in the
  1555. # opts at all.
  1556. defaults = self._get_defaults()
  1557. ret = salt.config.apply_minion_config(defaults=defaults)
  1558. self.assertNotIn('environment', ret)
  1559. self.assertEqual(ret['saltenv'], None)
  1560. # Same test as above but with saltenv explicitly set
  1561. defaults = self._get_defaults(saltenv='foo')
  1562. ret = salt.config.apply_minion_config(defaults=defaults)
  1563. self.assertNotIn('environment', ret)
  1564. self.assertEqual(ret['saltenv'], 'foo')
  1565. class APIConfigTestCase(DefaultConfigsBase, TestCase):
  1566. '''
  1567. TestCase for the api_config function in salt.config.__init__.py
  1568. '''
  1569. def setUp(self):
  1570. # Copy DEFAULT_API_OPTS to restore after the test
  1571. self.default_api_opts = salt.config.DEFAULT_API_OPTS.copy()
  1572. def tearDown(self):
  1573. # Reset DEFAULT_API_OPTS settings as to not interfere with other unit tests
  1574. salt.config.DEFAULT_API_OPTS = self.default_api_opts
  1575. def test_api_config_log_file_values(self):
  1576. '''
  1577. Tests the opts value of the 'log_file' after running through the
  1578. various default dict updates. 'log_file' should be updated to match
  1579. the DEFAULT_API_OPTS 'api_logfile' value.
  1580. '''
  1581. with patch('salt.config.client_config', MagicMock(return_value=self.mock_master_default_opts)):
  1582. expected = '{0}/var/log/salt/api'.format(
  1583. RUNTIME_VARS.TMP_ROOT_DIR if RUNTIME_VARS.TMP_ROOT_DIR != '/' else '')
  1584. if salt.utils.platform.is_windows():
  1585. expected = '{}\\var\\log\\salt\\api'.format(
  1586. RUNTIME_VARS.TMP_ROOT_DIR)
  1587. ret = salt.config.api_config('/some/fake/path')
  1588. self.assertEqual(ret['log_file'], expected)
  1589. def test_api_config_pidfile_values(self):
  1590. '''
  1591. Tests the opts value of the 'pidfile' after running through the
  1592. various default dict updates. 'pidfile' should be updated to match
  1593. the DEFAULT_API_OPTS 'api_pidfile' value.
  1594. '''
  1595. with patch('salt.config.client_config', MagicMock(return_value=self.mock_master_default_opts)):
  1596. expected = '{0}/var/run/salt-api.pid'.format(
  1597. RUNTIME_VARS.TMP_ROOT_DIR if RUNTIME_VARS.TMP_ROOT_DIR != '/' else '')
  1598. if salt.utils.platform.is_windows():
  1599. expected = '{}\\var\\run\\salt-api.pid'.format(
  1600. RUNTIME_VARS.TMP_ROOT_DIR)
  1601. ret = salt.config.api_config('/some/fake/path')
  1602. self.assertEqual(ret['pidfile'], expected)
  1603. def test_master_config_file_overrides_defaults(self):
  1604. '''
  1605. Tests the opts value of the api config values after running through the
  1606. various default dict updates that should be overridden by settings in
  1607. the user's master config file.
  1608. '''
  1609. foo_dir = os.path.join(RUNTIME_VARS.TMP_ROOT_DIR, 'foo/bar/baz')
  1610. hello_dir = os.path.join(RUNTIME_VARS.TMP_ROOT_DIR, 'hello/world')
  1611. if salt.utils.platform.is_windows():
  1612. foo_dir = 'c:\\{}'.format(foo_dir.replace('/', '\\'))
  1613. hello_dir = 'c:\\{}'.format(hello_dir.replace('/', '\\'))
  1614. mock_master_config = {
  1615. 'api_pidfile': foo_dir,
  1616. 'api_logfile': hello_dir,
  1617. 'rest_timeout': 5
  1618. }
  1619. mock_master_config.update(self.mock_master_default_opts.copy())
  1620. with patch('salt.config.client_config',
  1621. MagicMock(return_value=mock_master_config)):
  1622. ret = salt.config.api_config('/some/fake/path')
  1623. self.assertEqual(ret['rest_timeout'], 5)
  1624. self.assertEqual(ret['api_pidfile'], foo_dir)
  1625. self.assertEqual(ret['pidfile'], foo_dir)
  1626. self.assertEqual(ret['api_logfile'], hello_dir)
  1627. self.assertEqual(ret['log_file'], hello_dir)
  1628. def test_api_config_prepend_root_dirs_return(self):
  1629. '''
  1630. Tests the opts value of the api_logfile, log_file, api_pidfile, and pidfile
  1631. when a custom root directory is used. This ensures that each of these
  1632. values is present in the list of opts keys that should have the root_dir
  1633. prepended when the api_config function returns the opts dictionary.
  1634. '''
  1635. mock_log = '/mock/root/var/log/salt/api'
  1636. mock_pid = '/mock/root/var/run/salt-api.pid'
  1637. mock_master_config = self.mock_master_default_opts.copy()
  1638. mock_master_config['root_dir'] = '/mock/root/'
  1639. if salt.utils.platform.is_windows():
  1640. mock_log = 'c:\\mock\\root\\var\\log\\salt\\api'
  1641. mock_pid = 'c:\\mock\\root\\var\\run\\salt-api.pid'
  1642. mock_master_config['root_dir'] = 'c:\\mock\\root'
  1643. with patch('salt.config.client_config',
  1644. MagicMock(return_value=mock_master_config)):
  1645. ret = salt.config.api_config('/some/fake/path')
  1646. self.assertEqual(ret['api_logfile'], mock_log)
  1647. self.assertEqual(ret['log_file'], mock_log)
  1648. self.assertEqual(ret['api_pidfile'], mock_pid)
  1649. self.assertEqual(ret['pidfile'], mock_pid)