Coverage for osm_mon/tests/unit/collector/utils/test_openstack.py: 100%

72 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-06 19:04 +0000

1# -*- coding: utf-8 -*- 

2 

3# Copyright 2018 Whitestack, LLC 

4# ************************************************************* 

5 

6# This file is part of OSM Monitoring module 

7# All Rights Reserved to Whitestack, LLC 

8 

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

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

11# a copy of the License at 

12 

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

14 

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

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

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

18# License for the specific language governing permissions and limitations 

19# under the License. 

20# For those usages not covered by the Apache License, Version 2.0 please 

21# contact: bdiaz@whitestack.com or glavado@whitestack.com 

22# # 

23from unittest import TestCase, mock 

24 

25from osm_mon.collector.utils.openstack import OpenstackUtils 

26from osm_mon.core.exceptions import CertificateNotCreated 

27 

28 

29@mock.patch("osm_mon.collector.utils.openstack.session") 

30class OpenstackUtilsTest(TestCase): 

31 def setUp(self): 

32 super().setUp() 

33 

34 def test_session_without_insecure(self, mock_session): 

35 creds = { 

36 "config": {}, 

37 "vim_url": "url", 

38 "vim_user": "user", 

39 "vim_password": "password", 

40 "vim_tenant_name": "tenant_name", 

41 } 

42 OpenstackUtils.get_session(creds) 

43 

44 mock_session.Session.assert_called_once_with( 

45 auth=mock.ANY, verify=True, timeout=10 

46 ) 

47 

48 def test_session_with_insecure(self, mock_session): 

49 creds = { 

50 "config": {"insecure": True}, 

51 "vim_url": "url", 

52 "vim_user": "user", 

53 "vim_password": "password", 

54 "vim_tenant_name": "tenant_name", 

55 } 

56 OpenstackUtils.get_session(creds) 

57 

58 mock_session.Session.assert_called_once_with( 

59 auth=mock.ANY, verify=False, timeout=10 

60 ) 

61 

62 def test_session_with_insecure_false(self, mock_session): 

63 creds = { 

64 "config": {"insecure": False}, 

65 "vim_url": "url", 

66 "vim_user": "user", 

67 "vim_password": "password", 

68 "vim_tenant_name": "tenant_name", 

69 } 

70 OpenstackUtils.get_session(creds) 

71 mock_session.Session.assert_called_once_with( 

72 auth=mock.ANY, verify=True, timeout=10 

73 ) 

74 

75 @mock.patch("osm_mon.collector.utils.openstack.OpenstackUtils._create_file_cert") 

76 def test_session_with_ca_cert_content(self, mock_create_file_cert, mock_session): 

77 creds = { 

78 "_id": "1234", 

79 "config": {"ca_cert_content": "test"}, 

80 "vim_url": "url", 

81 "vim_user": "user", 

82 "vim_password": "password", 

83 "vim_tenant_name": "tenant_name", 

84 } 

85 mock_create_file_cert.return_value = {"ca_cert": "testfile"} 

86 OpenstackUtils.get_session(creds) 

87 mock_session.Session.assert_called_once_with( 

88 auth=mock.ANY, verify="testfile", timeout=10 

89 ) 

90 

91 @mock.patch("osm_mon.collector.utils.openstack.makedirs", return_value="") 

92 @mock.patch("osm_mon.collector.utils.openstack.path") 

93 def test_create_file_cert(self, mock_path, mock_makedirs, mock_session): 

94 vim_config = {"ca_cert_content": "test"} 

95 target_id = "1234" 

96 mock_path.isdir.return_value = False 

97 

98 with mock.patch("builtins.open", mock.mock_open()) as mocked_file: 

99 OpenstackUtils._create_file_cert(vim_config, target_id) 

100 mock_makedirs.assert_called_once_with("/app/osm_mon/certs/1234") 

101 mocked_file.assert_called_once_with( 

102 f"/app/osm_mon/certs/{target_id}/ca_cert", "w" 

103 ) 

104 assert vim_config["ca_cert"] == f"/app/osm_mon/certs/{target_id}/ca_cert" 

105 

106 @mock.patch("osm_mon.collector.utils.openstack.makedirs") 

107 @mock.patch("osm_mon.collector.utils.openstack.path") 

108 def test_create_file_cert_exists(self, mock_path, mock_makedirs, mock_session): 

109 vim_config = {"ca_cert_content": "test"} 

110 target_id = "1234" 

111 mock_path.isdir.return_value = True 

112 

113 with mock.patch("builtins.open", mock.mock_open()) as mocked_file: 

114 OpenstackUtils._create_file_cert(vim_config, target_id) 

115 mock_makedirs.assert_not_called() 

116 mocked_file.assert_called_once_with( 

117 f"/app/osm_mon/certs/{target_id}/ca_cert", "w" 

118 ) 

119 assert vim_config["ca_cert"] == f"/app/osm_mon/certs/{target_id}/ca_cert" 

120 

121 @mock.patch("osm_mon.collector.utils.openstack.makedirs", side_effect=Exception) 

122 @mock.patch("osm_mon.collector.utils.openstack.path") 

123 def test_create_file_cert_makedirs_except( 

124 self, mock_path, mock_makedirs, mock_session 

125 ): 

126 vim_config = {"ca_cert_content": "test"} 

127 target_id = "1234" 

128 mock_path.isdir.return_value = False 

129 

130 with mock.patch("builtins.open", mock.mock_open()) as mocked_file: 

131 with self.assertRaises(CertificateNotCreated): 

132 OpenstackUtils._create_file_cert(vim_config, target_id) 

133 mock_makedirs.assert_called_once_with("/app/osm_mon/certs/1234") 

134 mocked_file.assert_not_called() 

135 assert vim_config["ca_cert_content"] == "test" 

136 

137 @mock.patch("osm_mon.collector.utils.openstack.makedirs", return_value="") 

138 @mock.patch("osm_mon.collector.utils.openstack.path") 

139 def test_create_file_cert_open_excepts( 

140 self, mock_path, mock_makedirs, mock_session 

141 ): 

142 vim_config = {"ca_cert_content": "test"} 

143 target_id = "1234" 

144 mock_path.isdir.return_value = False 

145 

146 with mock.patch("builtins.open", mock.mock_open()) as mocked_file: 

147 mocked_file.side_effect = Exception 

148 with self.assertRaises(CertificateNotCreated): 

149 OpenstackUtils._create_file_cert(vim_config, target_id) 

150 mock_makedirs.assert_called_once_with("/app/osm_mon/certs/1234") 

151 mocked_file.assert_called_once_with( 

152 f"/app/osm_mon/certs/{target_id}/ca_cert", "w" 

153 ) 

154 assert vim_config["ca_cert_content"] == "test"