|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# --- BEGIN_HEADER --- |
| 4 | +# |
| 5 | +# test_mig_shared_cloud - unit test of the corresponding mig shared module |
| 6 | +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH |
| 7 | +# |
| 8 | +# This file is part of MiG. |
| 9 | +# |
| 10 | +# MiG is free software: you can redistribute it and/or modify |
| 11 | +# it under the terms of the GNU General Public License as published by |
| 12 | +# the Free Software Foundation; either version 2 of the License, or |
| 13 | +# (at your option) any later version. |
| 14 | +# |
| 15 | +# MiG is distributed in the hope that it will be useful, |
| 16 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +# GNU General Public License for more details. |
| 19 | +# |
| 20 | +# You should have received a copy of the GNU General Public License |
| 21 | +# along with this program; if not, write to the Free Software |
| 22 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 23 | +# USA. |
| 24 | +# |
| 25 | +# --- END_HEADER --- |
| 26 | +# |
| 27 | + |
| 28 | +"""Unit tests for the migrid module pointed to in the filename""" |
| 29 | + |
| 30 | +import os |
| 31 | +import sys |
| 32 | +import time |
| 33 | +import unittest |
| 34 | + |
| 35 | +sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)))) |
| 36 | + |
| 37 | +from tests.support import TEST_OUTPUT_DIR, MigTestCase, FakeConfiguration, \ |
| 38 | + cleanpath, testmain |
| 39 | +from mig.shared.cloud import cloud_load_instance, cloud_save_instance, \ |
| 40 | + allowed_cloud_images |
| 41 | + |
| 42 | +DUMMY_USER = 'dummy-user' |
| 43 | +DUMMY_SETTINGS_DIR = 'dummy_user_settings' |
| 44 | +DUMMY_SETTINGS_PATH = os.path.join(TEST_OUTPUT_DIR, DUMMY_SETTINGS_DIR) |
| 45 | +DUMMY_CLOUD = "CLOUD" |
| 46 | +DUMMY_FLAVOR = 'openstack' |
| 47 | +DUMMY_LABEL = 'dummy-label' |
| 48 | +DUMMY_IMAGE = 'dummy-image' |
| 49 | +DUMMY_HEX_ID = 'deadbeef-dead-beef-dead-beefdeadbeef' |
| 50 | + |
| 51 | +DUMMY_CLOUD_SPEC = {'service_title': 'CLOUDTITLE', 'service_name': 'CLOUDNAME', |
| 52 | + 'service_desc': 'A Cloud for migrid site', |
| 53 | + 'service_provider_flavor': 'openstack', |
| 54 | + 'service_hosts': 'https://myopenstack-cloud.org:5000/v3', |
| 55 | + 'service_rules_of_conduct': 'rules-of-conduct.pdf', |
| 56 | + 'service_max_user_instances': '0', |
| 57 | + 'service_max_user_instances_map': {DUMMY_USER: '1'}, |
| 58 | + 'service_allowed_images': DUMMY_IMAGE, |
| 59 | + 'service_allowed_images_map': {DUMMY_USER: 'ALL'}, |
| 60 | + 'service_user_map': {DUMMY_IMAGE, 'user'}, |
| 61 | + 'service_image_alias_map': {DUMMY_IMAGE.lower(): |
| 62 | + DUMMY_IMAGE}, |
| 63 | + 'service_flavor_id': DUMMY_HEX_ID, |
| 64 | + 'service_flavor_id_map': {DUMMY_USER: DUMMY_HEX_ID}, |
| 65 | + 'service_network_id': DUMMY_HEX_ID, |
| 66 | + 'service_key_id_map': {}, |
| 67 | + 'service_sec_group_id': DUMMY_HEX_ID, |
| 68 | + 'service_floating_network_id': DUMMY_HEX_ID, |
| 69 | + 'service_availability_zone': 'myopenstack', |
| 70 | + 'service_jumphost_address': 'jumphost.somewhere.org', |
| 71 | + 'service_jumphost_user': 'cloud', |
| 72 | + 'service_jumphost_manage_keys_script': |
| 73 | + 'cloud_manage_keys.py', |
| 74 | + 'service_jumphost_manage_keys_coding': 'base16', |
| 75 | + 'service_network_id_map': {}, |
| 76 | + 'service_sec_group_id_map': {}, |
| 77 | + 'service_floating_network_id_map': {}, |
| 78 | + 'service_availability_zone_map': {}, |
| 79 | + 'service_jumphost_address_map': {}, |
| 80 | + 'service_jumphost_user_map': {}} |
| 81 | +DUMMY_CONF = FakeConfiguration(user_settings=DUMMY_SETTINGS_PATH, |
| 82 | + site_cloud_access=[('distinguished_name', '.*')], |
| 83 | + cloud_services=[DUMMY_CLOUD_SPEC]) |
| 84 | + |
| 85 | +DUMMY_INSTANCE_ID = '%s:%s:%s' % (DUMMY_USER, DUMMY_LABEL, DUMMY_HEX_ID) |
| 86 | +DUMMY_INSTANCE_DICT = { |
| 87 | + DUMMY_INSTANCE_ID: { |
| 88 | + 'INSTANCE_LABEL': DUMMY_LABEL, |
| 89 | + 'INSTANCE_IMAGE': DUMMY_IMAGE, |
| 90 | + 'INSTANCE_ID': DUMMY_INSTANCE_ID, |
| 91 | + 'IMAGE_ID': DUMMY_IMAGE, |
| 92 | + 'CREATED_TIMESTAMP': "%d" % time.time(), |
| 93 | + 'USER_CERT': DUMMY_USER |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +class MigSharedCloud(MigTestCase): |
| 99 | + """Wrap unit tests for the corresponding module""" |
| 100 | + |
| 101 | + def test_cloud_save_load(self): |
| 102 | + os.makedirs(os.path.join(DUMMY_SETTINGS_PATH, DUMMY_USER)) |
| 103 | + cleanpath(DUMMY_SETTINGS_DIR, self) |
| 104 | + |
| 105 | + save_status = cloud_save_instance(DUMMY_CONF, DUMMY_USER, DUMMY_CLOUD, |
| 106 | + DUMMY_LABEL, DUMMY_INSTANCE_DICT) |
| 107 | + self.assertTrue(save_status) |
| 108 | + |
| 109 | + saved_path = os.path.join(DUMMY_SETTINGS_PATH, DUMMY_USER, |
| 110 | + '%s.state' % DUMMY_CLOUD) |
| 111 | + self.assertTrue(os.path.exists(saved_path)) |
| 112 | + |
| 113 | + instance = cloud_load_instance(DUMMY_CONF, DUMMY_USER, |
| 114 | + DUMMY_CLOUD, DUMMY_LABEL) |
| 115 | + # NOTE: instance should be a non-empty dict at this point |
| 116 | + self.assertTrue(isinstance(instance, dict)) |
| 117 | + # print(instance) |
| 118 | + self.assertTrue(DUMMY_INSTANCE_ID in instance) |
| 119 | + instance_dict = instance[DUMMY_INSTANCE_ID] |
| 120 | + self.assertEqual(instance_dict['INSTANCE_LABEL'], DUMMY_LABEL) |
| 121 | + self.assertEqual(instance_dict['INSTANCE_IMAGE'], DUMMY_IMAGE) |
| 122 | + self.assertEqual(instance_dict['INSTANCE_ID'], DUMMY_INSTANCE_ID) |
| 123 | + self.assertEqual(instance_dict['IMAGE_ID'], DUMMY_IMAGE) |
| 124 | + self.assertEqual(instance_dict['USER_CERT'], DUMMY_USER) |
| 125 | + |
| 126 | + @unittest.skip('Work in progress - currently requires remote openstack') |
| 127 | + def test_cloud_allowed_images(self): |
| 128 | + os.makedirs(os.path.join(DUMMY_SETTINGS_PATH, DUMMY_USER)) |
| 129 | + cleanpath(DUMMY_SETTINGS_DIR, self) |
| 130 | + |
| 131 | + allowed_images = allowed_cloud_images(DUMMY_CONF, DUMMY_USER, |
| 132 | + DUMMY_CLOUD, DUMMY_FLAVOR) |
| 133 | + self.assertTrue(isinstance(allowed_images, list)) |
| 134 | + print(allowed_images) |
| 135 | + self.assertTrue(DUMMY_IMAGE in allowed_images) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == '__main__': |
| 139 | + testmain() |
0 commit comments