# Path: k8s/config.py
# class FileTokenSource(object):
#     """Read API token from token_file, exposing it via token(). Calls to token() will re-read the token from file if
#     more than 1 minute has passed since the last read.
# 
#     Intended to support the BoundServiceAccountTokenVolume feature in Kubernetes 1.21 and later.
#     """
#     def __init__(self, token_file, now_func=datetime.now):
#         self._token_file = token_file
#         self._expires_at = datetime(MINYEAR, 1, 1)  # force read on initial call to _refresh_token
#         self._refresh_interval = timedelta(minutes=1)
#         self._token = self._refresh_token(now_func=now_func)  # fail on init if token_file can not be read
# 
#     def token(self, now_func=datetime.now):
#         return self._refresh_token(now_func=now_func)
# 
#     def _refresh_token(self, now_func=datetime.now):
#         now = now_func()
#         if self._expires_at <= now:
#             with open(self._token_file, 'r') as f:
#                 self._token = f.read().strip()
#                 self._expires_at = now + self._refresh_interval
#         return self._token


from datetime import datetime, timedelta
from k8s.config import FileTokenSource
import pytest




# pylint: disable=R0201
class TestFileTokenSource(object):

    @pytest.fixture
    def token_file(self, tmpdir_factory):
        fpath = tmpdir_factory.mktemp(self.__class__.__name__).join('token_file')
        try:
            yield fpath
        finally:
            if fpath.exists():
                fpath.remove()

    def test_token_read(self, token_file):
        token = "secret token"  # nosec
        token_file.write(token)
        token_source = FileTokenSource(token_file=str(token_file))

        assert token_source.token() == token

    def test_token_re_read_after_expiry(self, token_file):
        initial_token = "secret token 1"  # nosec
        token_file.write(initial_token)
        initial_time = datetime(2021, 1, 1, 10, 10)


# Path: k8s/config.py
# class FileTokenSource(object):
#     """Read API token from token_file, exposing it via token(). Calls to token() will re-read the token from file if
#     more than 1 minute has passed since the last read.
# 
#     Intended to support the BoundServiceAccountTokenVolume feature in Kubernetes 1.21 and later.
#     """
#     def __init__(self, token_file, now_func=datetime.now):
#         self._token_file = token_file
#         self._expires_at = datetime(MINYEAR, 1, 1)  # force read on initial call to _refresh_token
#         self._refresh_interval = timedelta(minutes=1)
#         self._token = self._refresh_token(now_func=now_func)  # fail on init if token_file can not be read
# 
#     def token(self, now_func=datetime.now):
#         return self._refresh_token(now_func=now_func)
# 
#     def _refresh_token(self, now_func=datetime.now):
#         now = now_func()
#         if self._expires_at <= now:
#             with open(self._token_file, 'r') as f:
#                 self._token = f.read().strip()
#                 self._expires_at = now + self._refresh_interval
#         return self._token


# Path: tests/k8s/test_config.py
from datetime import datetime, timedelta
from k8s.config import FileTokenSource
import pytest





# pylint: disable=R0201
class TestFileTokenSource(object):

    @pytest.fixture
    def token_file(self, tmpdir_factory):
        fpath = tmpdir_factory.mktemp(self.__class__.__name__).join('token_file')
        try:
            yield fpath
        finally:
            if fpath.exists():
                fpath.remove()

    def test_token_read(self, token_file):
        token = "secret token"  # nosec
        token_file.write(token)
        token_source = FileTokenSource(token_file=str(token_file))

        assert token_source.token() == token

    def test_token_re_read_after_expiry(self, token_file):
        initial_token = "secret token 1"  # nosec
        token_file.write(initial_token)
        initial_time = datetime(2021, 1, 1, 10, 10)


        token_source = FileTokenSource(token_file=str(token_file), now_func=lambda: initial_time)