From d684fb06f5fa33afe9672ab87d11376c1b051503 Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Wed, 30 Nov 2022 11:26:33 -0800 Subject: [PATCH] Python integration test --- tests/integration/integration_python_test.go | 12 +++ .../python/.gitignore | 5 + .../python/Pulumi.yaml | 3 + .../python/__main__.py | 93 +++++++++++++++++++ .../python/requirements.txt | 0 5 files changed, 113 insertions(+) create mode 100644 tests/integration/resource_refs_get_resource/python/.gitignore create mode 100644 tests/integration/resource_refs_get_resource/python/Pulumi.yaml create mode 100644 tests/integration/resource_refs_get_resource/python/__main__.py create mode 100644 tests/integration/resource_refs_get_resource/python/requirements.txt diff --git a/tests/integration/integration_python_test.go b/tests/integration/integration_python_test.go index 0567df22e700..7c4cb134fda2 100644 --- a/tests/integration/integration_python_test.go +++ b/tests/integration/integration_python_test.go @@ -994,3 +994,15 @@ func TestAboutPython(t *testing.T) { func TestConstructOutputValuesPython(t *testing.T) { testConstructOutputValues(t, "python", filepath.Join("..", "..", "sdk", "python", "env", "src")) } + +// TestResourceRefsGetResourcePython tests that invoking the built-in 'pulumi:pulumi:getResource' function +// returns resource references for any resource reference in a resource's state. +func TestResourceRefsGetResourcePython(t *testing.T) { + integration.ProgramTest(t, &integration.ProgramTestOptions{ + Dir: filepath.Join("resource_refs_get_resource", "python"), + Dependencies: []string{ + filepath.Join("..", "..", "sdk", "python", "env", "src"), + }, + Quick: true, + }) +} diff --git a/tests/integration/resource_refs_get_resource/python/.gitignore b/tests/integration/resource_refs_get_resource/python/.gitignore new file mode 100644 index 000000000000..3f47d8e79266 --- /dev/null +++ b/tests/integration/resource_refs_get_resource/python/.gitignore @@ -0,0 +1,5 @@ +*.pyc +/.pulumi/ +/dist/ +/*.egg-info +venv/ diff --git a/tests/integration/resource_refs_get_resource/python/Pulumi.yaml b/tests/integration/resource_refs_get_resource/python/Pulumi.yaml new file mode 100644 index 000000000000..07eca2067c65 --- /dev/null +++ b/tests/integration/resource_refs_get_resource/python/Pulumi.yaml @@ -0,0 +1,3 @@ +name: resource_refs_get_resource_py +description: A program that ensures invoking 'pulumi:pulumi:getResource' returns resource refs +runtime: python diff --git a/tests/integration/resource_refs_get_resource/python/__main__.py b/tests/integration/resource_refs_get_resource/python/__main__.py new file mode 100644 index 000000000000..0f48f93d1390 --- /dev/null +++ b/tests/integration/resource_refs_get_resource/python/__main__.py @@ -0,0 +1,93 @@ +# Copyright 2016-2022, Pulumi Corporation. All rights reserved. + +from typing import Optional + +import pulumi + + +class Child(pulumi.ComponentResource): + @pulumi.input_type + class ChildArgs: + pass + + def __init__( + self, + resource_name: str, + message: Optional[str] = None, + opts: Optional[pulumi.ResourceOptions] = None, + ): + props = Container.ContainerArgs.__new__(Container.ContainerArgs) + props.__dict__["message"] = message + super().__init__("test:index:Child", resource_name, props, opts) + if opts and opts.urn: + return + self.register_outputs({ "message": message }) + + @property + @pulumi.getter + def message(self) -> pulumi.Output[str]: + return pulumi.get(self, "message") + + +class Container(pulumi.ComponentResource): + @pulumi.input_type + class ContainerArgs: + pass + + def __init__( + self, + resource_name: str, + child: Optional[Child] = None, + opts: Optional[pulumi.ResourceOptions] = None, + ): + props = Container.ContainerArgs.__new__(Container.ContainerArgs) + props.__dict__["child"] = child + super().__init__("test:index:Container", resource_name, props, opts) + if opts and opts.urn: + return + self.register_outputs({ "child": child }) + + @property + @pulumi.getter + def child(self) -> pulumi.Output[Child]: + return pulumi.get(self, "child") + + +class Module(pulumi.runtime.ResourceModule): + def version(self): + return "0.0.1" + + def construct(self, name: str, typ: str, urn: str) -> pulumi.Resource: + if typ == "test:index:Child": + return Child(name, opts=pulumi.ResourceOptions(urn=urn)) + else: + raise Exception(f"unknown resource type {typ}") + + +pulumi.runtime.register_resource_module("test", "index", Module()) + + +child = Child("mychild", message="hello world!") +container = Container("mycontainer", child=child) + + +def assert_equal(args): + expected_urn = args["expected_urn"] + actual_urn = args["actual_urn"] + actual_message = args["actual_message"] + assert expected_urn == actual_urn, \ + f"expected urn '{expected_urn}' not equal to actual urn '{actual_urn}'" + assert "hello world!" == actual_message, \ + f"expected message 'hello world!' not equal to actual message '{actual_message}'" + + +def round_trip(urn: str): + round_tripped_container = Container("mycontainer", opts=pulumi.ResourceOptions(urn=urn)) + pulumi.Output.all( + expected_urn=child.urn, + actual_urn=round_tripped_container.child.urn, + actual_message=round_tripped_container.child.message, + ).apply(assert_equal) + + +container.urn.apply(round_trip) diff --git a/tests/integration/resource_refs_get_resource/python/requirements.txt b/tests/integration/resource_refs_get_resource/python/requirements.txt new file mode 100644 index 000000000000..e69de29bb2d1