Coverage for manila/tests/keymgr/test_barbican.py: 100%

53 statements  

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

1# Copyright 2025 Cloudification GmbH. 

2# All Rights Reserved. 

3# 

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

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

6# a copy of the License at 

7# 

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

9# 

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

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

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

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16from unittest import mock 

17 

18from oslo_config import cfg 

19from oslo_utils import uuidutils 

20 

21from manila import exception 

22from manila.keymgr import barbican as barbican_module 

23from manila import test 

24 

25CONF = cfg.CONF 

26 

27 

28class BarbicanSecretACLTestCase(test.TestCase): 

29 

30 def setUp(self): 

31 super(BarbicanSecretACLTestCase, self).setUp() 

32 self.context = mock.Mock() 

33 self.secret_ref = 'mock-secret-id' 

34 self.barbican_acl = barbican_module.BarbicanSecretACL(CONF) 

35 

36 def test_get_client_and_href_with_valid_secret(self): 

37 mock_href = uuidutils.generate_uuid() 

38 mock_client = mock.Mock() 

39 self.mock_object(self.barbican_acl, '_get_barbican_client', 

40 mock.Mock(return_value=(mock_client, mock.Mock()))) 

41 self.mock_object(self.barbican_acl, '_create_secret_ref', 

42 mock.Mock(return_value=mock_href)) 

43 

44 result_client, result_href = self.barbican_acl.get_client_and_href( 

45 self.context, mock_href) 

46 self.assertEqual(mock_client, result_client) 

47 self.assertEqual(mock_href, result_href) 

48 

49 def test_get_client_and_href_missing_backend(self): 

50 CONF.set_default('backend', 'wrong.backend', group='key_manager') 

51 self.assertRaises( 

52 exception.ManilaBarbicanACLError, 

53 self.barbican_acl.get_client_and_href, 

54 self.context, 

55 self.secret_ref) 

56 

57 def test_get_client_and_href_missing_secret_ref(self): 

58 self.assertRaises( 

59 exception.ManilaBarbicanACLError, 

60 self.barbican_acl.get_client_and_href, 

61 self.context, 

62 None) 

63 

64 

65class BarbicanUserAppCredsTestCase(test.TestCase): 

66 

67 def setUp(self): 

68 super(BarbicanUserAppCredsTestCase, self).setUp() 

69 self.context = mock.Mock() 

70 self.app_creds = barbican_module.BarbicanUserAppCreds(CONF) 

71 

72 @mock.patch('keystoneclient.v3.client.Client') 

73 @mock.patch('keystoneauth1.session.Session') 

74 @mock.patch('keystoneauth1.loading.load_auth_from_conf_options') 

75 def test_get_application_credentials_success(self, mock_auth, 

76 mock_session, mock_client): 

77 fake_cred = mock.Mock() 

78 mock_instance = mock.Mock() 

79 mock_instance.application_credentials.get.return_value = fake_cred 

80 mock_client.return_value = mock_instance 

81 

82 result = self.app_creds.get_application_credentials( 

83 self.context, 'fake_id') 

84 self.assertEqual(fake_cred, result) 

85 

86 def test_get_application_credentials_missing_id(self): 

87 self.assertRaises( 

88 exception.ManilaBarbicanAppCredsError, 

89 self.app_creds.get_application_credentials, 

90 self.context, 

91 None) 

92 

93 @mock.patch('keystoneclient.v3.client.Client') 

94 @mock.patch('keystoneauth1.session.Session') 

95 @mock.patch('keystoneauth1.loading.load_auth_from_conf_options') 

96 def test_delete_application_credentials(self, mock_auth, 

97 mock_session, mock_client): 

98 mock_instance = mock.Mock() 

99 mock_client.return_value = mock_instance 

100 self.app_creds.delete_application_credentials(self.context, 'cred_id') 

101 mock_instance.application_credentials.delete.assert_called_once() 

102 

103 def test_delete_application_credentials_missing_id(self): 

104 self.assertRaises( 

105 exception.ManilaBarbicanAppCredsError, 

106 self.app_creds.delete_application_credentials, 

107 self.context, 

108 None)