|
| 1 | +"""Module for IBM instance tests.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from pycloudlib.ibm.instance import IBMInstance, _IBMInstanceType |
| 7 | + |
| 8 | +SAMPLE_RAW_INSTANCE = { |
| 9 | + "id": "ibm1", |
| 10 | + "primary_network_interface": {"id": "nic1"}, |
| 11 | + "profile": {"name": "metal"}, |
| 12 | + "zone": {"name": "zone1"}, |
| 13 | +} |
| 14 | +M_PATH = "pycloudlib.ibm.instance." |
| 15 | + |
| 16 | + |
| 17 | +class TestIBMInstance: |
| 18 | + @pytest.mark.parametrize( |
| 19 | + "raw_instance,inst_id,zone_id,inst_type", |
| 20 | + ( |
| 21 | + ( |
| 22 | + SAMPLE_RAW_INSTANCE, |
| 23 | + "ibm1", |
| 24 | + "zone1", |
| 25 | + _IBMInstanceType.BARE_METAL_SERVER, |
| 26 | + ), |
| 27 | + ), |
| 28 | + ) |
| 29 | + @mock.patch(M_PATH + "VpcV1", autospec=True) |
| 30 | + def test_type_from_raw_instance( |
| 31 | + self, client, raw_instance, inst_id, zone_id, inst_type |
| 32 | + ): |
| 33 | + """Factory function inits the appropriate IBMInstanceType.""" |
| 34 | + inst = IBMInstance.from_raw_instance( |
| 35 | + key_pair=None, |
| 36 | + client=client, |
| 37 | + instance=raw_instance, |
| 38 | + ) |
| 39 | + assert client == inst._client |
| 40 | + assert inst_id == inst.id |
| 41 | + assert zone_id == inst.zone |
| 42 | + assert inst_type == inst._ibm_instance_type |
| 43 | + |
| 44 | + @mock.patch(M_PATH + "VpcV1", autospec=True) |
| 45 | + def test_attach_floating_ip(self, client, caplog): |
| 46 | + """ |
| 47 | + retry attach_floating_ip until VpcV1 lists non-empty floating_ips.""" |
| 48 | + |
| 49 | + inst = IBMInstance.from_raw_instance( |
| 50 | + key_pair=None, |
| 51 | + client=client, |
| 52 | + instance=SAMPLE_RAW_INSTANCE, |
| 53 | + ) |
| 54 | + metal_floating_ips = mock.Mock( |
| 55 | + get_result=mock.Mock( |
| 56 | + side_effect=[ |
| 57 | + {"floating_ips": []}, |
| 58 | + { |
| 59 | + "floating_ips": [ |
| 60 | + {"id": "floatingid1", "name": "floatingname"} |
| 61 | + ] |
| 62 | + }, |
| 63 | + ] |
| 64 | + ) |
| 65 | + ) |
| 66 | + with mock.patch.object( |
| 67 | + client, |
| 68 | + "list_bare_metal_server_network_interface_floating_ips", |
| 69 | + return_value=metal_floating_ips, |
| 70 | + ), mock.patch.object( |
| 71 | + inst, |
| 72 | + "_choose_from_existing_floating_ips", |
| 73 | + return_value={"name": "ci-ip-match-5", "id": "floatingid1"}, |
| 74 | + ): |
| 75 | + inst.attach_floating_ip(floating_ip_substring="ci-ip-match") |
| 76 | + for expected_log in [ |
| 77 | + "Failed to attach floating ip: ci-ip-match-5.", |
| 78 | + "Successfully attached floating ip: floatingname", |
| 79 | + ]: |
| 80 | + assert expected_log in caplog.text |
| 81 | + assert 2 == metal_floating_ips.get_result.call_count |
0 commit comments