1
0

test_client.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # encoding: utf-8
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import time
  6. # Import Salt Testing libs
  7. from tests.support.paths import TMP_CONF_DIR
  8. from tests.support.unit import TestCase, skipIf
  9. # Import Salt libs
  10. import salt.config
  11. import salt.netapi
  12. class NetapiClientTest(TestCase):
  13. eauth_creds = {
  14. 'username': 'saltdev_auto',
  15. 'password': 'saltdev',
  16. 'eauth': 'auto',
  17. }
  18. def setUp(self):
  19. '''
  20. Set up a NetapiClient instance
  21. '''
  22. opts = salt.config.client_config(os.path.join(TMP_CONF_DIR, 'master'))
  23. self.netapi = salt.netapi.NetapiClient(opts)
  24. def tearDown(self):
  25. del self.netapi
  26. def test_local(self):
  27. low = {'client': 'local', 'tgt': '*', 'fun': 'test.ping'}
  28. low.update(self.eauth_creds)
  29. ret = self.netapi.run(low)
  30. # If --proxy is set, it will cause an extra minion_id to be in the
  31. # response. Since there's not a great way to know if the test
  32. # runner's proxy minion is running, and we're not testing proxy
  33. # minions here anyway, just remove it from the response.
  34. ret.pop('proxytest', None)
  35. self.assertEqual(ret, {'minion': True, 'sub_minion': True})
  36. def test_local_batch(self):
  37. low = {'client': 'local_batch', 'tgt': '*', 'fun': 'test.ping'}
  38. low.update(self.eauth_creds)
  39. ret = self.netapi.run(low)
  40. rets = []
  41. for _ret in ret:
  42. rets.append(_ret)
  43. self.assertIn({'sub_minion': True}, rets)
  44. self.assertIn({'minion': True}, rets)
  45. def test_local_async(self):
  46. low = {'client': 'local_async', 'tgt': '*', 'fun': 'test.ping'}
  47. low.update(self.eauth_creds)
  48. ret = self.netapi.run(low)
  49. # Remove all the volatile values before doing the compare.
  50. self.assertIn('jid', ret)
  51. ret.pop('jid', None)
  52. ret['minions'] = sorted(ret['minions'])
  53. try:
  54. # If --proxy is set, it will cause an extra minion_id to be in the
  55. # response. Since there's not a great way to know if the test
  56. # runner's proxy minion is running, and we're not testing proxy
  57. # minions here anyway, just remove it from the response.
  58. ret['minions'].remove('proxytest')
  59. except ValueError:
  60. pass
  61. self.assertEqual(ret, {'minions': sorted(['minion', 'sub_minion'])})
  62. def test_wheel(self):
  63. low = {'client': 'wheel', 'fun': 'key.list_all'}
  64. low.update(self.eauth_creds)
  65. ret = self.netapi.run(low)
  66. # Remove all the volatile values before doing the compare.
  67. self.assertIn('tag', ret)
  68. ret.pop('tag')
  69. data = ret.get('data', {})
  70. self.assertIn('jid', data)
  71. data.pop('jid', None)
  72. self.assertIn('tag', data)
  73. data.pop('tag', None)
  74. ret.pop('_stamp', None)
  75. data.pop('_stamp', None)
  76. self.maxDiff = None
  77. self.assertTrue(set(['master.pem', 'master.pub']).issubset(set(ret['data']['return']['local'])))
  78. def test_wheel_async(self):
  79. # Give this test a little breathing room
  80. time.sleep(3)
  81. low = {'client': 'wheel_async', 'fun': 'key.list_all'}
  82. low.update(self.eauth_creds)
  83. ret = self.netapi.run(low)
  84. self.assertIn('jid', ret)
  85. self.assertIn('tag', ret)
  86. @skipIf(True, 'This is not testing anything. Skipping for now.')
  87. def test_runner(self):
  88. # TODO: fix race condition in init of event-- right now the event class
  89. # will finish init even if the underlying zmq socket hasn't connected yet
  90. # this is problematic for the runnerclient's master_call method if the
  91. # runner is quick
  92. #low = {'client': 'runner', 'fun': 'cache.grains'}
  93. low = {'client': 'runner', 'fun': 'test.sleep', 'arg': [2]}
  94. low.update(self.eauth_creds)
  95. ret = self.netapi.run(low)
  96. @skipIf(True, 'This is not testing anything. Skipping for now.')
  97. def test_runner_async(self):
  98. low = {'client': 'runner', 'fun': 'cache.grains'}
  99. low.update(self.eauth_creds)
  100. ret = self.netapi.run(low)