Skip to content

Commit

Permalink
test: Implement kubeconfig-default-token-ttl-minutes API baseline tests
Browse files Browse the repository at this point in the history
* implements 1392 automation, to test
  kubeconfig-default-token-ttl-minutes setting in Harvester
* introduces baseline tests for positive and negative

Issue: #1392
Resolves: test/implement-1392
See also: harvester/harvester#6011
  • Loading branch information
irishgordo authored and khushboo-rancher committed Aug 6, 2024
1 parent dea480b commit e1a7ba8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
4 changes: 3 additions & 1 deletion apiclient/harvester_api/managers/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import Mapping

from harvester_api.models.settings import BaseSettingSpec, BackupTargetSpec, StorageNetworkSpec
from harvester_api.models.settings import BaseSettingSpec, BackupTargetSpec, \
StorageNetworkSpec, KubeconfigDefaultTokenTTLSpec
from .base import BaseManager, merge_dict


Expand All @@ -13,6 +14,7 @@ class SettingManager(BaseManager):
Spec = BaseSettingSpec
BackupTargetSpec = BackupTargetSpec
StorageNetworkSpec = StorageNetworkSpec
KubeconfigDefaultTokenTTLSpec = KubeconfigDefaultTokenTTLSpec

def get(self, name="", *, raw=False):
return self._get(self.PATH_fmt.format(name=name))
Expand Down
21 changes: 21 additions & 0 deletions apiclient/harvester_api/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ def NFS(cls, endpoint):
return cls(dict(type="nfs", endpoint=endpoint))


class KubeconfigDefaultTokenTTLSpec(BaseSettingSpec):
_name = "kubeconfig-default-token-ttl-minutes"
_default = 0

@property
def type(self):
return self.value.get('type')

def clear(self):
self.value = self._default

@classmethod
def from_dict(cls, data):
value = loads(data.get('value', "{}"))
return cls(value)

@classmethod
def TTL(cls, minutes):
return cls(minutes)


class StorageNetworkSpec(BaseSettingSpec):
_name = "storage-network"

Expand Down
34 changes: 34 additions & 0 deletions apiclient/harvester_api/models/settings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,37 @@ class BackupTargetSpec(BaseSettingSpec):
def NFS(cls: Type[BackupTargetSpec], endpoint: str) -> Type[BackupTargetSpec]:
"""
"""

class KubeconfigDefaultTokenTTLSpec(BaseSettingSpec):
"""Kubeconfig Default Token TTL Spec setting
Args:
BaseSettingSpec (_type_): _description_
"""
value: Optional[dict]

def __init__(self, value: Optional[dict] = ...) -> NoReturn:
"""
"""
@property
def type(self) -> str | None:
"""
"""
def clear(self) -> NoReturn:
"""
"""
def to_dict(self) -> dict:
"""
"""

@classmethod
def TTL(cls: Type[KubeconfigDefaultTokenTTLSpec], minutes: int) -> Type[KubeconfigDefaultTokenTTLSpec]:
"""Class method builds token ttl
Args:
cls (Type[KubeconfigDefaultTokenTTLSpec]): spec of kubeconfig default token ttl
minutes (int): minutes must be integer
Returns:
Type[KubeconfigDefaultTokenTTLSpec]: base spec to return with single value int of minutes on token ttl
"""
24 changes: 24 additions & 0 deletions harvester_e2e_tests/apis/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,27 @@ def test_invalid_S3(self, api_client):
f"S3 backup-target should check key/secret/bucket/region"
f"API Status({code}): {data}"
)


@pytest.mark.p0
@pytest.mark.settings
class TestUpdateKubeconfigDefaultToken:
@pytest.mark.skip_version_before("v1.3.2", reason="Issue#5891 fixed after v1.3.2")
def test_invalid_kubeconfig_ttl_min(self, api_client):
KubeconfigTTLMinSpec = api_client.settings.KubeconfigDefaultTokenTTLSpec.TTL
spec = KubeconfigTTLMinSpec(99999999999999)
code, data = api_client.settings.update('kubeconfig-default-token-ttl-minutes', spec)
assert 422 == code, (
f"Kubeconfig Default Token TTL Minutes should not exceed 100yrs\n"
f"API Status({code}): {data}"
)

@pytest.mark.skip_version_before('v1.3.1')
def test_valid_kubeconfig_ttl_min(self, api_client):
KubeconfigTTLMinSpec = api_client.settings.KubeconfigDefaultTokenTTLSpec.TTL
spec = KubeconfigTTLMinSpec(172800)
code, data = api_client.settings.update('kubeconfig-default-token-ttl-minutes', spec)
assert 200 == code, (
f"Kubeconfig Default Token TTL Minutes be allowed to be set for 120 days\n"
f"API Status({code}): {data}"
)

0 comments on commit e1a7ba8

Please sign in to comment.