/test/test_utils.py

https://github.com/conjure-up/conjure-up · Python · 116 lines · 90 code · 16 blank · 10 comment · 4 complexity · e3cc5479c00319a51ab4a6941ccfa35b MD5 · raw file

  1. #!/usr/bin/env python
  2. #
  3. # tests test/utils.py
  4. #
  5. # Copyright Canonical, Ltd.
  6. import asyncio
  7. import logging
  8. import unittest
  9. from unittest.mock import patch
  10. from conjureup import utils
  11. from .helpers import test_loop
  12. class UtilsTestCase(unittest.TestCase):
  13. def test_valid_hostnames(self):
  14. "Verify is_valid_hostname"
  15. hostnames = [
  16. 'battlemidget.lol',
  17. 'www.battlemidget.lol',
  18. 'localhost',
  19. 'localhost.localdomain',
  20. '_underscores-is_ok',
  21. 'double--dash_is_ok'
  22. ]
  23. for hostname in hostnames:
  24. assert utils.is_valid_hostname(hostname)
  25. def test_invalid_hostnames(self):
  26. "Verify is_valid_hostname detects invalid hostnames"
  27. hostnames = [
  28. '-no-starting-dash.com',
  29. '.localhost',
  30. 'localhost.no-end-dash-'
  31. ]
  32. for hostname in hostnames:
  33. assert not utils.is_valid_hostname(hostname)
  34. @patch.object(utils, 'juju_version')
  35. @patch.object(utils, 'app')
  36. def test_sentry_report(self, app, juju_version):
  37. # test task schedule
  38. flag = asyncio.Event()
  39. with patch.object(utils, '_sentry_report',
  40. lambda *a, **kw: flag.set()):
  41. with test_loop() as loop:
  42. app.loop = loop
  43. utils.sentry_report('m')
  44. loop.run_until_complete(asyncio.wait_for(flag.wait(), 30))
  45. # test implementation
  46. app.config = {'spell': 'spell'}
  47. app.provider.cloud_type = 'type'
  48. app.provider.region = 'region'
  49. app.is_jaas = False
  50. app.headless = False
  51. juju_version.return_value = '2.j'
  52. app.no_report = True
  53. utils._sentry_report('message', tags={'foo': 'bar'})
  54. assert not app.sentry.capture.called
  55. app.no_report = False
  56. utils._sentry_report('message', tags={'foo': 'bar'})
  57. app.sentry.capture.assert_called_once_with(
  58. 'raven.events.Message',
  59. message='message',
  60. level=logging.WARNING,
  61. tags={
  62. 'spell': 'spell',
  63. 'cloud_type': 'type',
  64. 'region': 'region',
  65. 'jaas': False,
  66. 'headless': False,
  67. 'juju_version': '2.j',
  68. 'foo': 'bar'
  69. })
  70. app.sentry.capture.reset_mock()
  71. utils._sentry_report('message', 'exc_info')
  72. app.sentry.capture.assert_called_once_with(
  73. 'raven.events.Exception',
  74. level=logging.ERROR,
  75. exc_info='exc_info',
  76. tags={
  77. 'spell': 'spell',
  78. 'cloud_type': 'type',
  79. 'region': 'region',
  80. 'jaas': False,
  81. 'headless': False,
  82. 'juju_version': '2.j'
  83. })
  84. def test_subtract_dicts(self):
  85. d = {
  86. 'foo': {
  87. 'bar': 1,
  88. 'baz': 2,
  89. },
  90. 'qux': [1, 2],
  91. }
  92. d_orig = utils.merge_dicts(d, {}) # make deep copy
  93. # full key delete
  94. self.assertEqual(utils.subtract_dicts(d, {'foo': None}),
  95. {'qux': [1, 2]})
  96. # ensure no side-effects
  97. self.assertEqual(d, d_orig)
  98. # sub-key delete
  99. self.assertEqual(utils.subtract_dicts(d, {'foo': {'baz': None}}),
  100. {'foo': {'bar': 1}, 'qux': [1, 2]})