Skip to content

Commit

Permalink
udpate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dturn committed Nov 7, 2019
1 parent 50dec22 commit 7c85a9f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 51 deletions.
4 changes: 2 additions & 2 deletions lib/krane/global_deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def run!(verify_result: true, prune: true)

def deploy!(resources, verify_result, prune)
resource_deployer = ResourceDeployer.new(task_config: @task_config,
prune_whitelist: prune_whitelist, max_watch_seconds: @max_watch_seconds,
prune_whitelist: prune_whitelist, max_watch_seconds: @global_timeout,
selector: @selector, statsd_tags: statsd_tags)
resource_deployer.deploy!(resources, verify_result, prune)
end
Expand Down Expand Up @@ -199,7 +199,7 @@ def kubeclient_builder
def prune_whitelist
black_list = %w(Namespace Node)
cluster_resource_discoverer.prunable_resources.select do |gvk|
global_resource_kinds.any? { |g| gvk.include?(g) } && black_list.none? { |b| gvk.include?(b) }
global_kinds.any? { |g| gvk.include?(g) } && black_list.none? { |b| gvk.include?(b) }
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/globals/priority_class.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: scheduling.k8s.io/v1beta1
description: Used for testing global deploys and pruning.
kind: PriorityClass
metadata:
name: testing-priority-class
labels:
app: krane
value: 20
12 changes: 7 additions & 5 deletions test/helpers/fixture_deploy_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def deploy_global_fixtures(set, subset: nil, **args)
fixtures = load_fixtures(set, subset)
raise "Cannot deploy empty template set" if fixtures.empty?
args[:selector] ||= "test=#{@namespace}"
namespace_globals(fixtures)
namespace_globals(fixtures, args[:selector])

yield fixtures if block_given?

Expand Down Expand Up @@ -104,7 +104,7 @@ def deploy_dirs_without_profiling(dirs, wait: true, allow_protected_ns: false, p
)
end

def global_deploy_dirs_without_profiling(dirs, verify_result: true, prune: false,
def global_deploy_dirs_without_profiling(dirs, clean_up: true, verify_result: true, prune: true,
global_timeout: 300, selector:)
deploy = Krane::GlobalDeployTask.new(
context: KubeclientHelper::TEST_CONTEXT,
Expand All @@ -118,7 +118,7 @@ def global_deploy_dirs_without_profiling(dirs, verify_result: true, prune: false
prune: prune
)
ensure
delete_globals(Array(dirs))
delete_globals(Array(dirs)) if clean_up
end

# Deploys all fixtures in the given directories via KubernetesDeploy::DeployTask
Expand Down Expand Up @@ -180,13 +180,15 @@ def build_kubectl(log_failure_by_default: true, timeout: '5s')
log_failure_by_default: log_failure_by_default, default_timeout: timeout)
end

def namespace_globals(fixtures)
def namespace_globals(fixtures, selector)
selector_key, selector_value = selector.split("=")
fixtures.each do |_, kinds_map|
kinds_map.each do |_, resources|
resources.each do |resource|
resource["metadata"]["name"] = (resource["metadata"]["name"] + @namespace)[0..63]
resource["metadata"]["name"] += "0" if resource["metadata"]["name"].end_with?("-")
resource["metadata"]["labels"] ||= {}
resource["metadata"]["labels"]["test"] = @namespace
resource["metadata"]["labels"][selector_key] = selector_value
end
end
end
Expand Down
33 changes: 0 additions & 33 deletions test/integration-serial/serial_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -580,36 +580,6 @@ def test_global_deploy_black_box_timeout
refute_predicate(status, :success?)
assert_equal(status.exitstatus, 70)
end

def test_global_deploy_task
KubernetesDeploy::FormattedLogger.expects(:build).returns(@logger)
global_deploy = ::Krane::GlobalDeployTask.new(
context: task_config.context,
template_paths: [fixture_path('globals')],
max_watch_seconds: 300,
selector: KubernetesDeploy::LabelSelector.parse('app=krane')
)
global_deploy.run!(verify_result: true, prune: true)
assert_logs_match_all([
"Result: SUCCESS",
"Successfully deployed 2 resource",
"Successful resources",
"StorageClass/testing-storage-class",
"PriorityClass/testing-priority-class",
])
ensure
storage_v1_kubeclient.delete_storage_class("testing-storage-class")
scheduling_v1beta1_kubeclient.delete_priority_class("testing-priority-class")
end

def test_global_deploy_black_box_success
setup_template_dir("globals") 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("Success", err)
assert_predicate(status, :success?)
end
ensure
build_kubectl.run("delete", "-f", fixture_path("globals"), use_namespace: false, log_failure: false)
end
Expand All @@ -631,12 +601,9 @@ def test_global_deploy_validation_catches_namespaced_cr
])
ensure
wait_for_all_crd_deletion
build_kubectl.run("delete", "-f", fixture_path("globals"), use_namespace: false, log_failure: false)

end

