|
| 1 | +# Copyright 2024 Cloudbase Solutions Srl |
| 2 | +# All Rights Reserved. |
| 3 | + |
| 4 | +import argparse |
| 5 | +import ddt |
| 6 | +import os |
| 7 | +from unittest import mock |
| 8 | + |
| 9 | +from coriolisclient.cli import utils |
| 10 | +from coriolisclient.tests import test_base |
| 11 | + |
| 12 | + |
| 13 | +@ddt.ddt |
| 14 | +class UtilsTestCase(test_base.CoriolisBaseTestCase): |
| 15 | + """Test suite for the Coriolis Utils.""" |
| 16 | + |
| 17 | + def test_add_storage_mappings_arguments_to_parser(self): |
| 18 | + parser = argparse.ArgumentParser() |
| 19 | + |
| 20 | + utils.add_storage_mappings_arguments_to_parser(parser) |
| 21 | + args = parser.parse_args( |
| 22 | + ['--storage-backend-mapping', 'mock_source=mock_destination', |
| 23 | + '--disk-storage-mapping', 'mock_disk_id=mock_destination']) |
| 24 | + |
| 25 | + self.assertEqual( |
| 26 | + [{'disk_id': 'mock_disk_id', 'destination': 'mock_destination'}], |
| 27 | + args.disk_storage_mappings |
| 28 | + ) |
| 29 | + self.assertEqual( |
| 30 | + [{'source': 'mock_source', 'destination': 'mock_destination'}], |
| 31 | + args.storage_backend_mappings |
| 32 | + ) |
| 33 | + |
| 34 | + def test_get_storage_mappings_dict_from_args(self): |
| 35 | + args = mock.Mock() |
| 36 | + args.default_storage_backend = mock.sentinel.default_storage_backend |
| 37 | + args.disk_storage_mappings = mock.sentinel.disk_storage_mappings |
| 38 | + args.storage_backend_mappings = mock.sentinel.storage_backend_mappings |
| 39 | + |
| 40 | + result = utils.get_storage_mappings_dict_from_args(args) |
| 41 | + |
| 42 | + self.assertEqual( |
| 43 | + { |
| 44 | + "backend_mappings": mock.sentinel.storage_backend_mappings, |
| 45 | + "default": mock.sentinel.default_storage_backend, |
| 46 | + "disk_mappings": mock.sentinel.disk_storage_mappings |
| 47 | + }, |
| 48 | + result |
| 49 | + ) |
| 50 | + |
| 51 | + def test_format_mapping(self): |
| 52 | + mapping = { |
| 53 | + "mapping1": "mock_mapping1", |
| 54 | + "mapping2": "mock_mapping2", |
| 55 | + "mapping3": "mock_mapping3" |
| 56 | + } |
| 57 | + |
| 58 | + result = utils.format_mapping(mapping) |
| 59 | + |
| 60 | + self.assertEqual( |
| 61 | + ( |
| 62 | + "'mapping1'='mock_mapping1', " |
| 63 | + "'mapping2'='mock_mapping2', " |
| 64 | + "'mapping3'='mock_mapping3'" |
| 65 | + ), |
| 66 | + result |
| 67 | + ) |
| 68 | + |
| 69 | + def test_parse_storage_mappings(self): |
| 70 | + storage_mappings = { |
| 71 | + "default": "mock_default", |
| 72 | + "backend_mappings": [ |
| 73 | + { |
| 74 | + "source": "mock_source", |
| 75 | + "destination": "mock_destination" |
| 76 | + } |
| 77 | + ], |
| 78 | + "disk_mappings": [ |
| 79 | + { |
| 80 | + "disk_id": "mock_disk_id", |
| 81 | + "destination": "mock_destination" |
| 82 | + } |
| 83 | + ] |
| 84 | + } |
| 85 | + |
| 86 | + result = utils.parse_storage_mappings(storage_mappings) |
| 87 | + |
| 88 | + self.assertEqual( |
| 89 | + ( |
| 90 | + 'mock_default', |
| 91 | + {'mock_source': 'mock_destination'}, |
| 92 | + {'mock_disk_id': 'mock_destination'} |
| 93 | + ), |
| 94 | + result |
| 95 | + ) |
| 96 | + |
| 97 | + result = utils.parse_storage_mappings(None) |
| 98 | + |
| 99 | + self.assertEqual( |
| 100 | + (None, {}, {}), |
| 101 | + result |
| 102 | + ) |
| 103 | + |
| 104 | + def test_format_json_for_object_property(self): |
| 105 | + obj = mock.Mock() |
| 106 | + obj.prop_name = {"key1": "value1", "key2": "value2"} |
| 107 | + |
| 108 | + result = utils.format_json_for_object_property(obj, "prop_name") |
| 109 | + |
| 110 | + self.assertEqual( |
| 111 | + '{\n "key1": "value1",\n "key2": "value2"\n}', |
| 112 | + result |
| 113 | + ) |
| 114 | + |
| 115 | + obj = mock.Mock() |
| 116 | + obj.prop_name.to_dict.return_value = \ |
| 117 | + {"key1": "value1", "key2": "value2"} |
| 118 | + |
| 119 | + result = utils.format_json_for_object_property(obj, "prop_name") |
| 120 | + |
| 121 | + self.assertEqual( |
| 122 | + '{\n "key1": "value1",\n "key2": "value2"\n}', |
| 123 | + result |
| 124 | + ) |
| 125 | + |
| 126 | + obj.prop_name = None |
| 127 | + |
| 128 | + result = utils.format_json_for_object_property(obj, "prop_name") |
| 129 | + |
| 130 | + self.assertEqual( |
| 131 | + '{}', |
| 132 | + result |
| 133 | + ) |
| 134 | + |
| 135 | + def test_validate_uuid_string(self): |
| 136 | + result = utils.validate_uuid_string( |
| 137 | + "12345678-9ABC-DEF1-2345-6789abcdef12") |
| 138 | + |
| 139 | + self.assertEqual( |
| 140 | + True, |
| 141 | + result |
| 142 | + ) |
| 143 | + |
| 144 | + result = utils.validate_uuid_string( |
| 145 | + "123456789ABCDEF") |
| 146 | + |
| 147 | + self.assertEqual( |
| 148 | + False, |
| 149 | + result |
| 150 | + ) |
| 151 | + |
| 152 | + def test_add_args_for_json_option_to_parser(self): |
| 153 | + parser = mock.Mock() |
| 154 | + |
| 155 | + result = utils.add_args_for_json_option_to_parser( |
| 156 | + parser, "option_name") |
| 157 | + |
| 158 | + self.assertEqual( |
| 159 | + parser, |
| 160 | + result |
| 161 | + ) |
| 162 | + |
| 163 | + def test_get_option_value_from_args(self): |
| 164 | + args = mock.MagicMock() |
| 165 | + args.option_name = '{"option": "raw_value"}' |
| 166 | + args.option_name_file.__enter__.return_value.read.return_value = \ |
| 167 | + '{"option": "file_value"}' |
| 168 | + |
| 169 | + result = utils.get_option_value_from_args(args, "option-name") |
| 170 | + |
| 171 | + self.assertEqual( |
| 172 | + {'option': 'raw_value'}, |
| 173 | + result |
| 174 | + ) |
| 175 | + |
| 176 | + args.option_name = None |
| 177 | + |
| 178 | + result = utils.get_option_value_from_args(args, "option-name") |
| 179 | + |
| 180 | + self.assertEqual( |
| 181 | + {'option': 'file_value'}, |
| 182 | + result |
| 183 | + ) |
| 184 | + |
| 185 | + def test_get_option_value_from_args_no_value(self): |
| 186 | + args = mock.Mock() |
| 187 | + args.option_name = None |
| 188 | + args.option_name_file = None |
| 189 | + |
| 190 | + result = utils.get_option_value_from_args( |
| 191 | + args, "option-name", error_on_no_value=False) |
| 192 | + |
| 193 | + self.assertEqual( |
| 194 | + None, |
| 195 | + result |
| 196 | + ) |
| 197 | + |
| 198 | + self.assertRaises( |
| 199 | + ValueError, |
| 200 | + utils.get_option_value_from_args, |
| 201 | + args, |
| 202 | + "option-name" |
| 203 | + ) |
| 204 | + |
| 205 | + def test_get_option_value_from_args_json_value_error(self): |
| 206 | + args = mock.Mock() |
| 207 | + args.option_name = "invalid" |
| 208 | + args.option_name_file = None |
| 209 | + |
| 210 | + self.assertRaises( |
| 211 | + ValueError, |
| 212 | + utils.get_option_value_from_args, |
| 213 | + args, |
| 214 | + "option-name" |
| 215 | + ) |
| 216 | + |
| 217 | + @ddt.data( |
| 218 | + { |
| 219 | + "global_scripts": None, |
| 220 | + "instance_scripts": None, |
| 221 | + "expected_result": |
| 222 | + { |
| 223 | + "global": {}, |
| 224 | + "instances": {} |
| 225 | + } |
| 226 | + }, |
| 227 | + { |
| 228 | + "global_scripts": ["linux="], |
| 229 | + "instance_scripts": ["linux="], |
| 230 | + "expected_result": |
| 231 | + { |
| 232 | + "global": {"linux": None}, |
| 233 | + "instances": {"linux": None} |
| 234 | + } |
| 235 | + }, |
| 236 | + { |
| 237 | + "global_scripts": ["linux script"], |
| 238 | + "instance_scripts": ["linux script"], |
| 239 | + "expected_result": |
| 240 | + { |
| 241 | + "global": {}, |
| 242 | + "instances": {} |
| 243 | + } |
| 244 | + }, |
| 245 | + { |
| 246 | + "global_scripts": ["invalid_os=scrips"], |
| 247 | + "instance_scripts": None, |
| 248 | + "expected_result": None |
| 249 | + }, |
| 250 | + { |
| 251 | + "global_scripts": ["linux='invalid/file/path'"], |
| 252 | + "instance_scripts": None, |
| 253 | + "expected_result": None |
| 254 | + }, |
| 255 | + { |
| 256 | + "global_scripts": None, |
| 257 | + "instance_scripts": ["linux='invalid/file/path'"], |
| 258 | + "expected_result": None |
| 259 | + }, |
| 260 | + ) |
| 261 | + def test_compose_user_scripts(self, data): |
| 262 | + global_scripts = data["global_scripts"] |
| 263 | + global_scripts = data["global_scripts"] |
| 264 | + instance_scripts = data["instance_scripts"] |
| 265 | + expected_result = data["expected_result"] |
| 266 | + |
| 267 | + if expected_result: |
| 268 | + result = utils.compose_user_scripts( |
| 269 | + global_scripts, instance_scripts) |
| 270 | + |
| 271 | + self.assertEqual( |
| 272 | + expected_result, |
| 273 | + result |
| 274 | + ) |
| 275 | + else: |
| 276 | + self.assertRaises( |
| 277 | + ValueError, |
| 278 | + utils.compose_user_scripts, |
| 279 | + global_scripts, |
| 280 | + instance_scripts |
| 281 | + ) |
| 282 | + |
| 283 | + def test_compose_user_scripts_from_file(self): |
| 284 | + script_path = os.path.dirname(os.path.realpath(__file__)) |
| 285 | + script_path = os.path.join(script_path, 'data') |
| 286 | + script_path = os.path.join(script_path, 'user_scripts.yml') |
| 287 | + global_scripts = ["linux=%s" % script_path] |
| 288 | + instance_scripts = ["linux=%s" % script_path] |
| 289 | + |
| 290 | + result = utils.compose_user_scripts(global_scripts, instance_scripts) |
| 291 | + |
| 292 | + self.assertEqual( |
| 293 | + { |
| 294 | + 'global': {'linux': '"mock_script1"\n"mock_script2"\n'}, |
| 295 | + 'instances': {'linux': '"mock_script1"\n"mock_script2"\n'} |
| 296 | + }, |
| 297 | + result |
| 298 | + ) |
| 299 | + |
| 300 | + def test_add_minion_pool_args_to_parser(self): |
| 301 | + parser = argparse.ArgumentParser() |
| 302 | + |
| 303 | + utils.add_minion_pool_args_to_parser(parser) |
| 304 | + args = parser.parse_args( |
| 305 | + ['--osmorphing-minion-pool-mapping', |
| 306 | + 'mock_instance_id=mock_pool_id']) |
| 307 | + |
| 308 | + self.assertEqual( |
| 309 | + [{'instance_id': 'mock_instance_id', 'pool_id': 'mock_pool_id'}], |
| 310 | + args.instance_osmorphing_minion_pool_mappings |
| 311 | + ) |
0 commit comments