pulumi/sdk/python/lib/test/langhost
Zaid Ajaj 360ffc7884
[sdk/python] Always unwrap value from InvokeResult in invoke_async (#17349)
In #17275 I changed `invoke_async` to return `InvokeResult`, however
this variant of the invoke should always unwrap `value` from the
`InvokeResult`.

Reverted a changed test in #17275 thinking it was syntactic, however it
was not since it unwrapped the value in the test whereas the unwrapping
should have happened in the function implementation of `invoke_async`
```diff
async def await_invoke_async():
   value = await invoke_async(
        "test:index:MyFunction", props={"value": 41, "value2": get_value2()}
   )
   return value["value"]
```

Fixes #17347
2024-09-24 10:24:51 +00:00
..
aliases Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
asset Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
chained_failure Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
component_dependencies Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
component_provider_resolution Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
component_resource_list_of_providers Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
component_resource_single_provider Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
config Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
delete_before_replace Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
empty Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
first_class_provider Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
first_class_provider_invoke Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
first_class_provider_unknown Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
future_failure Handful of mypy fixes in the python test code (#16091) 2024-05-02 17:46:52 +00:00
future_input Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
ignore_changes Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
inherit_defaults Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
inheritance_translation Fixing a few pylint errors in the python test folder (#16962) 2024-08-14 08:26:34 +00:00
inheritance_types Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
input_type_mismatch Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
input_values_for_outputs Fixing a few pylint errors in the python test folder (#16962) 2024-08-14 08:26:34 +00:00
invalid_property_dependency Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
invoke [sdk/python] Always unwrap value from InvokeResult in invoke_async (#17349) 2024-09-24 10:24:51 +00:00
invoke_empty_return Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
invoke_future Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
invoke_types Start mypy linting lib/test (#16040) 2024-04-30 12:54:13 +00:00
large_resource Handful of mypy fixes in the python test code (#16091) 2024-05-02 17:46:52 +00:00
marshal_failure Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
one_complex_resource Handful of mypy fixes in the python test code (#16091) 2024-05-02 17:46:52 +00:00
one_resource Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
output_all Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
output_nested Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
output_property_dependencies Fixing a few pylint errors in the python test folder (#16962) 2024-08-14 08:26:34 +00:00
outputs_future Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
preview Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
property_dependencies Handful of mypy fixes in the python test code (#16091) 2024-05-02 17:46:52 +00:00
property_renaming Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
protect Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
read Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
remote_component_dependencies [sdk/*] Add support for resource source positions 2023-07-13 16:46:04 -07:00
remote_component_providers [sdk/*] Add support for resource source positions 2023-07-13 16:46:04 -07:00
replace_on_changes Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
resource_op_bad_inputs Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
resource_op_fail Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
resource_thens Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
runtime_settings Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
source_position Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
stack_output Fixing a few pylint errors in the python test folder (#16962) 2024-08-14 08:26:34 +00:00
ten_resources Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
types Start mypy linting lib/test (#16040) 2024-04-30 12:54:13 +00:00
versions Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00
README.md Fix an issue where we fail to rethrow exceptions arising from failed resource operations 2018-06-29 16:32:39 -07:00
__init__.py Python Language Host Tests (#1577) 2018-06-29 14:08:58 -07:00
util.py Use black to format lib/test (#16028) 2024-04-23 08:29:58 +00:00

README.md

Python Language Host Tests

The tests in this directory test the language host directly by posing as the engine and running programs in the same context that they would be run by the CLI. Programs run by these tests can create resources, read resource, invoke data sources, and generally do anything that a Pulumi program can do.

Language host tests provide a program to be run and an implementation of the LanghostTest class, which provides implementations for the four resource monitor endpoints that the language host speaks to:

  • invoke, for invoking data sources,
  • read_resource, for reading existing resources,
  • register_resource, for creating new resources,
  • register_resource_outputs, for registering outputs on component resources

Classes deriving from LanghostTest can override any of these methods to provide custom test functionality. This is commonly used to perform assertions or place the language host in unexpected situations.

Adding a new test

To add a new language host test, you can:

  1. Create a new directory in this directory with the name of your test
  2. Place an __init__.py and __main__.py in this directory. __init__.py convinces Python that this directory is a module, while __main__.py indicates to Python that this module is runnable.
  3. Write your Pulumi program in __main__.py. If you want to do assertions, use the assert keyword to do so.
  4. Add a test file, which can have any name. In this test file you'll want to provide a subclass of LanghostTest that drives your test. An example minimal test would be something like this:
from os import path
from ..util import LanghostTest


class EmptyTests(LanghostTest):
    def test_empty(self):
        self.run_test(
            program=path.join(self.base_path(), "empty"), # If your test is in the empty/ subdirectory
            expected_resource_count=0)                    # Assert there are 0 resource registrations

Your class can have any number of test_* methods in them. Language host tests are launched by invoking the run_test method inherited from LanghostTest. run_test accepts the following keyword arguments:

  • project - The name of the project that will be exposed to the running program
  • stack - The name of the stack that will be exposed to the running program
  • program - A path to the program to be run, relative to the working directory.
  • pwd - The working directory to use.
  • args - Command-line arguments to pass to the program.
  • config - A dict of configuration keys and values to pass to the program.
  • expected_resource_count - The number of resources this test is expected to register.
  • expected_error - If non-None, the exact error text that is expected to be received.
  • expected_stderr_contains - If non-None, asserts that the given substring exists in stderr

If expected_error is None, the expected error is asserted to be the empty string.

Note that your test method must begin with test_*, since this is how Python discovers what tests to run.

One additional thing to note is that this test harness explicitly ignores the registration of the top-level Stack resource, pulumi:pulumi:Stack, because it is annoying to write tests around. All expected resource counts do not count this resource as a registration and overridden resource monitor methods will never see a registration for pulumi:pulumi:Stack.