Coverage for manila/tests/image/test_image.py: 95%

38 statements  

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

1# All Rights Reserved. 

2# 

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

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

5# 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, WITHOUT 

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

12# License for the specific language governing permissions and limitations 

13# under the License. 

14 

15 

16from unittest import mock 

17 

18from manila import context 

19from manila.image import glance 

20from manila import test 

21from manila.tests import utils as test_utils 

22 

23 

24class FakeImageClient: 

25 

26 class Image: 

27 

28 def images(self, *args, **kwargs): 

29 return [{'id': 'id1'}, {'id': 'id2'}] 

30 

31 def __getattr__(self, item): 

32 return None 

33 

34 def __init__(self): 

35 self.image = self.Image() 

36 

37 

38class OpenStackClientTestCase(test.TestCase): 

39 

40 @mock.patch('manila.image.glance.openstack.connection.Connection') 

41 @mock.patch('manila.image.glance.ks_loading.load_session_from_conf_options') # noqa 

42 def test_auth(self, mock_load_session, mock_connection): 

43 mock_load_session.return_value = 'fake_session' 

44 fake_context = 'fake_context' 

45 data = { 

46 'glance': { 

47 'api_microversion': 'foo_api_microversion', 

48 'endpoint_type': 'internal', 

49 'region_name': 'foo_region_name' 

50 } 

51 } 

52 

53 with test_utils.create_temp_config_with_opts(data): 

54 glance.openstackclient(fake_context) 

55 

56 mock_connection.assert_called_once_with( 

57 session='fake_session', 

58 context=fake_context, 

59 image_version=data['glance']['api_microversion'], 

60 image_interface=data['glance']['endpoint_type'], 

61 region_name=data['glance']['region_name'], 

62 ) 

63 

64 

65class ImageApiTestCase(test.TestCase): 

66 def setUp(self): 

67 super().setUp() 

68 

69 self.api = glance.API() 

70 self.imageclient = FakeImageClient() 

71 self.ctx = context.get_admin_context() 

72 self.mock_object(glance, 'openstackclient', 

73 mock.Mock(return_value=self.imageclient)) 

74 

75 def test_image_list(self): 

76 image_list = ['fake', 'image', 'list'] 

77 

78 class FakeImageClient(object): 

79 def images(self): 

80 return image_list 

81 

82 self.imageclient.image = FakeImageClient() 

83 

84 result = self.api.image_list(self.ctx) 

85 

86 self.assertEqual(image_list, result)