Coverage for manila/tests/message/test_api.py: 100%

47 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Licensed under the Apache License, Version 2.0 (the "License"); you may 

2# not use this file except in compliance with the License. You may obtain 

3# a copy of the License at 

4# 

5# http://www.apache.org/licenses/LICENSE-2.0 

6# 

7# Unless required by applicable law or agreed to in writing, software 

8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

10# License for the specific language governing permissions and limitations 

11# under the License. 

12 

13import datetime 

14from unittest import mock 

15 

16from oslo_config import cfg 

17from oslo_utils import timeutils 

18 

19from manila import context 

20from manila.message import api as message_api 

21from manila.message.message_field import Action as MsgAction 

22from manila.message.message_field import Detail as MsgDetail 

23from manila.message import message_levels 

24from manila import test 

25 

26CONF = cfg.CONF 

27 

28 

29class MessageApiTest(test.TestCase): 

30 def setUp(self): 

31 super(MessageApiTest, self).setUp() 

32 self.message_api = message_api.API() 

33 self.mock_object(self.message_api, 'db') 

34 self.ctxt = context.RequestContext('admin', 'fakeproject', True) 

35 self.ctxt.request_id = 'fakerequestid' 

36 

37 @mock.patch.object(timeutils, 'utcnow') 

38 def test_create(self, mock_utcnow): 

39 CONF.set_override('message_ttl', 300) 

40 now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) 

41 mock_utcnow.return_value = now 

42 expected_expires_at = now + datetime.timedelta( 

43 seconds=300) 

44 expected_message_record = { 

45 'project_id': 'fakeproject', 

46 'request_id': 'fakerequestid', 

47 'resource_type': 'fake_resource_type', 

48 'resource_id': None, 

49 'action_id': MsgAction.ALLOCATE_HOST[0], 

50 'detail_id': MsgDetail.NO_VALID_HOST[0], 

51 'message_level': message_levels.ERROR, 

52 'expires_at': expected_expires_at, 

53 } 

54 

55 self.message_api.create(self.ctxt, 

56 MsgAction.ALLOCATE_HOST, 

57 "fakeproject", 

58 detail=MsgDetail.NO_VALID_HOST, 

59 resource_type="fake_resource_type") 

60 

61 self.message_api.db.message_create.assert_called_once_with( 

62 self.ctxt, expected_message_record) 

63 

64 def test_create_swallows_exception(self): 

65 self.mock_object(self.message_api.db, 'message_create', 

66 mock.Mock(side_effect=Exception())) 

67 exception_log = self.mock_object(message_api.LOG, 'exception') 

68 self.message_api.create(self.ctxt, 

69 MsgAction.ALLOCATE_HOST, 

70 'fakeproject', 

71 'fake_resource') 

72 

73 self.message_api.db.message_create.assert_called_once_with( 

74 self.ctxt, mock.ANY) 

75 exception_log.assert_called_once_with( 

76 'Failed to create message record for request_id %s', 

77 self.ctxt.request_id) 

78 

79 def test_get(self): 

80 self.message_api.get(self.ctxt, 'fake_id') 

81 

82 self.message_api.db.message_get.assert_called_once_with(self.ctxt, 

83 'fake_id') 

84 

85 def test_get_all(self): 

86 self.message_api.get_all(self.ctxt) 

87 

88 self.message_api.db.message_get_all.assert_called_once_with( 

89 self.ctxt, filters={}, limit=None, offset=None, 

90 sort_dir=None, sort_key=None) 

91 

92 def test_delete(self): 

93 self.message_api.delete(self.ctxt, 'fake_id') 

94 

95 self.message_api.db.message_destroy.assert_called_once_with( 

96 self.ctxt, 'fake_id') 

97 

98 def test_cleanup_expired_messages(self): 

99 admin_context = mock.Mock() 

100 self.mock_object(self.ctxt, 'elevated', 

101 mock.Mock(return_value=admin_context)) 

102 self.message_api.cleanup_expired_messages(self.ctxt) 

103 self.message_api.db.cleanup_expired_messages.assert_called_once_with( 

104 admin_context)