Language conformance tests#
Language conformance tests (often just conformance tests) are a class of integration test that assert various properties that should hold true across all language runtimes, in essence providing a specification that language runtimes must conform to. They are structured as follows:
The “program under test” is expressed using PCL. The program can specify resources and functions supplied by one or more test resource providers with fixed, known implementations, as well as provider-agnostic entities such as stack outputs.
A set of assertions are made about the state of the resources, functions, outputs, etc. in the program before and after it has been executed. These assertions should hold true regardless of the language being used to define and execute the program.
For each test run and language, then:
If the test requires one or more providers, SDKs are generated from the relevant test providers, exercising SDK generation for the given language.
The PCL program is converted into a program in the target language, exercising program generation for the given language.
The generated program is executed using a compiled language host running as a separate process and the assertions are checked, exercising program execution for the given language.
Generated code is snapshot tested to ensure that it doesn’t change unexpectedly.
Running#
Since the conformance test is implemented separately for each language, running
them depends on the language you want to test. Conformance tests are typically
implemented for a language in a language_test.go file adjacent to the language
host’s main.go file. For example, the NodeJS conformance tests are implemented
in pulumi/pulumi:sdk/nodejs/cmd/pulumi-language-nodejs/language_test.go,
next to the pulumi-language-nodejs’s main.go; Python at
pulumi/pulumi:sdk/python/cmd/pulumi-language-python/language_test.go next
to pulumi-language-python’s main.go, and so on. To run the Python tests,
therefore, you can use a command such as:
go test github.com/pulumi/pulumi/sdk/python/cmd/pulumi-language-python/v3 -count 1
Note
To run the above command from the root of the repository, you will likely need
an up-to-date go.work workspace definition. You can run make work to ensure
this.
Alternatively, you can cd into the relevant directory and use go test ./....
For the NodeJS tests, for instance:
(cd sdk/nodejs/cmd/pulumi-language-nodejs && go test ./... -count 1)
To run a single test, you can use the -run flag:
(cd sdk/python/cmd/pulumi-language-python && go test ./... -count 1 -run TestLanguage/default/l1-output-string)
Test names typically follow the pattern TestLanguage/${VARIANT}/${TEST_NAME},
where TEST_NAME is the name of the test and VARIANT represents
language-specific variations in the conditions under which the test runs. For
instance, Python can generate SDKs in multiple ways, either using setup.py or
pyproject.toml to hold package metadata, or using different input types
(classes-and-dicts or classes). Python also supports both Mypy and Pyright
as typecheckers. Each test is thus currently run three times, with the following
variants:
default, which usessetup.py, Mypy, and the default input types.toml, which usespyproject.toml, Pyright, andclasses-and-dicts.classes, which usessetup.py, Pyright, andclasses.
Python tests can thus be run as TestLanguage/default/${TEST_NAME},
TestLanguage/toml/${TEST_NAME}, and TestLanguage/classes/${TEST_NAME}. For
NodeJS, there are two variants: TypeScript (forceTsc=false) and plain
JavaScript (forceTsc=true; so named because the test setup runs tsc on the
project so it’s runnable as plain JavaScript). Tests are thus named for example
as TestLanguage/forceTsc=true/${TEST_NAME} or
TestLanguage/forceTsc=false/${TEST_NAME}.
To update the snapshots for a conformance test, run with the PULUMI_ACCEPT
environment variable set to a truthy value:
PULUMI_ACCEPT=1 go test github.com/pulumi/pulumi/sdk/python/cmd/pulumi-language-python/v3 -count 1
Debugging#
The simplest way to debug one or more conformance tests is probably to use
VSCode with the sample pulumi/pulumi:.vscode/launch.json.example file
copied to your .vscode/launch.json directory:
cp .vscode/launch.json.example .vscode/launch.json
With this, upon opening VSCode inside your pulumi worktree, you should have
options to run and debug the conformance tests for each language runtime. When
selecting an option, you’ll be prompted for a test name. Here you should enter a
valid name or pattern, such as l1-output-string or l1-* to run all level 1
tests. You can then set and hit breakpoints that will be triggered directly by
the test process, which will include anything the language host runs itself –
code generation, for instance.
Architecture#
Test providers are defined in pulumi/pulumi:pkg/testing/pulumi-test-language/providers. PCL programs for language conformance tests are defined in pulumi/pulumi:pkg/testing/pulumi-test-language/testdata. pulumi/pulumi:pkg/testing/pulumi-test-language/tests.go then references these programs and defines the assertions to be made about each. Tests are categorised as follows:
L1 tests are those which do not exercise provider code paths and use only the most basic of features (e.g. stack outputs).
L2 tests are those which do exercise provider code paths and use things such as custom resources, function invocations, and so on.
L3 tests exercise features that require more advanced language support such as first-class functions –
applyis a good example of this.Provider tests exercise language code that acts as a provider, that is
ConstructandCallimplementations.
Each language defines a test function (the language test host) responsible for
running its conformance test suite, if it implements one. For core languages
whose runtimes are written in Go, this typically lives in a language_test.go
file next to the relevant language host executable code – see for example
pulumi/pulumi:sdk/nodejs/cmd/pulumi-language-nodejs/language_test.go for
NodeJS/TypeScript and
pulumi/pulumi:sdk/python/cmd/pulumi-language-python/language_test.go for
Python. This function works as follows:
The relevant language runtime (e.g.
pulumi-language-nodejsfor NodeJS) is booted up as a separate process.The
pulumi-test-languageexecutable is booted up, with the language runtime’s gRPC server address as a parameter. The test language executable itself exposes a gRPC server which allows clients to e.g. retrieve a list of tests (GetLanguageTests) and execute a test (RunLanguageTest).In preparation for test execution, the language test host retrieves the list of tests from the
pulumi-test-languageserver. It 📞 Packs the core SDK (e.g.@pulumi/pulumiin TypeScript/NodeJS).For each test:
SDKs required by the test are generated by calling the language host’s 📞 GeneratePackage method. The generated code is written to a temporary directory where it is 📞 Packed for use in the test.
The 📞 GenerateProject method is invoked to convert the test’s PCL code into a program in the target language. Dependencies are installed with 📞 InstallDependencies and the test program is 📞 Run.
Assertions are verified and the next test is processed until there are no more remaining.
The lifecycle of a language conformance test suite#
Meta tests#
This module contains a number of _test.go files. These are tests of the
conformance test system itself. The actual conformance tests are all defined in
tests.go.