Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest fails when importing Testing from cdk8s #1075

Open
watkinsmike opened this issue Nov 24, 2022 · 5 comments
Open

Pytest fails when importing Testing from cdk8s #1075

watkinsmike opened this issue Nov 24, 2022 · 5 comments
Labels
bug Something isn't working effort/medium 1 week tops priority/p1 Should be on near term plans

Comments

@watkinsmike
Copy link

watkinsmike commented Nov 24, 2022

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 Report

@watkinsmike watkinsmike added bug Something isn't working needs-triage Priority and effort undetermined yet labels Nov 24, 2022
@watkinsmike
Copy link
Author

watkinsmike commented Nov 24, 2022

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
Copy link
Member

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?

@iliapolo iliapolo added effort/medium 1 week tops priority/p1 Should be on near term plans and removed needs-triage Priority and effort undetermined yet labels Nov 24, 2022
@martimors
Copy link

Confirm this is still a bug.

Copy link
Contributor

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.

@github-actions github-actions bot added the closing-soon Issue/PR will be closing soon if no response is provided label Aug 23, 2024
@watkinsmike
Copy link
Author

watkinsmike commented Aug 23, 2024

@RomainMuller
@iliapolo
Do not close

Confirmed this is still a bug with

python=3.12
cdk8s=2.68.97
pipenv=2024.0.1

@github-actions github-actions bot removed the closing-soon Issue/PR will be closing soon if no response is provided label Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working effort/medium 1 week tops priority/p1 Should be on near term plans
Projects
None yet
Development

No branches or pull requests

3 participants