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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 magnum.cmd import api
  16. from magnum.tests import base
  17. # NOTE(hieulq): need to mock MagnumObject, otherwise other test cases
  18. # will be failed because of setting wrong ovo indirection api
  19. @mock.patch('magnum.objects.base.MagnumObject')
  20. class TestMagnumAPI(base.TestCase):
  21. @mock.patch('werkzeug.serving.run_simple')
  22. @mock.patch.object(api, 'api_app')
  23. @mock.patch('magnum.common.service.prepare_service')
  24. def test_api_http(self, mock_prep, mock_app, mock_run, mock_base):
  25. api.main()
  26. app = mock_app.load_app.return_value
  27. mock_prep.assert_called_once_with(mock.ANY)
  28. mock_app.load_app.assert_called_once_with()
  29. mock_run.assert_called_once_with(base.CONF.api.host,
  30. base.CONF.api.port,
  31. app, ssl_context=None)
  32. @mock.patch('os.path.exists')
  33. @mock.patch('werkzeug.serving.run_simple')
  34. @mock.patch.object(api, 'api_app')
  35. @mock.patch('magnum.common.service.prepare_service')
  36. def test_api_https_no_cert(self, mock_prep, mock_app, mock_run,
  37. mock_exist, mock_base):
  38. self.config(enabled_ssl=True,
  39. ssl_cert_file='tmp_crt',
  40. group='api')
  41. mock_exist.return_value = False
  42. self.assertRaises(RuntimeError, api.main)
  43. mock_prep.assert_called_once_with(mock.ANY)
  44. mock_app.load_app.assert_called_once_with()
  45. mock_run.assert_not_called()
  46. mock_exist.assert_called_once_with('tmp_crt')
  47. @mock.patch('os.path.exists')
  48. @mock.patch('werkzeug.serving.run_simple')
  49. @mock.patch.object(api, 'api_app')
  50. @mock.patch('magnum.common.service.prepare_service')
  51. def test_api_https_no_key(self, mock_prep, mock_app, mock_run,
  52. mock_exist, mock_base):
  53. self.config(enabled_ssl=True,
  54. ssl_cert_file='tmp_crt',
  55. ssl_key_file='tmp_key',
  56. group='api')
  57. mock_exist.side_effect = [True, False]
  58. self.assertRaises(RuntimeError, api.main)
  59. mock_prep.assert_called_once_with(mock.ANY)
  60. mock_app.load_app.assert_called_once_with()
  61. mock_run.assert_not_called()
  62. mock_exist.assert_has_calls([mock.call('tmp_crt'),
  63. mock.call('tmp_key')])
  64. @mock.patch('os.path.exists')
  65. @mock.patch('werkzeug.serving.run_simple')
  66. @mock.patch.object(api, 'api_app')
  67. @mock.patch('magnum.common.service.prepare_service')
  68. def test_api_https(self, mock_prep, mock_app, mock_run,
  69. mock_exist, mock_base):
  70. self.config(enabled_ssl=True,
  71. ssl_cert_file='tmp_crt',
  72. ssl_key_file='tmp_key',
  73. group='api')
  74. mock_exist.side_effect = [True, True]
  75. api.main()
  76. app = mock_app.load_app.return_value
  77. mock_prep.assert_called_once_with(mock.ANY)
  78. mock_app.load_app.assert_called_once_with()
  79. mock_exist.assert_has_calls([mock.call('tmp_crt'),
  80. mock.call('tmp_key')])
  81. mock_run.assert_called_once_with(base.CONF.api.host,
  82. base.CONF.api.port, app,
  83. ssl_context=('tmp_crt', 'tmp_key'))