1
0

test_config.py 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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), set([
  475. os.path.join(tempdir, 'a'),
  476. os.path.join(tempdir, 'b'),
  477. os.path.join(tempdir, 'c')
  478. ]))
  479. def test_validate_bad_file_roots(self):
  480. expected = salt.config._expand_glob_path(
  481. [salt.syspaths.BASE_FILE_ROOTS_DIR]
  482. )
  483. with patch('salt.config._normalize_roots') as mk:
  484. ret = salt.config._validate_file_roots(None)
  485. assert not mk.called
  486. assert ret == {'base': expected}
  487. @with_tempfile()
  488. @with_tempdir()
  489. def test_master_pillar_roots_glob(self, tempdir, fpath):
  490. # Create some files.
  491. for f in 'abc':
  492. fpath = os.path.join(tempdir, f)
  493. with salt.utils.files.fopen(fpath, 'w') as wfh:
  494. wfh.write(f)
  495. with salt.utils.files.fopen(fpath, 'w') as wfh:
  496. wfh.write(
  497. 'pillar_roots:\n'
  498. ' base:\n'
  499. ' - {}'.format(os.path.join(tempdir, '*'))
  500. )
  501. config = salt.config.master_config(fpath)
  502. base = config['pillar_roots']['base']
  503. self.assertEqual(set(base), set([
  504. os.path.join(tempdir, 'a'),
  505. os.path.join(tempdir, 'b'),
  506. os.path.join(tempdir, 'c')
  507. ]))
  508. def test_validate_bad_pillar_roots(self):
  509. expected = salt.config._expand_glob_path(
  510. [salt.syspaths.BASE_PILLAR_ROOTS_DIR]
  511. )
  512. with patch('salt.config._normalize_roots') as mk:
  513. ret = salt.config._validate_pillar_roots(None)
  514. assert not mk.called
  515. assert ret == {'base': expected}
  516. @with_tempdir()
  517. def test_master_id_function(self, tempdir):
  518. master_config = os.path.join(tempdir, 'master')
  519. with salt.utils.files.fopen(master_config, 'w') as fp_:
  520. fp_.write(
  521. 'id_function:\n'
  522. ' test.echo:\n'
  523. ' text: hello_world\n'
  524. 'root_dir: {}\n'
  525. 'log_file: {}\n'.format(tempdir, master_config)
  526. )
  527. # Let's load the configuration
  528. config = salt.config.master_config(master_config)
  529. self.assertEqual(config['log_file'], master_config)
  530. # 'master_config' appends '_master' to the ID
  531. self.assertEqual(config['id'], 'hello_world_master')
  532. @with_tempfile()
  533. @with_tempdir()
  534. def test_minion_file_roots_glob(self, tempdir, fpath):
  535. # Create some files.
  536. for f in 'abc':
  537. fpath = os.path.join(tempdir, f)
  538. with salt.utils.files.fopen(fpath, 'w') as wfh:
  539. wfh.write(f)
  540. with salt.utils.files.fopen(fpath, 'w') as wfh:
  541. wfh.write(
  542. 'file_roots:\n'
  543. ' base:\n'
  544. ' - {}'.format(os.path.join(tempdir, '*'))
  545. )
  546. config = salt.config.minion_config(fpath)
  547. base = config['file_roots']['base']
  548. self.assertEqual(set(base), set([
  549. os.path.join(tempdir, 'a'),
  550. os.path.join(tempdir, 'b'),
  551. os.path.join(tempdir, 'c')
  552. ]))
  553. @with_tempfile()
  554. @with_tempdir()
  555. def test_minion_pillar_roots_glob(self, tempdir, fpath):
  556. # Create some files.
  557. for f in 'abc':
  558. fpath = os.path.join(tempdir, f)
  559. with salt.utils.files.fopen(fpath, 'w') as wfh:
  560. wfh.write(f)
  561. with salt.utils.files.fopen(fpath, 'w') as wfh:
  562. wfh.write(
  563. 'pillar_roots:\n'
  564. ' base:\n'
  565. ' - {}'.format(os.path.join(tempdir, '*'))
  566. )
  567. config = salt.config.minion_config(fpath)
  568. base = config['pillar_roots']['base']
  569. self.assertEqual(set(base), set([
  570. os.path.join(tempdir, 'a'),
  571. os.path.join(tempdir, 'b'),
  572. os.path.join(tempdir, 'c')
  573. ]))
  574. @with_tempdir()
  575. def test_minion_id_function(self, tempdir):
  576. minion_config = os.path.join(tempdir, 'minion')
  577. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  578. fp_.write(
  579. 'id_function:\n'
  580. ' test.echo:\n'
  581. ' text: hello_world\n'
  582. 'root_dir: {}\n'
  583. 'log_file: {}\n'.format(tempdir, minion_config)
  584. )
  585. # Let's load the configuration
  586. config = salt.config.minion_config(minion_config)
  587. self.assertEqual(config['log_file'], minion_config)
  588. self.assertEqual(config['id'], 'hello_world')
  589. @with_tempdir()
  590. def test_minion_id_lowercase(self, tempdir):
  591. '''
  592. This tests that setting `minion_id_lowercase: True` does lower case
  593. the minion id. Lowercase does not operate on a static `id: KING_BOB`
  594. setting, or a cached id.
  595. '''
  596. minion_config = os.path.join(tempdir, 'minion')
  597. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  598. fp_.write(textwrap.dedent('''\
  599. id_function:
  600. test.echo:
  601. text: KING_BOB
  602. minion_id_caching: False
  603. minion_id_lowercase: True
  604. '''))
  605. config = salt.config.minion_config(minion_config) # Load the configuration
  606. self.assertEqual(config['minion_id_caching'], False) # Check the configuration
  607. self.assertEqual(config['minion_id_lowercase'], True) # Check the configuration
  608. self.assertEqual(config['id'], 'king_bob')
  609. @with_tempdir()
  610. def test_minion_id_remove_domain_string_positive(self, tempdir):
  611. '''
  612. This tests that the values of `minion_id_remove_domain` is suppressed from a generated minion id,
  613. effectivly generating a hostname minion_id.
  614. '''
  615. minion_config = os.path.join(tempdir, 'minion')
  616. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  617. fp_.write(textwrap.dedent('''\
  618. id_function:
  619. test.echo:
  620. text: king_bob.foo.org
  621. minion_id_remove_domain: foo.org
  622. minion_id_caching: False
  623. '''))
  624. # Let's load the configuration
  625. config = salt.config.minion_config(minion_config)
  626. self.assertEqual(config['minion_id_remove_domain'], 'foo.org')
  627. self.assertEqual(config['id'], 'king_bob')
  628. @with_tempdir()
  629. def test_minion_id_remove_domain_string_negative(self, tempdir):
  630. '''
  631. See above
  632. '''
  633. minion_config = os.path.join(tempdir, 'minion')
  634. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  635. fp_.write(textwrap.dedent('''\
  636. id_function:
  637. test.echo:
  638. text: king_bob.foo.org
  639. minion_id_remove_domain: bar.org
  640. minion_id_caching: False
  641. '''))
  642. config = salt.config.minion_config(minion_config)
  643. self.assertEqual(config['id'], 'king_bob.foo.org')
  644. @with_tempdir()
  645. def test_minion_id_remove_domain_bool_true(self, tempdir):
  646. '''
  647. See above
  648. '''
  649. minion_config = os.path.join(tempdir, 'minion')
  650. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  651. fp_.write(textwrap.dedent('''\
  652. id_function:
  653. test.echo:
  654. text: king_bob.foo.org
  655. minion_id_remove_domain: True
  656. minion_id_caching: False
  657. '''))
  658. config = salt.config.minion_config(minion_config)
  659. self.assertEqual(config['id'], 'king_bob')
  660. @with_tempdir()
  661. def test_minion_id_remove_domain_bool_false(self, tempdir):
  662. '''
  663. See above
  664. '''
  665. minion_config = os.path.join(tempdir, 'minion')
  666. with salt.utils.files.fopen(minion_config, 'w') as fp_:
  667. fp_.write(textwrap.dedent('''\
  668. id_function:
  669. test.echo:
  670. text: king_bob.foo.org
  671. minion_id_remove_domain: False
  672. minion_id_caching: False
  673. '''))
  674. config = salt.config.minion_config(minion_config)
  675. self.assertEqual(config['id'], 'king_bob.foo.org')
  676. @with_tempdir()
  677. def test_backend_rename(self, tempdir):
  678. '''
  679. This tests that we successfully rename git, hg, svn, and minion to
  680. gitfs, hgfs, svnfs, and minionfs in the master and minion opts.
  681. '''
  682. fpath = salt.utils.files.mkstemp(dir=tempdir)
  683. with salt.utils.files.fopen(fpath, 'w') as fp_:
  684. fp_.write(textwrap.dedent('''\
  685. fileserver_backend:
  686. - roots
  687. - git
  688. - hg
  689. - svn
  690. - minion
  691. '''))
  692. master_config = salt.config.master_config(fpath)
  693. minion_config = salt.config.minion_config(fpath)
  694. expected = ['roots', 'gitfs', 'hgfs', 'svnfs', 'minionfs']
  695. self.assertEqual(master_config['fileserver_backend'], expected)
  696. self.assertEqual(minion_config['fileserver_backend'], expected)
  697. def test_syndic_config(self):
  698. minion_conf_path = self.get_config_file_path('syndic')
  699. master_conf_path = os.path.join(os.path.dirname(minion_conf_path), 'master')
  700. syndic_opts = salt.config.syndic_config(master_conf_path, minion_conf_path)
  701. root_dir = syndic_opts['root_dir']
  702. # id & pki dir are shared & so configured on the minion side
  703. self.assertEqual(syndic_opts['id'], 'syndic')
  704. self.assertEqual(syndic_opts['pki_dir'], os.path.join(root_dir, 'pki'))
  705. # the rest is configured master side
  706. if RUNTIME_VARS.PYTEST_SESSION is False:
  707. # Pytest assigns ports dynamically
  708. self.assertEqual(syndic_opts['master_port'], 54506)
  709. self.assertEqual(syndic_opts['master'], 'localhost')
  710. self.assertEqual(syndic_opts['sock_dir'], os.path.join(root_dir, 'syndic_sock'))
  711. self.assertEqual(syndic_opts['cachedir'], os.path.join(root_dir, 'cache'))
  712. self.assertEqual(syndic_opts['log_file'], os.path.join(root_dir, 'logs', 'syndic.log'))
  713. self.assertEqual(syndic_opts['pidfile'], os.path.join(root_dir, 'run', 'syndic.pid'))
  714. # Show that the options of localclient that repub to local master
  715. # are not merged with syndic ones
  716. self.assertEqual(syndic_opts['_master_conf_file'], minion_conf_path)
  717. self.assertEqual(syndic_opts['_minion_conf_file'], master_conf_path)
  718. @with_tempfile()
  719. def _get_tally(self, fpath, conf_func):
  720. '''
  721. This ensures that any strings which are loaded are unicode strings
  722. '''
  723. tally = {}
  724. def _count_strings(config):
  725. if isinstance(config, dict):
  726. for key, val in six.iteritems(config):
  727. log.debug('counting strings in dict key: %s', key)
  728. log.debug('counting strings in dict val: %s', val)
  729. _count_strings(key)
  730. _count_strings(val)
  731. elif isinstance(config, list):
  732. log.debug('counting strings in list: %s', config)
  733. for item in config:
  734. _count_strings(item)
  735. else:
  736. if isinstance(config, six.string_types):
  737. if isinstance(config, six.text_type):
  738. tally['unicode'] = tally.get('unicode', 0) + 1
  739. else:
  740. # We will never reach this on PY3
  741. tally.setdefault('non_unicode', []).append(config)
  742. with salt.utils.files.fopen(fpath, 'w') as wfh:
  743. wfh.write(textwrap.dedent('''
  744. foo: bar
  745. mylist:
  746. - somestring
  747. - 9
  748. - 123.456
  749. - True
  750. - nested:
  751. - key: val
  752. - nestedlist:
  753. - foo
  754. - bar
  755. - baz
  756. mydict:
  757. - somestring: 9
  758. - 123.456: 789
  759. - True: False
  760. - nested:
  761. - key: val
  762. - nestedlist:
  763. - foo
  764. - bar
  765. - baz'''))
  766. if conf_func is salt.config.master_config:
  767. wfh.write('\n\n')
  768. wfh.write(textwrap.dedent('''
  769. rest_cherrypy:
  770. port: 8000
  771. disable_ssl: True
  772. app_path: /beacon_demo
  773. app: /srv/web/html/index.html
  774. static: /srv/web/static'''))
  775. config = conf_func(fpath)
  776. _count_strings(config)
  777. return tally
  778. def test_conf_file_strings_are_unicode_for_master(self):
  779. '''
  780. This ensures that any strings which are loaded are unicode strings
  781. '''
  782. tally = self._get_tally(salt.config.master_config) # pylint: disable=no-value-for-parameter
  783. non_unicode = tally.get('non_unicode', [])
  784. self.assertEqual(len(non_unicode), 8 if six.PY2 else 0, non_unicode)
  785. self.assertTrue(tally['unicode'] > 0)
  786. def test_conf_file_strings_are_unicode_for_minion(self):
  787. '''
  788. This ensures that any strings which are loaded are unicode strings
  789. '''
  790. tally = self._get_tally(salt.config.minion_config) # pylint: disable=no-value-for-parameter
  791. non_unicode = tally.get('non_unicode', [])
  792. self.assertEqual(len(non_unicode), 0, non_unicode)
  793. self.assertTrue(tally['unicode'] > 0)
  794. # <---- Salt Cloud Configuration Tests ---------------------------------------------
  795. # cloud_config tests
  796. def test_cloud_config_double_master_path(self):
  797. '''
  798. Tests passing in master_config_path and master_config kwargs.
  799. '''
  800. with patch('salt.config.load_config', MagicMock(return_value={})):
  801. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  802. master_config_path='foo', master_config='bar')
  803. def test_cloud_config_double_providers_path(self):
  804. '''
  805. Tests passing in providers_config_path and providers_config kwargs.
  806. '''
  807. with patch('salt.config.load_config', MagicMock(return_value={})):
  808. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  809. providers_config_path='foo', providers_config='bar')
  810. def test_cloud_config_double_profiles_path(self):
  811. '''
  812. Tests passing in profiles_config_path and profiles_config kwargs.
  813. '''
  814. with patch('salt.config.load_config', MagicMock(return_value={})):
  815. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  816. profiles_config_path='foo', profiles_config='bar')
  817. def test_cloud_config_providers_in_opts(self):
  818. '''
  819. Tests mixing old cloud providers with pre-configured providers configurations
  820. using the providers_config kwarg
  821. '''
  822. with patch('salt.config.load_config', MagicMock(return_value={})):
  823. with patch('salt.config.apply_cloud_config',
  824. MagicMock(return_value={'providers': 'foo'})):
  825. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  826. providers_config='bar')
  827. def test_cloud_config_providers_in_opts_path(self):
  828. '''
  829. Tests mixing old cloud providers with pre-configured providers configurations
  830. using the providers_config_path kwarg
  831. '''
  832. with patch('salt.config.load_config', MagicMock(return_value={})):
  833. with patch('salt.config.apply_cloud_config',
  834. MagicMock(return_value={'providers': 'foo'})):
  835. with patch('os.path.isfile', MagicMock(return_value=True)):
  836. self.assertRaises(SaltCloudConfigError, salt.config.cloud_config, PATH,
  837. providers_config_path='bar')
  838. def test_cloud_config_deploy_scripts_search_path(self):
  839. '''
  840. Tests the contents of the 'deploy_scripts_search_path' tuple to ensure that
  841. the correct deploy search paths are present.
  842. There should be two search paths reported in the tuple: ``/etc/salt/cloud.deploy.d``
  843. and ``<path-to-salt-install>/salt/cloud/deploy``. The first element is usually
  844. ``/etc/salt/cloud.deploy.d``, but sometimes is can be something like
  845. ``/etc/local/salt/cloud.deploy.d``, so we'll only test against the last part of
  846. the path.
  847. '''
  848. with patch('os.path.isdir', MagicMock(return_value=True)):
  849. search_paths = salt.config.cloud_config('/etc/salt/cloud').get('deploy_scripts_search_path')
  850. etc_deploy_path = '/salt/cloud.deploy.d'
  851. deploy_path = '/salt/cloud/deploy'
  852. if salt.utils.platform.is_windows():
  853. etc_deploy_path = '/salt\\cloud.deploy.d'
  854. deploy_path = '\\salt\\cloud\\deploy'
  855. # Check cloud.deploy.d path is the first element in the search_paths tuple
  856. self.assertTrue(search_paths[0].endswith(etc_deploy_path))
  857. # Check the second element in the search_paths tuple
  858. self.assertTrue(search_paths[1].endswith(deploy_path))
  859. # apply_cloud_config tests
  860. def test_apply_cloud_config_no_provider_detail_list(self):
  861. '''
  862. Tests when the provider is not contained in a list of details
  863. '''
  864. overrides = {'providers': {'foo': [{'bar': 'baz'}]}}
  865. self.assertRaises(SaltCloudConfigError, salt.config.apply_cloud_config,
  866. overrides, defaults=DEFAULT)
  867. def test_apply_cloud_config_no_provider_detail_dict(self):
  868. '''
  869. Tests when the provider is not contained in the details dictionary
  870. '''
  871. overrides = {'providers': {'foo': {'bar': 'baz'}}}
  872. self.assertRaises(SaltCloudConfigError, salt.config.apply_cloud_config,
  873. overrides, defaults=DEFAULT)
  874. def test_apply_cloud_config_success_list(self):
  875. '''
  876. Tests success when valid data is passed into the function as a list
  877. '''
  878. with patch('salt.config.old_to_new',
  879. MagicMock(return_value={'default_include': 'path/to/some/cloud/conf/file',
  880. 'providers': {
  881. 'foo': {
  882. 'bar': {
  883. 'driver': 'foo:bar'}}}})):
  884. overrides = {'providers': {'foo': [{'driver': 'bar'}]}}
  885. ret = {'default_include': 'path/to/some/cloud/conf/file',
  886. 'providers': {'foo': {'bar': {'driver': 'foo:bar'}}}}
  887. self.assertEqual(salt.config.apply_cloud_config(overrides, defaults=DEFAULT), ret)
  888. def test_apply_cloud_config_success_dict(self):
  889. '''
  890. Tests success when valid data is passed into function as a dictionary
  891. '''
  892. with patch('salt.config.old_to_new',
  893. MagicMock(return_value={'default_include': 'path/to/some/cloud/conf/file',
  894. 'providers': {
  895. 'foo': {
  896. 'bar': {
  897. 'driver': 'foo:bar'}}}})):
  898. overrides = {'providers': {'foo': {'driver': 'bar'}}}
  899. ret = {'default_include': 'path/to/some/cloud/conf/file',
  900. 'providers': {'foo': {'bar': {'driver': 'foo:bar'}}}}
  901. self.assertEqual(salt.config.apply_cloud_config(overrides, defaults=DEFAULT), ret)
  902. # apply_vm_profiles_config tests
  903. def test_apply_vm_profiles_config_bad_profile_format(self):
  904. '''
  905. Tests passing in a bad profile format in overrides
  906. '''
  907. overrides = {'foo': 'bar', 'conf_file': PATH}
  908. self.assertRaises(SaltCloudConfigError, salt.config.apply_vm_profiles_config,
  909. PATH, overrides, defaults=DEFAULT)
  910. def test_apply_vm_profiles_config_success(self):
  911. '''
  912. Tests passing in valid provider and profile config files successfully
  913. '''
  914. providers = {'test-provider':
  915. {'digitalocean':
  916. {'driver': 'digitalocean', 'profiles': {}}}}
  917. overrides = {'test-profile':
  918. {'provider': 'test-provider',
  919. 'image': 'Ubuntu 12.10 x64',
  920. 'size': '512MB'},
  921. 'conf_file': PATH}
  922. ret = {'test-profile':
  923. {'profile': 'test-profile',
  924. 'provider': 'test-provider:digitalocean',
  925. 'image': 'Ubuntu 12.10 x64',
  926. 'size': '512MB'}}
  927. self.assertEqual(salt.config.apply_vm_profiles_config(providers,
  928. overrides,
  929. defaults=DEFAULT), ret)
  930. def test_apply_vm_profiles_config_extend_success(self):
  931. '''
  932. Tests profile extends functionality with valid provider and profile configs
  933. '''
  934. providers = {'test-config': {'ec2': {'profiles': {}, 'driver': 'ec2'}}}
  935. overrides = {'Amazon': {'image': 'test-image-1',
  936. 'extends': 'dev-instances'},
  937. 'Fedora': {'image': 'test-image-2',
  938. 'extends': 'dev-instances'},
  939. 'conf_file': PATH,
  940. 'dev-instances': {'ssh_username': 'test_user',
  941. 'provider': 'test-config'}}
  942. ret = {'Amazon': {'profile': 'Amazon',
  943. 'ssh_username': 'test_user',
  944. 'image': 'test-image-1',
  945. 'provider': 'test-config:ec2'},
  946. 'Fedora': {'profile': 'Fedora',
  947. 'ssh_username': 'test_user',
  948. 'image': 'test-image-2',
  949. 'provider': 'test-config:ec2'},
  950. 'dev-instances': {'profile': 'dev-instances',
  951. 'ssh_username': 'test_user',
  952. 'provider': 'test-config:ec2'}}
  953. self.assertEqual(salt.config.apply_vm_profiles_config(providers,
  954. overrides,
  955. defaults=DEFAULT), ret)
  956. def test_apply_vm_profiles_config_extend_override_success(self):
  957. '''
  958. Tests profile extends and recursively merges data elements
  959. '''
  960. self.maxDiff = None
  961. providers = {'test-config': {'ec2': {'profiles': {}, 'driver': 'ec2'}}}
  962. overrides = {'Fedora': {'image': 'test-image-2',
  963. 'extends': 'dev-instances',
  964. 'minion': {'grains': {'stage': 'experimental'}}},
  965. 'conf_file': PATH,
  966. 'dev-instances': {'ssh_username': 'test_user',
  967. 'provider': 'test-config',
  968. 'minion': {'grains': {'role': 'webserver'}}}}
  969. ret = {'Fedora': {'profile': 'Fedora',
  970. 'ssh_username': 'test_user',
  971. 'image': 'test-image-2',
  972. 'minion': {'grains': {'role': 'webserver',
  973. 'stage': 'experimental'}},
  974. 'provider': 'test-config:ec2'},
  975. 'dev-instances': {'profile': 'dev-instances',
  976. 'ssh_username': 'test_user',
  977. 'minion': {'grains': {'role': 'webserver'}},
  978. 'provider': 'test-config:ec2'}}
  979. self.assertEqual(salt.config.apply_vm_profiles_config(providers,
  980. overrides,
  981. defaults=DEFAULT), ret)
  982. # apply_cloud_providers_config tests
  983. def test_apply_cloud_providers_config_same_providers(self):
  984. '''
  985. Tests when two providers are given with the same provider name
  986. '''
  987. overrides = {'my-dev-envs':
  988. [{'id': 'ABCDEFGHIJKLMNOP',
  989. 'key': 'supersecretkeysupersecretkey',
  990. 'driver': 'ec2'},
  991. {'apikey': 'abcdefghijklmnopqrstuvwxyz',
  992. 'password': 'supersecret',
  993. 'driver': 'ec2'}],
  994. 'conf_file': PATH}
  995. self.assertRaises(SaltCloudConfigError,
  996. salt.config.apply_cloud_providers_config,
  997. overrides,
  998. DEFAULT)
  999. def test_apply_cloud_providers_config_extend(self):
  1000. '''
  1001. Tests the successful extension of a cloud provider
  1002. '''
  1003. overrides = {'my-production-envs':
  1004. [{'extends': 'my-dev-envs:ec2',
  1005. 'location': 'us-east-1',
  1006. 'user': 'ec2-user@mycorp.com'
  1007. }],
  1008. 'my-dev-envs':
  1009. [{'id': 'ABCDEFGHIJKLMNOP',
  1010. 'user': 'user@mycorp.com',
  1011. 'location': 'ap-southeast-1',
  1012. 'key': 'supersecretkeysupersecretkey',
  1013. 'driver': 'ec2'
  1014. },
  1015. {'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1016. 'password': 'supersecret',
  1017. 'driver': 'linode'
  1018. }],
  1019. 'conf_file': PATH}
  1020. ret = {'my-production-envs':
  1021. {'ec2':
  1022. {'profiles': {},
  1023. 'location': 'us-east-1',
  1024. 'key': 'supersecretkeysupersecretkey',
  1025. 'driver': 'ec2',
  1026. 'id': 'ABCDEFGHIJKLMNOP',
  1027. 'user': 'ec2-user@mycorp.com'}},
  1028. 'my-dev-envs':
  1029. {'linode':
  1030. {'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1031. 'password': 'supersecret',
  1032. 'profiles': {},
  1033. 'driver': 'linode'},
  1034. 'ec2':
  1035. {'profiles': {},
  1036. 'location': 'ap-southeast-1',
  1037. 'key': 'supersecretkeysupersecretkey',
  1038. 'driver': 'ec2',
  1039. 'id': 'ABCDEFGHIJKLMNOP',
  1040. 'user': 'user@mycorp.com'}}}
  1041. self.assertEqual(ret,
  1042. salt.config.apply_cloud_providers_config(
  1043. overrides,
  1044. defaults=DEFAULT))
  1045. def test_apply_cloud_providers_config_extend_multiple(self):
  1046. '''
  1047. Tests the successful extension of two cloud providers
  1048. '''
  1049. overrides = {'my-production-envs':
  1050. [{'extends': 'my-dev-envs:ec2',
  1051. 'location': 'us-east-1',
  1052. 'user': 'ec2-user@mycorp.com'},
  1053. {'password': 'new-password',
  1054. 'extends': 'my-dev-envs:linode',
  1055. 'location': 'Salt Lake City'
  1056. }],
  1057. 'my-dev-envs':
  1058. [{'id': 'ABCDEFGHIJKLMNOP',
  1059. 'user': 'user@mycorp.com',
  1060. 'location': 'ap-southeast-1',
  1061. 'key': 'supersecretkeysupersecretkey',
  1062. 'driver': 'ec2'},
  1063. {'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1064. 'password': 'supersecret',
  1065. 'driver': 'linode'}],
  1066. 'conf_file': PATH}
  1067. ret = {'my-production-envs':
  1068. {'linode':
  1069. {'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1070. 'profiles': {},
  1071. 'location': 'Salt Lake City',
  1072. 'driver': 'linode',
  1073. 'password': 'new-password'},
  1074. 'ec2':
  1075. {'user': 'ec2-user@mycorp.com',
  1076. 'key': 'supersecretkeysupersecretkey',
  1077. 'driver': 'ec2',
  1078. 'id': 'ABCDEFGHIJKLMNOP',
  1079. 'profiles': {},
  1080. 'location': 'us-east-1'}},
  1081. 'my-dev-envs':
  1082. {'linode':
  1083. {'apikey': 'abcdefghijklmnopqrstuvwxyz',
  1084. 'password': 'supersecret',
  1085. 'profiles': {},
  1086. 'driver': 'linode'},
  1087. 'ec2':
  1088. {'profiles': {},
  1089. 'user': 'user@mycorp.com',
  1090. 'key': 'supersecretkeysupersecretkey',
  1091. 'driver': 'ec2',
  1092. 'id': 'ABCDEFGHIJKLMNOP',
  1093. 'location': 'ap-southeast-1'}}}
  1094. self.assertEqual(ret, salt.config.apply_cloud_providers_config(
  1095. overrides,
  1096. defaults=DEFAULT))
  1097. def test_apply_cloud_providers_config_extends_bad_alias(self):
  1098. '''
  1099. Tests when the extension contains an alias not found in providers list
  1100. '''
  1101. overrides = {'my-production-envs':
  1102. [{'extends': 'test-alias:ec2',
  1103. 'location': 'us-east-1',
  1104. 'user': 'ec2-user@mycorp.com'}],
  1105. 'my-dev-envs':
  1106. [{'id': 'ABCDEFGHIJKLMNOP',
  1107. 'user': 'user@mycorp.com',
  1108. 'location': 'ap-southeast-1',
  1109. 'key': 'supersecretkeysupersecretkey',
  1110. 'driver': 'ec2'}],
  1111. 'conf_file': PATH}
  1112. self.assertRaises(SaltCloudConfigError,
  1113. salt.config.apply_cloud_providers_config,
  1114. overrides,
  1115. DEFAULT)
  1116. def test_apply_cloud_providers_config_extends_bad_provider(self):
  1117. '''
  1118. Tests when the extension contains a provider not found in providers list
  1119. '''
  1120. overrides = {'my-production-envs':
  1121. [{'extends': 'my-dev-envs:linode',
  1122. 'location': 'us-east-1',
  1123. 'user': 'ec2-user@mycorp.com'}],
  1124. 'my-dev-envs':
  1125. [{'id': 'ABCDEFGHIJKLMNOP',
  1126. 'user': 'user@mycorp.com',
  1127. 'location': 'ap-southeast-1',
  1128. 'key': 'supersecretkeysupersecretkey',
  1129. 'driver': 'ec2'}],
  1130. 'conf_file': PATH}
  1131. self.assertRaises(SaltCloudConfigError,
  1132. salt.config.apply_cloud_providers_config,
  1133. overrides,
  1134. DEFAULT)
  1135. def test_apply_cloud_providers_config_extends_no_provider(self):
  1136. '''
  1137. Tests when no provider is supplied in the extends statement
  1138. '''
  1139. overrides = {'my-production-envs':
  1140. [{'extends': 'my-dev-envs',
  1141. 'location': 'us-east-1',
  1142. 'user': 'ec2-user@mycorp.com'}],
  1143. 'my-dev-envs':
  1144. [{'id': 'ABCDEFGHIJKLMNOP',
  1145. 'user': 'user@mycorp.com',
  1146. 'location': 'ap-southeast-1',
  1147. 'key': 'supersecretkeysupersecretkey',
  1148. 'driver': 'linode'}],
  1149. 'conf_file': PATH}
  1150. self.assertRaises(SaltCloudConfigError,
  1151. salt.config.apply_cloud_providers_config,
  1152. overrides,
  1153. DEFAULT)
  1154. def test_apply_cloud_providers_extends_not_in_providers(self):
  1155. '''
  1156. Tests when extends is not in the list of providers
  1157. '''
  1158. overrides = {'my-production-envs':
  1159. [{'extends': 'my-dev-envs ec2',
  1160. 'location': 'us-east-1',
  1161. 'user': 'ec2-user@mycorp.com'}],
  1162. 'my-dev-envs':
  1163. [{'id': 'ABCDEFGHIJKLMNOP',
  1164. 'user': 'user@mycorp.com',
  1165. 'location': 'ap-southeast-1',
  1166. 'key': 'supersecretkeysupersecretkey',
  1167. 'driver': 'linode'}],
  1168. 'conf_file': PATH}
  1169. self.assertRaises(SaltCloudConfigError,
  1170. salt.config.apply_cloud_providers_config,
  1171. overrides,
  1172. DEFAULT)
  1173. # is_provider_configured tests
  1174. def test_is_provider_configured_no_alias(self):
  1175. '''
  1176. Tests when provider alias is not in opts
  1177. '''
  1178. opts = {'providers': 'test'}
  1179. provider = 'foo:bar'
  1180. self.assertFalse(salt.config.is_provider_configured(opts, provider))
  1181. def test_is_provider_configured_no_driver(self):
  1182. '''
  1183. Tests when provider driver is not in opts
  1184. '''
  1185. opts = {'providers': {'foo': 'baz'}}
  1186. provider = 'foo:bar'
  1187. self.assertFalse(salt.config.is_provider_configured(opts, provider))
  1188. def test_is_provider_configured_key_is_none(self):
  1189. '''
  1190. Tests when a required configuration key is not set
  1191. '''
  1192. opts = {'providers': {'foo': {'bar': {'api_key': None}}}}
  1193. provider = 'foo:bar'
  1194. self.assertFalse(
  1195. salt.config.is_provider_configured(opts,
  1196. provider,
  1197. required_keys=('api_key',)))
  1198. def test_is_provider_configured_success(self):
  1199. '''
  1200. Tests successful cloud provider configuration
  1201. '''
  1202. opts = {'providers': {'foo': {'bar': {'api_key': 'baz'}}}}
  1203. provider = 'foo:bar'
  1204. ret = {'api_key': 'baz'}
  1205. self.assertEqual(
  1206. salt.config.is_provider_configured(opts,
  1207. provider,
  1208. required_keys=('api_key',)), ret)
  1209. def test_is_provider_configured_multiple_driver_not_provider(self):
  1210. '''
  1211. Tests when the drive is not the same as the provider when
  1212. searching through multiple providers
  1213. '''
  1214. opts = {'providers': {'foo': {'bar': {'api_key': 'baz'}}}}
  1215. provider = 'foo'
  1216. self.assertFalse(salt.config.is_provider_configured(opts, provider))
  1217. def test_is_provider_configured_multiple_key_is_none(self):
  1218. '''
  1219. Tests when a required configuration key is not set when
  1220. searching through multiple providers
  1221. '''
  1222. opts = {'providers': {'foo': {'bar': {'api_key': None}}}}
  1223. provider = 'bar'
  1224. self.assertFalse(
  1225. salt.config.is_provider_configured(opts,
  1226. provider,
  1227. required_keys=('api_key',)))
  1228. def test_is_provider_configured_multiple_success(self):
  1229. '''
  1230. Tests successful cloud provider configuration when searching
  1231. through multiple providers
  1232. '''
  1233. opts = {'providers': {'foo': {'bar': {'api_key': 'baz'}}}}
  1234. provider = 'bar'
  1235. ret = {'api_key': 'baz'}
  1236. self.assertEqual(
  1237. salt.config.is_provider_configured(opts,
  1238. provider,
  1239. required_keys=('api_key',)), ret)
  1240. # other cloud configuration tests
  1241. @skipIf(
  1242. salt.utils.platform.is_windows(),
  1243. 'You can\'t set an environment dynamically in Windows')
  1244. @with_tempdir()
  1245. def test_load_cloud_config_from_environ_var(self, tempdir):
  1246. env_root_dir = os.path.join(tempdir, 'foo', 'env')
  1247. os.makedirs(env_root_dir)
  1248. env_fpath = os.path.join(env_root_dir, 'config-env')
  1249. with salt.utils.files.fopen(env_fpath, 'w') as fp_:
  1250. fp_.write(
  1251. 'root_dir: {}\n'
  1252. 'log_file: {}\n'.format(env_root_dir, env_fpath)
  1253. )
  1254. with patched_environ(SALT_CLOUD_CONFIG=env_fpath):
  1255. # Should load from env variable, not the default configuration file
  1256. config = salt.config.cloud_config('/etc/salt/cloud')
  1257. self.assertEqual(config['log_file'], env_fpath)
  1258. root_dir = os.path.join(tempdir, 'foo', 'bar')
  1259. os.makedirs(root_dir)
  1260. fpath = os.path.join(root_dir, 'config')
  1261. with salt.utils.files.fopen(fpath, 'w') as fp_:
  1262. fp_.write(
  1263. 'root_dir: {}\n'
  1264. 'log_file: {}\n'.format(root_dir, fpath)
  1265. )
  1266. # Let's set the environment variable, yet, since the configuration
  1267. # file path is not the default one, i.e., the user has passed an
  1268. # alternative configuration file form the CLI parser, the
  1269. # environment variable will be ignored.
  1270. with patched_environ(SALT_CLOUD_CONFIG=env_fpath):
  1271. config = salt.config.cloud_config(fpath)
  1272. self.assertEqual(config['log_file'], fpath)
  1273. @with_tempdir()
  1274. def test_deploy_search_path_as_string(self, temp_conf_dir):
  1275. config_file_path = os.path.join(temp_conf_dir, 'cloud')
  1276. deploy_dir_path = os.path.join(temp_conf_dir, 'test-deploy.d')
  1277. for directory in (temp_conf_dir, deploy_dir_path):
  1278. if not os.path.isdir(directory):
  1279. os.makedirs(directory)
  1280. default_config = salt.config.cloud_config(config_file_path)
  1281. default_config['deploy_scripts_search_path'] = deploy_dir_path
  1282. with salt.utils.files.fopen(config_file_path, 'w') as cfd:
  1283. salt.utils.yaml.safe_dump(default_config, cfd, default_flow_style=False)
  1284. default_config = salt.config.cloud_config(config_file_path)
  1285. # Our custom deploy scripts path was correctly added to the list
  1286. self.assertIn(
  1287. deploy_dir_path,
  1288. default_config['deploy_scripts_search_path']
  1289. )
  1290. # And it's even the first occurrence as it should
  1291. self.assertEqual(
  1292. deploy_dir_path,
  1293. default_config['deploy_scripts_search_path'][0]
  1294. )
  1295. def test_includes_load(self):
  1296. '''
  1297. Tests that cloud.{providers,profiles}.d directories are loaded, even if not
  1298. directly passed in through path
  1299. '''
  1300. log.warning('Clound config file path: %s', self.get_config_file_path('cloud'))
  1301. config = salt.config.cloud_config(self.get_config_file_path('cloud'))
  1302. self.assertIn('ec2-config', config['providers'])
  1303. self.assertIn('ec2-test', config['profiles'])
  1304. # <---- Salt Cloud Configuration Tests ---------------------------------------------
  1305. def test_include_config_without_errors(self):
  1306. '''
  1307. Tests that include_config function returns valid configuration
  1308. '''
  1309. include_file = 'minion.d/my.conf'
  1310. config_path = '/etc/salt/minion'
  1311. config_opts = {'id': 'myminion.example.com'}
  1312. with patch('glob.glob', MagicMock(return_value=include_file)):
  1313. with patch('salt.config._read_conf_file', MagicMock(return_value=config_opts)):
  1314. configuration = salt.config.include_config(include_file, config_path, verbose=False)
  1315. self.assertEqual(config_opts, configuration)
  1316. def test_include_config_with_errors(self):
  1317. '''
  1318. Tests that include_config function returns valid configuration even on errors
  1319. '''
  1320. include_file = 'minion.d/my.conf'
  1321. config_path = '/etc/salt/minion'
  1322. config_opts = {}
  1323. with patch('glob.glob', MagicMock(return_value=include_file)):
  1324. with patch('salt.config._read_conf_file', _salt_configuration_error):
  1325. configuration = salt.config.include_config(include_file, config_path, verbose=False)
  1326. self.assertEqual(config_opts, configuration)
  1327. def test_include_config_with_errors_exit(self):
  1328. '''
  1329. Tests that include_config exits on errors
  1330. '''
  1331. include_file = 'minion.d/my.conf'
  1332. config_path = '/etc/salt/minion'
  1333. with patch('glob.glob', MagicMock(return_value=include_file)):
  1334. with patch('salt.config._read_conf_file', _salt_configuration_error):
  1335. with self.assertRaises(SystemExit):
  1336. salt.config.include_config(include_file,
  1337. config_path,
  1338. verbose=False,
  1339. exit_on_config_errors=True)
  1340. @staticmethod
  1341. def _get_defaults(**kwargs):
  1342. ret = {
  1343. 'saltenv': kwargs.pop('saltenv', None),
  1344. 'id': 'test',
  1345. 'cachedir': '/A',
  1346. 'sock_dir': '/B',
  1347. 'root_dir': '/C',
  1348. 'fileserver_backend': 'roots',
  1349. 'open_mode': False,
  1350. 'auto_accept': False,
  1351. 'file_roots': {},
  1352. 'pillar_roots': {},
  1353. 'file_ignore_glob': [],
  1354. 'file_ignore_regex': [],
  1355. 'worker_threads': 5,
  1356. 'hash_type': 'sha256',
  1357. 'log_file': 'foo.log',
  1358. }
  1359. ret.update(kwargs)
  1360. return ret
  1361. def test_apply_config(self):
  1362. '''
  1363. Ensure that the environment and saltenv options work properly
  1364. '''
  1365. with patch.object(salt.config, '_adjust_log_file_override', Mock()), \
  1366. patch.object(salt.config, '_update_ssl_config', Mock()), \
  1367. patch.object(salt.config, '_update_discovery_config', Mock()):
  1368. # MASTER CONFIG
  1369. # Ensure that environment overrides saltenv when saltenv not
  1370. # explicitly passed.
  1371. defaults = self._get_defaults(environment='foo')
  1372. ret = salt.config.apply_master_config(defaults=defaults)
  1373. self.assertEqual(ret['environment'], 'foo')
  1374. self.assertEqual(ret['saltenv'], 'foo')
  1375. # Ensure that environment overrides saltenv when saltenv not
  1376. # explicitly passed.
  1377. defaults = self._get_defaults(environment='foo', saltenv='bar')
  1378. ret = salt.config.apply_master_config(defaults=defaults)
  1379. self.assertEqual(ret['environment'], 'bar')
  1380. self.assertEqual(ret['saltenv'], 'bar')
  1381. # If environment was not explicitly set, it should not be in the
  1382. # opts at all.
  1383. defaults = self._get_defaults()
  1384. ret = salt.config.apply_master_config(defaults=defaults)
  1385. self.assertNotIn('environment', ret)
  1386. self.assertEqual(ret['saltenv'], None)
  1387. # Same test as above but with saltenv explicitly set
  1388. defaults = self._get_defaults(saltenv='foo')
  1389. ret = salt.config.apply_master_config(defaults=defaults)
  1390. self.assertNotIn('environment', ret)
  1391. self.assertEqual(ret['saltenv'], 'foo')
  1392. # MINION CONFIG
  1393. # Ensure that environment overrides saltenv when saltenv not
  1394. # explicitly passed.
  1395. defaults = self._get_defaults(environment='foo')
  1396. ret = salt.config.apply_minion_config(defaults=defaults)
  1397. self.assertEqual(ret['environment'], 'foo')
  1398. self.assertEqual(ret['saltenv'], 'foo')
  1399. # Ensure that environment overrides saltenv when saltenv not
  1400. # explicitly passed.
  1401. defaults = self._get_defaults(environment='foo', saltenv='bar')
  1402. ret = salt.config.apply_minion_config(defaults=defaults)
  1403. self.assertEqual(ret['environment'], 'bar')
  1404. self.assertEqual(ret['saltenv'], 'bar')
  1405. # If environment was not explicitly set, it should not be in the
  1406. # opts at all.
  1407. defaults = self._get_defaults()
  1408. ret = salt.config.apply_minion_config(defaults=defaults)
  1409. self.assertNotIn('environment', ret)
  1410. self.assertEqual(ret['saltenv'], None)
  1411. # Same test as above but with saltenv explicitly set
  1412. defaults = self._get_defaults(saltenv='foo')
  1413. ret = salt.config.apply_minion_config(defaults=defaults)
  1414. self.assertNotIn('environment', ret)
  1415. self.assertEqual(ret['saltenv'], 'foo')
  1416. class APIConfigTestCase(DefaultConfigsBase, TestCase):
  1417. '''
  1418. TestCase for the api_config function in salt.config.__init__.py
  1419. '''
  1420. def setUp(self):
  1421. # Copy DEFAULT_API_OPTS to restore after the test
  1422. self.default_api_opts = salt.config.DEFAULT_API_OPTS.copy()
  1423. def tearDown(self):
  1424. # Reset DEFAULT_API_OPTS settings as to not interfere with other unit tests
  1425. salt.config.DEFAULT_API_OPTS = self.default_api_opts
  1426. def test_api_config_log_file_values(self):
  1427. '''
  1428. Tests the opts value of the 'log_file' after running through the
  1429. various default dict updates. 'log_file' should be updated to match
  1430. the DEFAULT_API_OPTS 'api_logfile' value.
  1431. '''
  1432. with patch('salt.config.client_config', MagicMock(return_value=self.mock_master_default_opts)):
  1433. expected = '{}/var/log/salt/api'.format(
  1434. RUNTIME_VARS.TMP_ROOT_DIR if RUNTIME_VARS.TMP_ROOT_DIR != '/' else '')
  1435. if salt.utils.platform.is_windows():
  1436. expected = '{}\\var\\log\\salt\\api'.format(
  1437. RUNTIME_VARS.TMP_ROOT_DIR)
  1438. ret = salt.config.api_config('/some/fake/path')
  1439. self.assertEqual(ret['log_file'], expected)
  1440. def test_api_config_pidfile_values(self):
  1441. '''
  1442. Tests the opts value of the 'pidfile' after running through the
  1443. various default dict updates. 'pidfile' should be updated to match
  1444. the DEFAULT_API_OPTS 'api_pidfile' value.
  1445. '''
  1446. with patch('salt.config.client_config', MagicMock(return_value=self.mock_master_default_opts)):
  1447. expected = '{}/var/run/salt-api.pid'.format(
  1448. RUNTIME_VARS.TMP_ROOT_DIR if RUNTIME_VARS.TMP_ROOT_DIR != '/' else '')
  1449. if salt.utils.platform.is_windows():
  1450. expected = '{}\\var\\run\\salt-api.pid'.format(
  1451. RUNTIME_VARS.TMP_ROOT_DIR)
  1452. ret = salt.config.api_config('/some/fake/path')
  1453. self.assertEqual(ret['pidfile'], expected)
  1454. def test_master_config_file_overrides_defaults(self):
  1455. '''
  1456. Tests the opts value of the api config values after running through the
  1457. various default dict updates that should be overridden by settings in
  1458. the user's master config file.
  1459. '''
  1460. foo_dir = os.path.join(RUNTIME_VARS.TMP_ROOT_DIR, 'foo/bar/baz')
  1461. hello_dir = os.path.join(RUNTIME_VARS.TMP_ROOT_DIR, 'hello/world')
  1462. if salt.utils.platform.is_windows():
  1463. foo_dir = 'c:\\{}'.format(foo_dir.replace('/', '\\'))
  1464. hello_dir = 'c:\\{}'.format(hello_dir.replace('/', '\\'))
  1465. mock_master_config = {
  1466. 'api_pidfile': foo_dir,
  1467. 'api_logfile': hello_dir,
  1468. 'rest_timeout': 5
  1469. }
  1470. mock_master_config.update(self.mock_master_default_opts.copy())
  1471. with patch('salt.config.client_config',
  1472. MagicMock(return_value=mock_master_config)):
  1473. ret = salt.config.api_config('/some/fake/path')
  1474. self.assertEqual(ret['rest_timeout'], 5)
  1475. self.assertEqual(ret['api_pidfile'], foo_dir)
  1476. self.assertEqual(ret['pidfile'], foo_dir)
  1477. self.assertEqual(ret['api_logfile'], hello_dir)
  1478. self.assertEqual(ret['log_file'], hello_dir)
  1479. def test_api_config_prepend_root_dirs_return(self):
  1480. '''
  1481. Tests the opts value of the api_logfile, log_file, api_pidfile, and pidfile
  1482. when a custom root directory is used. This ensures that each of these
  1483. values is present in the list of opts keys that should have the root_dir
  1484. prepended when the api_config function returns the opts dictionary.
  1485. '''
  1486. mock_log = '/mock/root/var/log/salt/api'
  1487. mock_pid = '/mock/root/var/run/salt-api.pid'
  1488. mock_master_config = self.mock_master_default_opts.copy()
  1489. mock_master_config['root_dir'] = '/mock/root/'
  1490. if salt.utils.platform.is_windows():
  1491. mock_log = 'c:\\mock\\root\\var\\log\\salt\\api'
  1492. mock_pid = 'c:\\mock\\root\\var\\run\\salt-api.pid'
  1493. mock_master_config['root_dir'] = 'c:\\mock\\root'
  1494. with patch('salt.config.client_config',
  1495. MagicMock(return_value=mock_master_config)):
  1496. ret = salt.config.api_config('/some/fake/path')
  1497. self.assertEqual(ret['api_logfile'], mock_log)
  1498. self.assertEqual(ret['log_file'], mock_log)
  1499. self.assertEqual(ret['api_pidfile'], mock_pid)
  1500. self.assertEqual(ret['pidfile'], mock_pid)