Merge pull request #1585 from pulumi/swgillespie/python-throw

Fix an issue where we fail to rethrow exceptions arising from failed …
This commit is contained in:
Joe Duffy 2018-06-30 08:47:22 -07:00 committed by GitHub
commit e9ed27792d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 2 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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")

View file

@ -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")

View file

@ -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)