-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Zubenko
committed
Jul 17, 2023
1 parent
5b01fe7
commit 95d94ac
Showing
2 changed files
with
118 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import os | ||
import tempfile | ||
from collections.abc import AsyncIterator, Iterator | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
import aiohttp | ||
import aiohttp.web | ||
import pytest | ||
|
||
from platform_secrets.config import KubeClientAuthType | ||
from platform_secrets.kube_client import KubeClient | ||
|
||
from .conftest import create_local_app_server | ||
|
||
|
||
class TestKubeClientTokenUpdater: | ||
@pytest.fixture | ||
async def kube_app(self) -> aiohttp.web.Application: | ||
async def _get_secrets(request: aiohttp.web.Request) -> aiohttp.web.Response: | ||
auth = request.headers["Authorization"] | ||
token = auth.split()[-1] | ||
app["token"]["value"] = token | ||
return aiohttp.web.json_response({"kind": "SecretList", "items": []}) | ||
|
||
app = aiohttp.web.Application() | ||
app["token"] = {"value": ""} | ||
app.router.add_routes( | ||
[aiohttp.web.get("/api/v1/namespaces/default/secrets", _get_secrets)] | ||
) | ||
return app | ||
|
||
@pytest.fixture | ||
async def kube_server( | ||
self, kube_app: aiohttp.web.Application, unused_tcp_port_factory: Any | ||
) -> AsyncIterator[str]: | ||
async with create_local_app_server( | ||
kube_app, port=unused_tcp_port_factory() | ||
) as address: | ||
yield f"http://{address.host}:{address.port}" | ||
|
||
@pytest.fixture | ||
def kube_token_path(self) -> Iterator[str]: | ||
_, path = tempfile.mkstemp() | ||
Path(path).write_text("token-1") | ||
yield path | ||
os.remove(path) | ||
|
||
@pytest.fixture | ||
async def kube_client( | ||
self, kube_server: str, kube_token_path: str | ||
) -> AsyncIterator[KubeClient]: | ||
async with KubeClient( | ||
base_url=kube_server, | ||
namespace="default", | ||
auth_type=KubeClientAuthType.TOKEN, | ||
token_path=kube_token_path, | ||
token_update_interval_s=1, | ||
) as client: | ||
yield client | ||
|
||
async def test_token_periodically_updated( | ||
self, | ||
kube_app: aiohttp.web.Application, | ||
kube_client: KubeClient, | ||
kube_token_path: str, | ||
) -> None: | ||
await kube_client.list_secrets() | ||
assert kube_app["token"]["value"] == "token-1" | ||
|
||
Path(kube_token_path).write_text("token-2") | ||
await asyncio.sleep(2) | ||
|
||
await kube_client.list_secrets() | ||
assert kube_app["token"]["value"] == "token-2" |