|
| 1 | +# Copyright IBM Corp. 2025, 2026 |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +"""Unit tests for the workspace Variables resource. |
| 5 | +
|
| 6 | +The headline cases here are regression tests for hashicorp/python-tfe#181: |
| 7 | +``variables.list()`` infinite-looping on workspaces with >= 100 variables |
| 8 | +because the ``/vars`` (and ``/all-vars``) endpoints are not paginated. |
| 9 | +""" |
| 10 | + |
| 11 | +from unittest.mock import Mock |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from pytfe._http import HTTPTransport |
| 16 | +from pytfe.errors import ERR_INVALID_WORKSPACE_ID |
| 17 | +from pytfe.models.variable import Variable |
| 18 | +from pytfe.resources._base import _Service |
| 19 | +from pytfe.resources.variable import Variables |
| 20 | + |
| 21 | + |
| 22 | +def _vars_payload(count: int) -> dict: |
| 23 | + """A /vars-style response: full set in one page, no meta.pagination.""" |
| 24 | + return { |
| 25 | + "data": [ |
| 26 | + { |
| 27 | + "id": f"var-{i}", |
| 28 | + "type": "vars", |
| 29 | + "attributes": {"key": f"key-{i}", "value": f"value-{i}"}, |
| 30 | + } |
| 31 | + for i in range(count) |
| 32 | + ] |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +class TestVariablesList: |
| 37 | + """Tests for Variables.list / list_all.""" |
| 38 | + |
| 39 | + def setup_method(self): |
| 40 | + self.mock_transport = Mock(spec=HTTPTransport) |
| 41 | + self.variables = Variables(self.mock_transport) |
| 42 | + self.workspace_id = "ws-test123" |
| 43 | + |
| 44 | + def test_list_validations(self): |
| 45 | + with pytest.raises(ValueError, match=ERR_INVALID_WORKSPACE_ID): |
| 46 | + list(self.variables.list("")) |
| 47 | + with pytest.raises(ValueError, match=ERR_INVALID_WORKSPACE_ID): |
| 48 | + list(self.variables.list(None)) |
| 49 | + |
| 50 | + def test_list_all_validations(self): |
| 51 | + with pytest.raises(ValueError, match=ERR_INVALID_WORKSPACE_ID): |
| 52 | + list(self.variables.list_all("")) |
| 53 | + with pytest.raises(ValueError, match=ERR_INVALID_WORKSPACE_ID): |
| 54 | + list(self.variables.list_all(None)) |
| 55 | + |
| 56 | + def test_list_does_not_paginate_with_100_plus_variables(self): |
| 57 | + """Regression for #181: a workspace with >= 100 vars must not loop. |
| 58 | +
|
| 59 | + The endpoint ignores page params and re-returns the full set, so the |
| 60 | + old pagination heuristic looped forever. We now issue exactly one |
| 61 | + request and return each variable once. |
| 62 | + """ |
| 63 | + response = Mock() |
| 64 | + response.json.return_value = _vars_payload(150) |
| 65 | + self.mock_transport.request.return_value = response |
| 66 | + |
| 67 | + result = list(self.variables.list(self.workspace_id)) |
| 68 | + |
| 69 | + # Exactly one request — no follow-up page fetches. |
| 70 | + self.mock_transport.request.assert_called_once_with( |
| 71 | + "GET", |
| 72 | + f"/api/v2/workspaces/{self.workspace_id}/vars", |
| 73 | + params={}, |
| 74 | + ) |
| 75 | + # All 150 variables, no duplication. |
| 76 | + assert len(result) == 150 |
| 77 | + assert all(isinstance(v, Variable) for v in result) |
| 78 | + assert [v.id for v in result] == [f"var-{i}" for i in range(150)] |
| 79 | + |
| 80 | + def test_list_all_does_not_paginate_with_100_plus_variables(self): |
| 81 | + response = Mock() |
| 82 | + response.json.return_value = _vars_payload(120) |
| 83 | + self.mock_transport.request.return_value = response |
| 84 | + |
| 85 | + result = list(self.variables.list_all(self.workspace_id)) |
| 86 | + |
| 87 | + self.mock_transport.request.assert_called_once_with( |
| 88 | + "GET", |
| 89 | + f"/api/v2/workspaces/{self.workspace_id}/all-vars", |
| 90 | + params={}, |
| 91 | + ) |
| 92 | + assert len(result) == 120 |
| 93 | + assert [v.id for v in result] == [f"var-{i}" for i in range(120)] |
| 94 | + |
| 95 | + def test_list_exactly_100_variables(self): |
| 96 | + """The exactly-page-size boundary also looped under the old logic.""" |
| 97 | + response = Mock() |
| 98 | + response.json.return_value = _vars_payload(100) |
| 99 | + self.mock_transport.request.return_value = response |
| 100 | + |
| 101 | + result = list(self.variables.list(self.workspace_id)) |
| 102 | + |
| 103 | + self.mock_transport.request.assert_called_once() |
| 104 | + assert len(result) == 100 |
| 105 | + |
| 106 | + def test_list_empty(self): |
| 107 | + response = Mock() |
| 108 | + response.json.return_value = {"data": []} |
| 109 | + self.mock_transport.request.return_value = response |
| 110 | + |
| 111 | + result = list(self.variables.list(self.workspace_id)) |
| 112 | + |
| 113 | + self.mock_transport.request.assert_called_once() |
| 114 | + assert result == [] |
| 115 | + |
| 116 | + |
| 117 | +class TestListSafetyNet: |
| 118 | + """The generic _list safety net protects any non-paginated endpoint.""" |
| 119 | + |
| 120 | + def test_full_page_without_metadata_is_treated_as_single_page(self): |
| 121 | + """A paginated (paginated=True) call that gets a full page with no |
| 122 | + meta.pagination must stop after one request rather than loop.""" |
| 123 | + transport = Mock(spec=HTTPTransport) |
| 124 | + response = Mock() |
| 125 | + response.json.return_value = _vars_payload(100) # full page, no meta |
| 126 | + transport.request.return_value = response |
| 127 | + |
| 128 | + service = _Service(transport) |
| 129 | + result = list(service._list("/api/v2/some/unpaginated", params={})) |
| 130 | + |
| 131 | + transport.request.assert_called_once() |
| 132 | + assert len(result) == 100 |
0 commit comments