Container Infrastructure Management Service for OpenStack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_api.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright 2016 - Fujitsu, Ltd.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import mock
  15. from oslo_concurrency import processutils
  16. from magnum.cmd import api
  17. from magnum.tests import base
  18. # NOTE(hieulq): need to mock MagnumObject, otherwise other test cases
  19. # will be failed because of setting wrong ovo indirection api
  20. @mock.patch('magnum.objects.base.MagnumObject')
  21. class TestMagnumAPI(base.TestCase):
  22. @mock.patch('werkzeug.serving.run_simple')
  23. @mock.patch.object(api, 'api_app')
  24. @mock.patch('magnum.common.service.prepare_service')
  25. def test_api_http(self, mock_prep, mock_app, mock_run, mock_base):
  26. api.main()
  27. app = mock_app.load_app.return_value
  28. mock_prep.assert_called_once_with(mock.ANY)
  29. mock_app.load_app.assert_called_once_with()
  30. workers = processutils.get_worker_count()
  31. mock_run.assert_called_once_with(base.CONF.api.host,
  32. base.CONF.api.port,
  33. app, processes=workers,
  34. ssl_context=None)
  35. @mock.patch('werkzeug.serving.run_simple')
  36. @mock.patch.object(api, 'api_app')
  37. @mock.patch('magnum.common.service.prepare_service')
  38. def test_api_http_config_workers(self, mock_prep, mock_app,
  39. mock_run, mock_base):
  40. fake_workers = 8
  41. self.config(workers=fake_workers, group='api')
  42. api.main()
  43. app = mock_app.load_app.return_value
  44. mock_prep.assert_called_once_with(mock.ANY)
  45. mock_app.load_app.assert_called_once_with()
  46. mock_run.assert_called_once_with(base.CONF.api.host,
  47. base.CONF.api.port,
  48. app, processes=fake_workers,
  49. ssl_context=None)
  50. @mock.patch('os.path.exists')
  51. @mock.patch('werkzeug.serving.run_simple')
  52. @mock.patch.object(api, 'api_app')
  53. @mock.patch('magnum.common.service.prepare_service')
  54. def test_api_https_no_cert(self, mock_prep, mock_app, mock_run,
  55. mock_exist, mock_base):
  56. self.config(enabled_ssl=True,
  57. ssl_cert_file='tmp_crt',
  58. group='api')
  59. mock_exist.return_value = False
  60. self.assertRaises(RuntimeError, api.main)
  61. mock_prep.assert_called_once_with(mock.ANY)
  62. mock_app.load_app.assert_called_once_with()
  63. mock_run.assert_not_called()
  64. mock_exist.assert_called_once_with('tmp_crt')
  65. @mock.patch('os.path.exists')
  66. @mock.patch('werkzeug.serving.run_simple')
  67. @mock.patch.object(api, 'api_app')
  68. @mock.patch('magnum.common.service.prepare_service')
  69. def test_api_https_no_key(self, mock_prep, mock_app, mock_run,
  70. mock_exist, mock_base):
  71. self.config(enabled_ssl=True,
  72. ssl_cert_file='tmp_crt',
  73. ssl_key_file='tmp_key',
  74. group='api')
  75. mock_exist.side_effect = [True, False]
  76. self.assertRaises(RuntimeError, api.main)
  77. mock_prep.assert_called_once_with(mock.ANY)
  78. mock_app.load_app.assert_called_once_with()
  79. mock_run.assert_not_called()
  80. mock_exist.assert_has_calls([mock.call('tmp_crt'),
  81. mock.call('tmp_key')])
  82. @mock.patch('os.path.exists')
  83. @mock.patch('werkzeug.serving.run_simple')
  84. @mock.patch.object(api, 'api_app')
  85. @mock.patch('magnum.common.service.prepare_service')
  86. def test_api_https(self, mock_prep, mock_app, mock_run,
  87. mock_exist, mock_base):
  88. self.config(enabled_ssl=True,
  89. ssl_cert_file='tmp_crt',
  90. ssl_key_file='tmp_key',
  91. group='api')
  92. mock_exist.side_effect = [True, True]
  93. api.main()
  94. app = mock_app.load_app.return_value
  95. mock_prep.assert_called_once_with(mock.ANY)
  96. mock_app.load_app.assert_called_once_with()
  97. mock_exist.assert_has_calls([mock.call('tmp_crt'),
  98. mock.call('tmp_key')])
  99. workers = processutils.get_worker_count()
  100. mock_run.assert_called_once_with(base.CONF.api.host,
  101. base.CONF.api.port, app,
  102. processes=workers,
  103. ssl_context=('tmp_crt', 'tmp_key'))