cdk8s-team / cdk8s

Define Kubernetes native apps and abstractions using object-oriented programming
https://cdk8s.io
Apache License 2.0
4.35k stars 293 forks source link

Pytest fails when importing Testing from cdk8s #1075

Open watkinsmike opened 1 year ago

watkinsmike commented 1 year ago

Description of the bug:

pytest throws AttributeError on importing Testing from cdk8s

Reproduction Steps:

❯ cdk8s init python-app
❯ pipenv install -d pytest
❯ echo "from cdk8s import Testing" > tests/test_app.py
❯ pipenv run pytest

Error Log:

ERROR tests/test_app.py::Testing - AttributeError: 'Testing' object has no attribute '__jsii_ref__'. Did you mean: '__jsii_type__'?
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================== 1 error in 0.82s 

Environment:

python_version = "3"

[packages]
constructs = "~=10.1.169"
cdk8s = "~=2.5.54"
cdk8s-plus-25 = "~=2.0.39"

Other:

cls = <class 'cdk8s.Testing'>, args = (), kwargs = {}, inst = <cdk8s.Testing object at 0x1040b0a00>

    def __call__(cls: Type[Any], *args: Any, **kwargs) -> Any:
        inst = super().__call__(*args, **kwargs)

        # Register this instance with our reference map.
>       _reference_map.register_reference(inst)

../../../../.local/share/virtualenvs/cdk8s-pytest-2WqUj06C/lib/python3.10/site-packages/jsii/_runtime.py:112:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <jsii._reference_map._ReferenceMap object at 0x103f39030>, inst = <cdk8s.Testing object at 0x1040b0a00>

    def register(self, inst: Any):
>       self._refs[inst.__jsii_ref__.ref] = inst
E       AttributeError: 'Testing' object has no attribute '__jsii_ref__'. Did you mean: '__jsii_type__'?

../../../../.local/share/virtualenvs/cdk8s-pytest-2WqUj06C/lib/python3.10/site-packages/jsii/_reference_map.py:44: AttributeError 

This is :bug: Bug Report

watkinsmike commented 1 year ago

I was able to get this working by importing cdk8s directly. I am not sure if this is expected behavior, but I have not seen it documented anywhere that it would be. Leaving open until it is triaged.

from constructs import Construct
import cdk8s

from imports import k8s

class MyChart(cdk8s.Chart):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        k8s.KubeService(self, "service", spec=k8s.ServiceSpec())

def test_snapshot():
    app = cdk8s.Testing.app(yaml_output_type=cdk8s.YamlOutputType.FILE_PER_CHART)
    result = cdk8s.Testing.synth(chart=MyChart(app, "test-service"))
    assert result[0] == {
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {"name": "test-service-service-c807c0b4"},
        "spec": {},
    }
collected 1 item

tests/test_app.py::test_snapshot PASSED                                                                                                    [100%]

======================================================================= 1 passed in 0.84s ========================================================================

importing Testing from cdk8s fails with the same test setup:

from cdk8s import Testing, YamlOutputType, Chart
from constructs import Construct

from imports import k8s

class MyChart(Chart):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        k8s.KubeService(self, "service", spec=k8s.ServiceSpec())

def test_snapshot():
    app = Testing.app(yaml_output_type=YamlOutputType.FILE_PER_CHART)
    result = Testing.synth(chart=MyChart(app, "test-service"))
    assert result[0] == {
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {"name": "test-service-service-c807c0b4"},
        "spec": {},
    }
_______________________________________________________ ERROR collecting tests/test_app.py 
ERROR tests/test_app.py::Testing - AttributeError: 'Testing' object has no attribute '__jsii_ref__'. Did you mean: '__jsii_type__'?
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================== 1 error in 1.28s ========================================================================
iliapolo commented 1 year ago

Interesting. Definitely surprising behavior we should investigate. It looks like pytest specific because if I use from cdk8s import Testing in main.py, I am able to synth correctly.

@RomainMuller any idea?

martimors commented 1 year ago

Confirm this is still a bug.

github-actions[bot] commented 2 months ago

This issue has not received any attention in 1 year and will be closed soon. If you want to keep it open, please leave a comment below @mentioning a maintainer.

watkinsmike commented 2 months ago

@RomainMuller @iliapolo Do not close

Confirmed this is still a bug with

python=3.12
cdk8s=2.68.97
pipenv=2024.0.1