def test_global_deploy_prune_black_box_success
pc_name = "testing-priority-class"
setup_template_dir("globals") do |target_dir|
flags = "-f #{target_dir} --selector app=krane"
out, err, status = krane_black_box("global-deploy", "#{KubeclientHelper::TEST_CONTEXT} #{flags}")
Expand Down
82 changes: 71 additions & 11 deletions test/integration/global_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ def test_global_deploy_task_success
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/testing-storage-class[\w-]+\s+Not Found},
"Phase 3: Deploying all resources",
%r{Deploying StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
"Deploying resources:",
%r{StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
%r{PriorityClass/testing-priority-class[\w-]+ \(timeout: 300s\)},
"Don't know how to monitor resources of type StorageClass.",
%r{Assuming StorageClass\/testing-storage-class[\w-]+ deployed successfully.},
%r{Successfully deployed in [\d.]+s: StorageClass\/testing-storage-class},
%r{Successfully deployed in [\d.]+s: PriorityClass/testing-priority-class[\w-]+, StorageClass\/testing-storage-},
"Result: SUCCESS",
"Successfully deployed 1 resource",
"Successfully deployed 2 resource",
"Successful resources",
"StorageClass/testing-storage-class",
"PriorityClass/testing-priority-class",
])
end

Expand All @@ -37,9 +40,9 @@ def test_global_deploy_task_success_timeout
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/testing-storage-class[\w-]+\s+Not Found},
"Phase 3: Deploying all resources",
%r{Deploying StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
"Deploying resources:",
"Result: TIMED OUT",
"Timed out waiting for 1 resource to deploy",
"Timed out waiting for 2 resources to deploy",
%r{StorageClass\/testing-storage-class[\w-]+: GLOBAL WATCH TIMEOUT \(0 seconds\)},
"If you expected it to take longer than 0 seconds for your deploy to roll out, increase --max-watch-seconds.",
])
Expand All @@ -54,12 +57,16 @@ def test_global_deploy_task_success_verify_false
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/testing-storage-class",
" - PriorityClass/testing-priority-class",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/testing-storage-class[\w-]+\s+Not Found},
%r{PriorityClass/testing-priority-class[\w-]+\s+Not Found},
"Phase 3: Deploying all resources",
%r{Deploying StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
"Deploying resources:",
%r{StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
%r{PriorityClass/testing-priority-class[\w-]+ \(timeout: 300s\)},
"Result: SUCCESS",
"Deployed 1 resource",
"Deployed 2 resource",
"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.",
Expand All @@ -77,7 +84,7 @@ def test_global_deploy_task_empty_selector_validation_failure
end

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

assert_logs_match_all([
Expand All @@ -89,14 +96,17 @@ def test_global_deploy_task_success_selector
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/testing-storage-class[\w-]+\s+Not Found},
"Phase 3: Deploying all resources",
%r{Deploying StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
"Deploying resources:",
%r{PriorityClass/testing-priority-class[\w-]+ \(timeout: 300s\)},
%r{StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
"Don't know how to monitor resources of type StorageClass.",
%r{Assuming StorageClass\/testing-storage-class[\w-]+ deployed successfully.},
%r{Successfully deployed in [\d.]+s: StorageClass\/testing-storage-class},
/Successfully deployed in [\d.]+s/,
"Result: SUCCESS",
"Successfully deployed 1 resource",
"Successfully deployed 2 resource",
"Successful resources",
"StorageClass/testing-storage-class",
"PriorityClass/testing-priority-class",
])
end

Expand All @@ -116,4 +126,54 @@ def test_global_deploy_task_failure
"Template validation failed",
])
end

def test_global_deploy_prune_black_box_success
assert_deploy_success(deploy_global_fixtures('globals', clean_up: false, selector: 'test=prune1'))
reset_logger
assert_deploy_success(deploy_global_fixtures('globals', subset: 'storage_classes.yml', selector: 'test=prune1'))
assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector test=prune1",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/testing-storage-class",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/testing-storage-class[\w-]+\s+Exists},
"Phase 3: Deploying all resources",
%r{Deploying StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
"Don't know how to monitor resources of type StorageClass.",
%r{Assuming StorageClass\/testing-storage-class[\w-]+ deployed successfully.},
%r{Successfully deployed in [\d.]+s: StorageClass\/testing-storage-class},
"Result: SUCCESS",
"Pruned 1 resource and successfully deployed 1 resource",
"Successful resources",
"StorageClass/testing-storage-class",
])
end

def test_global_deploy_prune_black_box_success_no_prune
assert_deploy_success(deploy_global_fixtures('globals', clean_up: false, selector: 'test=prune2'))
reset_logger
assert_deploy_success(deploy_global_fixtures('globals', subset: 'storage_classes.yml',
selector: "test=prune2", prune: false))
assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector test=prune2",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/testing-storage-class",
"Phase 2: Checking initial resource statuses",
%r{StorageClass\/testing-storage-class[\w-]+\s+Exists},
"Phase 3: Deploying all resources",
%r{Deploying StorageClass\/testing-storage-class[\w-]+ \(timeout: 300s\)},
"Don't know how to monitor resources of type StorageClass.",
%r{Assuming StorageClass\/testing-storage-class[\w-]+ deployed successfully.},
%r{Successfully deployed in [\d.]+s: StorageClass\/testing-storage-class},
"Result: SUCCESS",
"Successfully deployed 1 resource",
"Successful resources",
"StorageClass/testing-storage-class",
])
assert_deploy_success(deploy_global_fixtures('globals', selector: 'test=prune2'))
end
end
9 changes: 9 additions & 0 deletions test/unit/cluster_resource_discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ def test_global_resource_kinds_success
end
end

def test_prunable_resources
crd = mocked_cluster_resource_discovery(full_response)
kinds = crd.prunable_resources
assert_equal(kinds.length, 15)
%w(scheduling.k8s.io/v1/PriorityClass storage.k8s.io/v1/StorageClass).each do |kind|
assert_includes(kinds, kind)
end
end

private

def mocked_cluster_resource_discovery(response, success: true)
Expand Down

0 comments on commit 7c85a9f

Please sign in to comment.