Skip to content

Commit

Permalink
rebasish
Browse files Browse the repository at this point in the history
  • Loading branch information
timothysmith0609 committed Jan 29, 2024
1 parent 95f2a6d commit 924f6c0
Show file tree
Hide file tree
Showing 7 changed files with 1,550 additions and 1 deletion.
191 changes: 191 additions & 0 deletions test/integration/global_deploy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# frozen_string_literal: true
require 'integration_test_helper'

class GlobalDeployTest < Krane::IntegrationTest
def test_global_deploy_task_success
assert_deploy_success(deploy_global_fixtures('globals'))

assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector app=krane,test=",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/#{storage_class_name}",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/#{storage_class_name}\s+Not Found},
"Phase 3: Deploying all resources",
"Deploying resources:",
%r{StorageClass\/#{storage_class_name} \(timeout: 300s\)},
%r{PriorityClass/#{priority_class_name} \(timeout: 300s\)},
"Don't know how to monitor resources of type StorageClass.",
%r{Assuming StorageClass\/#{storage_class_name} deployed successfully.},
%r{Successfully deployed in [\d.]+s: PriorityClass/#{priority_class_name}, StorageClass\/#{storage_class_name}},
"Result: SUCCESS",
"Successfully deployed 2 resources",
"Successful resources",
"StorageClass/#{storage_class_name}",
"PriorityClass/#{priority_class_name}",
])
end

def test_global_deploy_task_success_timeout
assert_deploy_failure(deploy_global_fixtures('globals', global_timeout: 0), :timed_out)

assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector app=krane,test=",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/#{storage_class_name}",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/#{storage_class_name}\s+Not Found},
"Phase 3: Deploying all resources",
"Deploying resources:",
"Result: TIMED OUT",
"Timed out waiting for 2 resources to deploy",
%r{StorageClass\/#{storage_class_name}: GLOBAL WATCH TIMEOUT \(0 seconds\)},
"If you expected it to take longer than 0 seconds for your deploy to roll out, increase --global-timeout.",
])
end

def test_global_deploy_task_success_verify_false
assert_deploy_success(deploy_global_fixtures('globals', verify_result: false))

assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector app=krane,test=",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/#{storage_class_name}",
" - PriorityClass/#{priority_class_name}",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/#{storage_class_name}\s+Not Found},
%r{PriorityClass/#{priority_class_name}\s+Not Found},
"Phase 3: Deploying all resources",
"Deploying resources:",
%r{StorageClass\/#{storage_class_name} \(timeout: 300s\)},
%r{PriorityClass/#{priority_class_name} \(timeout: 300s\)},
"Result: SUCCESS",
"Deployed 2 resources",
"Deploy result verification is disabled for this deploy.",
"This means the desired changes were communicated to Kubernetes, but the"\
" deploy did not make sure they actually succeeded.",
])
end

def test_global_deploy_task_empty_selector_validation_failure
assert_deploy_failure(deploy_global_fixtures('globals', selector: false))
assert_logs_match_all([
"Phase 1: Initializing deploy",
"Result: FAILURE",
"Configuration invalid",
"- Selector is required",
])
end

def test_global_deploy_task_success_selector
selector = "extraSelector=krane2"
assert_deploy_success(deploy_global_fixtures('globals', selector: selector))

assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector #{selector}", # there are more, but this one should be listed first
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/#{storage_class_name}",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/#{storage_class_name}\s+Not Found},
"Phase 3: Deploying all resources",
"Deploying resources:",
%r{PriorityClass/#{priority_class_name} \(timeout: 300s\)},
%r{StorageClass\/#{storage_class_name} \(timeout: 300s\)},
"Don't know how to monitor resources of type StorageClass.",
"Assuming StorageClass/#{storage_class_name} deployed successfully.",
/Successfully deployed in [\d.]+s/,
"Result: SUCCESS",
"Successfully deployed 2 resources",
"Successful resources",
"StorageClass/#{storage_class_name}",
"PriorityClass/#{priority_class_name}",
])
end

def test_global_deploy_task_failure
result = deploy_global_fixtures('globals') do |fixtures|
fixtures.dig("storage_classes.yml", "StorageClass").first["metadata"]['badField'] = "true"
end
assert_deploy_failure(result)

assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector app=krane,test=",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/#{storage_class_name}",
"Result: FAILURE",
"Invalid template",
])
end

