diff --git a/service_client/assisted_service_api.py b/service_client/assisted_service_api.py index 317fdd6..5373337 100644 --- a/service_client/assisted_service_api.py +++ b/service_client/assisted_service_api.py @@ -34,12 +34,19 @@ class InventoryClient: def __init__(self, access_token: str): """Initialize the InventoryClient with an access token.""" self.access_token = access_token - self.pull_secret = self._get_pull_secret() + self._pull_secret: Optional[str] = None self.inventory_url = os.environ.get( "INVENTORY_URL", "https://api.openshift.com/api/assisted-install/v2" ) self.client_debug = os.environ.get("CLIENT_DEBUG", "False").lower() == "true" + @property + def pull_secret(self) -> str: + """Lazy-load the pull secret when first accessed.""" + if self._pull_secret is None: + self._pull_secret = self._get_pull_secret() + return self._pull_secret + def _get_pull_secret(self) -> str: url = os.environ.get( "PULL_SECRET_URL", diff --git a/tests/test_assisted_service_api.py b/tests/test_assisted_service_api.py index 4fa6fd1..d4053e0 100644 --- a/tests/test_assisted_service_api.py +++ b/tests/test_assisted_service_api.py @@ -79,12 +79,15 @@ def test_get_pull_secret_success( client = InventoryClient(mock_access_token) + # Access the pull_secret property to trigger lazy loading + pull_secret = client.pull_secret + mock_post.assert_called_once_with( "https://api.openshift.com/api/accounts_mgmt/v1/access_token", headers={"Authorization": f"Bearer {mock_access_token}"}, timeout=30, ) - assert client.pull_secret == "pull-secret-content" + assert pull_secret == "pull-secret-content" @patch("requests.post") def test_get_pull_secret_failure( @@ -93,8 +96,11 @@ def test_get_pull_secret_failure( """Test pull secret retrieval failure.""" mock_post.side_effect = RequestException("Network error") + client = InventoryClient(mock_access_token) + + # Exception should be raised when accessing pull_secret property with pytest.raises(RequestException): - InventoryClient(mock_access_token) + _ = client.pull_secret @patch("requests.post") def test_get_pull_secret_with_custom_url( @@ -107,7 +113,10 @@ def test_get_pull_secret_with_custom_url( mock_post.return_value = mock_response with patch.dict(os.environ, {"PULL_SECRET_URL": custom_url}): - InventoryClient(mock_access_token) + client = InventoryClient(mock_access_token) + + # Access the pull_secret property to trigger lazy loading + _ = client.pull_secret mock_post.assert_called_once_with( custom_url, @@ -356,7 +365,10 @@ async def test_create_cluster_success(self, client: InventoryClient) -> None: openshift_version=version, ) - with patch.object(client, "_installer_api") as mock_installer_api: + with ( + patch.object(client, "_installer_api") as mock_installer_api, + patch.object(client, "_get_pull_secret", return_value="mock-pull-secret"), + ): mock_api = Mock() mock_api.v2_register_cluster.return_value = cluster mock_installer_api.return_value = mock_api @@ -372,7 +384,7 @@ async def test_create_cluster_success(self, client: InventoryClient) -> None: cluster_params = kwargs["new_cluster_params"] assert cluster_params.name == name assert cluster_params.openshift_version == version - assert cluster_params.pull_secret == client.pull_secret + assert cluster_params.pull_secret == "mock-pull-secret" @pytest.mark.asyncio async def test_create_cluster_single_node(self, client: InventoryClient) -> None: @@ -386,7 +398,10 @@ async def test_create_cluster_single_node(self, client: InventoryClient) -> None openshift_version=version, ) - with patch.object(client, "_installer_api") as mock_installer_api: + with ( + patch.object(client, "_installer_api") as mock_installer_api, + patch.object(client, "_get_pull_secret", return_value="mock-pull-secret"), + ): mock_api = Mock() mock_api.v2_register_cluster.return_value = cluster mock_installer_api.return_value = mock_api @@ -410,7 +425,10 @@ async def test_create_infra_env_success(self, client: InventoryClient) -> None: name=name, ) - with patch.object(client, "_installer_api") as mock_installer_api: + with ( + patch.object(client, "_installer_api") as mock_installer_api, + patch.object(client, "_get_pull_secret", return_value="mock-pull-secret"), + ): mock_api = Mock() mock_api.register_infra_env.return_value = infra_env mock_installer_api.return_value = mock_api