Skip to content

Commit

Permalink
Add test for CRD with status subresource and without (#121)
Browse files Browse the repository at this point in the history
* Add test for CRD with status subresource and without

* Add more tests

* Improve Subresources field

* Remove EmptyField

---------

Co-authored-by: herodes1991 <eloymaillo@MacBook-Pro-3.local>
  • Loading branch information
herodes1991 and herodes1991 authored Oct 25, 2023
1 parent d70e215 commit 7df6622
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
5 changes: 0 additions & 5 deletions k8s/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,3 @@ def typename(i):
@property
def default_value(self):
return copy.copy(self._default_value)


class EmptyField(Field):
def _as_dict(self, value):
return {}
13 changes: 11 additions & 2 deletions k8s/models/apiextensions_v1_custom_resource_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from .common import ObjectMeta
from ..base import Model, SelfModel
from ..fields import Field, ListField, JSONField, EmptyField
from ..fields import Field, ListField, JSONField


class ExternalDocumentation(Model):
Expand Down Expand Up @@ -149,9 +149,18 @@ class CustomResourceSubresourceScale(Model):
statusReplicasPath = Field(str)


class CustomResourceSubresourceStatusEnabled(Model):
def as_dict(self):
return {}


class CustomResourceSubresourceStatusDisabled(Model):
pass


class CustomResourceSubresources(Model):
scale = Field(CustomResourceSubresourceScale)
status = EmptyField(dict)
status = Field(CustomResourceSubresourceStatusDisabled, alt_type=CustomResourceSubresourceStatusEnabled)


class CustomResourceDefinitionVersion(Model):
Expand Down
47 changes: 44 additions & 3 deletions tests/k8s/test_apiextensions_v1_custom_resource_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
from k8s.models.common import ObjectMeta
from k8s.models.apiextensions_v1_custom_resource_definition import (
CustomResourceConversion, CustomResourceDefinition, CustomResourceDefinitionNames,
CustomResourceDefinitionSpec, CustomResourceDefinitionVersion, JSONSchemaProps)
CustomResourceDefinitionSpec, CustomResourceDefinitionVersion, JSONSchemaProps,
CustomResourceSubresourceScale, CustomResourceSubresourceStatusEnabled, CustomResourceSubresources,
CustomResourceSubresourceStatusDisabled)

NAME = "my-name"

Expand Down Expand Up @@ -87,6 +89,34 @@ def test_json_schema_props_any_field(self, put):
crd.save()
pytest.helpers.assert_any_call(put, _uri(NAME), call_params)

@pytest.mark.parametrize("subresources,scale,status,expected_scale,expected_status", [
(False, False, None, False, False),
(True, False, None, False, False),
(True, True, None, True, False),
(True, False, CustomResourceSubresourceStatusEnabled(), False, True),
(True, True, CustomResourceSubresourceStatusEnabled(), True, True),
(True, True, CustomResourceSubresourceStatusDisabled(), True, False),
])
def test_custom_resource_definition_with_status_enabled_version(self, subresources, scale, status,
expected_scale, expected_status):
subr = _create_subresource(status, scale) if subresources else None
crd = _create_default_crd(subr)

result = crd.as_dict()

if subresources and (expected_scale or expected_status):
assert 'subresources' in result['spec']['versions'][0]
if expected_scale:
assert 'scale' in result['spec']['versions'][0]['subresources']
else:
assert 'scale' not in result['spec']['versions'][0]['subresources']
if expected_status:
assert 'status' in result['spec']['versions'][0]['subresources']
else:
assert 'status' not in result['spec']['versions'][0]['subresources']
else:
assert 'subresources' not in result['spec']['versions'][0]


def _json_schema_props_crd_dict(default=None):
return {
Expand Down Expand Up @@ -177,18 +207,29 @@ def _create_mock_response():
return mock_response


def _create_default_crd():
def _create_default_crd(subresources=None):
object_meta = ObjectMeta(name=NAME, labels={"test": "true"})
spec = CustomResourceDefinitionSpec(
conversion=CustomResourceConversion(strategy="None"),
group="example.com",
names=CustomResourceDefinitionNames(kind="MyCustomResource", plural="mycustomresources"),
scope="Namespaced",
versions=[CustomResourceDefinitionVersion(name="v42", served=True)]
versions=[CustomResourceDefinitionVersion(name="v42", served=True, subresources=subresources)],
)
crd = CustomResourceDefinition(metadata=object_meta, spec=spec)
return crd


def _create_subresource(status, has_scale):
scale = _create_subresource_scale() if has_scale else None
return CustomResourceSubresources(status=status, scale=scale)


def _create_subresource_scale():
return CustomResourceSubresourceScale(labelSelectorPath="label",
specReplicasPath="spec",
statusReplicasPath="status")


def _uri(name=""):
return "/apis/apiextensions.k8s.io/v1/customresourcedefinitions/{name}".format(name=name)

0 comments on commit 7df6622

Please sign in to comment.