Skip to content

Commit

Permalink
Merge pull request #82 from Shopify/raises_msg
Browse files Browse the repository at this point in the history
Fix assertions on exception messages
  • Loading branch information
KnVerey authored Apr 21, 2017
2 parents ad00e3d + cfdf14d commit 6f41fb6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
17 changes: 10 additions & 7 deletions test/integration/kubernetes_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ def test_deploying_to_protected_namespace_with_override_does_not_prune
end

def test_refuses_deploy_to_protected_namespace_with_override_if_pruning_enabled
assert_raises(KubernetesDeploy::FatalDeploymentError, /Refusing to deploy to protected namespace with pruning/) do
expected_msg = /Refusing to deploy to protected namespace .* pruning enabled/
assert_raises_message(KubernetesDeploy::FatalDeploymentError, expected_msg) do
KubernetesDeploy::Runner.stub_const(:PROTECTED_NAMESPACES, [@namespace]) do
deploy_fixtures("hello-cloud", allow_protected_ns: true, prune: true)
end
end
end

def test_refuses_deploy_to_protected_namespace_without_override
assert_raises(KubernetesDeploy::FatalDeploymentError, /Refusing to deploy to protected namespace/) do
assert_raises_message(KubernetesDeploy::FatalDeploymentError, /Refusing to deploy to protected namespace/) do
KubernetesDeploy::Runner.stub_const(:PROTECTED_NAMESPACES, [@namespace]) do
deploy_fixtures("hello-cloud", prune: false)
end
Expand Down Expand Up @@ -99,13 +100,13 @@ def test_success_with_unrecognized_resource_type
end

def test_invalid_yaml_fails_fast
assert_raises(KubernetesDeploy::FatalDeploymentError, /Template \S+yaml-error\S+ cannot be parsed/) do
assert_raises_message(KubernetesDeploy::FatalDeploymentError, /Template yaml-error.yml cannot be parsed/) do
deploy_dir(fixture_path("invalid"))
end
end

def test_invalid_k8s_spec_that_is_valid_yaml_fails_fast
assert_raises(KubernetesDeploy::FatalDeploymentError, /Dry run failed for template configmap-data/) do
assert_raises_message(KubernetesDeploy::FatalDeploymentError, /Dry run failed for template configmap-data/) do
deploy_fixtures("hello-cloud", subset: ["configmap-data.yml"]) do |fixtures|
configmap = fixtures["configmap-data.yml"]["ConfigMap"].first
configmap["metadata"]["myKey"] = "uhOh"
Expand Down Expand Up @@ -133,7 +134,7 @@ def test_invalid_k8s_spec_that_is_valid_yaml_fails_on_apply
}
end
end
assert_match(/The following command failed/, err.to_s)
assert_match(/The following command failed: apply/, err.to_s)
assert_match(/Error from server \(BadRequest\): error when creating/, err.to_s)
assert_logs_match(/Inspecting the file mentioned in the error message/)
end
Expand Down Expand Up @@ -161,7 +162,8 @@ def test_dead_pods_in_old_replicaset_are_ignored
end

def test_bad_container_image_on_run_once_halts_and_fails_deploy
assert_raises(KubernetesDeploy::FatalDeploymentError, /1 priority resources failed to deploy/) do
expected_msg = %r{The following priority resources failed to deploy: Pod\/unmanaged-pod}
assert_raises_message(KubernetesDeploy::FatalDeploymentError, expected_msg) do
deploy_fixtures("hello-cloud") do |fixtures|
pod = fixtures["unmanaged-pod.yml.erb"]["Pod"].first
pod["spec"]["activeDeadlineSeconds"] = 1
Expand All @@ -177,7 +179,8 @@ def test_bad_container_image_on_run_once_halts_and_fails_deploy
end

def test_wait_false_still_waits_for_priority_resources
assert_raises(KubernetesDeploy::FatalDeploymentError, /1 priority resources failed to deploy/) do
expected_msg = %r{The following priority resources failed to deploy: Pod\/unmanaged-pod}
assert_raises_message(KubernetesDeploy::FatalDeploymentError, expected_msg) do
deploy_fixtures("hello-cloud") do |fixtures|
pod = fixtures["unmanaged-pod.yml.erb"]["Pod"].first
pod["spec"]["activeDeadlineSeconds"] = 1
Expand Down
18 changes: 18 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ def assert_logs_match(regexp, times = nil)
assert_match regexp, @logger_stream.read
end
end

alias_method :orig_assert_raises, :assert_raises
def assert_raises(*exp, message: nil)
case exp.last
when String, Regexp
raise ArgumentError, "Please use the kwarg message instead of the positional one.\n"\
"To assert the message exception, use `assert_raises_message` or the return value of `assert_raises`"
else
exp += Array(message)
orig_assert_raises(*exp) { yield }
end
end

def assert_raises_message(exception_class, exception_message)
exception = orig_assert_raises(exception_class) { yield }
assert_match exception_message, exception.message
exception
end
end

class IntegrationTest < KubernetesDeploy::TestCase
Expand Down
4 changes: 2 additions & 2 deletions test/unit/kubernetes-deploy/resource_watcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

class ResourceWatcherTest < KubernetesDeploy::TestCase
def test_requires_enumerable
err = assert_raises(ArgumentError) do
expected_msg = "ResourceWatcher expects Enumerable collection, got `Object` instead"
assert_raises_message(ArgumentError, expected_msg) do
KubernetesDeploy::ResourceWatcher.new(Object.new)
end
assert_equal "ResourceWatcher expects Enumerable collection, got `Object` instead", err.to_s

KubernetesDeploy::ResourceWatcher.new([])
end
Expand Down

0 comments on commit 6f41fb6

Please sign in to comment.