Allow pulumi.export calls from unit tests (#4670)

The previous attempt to allow this didn't actually allow it, so this is
take two. As part of the previous attempt, I thought after tweaking the
test I had observed the test failing, and then succeeding after making
the product changes, but I must have been mistaken.

It turns out that our existing mocks tests weren't running at all
because of a missing `__init__.py` file. Once the missing `__init__.py`
is added, the tests run, but other tests ("test mode" tests) fail
because the code that creates the mocks and resources will run during
test discovery, and setting the mocks modifies global state.

To address the test issue, I've moved the mocks tests into their own
`test_with_mocks` package that can be run separately from other tests.

And addressed the original issue, by creating a root Stack resource if
one isn't already present when the mocks are set.
This commit is contained in:
Justin Van Patten 2020-05-20 09:54:40 -07:00 committed by GitHub
parent bfe99021a1
commit af3d4b890d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 20 additions and 17 deletions

View file

@ -12,6 +12,9 @@ CHANGELOG
- Fix a Regression in .NET unit testing.
[#4656](https://github.com/pulumi/pulumi/pull/4656)
- Allow `pulumi.export` calls from Python unit tests.
[#4670](https://github.com/pulumi/pulumi/pull/4670)
## 2.2.1 (2020-05-13)
- Add new brew target to fix homebrew builds
[#4633](https://github.com/pulumi/pulumi/pull/4633)
@ -82,9 +85,6 @@ CHANGELOG
- Increase the MaxCallRecvMsgSize for all RPC calls.
[#4455](https://github.com/pulumi/pulumi/pull/4455)
- Allow `pulumi.export` calls from Python unit tests.
[#4518](https://github.com/pulumi/pulumi/pull/4518)
## 2.0.0 (2020-04-16)
- CLI behavior change. Commands in non-interactive mode (i.e. when `pulumi` has its output piped to

View file

@ -41,6 +41,7 @@ install:: install_package install_plugin
test_fast:: build
pipenv run pip install ./env/src
pipenv run python -m unittest discover -s lib/test -v
pipenv run python -m unittest discover -s lib/test_with_mocks -v
test_all:: test_fast

View file

@ -23,10 +23,10 @@ from typing import Optional, Awaitable, Tuple, Union, Any, TYPE_CHECKING
import grpc
from google.protobuf import empty_pb2
from . import rpc
from .settings import Settings, configure, get_stack, get_project
from .settings import Settings, configure, get_stack, get_project, get_root_resource
from .sync_await import _sync_await
from ..runtime.proto import engine_pb2, engine_pb2_grpc, provider_pb2, resource_pb2, resource_pb2_grpc
from ..runtime.stack import _run_test
from ..runtime.stack import Stack, run_pulumi_func
from ..output import Output
if TYPE_CHECKING:
@ -35,7 +35,7 @@ if TYPE_CHECKING:
def test(fn):
def wrapper(*args, **kwargs):
_sync_await(_run_test(lambda: _sync_await(Output.from_input(fn(*args, **kwargs)).future())))
_sync_await(run_pulumi_func(lambda: _sync_await(Output.from_input(fn(*args, **kwargs)).future())))
return wrapper
class Mocks(ABC):
@ -159,3 +159,7 @@ def set_mocks(mocks: Mocks,
dry_run=preview,
test_mode_enabled=True)
configure(settings)
# Ensure a new root stack resource has been initialized.
if get_root_resource() is None:
Stack(lambda: None)

View file

@ -80,15 +80,6 @@ async def run_in_stack(func: Callable):
"""
await run_pulumi_func(lambda: Stack(func))
async def _run_test(func: Callable):
"""
Run the given function after ensuring a new stack resource has been initialized. This is meant
for internal runtime use only.
"""
if get_root_resource() is None:
Stack(lambda: None)
await run_pulumi_func(func)
@known_types.stack
class Stack(ComponentResource):

View file

@ -1 +0,0 @@
venv

View file

@ -12,4 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import resources
"""
The Pulumi SDK test package for testing with mocks.
"""
# The mocks tests are in their own `test_with_mocks` package so they can be run separately
# from other tests in the `test` package. Otherwise, if the mocks tests were in the same
# package as other tests, the code that initializes the mocks and test resources would run
# during test discovery and impact the behavior of other tests due to modifying global state.

View file

@ -30,5 +30,6 @@ myinstance = Instance("instance",
value=pulumi.Output.secret("secret_value"))
invoke_result = do_invoke()
pulumi.export("hello", "world")
pulumi.export("outprop", mycomponent.outprop)
pulumi.export("public_ip", myinstance.public_ip)