|
3 | 3 | import pytest
|
4 | 4 | from unittest import mock
|
5 | 5 |
|
6 |
| -from pycloudlib.ibm.instance import IBMInstance, _IBMInstanceType |
| 6 | +from pycloudlib.ibm.instance import IBMInstance, _IBMInstanceType, _Status |
7 | 7 |
|
8 | 8 | SAMPLE_RAW_INSTANCE = {
|
9 | 9 | "id": "ibm1",
|
@@ -39,6 +39,7 @@ def test_type_from_raw_instance(self, client, raw_instance, inst_id, zone_id, in
|
39 | 39 | assert zone_id == inst.zone
|
40 | 40 | assert inst_type == inst._ibm_instance_type
|
41 | 41 |
|
| 42 | + |
42 | 43 | @mock.patch("time.sleep") # bypass the time.sleep call in _attach_floating_ip()
|
43 | 44 | @mock.patch(M_PATH + "VpcV1", autospec=True)
|
44 | 45 | def test_attach_floating_ip(self, m_sleep, client, caplog):
|
@@ -94,3 +95,55 @@ def test_attach_floating_ip(self, m_sleep, client, caplog):
|
94 | 95 |
|
95 | 96 | assert inst._floating_ip["id"] == fi_id
|
96 | 97 | assert inst._floating_ip["name"] == fi_name
|
| 98 | + |
| 99 | + |
| 100 | + @pytest.mark.parametrize( |
| 101 | + "kwargs, expected_timeout", |
| 102 | + [ |
| 103 | + pytest.param({}, 900, id="default_timeout"), |
| 104 | + pytest.param({"start_timeout": 123}, 123, id="custom_timeout"), |
| 105 | + ], |
| 106 | + ) |
| 107 | + @mock.patch(M_PATH + "VpcV1", autospec=True) |
| 108 | + def test_wait_for_instance_start(self, m_client, kwargs, expected_timeout, caplog): |
| 109 | + """Test _wait_for_instance_start calls _wait_for_status correctly.""" |
| 110 | + inst = IBMInstance.from_raw_instance( |
| 111 | + key_pair=None, |
| 112 | + client=m_client, |
| 113 | + instance=SAMPLE_RAW_INSTANCE, |
| 114 | + ) |
| 115 | + |
| 116 | + with mock.patch.object(inst, "_wait_for_status") as m_wait_for_status: |
| 117 | + inst._wait_for_instance_start(**kwargs) |
| 118 | + assert "Waiting for instance to finish provisioning." in caplog.text |
| 119 | + |
| 120 | + m_wait_for_status.assert_called_once_with( |
| 121 | + _Status.RUNNING, |
| 122 | + sleep_seconds=expected_timeout, |
| 123 | + side_effect_fn=inst._check_instance_failed_status, |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | + @mock.patch(M_PATH + "VpcV1", autospec=True) |
| 128 | + def test_wait_passes_kwargs(self, m_client): |
| 129 | + """Test that wait() correctly passes kwargs to internal methods.""" |
| 130 | + inst = IBMInstance.from_raw_instance( |
| 131 | + key_pair=None, |
| 132 | + client=m_client, |
| 133 | + instance=SAMPLE_RAW_INSTANCE, |
| 134 | + ) |
| 135 | + custom_timeout = 123 |
| 136 | + |
| 137 | + with mock.patch.multiple( |
| 138 | + inst, |
| 139 | + _wait_for_instance_start=mock.DEFAULT, |
| 140 | + _wait_for_execute=mock.DEFAULT, |
| 141 | + _wait_for_cloudinit=mock.DEFAULT, |
| 142 | + ) as mocks: |
| 143 | + inst.wait(start_timeout=custom_timeout) |
| 144 | + |
| 145 | + mocks["_wait_for_instance_start"].assert_called_once_with( |
| 146 | + start_timeout=custom_timeout |
| 147 | + ) |
| 148 | + mocks["_wait_for_execute"].assert_called_once() |
| 149 | + mocks["_wait_for_cloudinit"].assert_called_once() |
0 commit comments