def test_global_deploy_prune_success
selector = 'extraSelector=prune1'
assert_deploy_success(deploy_global_fixtures('globals', selector: selector))
reset_logger
assert_deploy_success(deploy_global_fixtures('globals', subset: 'storage_classes.yml', selector: selector))

assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector #{selector}",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/#{storage_class_name}",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/#{storage_class_name}\s+Exists},
"Phase 3: Deploying all resources",
%r{Deploying StorageClass\/#{storage_class_name} \(timeout: 300s\)},
"The following resources were pruned: priorityclass.scheduling.k8s.io/#{priority_class_name}",
%r{Successfully deployed in [\d.]+s: StorageClass\/#{storage_class_name}},
"Result: SUCCESS",
"Pruned 1 resource and successfully deployed 1 resource",
"Successful resources",
"StorageClass/#{storage_class_name}",
])
end

def test_no_prune_global_deploy_success
selector = 'extraSelector=prune2'
assert_deploy_success(deploy_global_fixtures('globals', selector: selector))
reset_logger
assert_deploy_success(deploy_global_fixtures('globals', subset: 'storage_classes.yml',
selector: selector, prune: false))
assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector #{selector}",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/#{storage_class_name}",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/#{storage_class_name}\s+Exists},
"Phase 3: Deploying all resources",
%r{Deploying StorageClass\/#{storage_class_name} \(timeout: 300s\)},
%r{Successfully deployed in [\d.]+s: StorageClass\/#{storage_class_name}},
"Result: SUCCESS",
"Successfully deployed 1 resource",
"Successful resources",
"StorageClass/#{storage_class_name}",
])
refute_logs_match(/[pP]runed/)
refute_logs_match(priority_class_name)
assert_deploy_success(deploy_global_fixtures('globals', selector: selector))
end

private

def storage_class_name
@storage_class_name ||= add_unique_prefix_for_test("testing-storage-class")
end

def priority_class_name
@priority_class_name ||= add_unique_prefix_for_test("testing-priority-class")
end
end
147 changes: 147 additions & 0 deletions test/integration/krane_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# frozen_string_literal: true
require 'integration_test_helper'

class KraneTest < Krane::IntegrationTest
def test_restart_black_box
assert_deploy_success(
deploy_fixtures("hello-cloud", subset: ["configmap-data.yml", "web.yml.erb", "redis.yml"], render_erb: true)
)
refute(fetch_restarted_at("web"), "restart annotation on fresh deployment")
refute(fetch_restarted_at("redis"), "restart annotation on fresh deployment")

out, err, status = krane_black_box("restart", "#{@namespace} #{KubeclientHelper::TEST_CONTEXT} --deployments web")
assert_empty(out)
assert_match("Success", err)
assert_predicate(status, :success?)

assert(fetch_restarted_at("web"), "no restart annotation is present after the restart")
refute(fetch_restarted_at("redis"), "restart annotation is present")
end

def test_run_success_black_box
assert_empty(task_runner_pods)
assert_deploy_success(deploy_fixtures("hello-cloud", subset: ["template-runner.yml", "configmap-data.yml"]))
template = "hello-cloud-template-runner"
out, err, status = krane_black_box("run",
"#{@namespace} #{KubeclientHelper::TEST_CONTEXT} --template #{template} --command ls --arguments '-a /'")
assert_match("Success", err)
assert_empty(out)
assert_predicate(status, :success?)
assert_equal(1, task_runner_pods.count)
end

def test_render_black_box
# Ordered so that template requiring bindings comes first
paths = ["test/fixtures/test-partials/partials/independent-configmap.yml.erb",
"test/fixtures/hello-cloud/web.yml.erb"]
data_value = rand(10_000).to_s
bindings = "data=#{data_value}"
test_sha = rand(10_000).to_s

