|
| 1 | +# Copyright 2024 Canonical Ltd. |
| 2 | +# |
| 3 | +# This program is free software; you can redistribute it and/or |
| 4 | +# modify it under the terms of the GNU Lesser General Public |
| 5 | +# License version 3 as published by the Free Software Foundation. |
| 6 | +# |
| 7 | +# This program is distributed in the hope that it will be useful, |
| 8 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 | +# Lesser General Public License for more details. |
| 11 | +# |
| 12 | +# You should have received a copy of the GNU Lesser General Public License |
| 13 | +# along with this program; if not, write to the Free Software Foundation, |
| 14 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 15 | +# |
| 16 | + |
| 17 | +from unittest.mock import MagicMock, call |
| 18 | + |
| 19 | +import pytest |
| 20 | +from craft_providers.hookutil import ( |
| 21 | + HookError, |
| 22 | + HookHelper, |
| 23 | + Instance, |
| 24 | + configure_hook, |
| 25 | + remove_hook, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +PROJECT_NAME = "fakeproj" |
| 30 | + |
| 31 | + |
| 32 | +def test_no_projects(): |
| 33 | + """Make sure HookError is raised if there is no corresponding lxc project.""" |
| 34 | + # Use our own mock HookHelper rather than the fixture, we need to do things a little |
| 35 | + # differently here |
| 36 | + original_lxc_func = HookHelper.lxc |
| 37 | + |
| 38 | + def fake_lxc(self, *args, **kwargs): |
| 39 | + if len(args) == 2 and args[0:2] == ("project", "list"): |
| 40 | + return [] |
| 41 | + return original_lxc_func(*args, **kwargs) |
| 42 | + |
| 43 | + HookHelper.lxc = fake_lxc |
| 44 | + |
| 45 | + with pytest.raises(HookError) as e: |
| 46 | + HookHelper(project_name=PROJECT_NAME, simulate=False, debug=True) |
| 47 | + assert f"Project {PROJECT_NAME} does not exist in LXD" in str(e) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def fake_hookhelper(): |
| 52 | + def fake_hookhelper(instance_list): |
| 53 | + HookHelper._check_project_exists = MagicMock() # raise nothing |
| 54 | + helper = HookHelper(project_name=PROJECT_NAME, simulate=False, debug=True) |
| 55 | + |
| 56 | + original_lxc_func = helper.lxc |
| 57 | + |
| 58 | + def fake_lxc(*args, **kwargs): |
| 59 | + if len(args) == 1 and args[0] == "list": |
| 60 | + return instance_list |
| 61 | + return original_lxc_func(*args, **kwargs) |
| 62 | + |
| 63 | + helper.lxc = fake_lxc |
| 64 | + |
| 65 | + helper.delete_instance = MagicMock() |
| 66 | + helper.delete_project = MagicMock() |
| 67 | + helper.delete_all_images = MagicMock() |
| 68 | + return helper |
| 69 | + |
| 70 | + return fake_hookhelper |
| 71 | + |
| 72 | + |
| 73 | +def assert_instances_deleted(helper, instances): |
| 74 | + """Transform json list to instance calls for passing to assert_has_calls.""" |
| 75 | + helper.delete_instance.assert_has_calls( |
| 76 | + [call(Instance.from_dict(instance)) for instance in instances], any_order=True |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def test_configure_nothing_to_delete(fake_hookhelper): |
| 81 | + """Test the configure hook logic with mocked lxc calls.""" |
| 82 | + instances = [ |
| 83 | + { |
| 84 | + "name": f"base-instance-{PROJECT_NAME}-buildd-base-v7-c-a839ea97c42df2065713", |
| 85 | + "created_at": "2024-11-15T03:14:36.041502388Z", |
| 86 | + "expanded_config": { |
| 87 | + "image.description": f"base-instance-{PROJECT_NAME}-buildd-base-v7-craft-com.ubuntu.cloud-buildd-daily-core24", |
| 88 | + "image.version": "24.04", |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + "name": f"{PROJECT_NAME}-busybox-gadget-on-amd64-for-amd64-13389833", |
| 93 | + "created_at": "2024-11-15T03:15:33.48330342Z", |
| 94 | + "expanded_config": { |
| 95 | + "image.description": f"base-instance-{PROJECT_NAME}-buildd-base-v7-craft-com.ubuntu.cloud-buildd-daily-core24", |
| 96 | + "image.version": "24.04", |
| 97 | + }, |
| 98 | + }, |
| 99 | + ] |
| 100 | + helper = fake_hookhelper(instances) |
| 101 | + |
| 102 | + configure_hook(helper) |
| 103 | + |
| 104 | + helper.delete_instance.assert_not_called() |
| 105 | + helper.delete_project.assert_not_called() |
| 106 | + |
| 107 | + |
| 108 | +def test_configure_simple_delete_superseded(fake_hookhelper): |
| 109 | + """Test a simple case where some images with out-of-date compat tags are deleted.""" |
| 110 | + instances = [ |
| 111 | + { |
| 112 | + "name": f"base-instance-{PROJECT_NAME}-buildd-base-v7-c-a839ea97c42df2065713", |
| 113 | + "created_at": "2024-11-15T03:14:36.041502388Z", |
| 114 | + "expanded_config": { |
| 115 | + "image.description": f"base-instance-{PROJECT_NAME}-buildd-base-v7-craft-com.ubuntu.cloud-buildd-daily-core24", |
| 116 | + "image.version": "24.04", |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + "name": f"{PROJECT_NAME}-busybox-gadget-on-amd64-for-amd64-13389833", |
| 121 | + "created_at": "2024-11-15T03:15:33.48330342Z", |
| 122 | + "expanded_config": { |
| 123 | + "image.description": f"base-instance-{PROJECT_NAME}-buildd-base-v7-craft-com.ubuntu.cloud-buildd-daily-core24", |
| 124 | + "image.version": "24.04", |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + "name": f"base-instance-{PROJECT_NAME}-buildd-base-v6-c-a839ea97c42df2065712", |
| 129 | + "created_at": "2024-11-15T02:14:36.041502388Z", |
| 130 | + "expanded_config": { |
| 131 | + "image.description": f"base-instance-{PROJECT_NAME}-buildd-base-v6-craft-com.ubuntu.cloud-buildd-daily-core22", |
| 132 | + "image.version": "22.04", |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + "name": f"{PROJECT_NAME}-busybox-gadget-on-amd64-for-amd64-13389832", |
| 137 | + "created_at": "2024-11-15T02:15:33.48330342Z", |
| 138 | + "expanded_config": { |
| 139 | + "image.description": f"base-instance-{PROJECT_NAME}-buildd-base-v6-craft-com.ubuntu.cloud-buildd-daily-core22", |
| 140 | + "image.version": "22.04", |
| 141 | + }, |
| 142 | + }, |
| 143 | + ] |
| 144 | + helper = fake_hookhelper(instances) |
| 145 | + configure_hook(helper) |
| 146 | + assert_instances_deleted(helper, instances[2:2]) |
| 147 | + |
| 148 | + |
| 149 | +def test_remove_simple_delete(fake_hookhelper): |
| 150 | + """Test the remove hook logic with mocked lxc calls.""" |
| 151 | + instances = [ |
| 152 | + { |
| 153 | + "name": f"base-instance-{PROJECT_NAME}-buildd-base-v7-c-a839ea97c42df2065713", |
| 154 | + "created_at": "2024-11-15T03:14:36.041502388Z", |
| 155 | + "expanded_config": { |
| 156 | + "image.description": f"base-instance-{PROJECT_NAME}-buildd-base-v7-craft-com.ubuntu.cloud-buildd-daily-core24", |
| 157 | + "image.version": "24.04", |
| 158 | + }, |
| 159 | + }, |
| 160 | + { |
| 161 | + "name": f"{PROJECT_NAME}-busybox-gadget-on-amd64-for-amd64-13389833", |
| 162 | + "created_at": "2024-11-15T03:15:33.48330342Z", |
| 163 | + "expanded_config": { |
| 164 | + "image.description": f"base-instance-{PROJECT_NAME}-buildd-base-v7-craft-com.ubuntu.cloud-buildd-daily-core24", |
| 165 | + "image.version": "24.04", |
| 166 | + }, |
| 167 | + }, |
| 168 | + ] |
| 169 | + helper = fake_hookhelper(instances) |
| 170 | + remove_hook(helper) |
| 171 | + assert_instances_deleted(helper, instances) |
| 172 | + helper.delete_all_images.assert_called_once() |
| 173 | + helper.delete_project.assert_called_once() |
0 commit comments