-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
196 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
MockResource = Struct.new(:id, :hits_to_complete, :status) do | ||
def debug_message(*) | ||
@debug_message | ||
end | ||
|
||
def sync(_cache) | ||
@hits ||= 0 | ||
@hits += 1 | ||
end | ||
|
||
def after_sync | ||
end | ||
|
||
def type | ||
"MockResource" | ||
end | ||
alias_method :kubectl_resource_type, :type | ||
|
||
def pretty_timeout_type | ||
end | ||
|
||
def deploy_method | ||
:apply | ||
end | ||
|
||
def file_path | ||
"/dev/null" | ||
end | ||
|
||
def deploy_started_at=(_) | ||
end | ||
|
||
def sensitive_template_content? | ||
true | ||
end | ||
|
||
def global? | ||
false | ||
end | ||
|
||
def deploy_succeeded? | ||
status == "success" && hits_complete? | ||
end | ||
|
||
def deploy_failed? | ||
status == "failed" && hits_complete? | ||
end | ||
|
||
def deploy_timed_out? | ||
status == "timeout" && hits_complete? | ||
end | ||
|
||
def timeout | ||
hits_to_complete | ||
end | ||
|
||
def sync_debug_info(_) | ||
@debug_message = "Something went wrong" | ||
end | ||
|
||
def pretty_status | ||
"#{id} #{status} (#{@hits} hits)" | ||
end | ||
|
||
def report_status_to_statsd(watch_time) | ||
end | ||
|
||
private | ||
|
||
def hits_complete? | ||
@hits ||= 0 | ||
@hits >= hits_to_complete | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
require 'kubernetes-deploy/resource_deployer' | ||
|
||
class ResourceDeployerTest < KubernetesDeploy::TestCase | ||
def test_deploy_prune_builds_whitelist | ||
whitelist_kind = "fake_kind" | ||
resource = build_mock_resource | ||
KubernetesDeploy::Kubectl.any_instance.expects(:run).with do |*args| | ||
args.include?("--prune-whitelist=#{whitelist_kind}") | ||
end.returns(["", "", stub(success?: true)]) | ||
resource_deployer(kubectl_times: 0, prune_whitelist: [whitelist_kind]).deploy!([resource], false, true) | ||
end | ||
|
||
def test_deploy_no_prune_doesnt_prune | ||
whitelist_kind = "fake_kind" | ||
resource = build_mock_resource | ||
KubernetesDeploy::Kubectl.any_instance.expects(:run).with do |*args| | ||
!args.include?("--prune-whitelist=#{whitelist_kind}") | ||
end.returns(["", "", stub(success?: true)]) | ||
resource_deployer(kubectl_times: 0, prune_whitelist: [whitelist_kind]).deploy!([resource], false, false) | ||
end | ||
|
||
def test_deploy_verify_false_message | ||
resource = build_mock_resource | ||
resource_deployer.deploy!([resource], false, false) | ||
logger.print_summary(:done) # Force logger to flush | ||
assert_logs_match_all(["Deploy result verification is disabled for this deploy."]) | ||
end | ||
|
||
def test_deploy_time_out_error | ||
resource = build_mock_resource(final_status: "timeout") | ||
watcher = mock("ResourceWatcher") | ||
watcher.expects(:run).returns(true) | ||
KubernetesDeploy::ResourceWatcher.expects(:new).returns(watcher) | ||
assert_raises(KubernetesDeploy::DeploymentTimeoutError) do | ||
resource_deployer.deploy!([resource], true, false) | ||
end | ||
end | ||
|
||
def test_deploy_verify_false_no_timeout | ||
resource = build_mock_resource(final_status: "timeout") | ||
resource_deployer.deploy!([resource], false, false) | ||
logger.print_summary(:done) # Force logger to flush | ||
assert_logs_match_all(["Deploy result verification is disabled for this deploy."]) | ||
end | ||
|
||
def test_deploy_failure_error | ||
resource = build_mock_resource(final_status: "failure") | ||
watcher = mock("ResourceWatcher") | ||
watcher.expects(:run).returns(true) | ||
KubernetesDeploy::ResourceWatcher.expects(:new).returns(watcher) | ||
assert_raises(KubernetesDeploy::FatalDeploymentError) do | ||
resource_deployer.deploy!([resource], true, false) | ||
end | ||
end | ||
|
||
def test_deploy_verify_false_no_failure_error | ||
resource = build_mock_resource(final_status: "failure") | ||
resource_deployer.deploy!([resource], false, false) | ||
logger.print_summary(:done) # Force logger to flush | ||
assert_logs_match_all(["Deploy result verification is disabled for this deploy."]) | ||
end | ||
|
||
def test_predeploy_priority_resources_respectes_pre_deploy_list | ||
kind = "MockResource" | ||
resource = build_mock_resource | ||
watcher = mock("ResourceWatcher") | ||
watcher.expects(:run).returns(true) | ||
KubernetesDeploy::ResourceWatcher.expects(:new).returns(watcher) | ||
priority_list = [kind] | ||
resource_deployer.predeploy_priority_resources([resource], priority_list) | ||
end | ||
|
||
def test_predeploy_priority_resources_respectes_empty_pre_deploy_list | ||
resource = build_mock_resource | ||
priority_list = [] | ||
KubernetesDeploy::ResourceWatcher.expects(:new).times(0) | ||
resource_deployer(kubectl_times: 0).predeploy_priority_resources([resource], priority_list) | ||
end | ||
|
||
private | ||
|
||
def resource_deployer(kubectl_times: 2, prune_whitelist: []) | ||
unless kubectl_times == 0 | ||
KubernetesDeploy::Kubectl.expects(:new).returns(build_runless_kubectl).times(kubectl_times) | ||
end | ||
@deployer = KubernetesDeploy::ResourceDeployer.new(current_sha: 'test-sha', | ||
statsd_tags: [], task_config: task_config, prune_whitelist: prune_whitelist, | ||
max_watch_seconds: 60, selector: nil) | ||
end | ||
|
||
def build_mock_resource(final_status: "success", hits_to_complete: 0, name: "web-pod") | ||
MockResource.new(name, hits_to_complete, final_status) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters