diff --git a/sdk/python/lib/pulumi/runtime/resource.py b/sdk/python/lib/pulumi/runtime/resource.py index eb921a10e..5d2c902df 100644 --- a/sdk/python/lib/pulumi/runtime/resource.py +++ b/sdk/python/lib/pulumi/runtime/resource.py @@ -111,6 +111,11 @@ def register_resource(typ, name, custom, props, opts): if exn.code() == grpc.StatusCode.UNAVAILABLE: wait_for_death() + # If the RPC otherwise failed, re-throw an exception with the message details - the contents + # are suitable for user presentation. + raise Exception(exn.details()) + + # Return the URN, ID, and output properties. urn = resp.urn if custom: @@ -151,6 +156,11 @@ def register_resource_outputs(res, outputs): # pylint: disable=no-member if exn.code() == grpc.StatusCode.UNAVAILABLE: wait_for_death() + + # If the RPC otherwise failed, re-throw an exception with the message details - the contents + # are suitable for user presentation. + raise Exception(exn.details()) + # wait_for_death loops forever. This is a hack. diff --git a/sdk/python/lib/test/langhost/README.md b/sdk/python/lib/test/langhost/README.md index 256b81b67..d66db7a88 100644 --- a/sdk/python/lib/test/langhost/README.md +++ b/sdk/python/lib/test/langhost/README.md @@ -57,6 +57,7 @@ keyword arguments: * `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. diff --git a/sdk/python/lib/test/langhost/resource_op_fail/__init__.py b/sdk/python/lib/test/langhost/resource_op_fail/__init__.py new file mode 100644 index 000000000..334a89c27 --- /dev/null +++ b/sdk/python/lib/test/langhost/resource_op_fail/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2016-2018, Pulumi Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/sdk/python/lib/test/langhost/resource_op_fail/__main__.py b/sdk/python/lib/test/langhost/resource_op_fail/__main__.py new file mode 100644 index 000000000..045cc5ec1 --- /dev/null +++ b/sdk/python/lib/test/langhost/resource_op_fail/__main__.py @@ -0,0 +1,22 @@ +# Copyright 2016-2018, Pulumi Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from pulumi import CustomResource + + +class MyResource(CustomResource): + def __init__(self, name): + CustomResource.__init__(self, "test:index:MyResource", name) + + +MyResource("testResource1") diff --git a/sdk/python/lib/test/langhost/resource_op_fail/test_resource_op_fail.py b/sdk/python/lib/test/langhost/resource_op_fail/test_resource_op_fail.py new file mode 100644 index 000000000..5d226533c --- /dev/null +++ b/sdk/python/lib/test/langhost/resource_op_fail/test_resource_op_fail.py @@ -0,0 +1,27 @@ +# Copyright 2016-2018, Pulumi Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from os import path +from ..util import LanghostTest + + +class UnhandledExceptionTest(LanghostTest): + def test_unhandled_exception(self): + self.run_test( + program=path.join(self.base_path(), "resource_op_fail"), + expected_stderr_contains="oh no", + expected_error="Program exited with non-zero exit code: 1") + + def register_resource(self, _ctx, _dry_run, _ty, _name, _resource, + _dependencies): + raise Exception("oh no") diff --git a/sdk/python/lib/test/langhost/util.py b/sdk/python/lib/test/langhost/util.py index 1b76c1a01..1e0077097 100644 --- a/sdk/python/lib/test/langhost/util.py +++ b/sdk/python/lib/test/langhost/util.py @@ -20,12 +20,23 @@ from __future__ import print_function import unittest from collections import namedtuple from concurrent import futures +import logging import subprocess from os import path import grpc from pulumi.runtime import proto, rpc from pulumi.runtime.proto import resource_pb2_grpc, language_pb2_grpc +# gRPC by default logs exceptions to the root `logging` logger. We don't +# want this because it spews garbage to stderr and messes up our beautiful +# test output. +# +# To combat this, we set the root logger handler to be `NullHandler`, which is +# a logger that does nothing. +class NullHandler(logging.Handler): + def emit(self, record): + pass +logging.getLogger().addHandler(NullHandler()) class LanghostMockResourceMonitor(proto.ResourceMonitorServicer): """ @@ -123,7 +134,8 @@ class LanghostTest(unittest.TestCase): args=None, config=None, expected_resource_count=None, - expected_error=None): + expected_error=None, + expected_stderr_contains=None): """ Runs a language host test. The basic flow of a language host test is that a test is launched using the real language host while mocking out the resource @@ -138,6 +150,7 @@ class LanghostTest(unittest.TestCase): :param config: Configuration keys for the program. :param expected_resource_count: The number of resources this program is expected to create. :param expected_error: If present, the expected error that should arise when running this program. + :param expected_stderr_contains: If present, the standard error of the process should contain this string """ # For each test case, we'll do a preview followed by an update. for dryrun in [True, False]: @@ -160,7 +173,7 @@ class LanghostTest(unittest.TestCase): # Tear down the language host process we just spun up. langhost.process.kill() stdout, stderr = langhost.process.communicate() - if stdout or stderr: + if not expected_stderr_contains and (stdout or stderr): print("PREVIEW:" if dryrun else "UPDATE:") print("stdout:", stdout) print("stderr:", stderr) @@ -170,6 +183,11 @@ class LanghostTest(unittest.TestCase): expected = expected_error or "" self.assertEqual(result, expected) + if expected_stderr_contains: + if not expected_stderr_contains in stderr: + print("stderr:", stderr) + self.fail("expected stderr to contain '" + expected_stderr_contains + "'") + if expected_resource_count is not None: self.assertEqual(expected_resource_count, monitor.monitor.reg_count)