out, err, status = krane_black_box("render",
"-f #{paths.join(' ')} --bindings #{bindings} --current-sha #{test_sha}")

assert_predicate(status, :success?)
assert_match("Success", err)
assert_match(test_sha, out)
assert_match(data_value, out)

out, err, status = krane_black_box("render", "-f #{paths.join(' ')} --current-sha #{test_sha}")

refute_predicate(status, :success?)
assert_match("FAILURE", err)
refute_match(data_value, out)
assert_match(test_sha, out)
end

def test_render_black_box_stdin
file = "test/fixtures/branched/web.yml.erb"
template = File.read(file)
data_value = rand(10_000).to_s
bindings = "branch=#{data_value}"
test_sha = rand(10_000).to_s

out, err, status = krane_black_box("render",
"--filenames - --bindings #{bindings} --current-sha #{test_sha}", stdin: template)

assert_predicate(status, :success?)
assert_match("Success", err)
assert_match(test_sha, out)
assert_match(data_value, out)
end

def test_render_current_sha_cant_be_blank
paths = ["test/fixtures/test-partials/partials/independent-configmap.yml.erb"]
_, err, status = krane_black_box("render", "-f #{paths.join(' ')} --current-sha")
refute_predicate(status, :success?)
assert_match("FAILURE", err)
assert_match("current-sha is optional but can not be blank", err)
end

def test_deploy_black_box_success
setup_template_dir("hello-cloud", subset: %w(bare_replica_set.yml)) do |target_dir|
flags = "-f #{target_dir}"
out, err, status = krane_black_box("deploy", "#{@namespace} #{KubeclientHelper::TEST_CONTEXT} #{flags}")
assert_empty(out)
assert_match("Success", err)
assert_predicate(status, :success?)
end
end

def test_deploy_black_box_success_stdin
render_out, _, render_status = krane_black_box("render",
"-f #{fixture_path('hello-cloud')} --bindings deployment_id=1 current_sha=123")
assert_predicate(render_status, :success?)

out, err, status = krane_black_box("deploy", "#{@namespace} #{KubeclientHelper::TEST_CONTEXT} --filenames -",
stdin: render_out)
assert_empty(out)
assert_match("Success", err)
assert_predicate(status, :success?)
end

def test_deploy_black_box_failure
out, err, status = krane_black_box("deploy", "#{@namespace} #{KubeclientHelper::TEST_CONTEXT}")
assert_empty(out)
assert_match("--filenames must be set and not empty", err)
refute_predicate(status, :success?)
assert_equal(status.exitstatus, 1)
end

def test_deploy_black_box_timeout
setup_template_dir("hello-cloud", subset: %w(bare_replica_set.yml)) do |target_dir|
flags = "-f #{target_dir} --global-timeout=0.1s"
out, err, status = krane_black_box("deploy", "#{@namespace} #{KubeclientHelper::TEST_CONTEXT} #{flags}")
assert_empty(out)
assert_match("TIMED OUT", err)
refute_predicate(status, :success?)
assert_equal(status.exitstatus, 70)
end
end

# test_global_deploy_black_box_success and test_global_deploy_black_box_timeout
# are in test/integration-serial/serial_deploy_test.rb because they modify
# global state

def test_global_deploy_black_box_failure
setup_template_dir("resource-quota") do |target_dir|
flags = "-f #{target_dir} --selector app=krane"
out, err, status = krane_black_box("global-deploy", "#{KubeclientHelper::TEST_CONTEXT} #{flags}")
assert_empty(out)
assert_match("FAILURE", err)
refute_predicate(status, :success?)
assert_equal(status.exitstatus, 1)
end
end

private

def task_runner_pods
kubeclient.get_pods(namespace: @namespace, label_selector: "name=runner,app=hello-cloud")
end

def fetch_restarted_at(deployment_name)
deployment = apps_v1_kubeclient.get_deployment(deployment_name, @namespace)
deployment.spec.template.metadata.annotations&.dig(Krane::RestartTask::RESTART_TRIGGER_ANNOTATION)
end
end
Loading

0 comments on commit 924f6c0

Please sign in to comment.