From 8c42169e7051ee591b92f4383fb7c452adbcd101 Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Mon, 4 Nov 2019 11:22:56 -0800 Subject: [PATCH] udpate tests --- lib/krane/global_deploy_task.rb | 4 +- test/fixtures/globals/priority_class.yml | 8 ++ test/helpers/fixture_deploy_helper.rb | 12 +-- test/integration-serial/serial_deploy_test.rb | 33 -------- test/integration/global_deploy_test.rb | 82 ++++++++++++++++--- test/unit/cluster_resource_discovery_test.rb | 9 ++ 6 files changed, 97 insertions(+), 51 deletions(-) create mode 100644 test/fixtures/globals/priority_class.yml diff --git a/lib/krane/global_deploy_task.rb b/lib/krane/global_deploy_task.rb index 88d1192a2..3b49d8f98 100644 --- a/lib/krane/global_deploy_task.rb +++ b/lib/krane/global_deploy_task.rb @@ -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 @@ -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 diff --git a/test/fixtures/globals/priority_class.yml b/test/fixtures/globals/priority_class.yml new file mode 100644 index 000000000..30677c429 --- /dev/null +++ b/test/fixtures/globals/priority_class.yml @@ -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 diff --git a/test/helpers/fixture_deploy_helper.rb b/test/helpers/fixture_deploy_helper.rb index b51f87aad..4a157fd84 100644 --- a/test/helpers/fixture_deploy_helper.rb +++ b/test/helpers/fixture_deploy_helper.rb @@ -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? @@ -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, @@ -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 @@ -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 diff --git a/test/integration-serial/serial_deploy_test.rb b/test/integration-serial/serial_deploy_test.rb index 0e34b32c4..3c9f023f5 100644 --- a/test/integration-serial/serial_deploy_test.rb +++ b/test/integration-serial/serial_deploy_test.rb @@ -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 @@ -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}") diff --git a/test/integration/global_deploy_test.rb b/test/integration/global_deploy_test.rb index 9585d8252..0f6164183 100644 --- a/test/integration/global_deploy_test.rb +++ b/test/integration/global_deploy_test.rb @@ -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 @@ -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.", ]) @@ -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.", @@ -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([ @@ -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 @@ -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 diff --git a/test/unit/cluster_resource_discovery_test.rb b/test/unit/cluster_resource_discovery_test.rb index 3a084ec0f..0af611722 100644 --- a/test/unit/cluster_resource_discovery_test.rb +++ b/test/unit/cluster_resource_discovery_test.rb @@ -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)