|
| 1 | +"""Tests for ``orca server clone`` boot-mode selection (ephemeral vs BFV).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from orca_cli.core.config import save_profile, set_active_profile |
| 6 | + |
| 7 | +SRC_ID = "11112222-3333-4444-5555-666677778888" |
| 8 | +IMG_ID = "55556666-7777-8888-9999-000011112222" |
| 9 | +NET_ID = "44445555-6666-7777-8888-999900001111" |
| 10 | +VOL_ID = "22223333-4444-5555-6666-777788889999" |
| 11 | +FLAVOR_WITH_DISK = "flav-disk-20" |
| 12 | +FLAVOR_DISKLESS = "flav-disk-0" |
| 13 | + |
| 14 | + |
| 15 | +def _mock_clone_environment(mock_client, flavor_id, flavor_disk): |
| 16 | + """Wire a mock_client that serves the GETs needed by `server clone`.""" |
| 17 | + mock_client.compute_url = "https://nova.example.com/v2.1" |
| 18 | + mock_client.volume_url = "https://cinder.example.com/v3" |
| 19 | + state = {"posted": {}} |
| 20 | + |
| 21 | + def _get(url, **kwargs): |
| 22 | + if url.endswith(f"/flavors/{flavor_id}"): |
| 23 | + return {"flavor": {"id": flavor_id, "disk": flavor_disk}} |
| 24 | + if f"servers/{SRC_ID}/os-volume_attachments" in url: |
| 25 | + return {"volumeAttachments": [ |
| 26 | + {"id": "att-1", "volumeId": VOL_ID, "device": "/dev/vda"}, |
| 27 | + ]} |
| 28 | + if f"servers/{SRC_ID}/os-interface" in url: |
| 29 | + return {"interfaceAttachments": [ |
| 30 | + {"net_id": NET_ID, "port_id": "p", "fixed_ips": []}, |
| 31 | + ]} |
| 32 | + if f"servers/{SRC_ID}" in url: |
| 33 | + return {"server": { |
| 34 | + "id": SRC_ID, "name": "source", |
| 35 | + "flavor": {"id": flavor_id}, |
| 36 | + "image": {"id": IMG_ID}, |
| 37 | + "security_groups": [{"name": "default"}], |
| 38 | + "key_name": "k", |
| 39 | + "addresses": {}, |
| 40 | + }} |
| 41 | + if f"volumes/{VOL_ID}" in url: |
| 42 | + return {"volume": { |
| 43 | + "size": 20, |
| 44 | + "volume_image_metadata": {"image_id": IMG_ID}, |
| 45 | + }} |
| 46 | + return {} |
| 47 | + |
| 48 | + def _post(url, **kwargs): |
| 49 | + state["posted"]["url"] = url |
| 50 | + state["posted"]["body"] = kwargs.get("json", {}).get("server", {}) |
| 51 | + return {"server": {"id": "clone-1"}} |
| 52 | + |
| 53 | + mock_client.get = _get |
| 54 | + mock_client.post = _post |
| 55 | + return state |
| 56 | + |
| 57 | + |
| 58 | +class TestCloneAutoDetect: |
| 59 | + |
| 60 | + def test_flavor_with_disk_defaults_to_boot_from_image( |
| 61 | + self, invoke, config_dir, mock_client, sample_profile, |
| 62 | + ): |
| 63 | + save_profile("p", sample_profile) |
| 64 | + set_active_profile("p") |
| 65 | + state = _mock_clone_environment(mock_client, FLAVOR_WITH_DISK, 20) |
| 66 | + |
| 67 | + result = invoke(["server", "clone", SRC_ID, "--name", "dst"]) |
| 68 | + |
| 69 | + assert result.exit_code == 0, result.output |
| 70 | + body = state["posted"]["body"] |
| 71 | + assert body["imageRef"] == IMG_ID |
| 72 | + assert "block_device_mapping_v2" not in body |
| 73 | + assert "from image" in result.output.lower() |
| 74 | + |
| 75 | + def test_diskless_flavor_falls_back_to_bfv( |
| 76 | + self, invoke, config_dir, mock_client, sample_profile, |
| 77 | + ): |
| 78 | + save_profile("p", sample_profile) |
| 79 | + set_active_profile("p") |
| 80 | + state = _mock_clone_environment(mock_client, FLAVOR_DISKLESS, 0) |
| 81 | + |
| 82 | + result = invoke(["server", "clone", SRC_ID, "--name", "dst"]) |
| 83 | + |
| 84 | + assert result.exit_code == 0, result.output |
| 85 | + body = state["posted"]["body"] |
| 86 | + assert "block_device_mapping_v2" in body |
| 87 | + assert "imageRef" not in body |
| 88 | + |
| 89 | + |
| 90 | +class TestCloneExplicitFlags: |
| 91 | + |
| 92 | + def test_boot_from_volume_override( |
| 93 | + self, invoke, config_dir, mock_client, sample_profile, |
| 94 | + ): |
| 95 | + save_profile("p", sample_profile) |
| 96 | + set_active_profile("p") |
| 97 | + state = _mock_clone_environment(mock_client, FLAVOR_WITH_DISK, 20) |
| 98 | + |
| 99 | + result = invoke(["server", "clone", SRC_ID, "--name", "dst", |
| 100 | + "--boot-from-volume", "--disk-size", "50"]) |
| 101 | + |
| 102 | + assert result.exit_code == 0, result.output |
| 103 | + body = state["posted"]["body"] |
| 104 | + assert "block_device_mapping_v2" in body |
| 105 | + assert body["block_device_mapping_v2"][0]["volume_size"] == 50 |
| 106 | + assert "imageRef" not in body |
| 107 | + |
| 108 | + def test_boot_from_image_override( |
| 109 | + self, invoke, config_dir, mock_client, sample_profile, |
| 110 | + ): |
| 111 | + save_profile("p", sample_profile) |
| 112 | + set_active_profile("p") |
| 113 | + state = _mock_clone_environment(mock_client, FLAVOR_WITH_DISK, 20) |
| 114 | + |
| 115 | + result = invoke(["server", "clone", SRC_ID, "--name", "dst", |
| 116 | + "--boot-from-image"]) |
| 117 | + |
| 118 | + assert result.exit_code == 0, result.output |
| 119 | + body = state["posted"]["body"] |
| 120 | + assert body["imageRef"] == IMG_ID |
| 121 | + assert "block_device_mapping_v2" not in body |
| 122 | + |
| 123 | + def test_boot_from_image_on_diskless_errors( |
| 124 | + self, invoke, config_dir, mock_client, sample_profile, |
| 125 | + ): |
| 126 | + save_profile("p", sample_profile) |
| 127 | + set_active_profile("p") |
| 128 | + state = _mock_clone_environment(mock_client, FLAVOR_DISKLESS, 0) |
| 129 | + |
| 130 | + result = invoke(["server", "clone", SRC_ID, "--name", "dst", |
| 131 | + "--boot-from-image"]) |
| 132 | + |
| 133 | + assert result.exit_code != 0 |
| 134 | + assert "disk=0" in result.output |
| 135 | + assert "body" not in state["posted"] |
| 136 | + |
| 137 | + def test_mutual_exclusion( |
| 138 | + self, invoke, config_dir, mock_client, sample_profile, |
| 139 | + ): |
| 140 | + save_profile("p", sample_profile) |
| 141 | + set_active_profile("p") |
| 142 | + state = _mock_clone_environment(mock_client, FLAVOR_WITH_DISK, 20) |
| 143 | + |
| 144 | + result = invoke(["server", "clone", SRC_ID, "--name", "dst", |
| 145 | + "--boot-from-image", "--boot-from-volume"]) |
| 146 | + |
| 147 | + assert result.exit_code != 0 |
| 148 | + assert "mutually exclusive" in result.output |
| 149 | + assert "body" not in state["posted"] |
0 commit comments