Skip to content

Commit

Permalink
Test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dturn committed Nov 2, 2019
1 parent 114d52e commit 5994edb
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 40 deletions.
14 changes: 7 additions & 7 deletions test/exe/global_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_deploy_passes_filename
end

def test_deploy_parses_selector
selector = 'name:web'
selector = 'name=web'
set_krane_global_deploy_expectations!(new_args: { selector: selector })
krane_global_deploy!(flags: "--selector #{selector}")
end
Expand All @@ -40,17 +40,17 @@ def test_deploy_parses_selector

def set_krane_global_deploy_expectations!(new_args: {}, run_args: {})
options = default_options(new_args, run_args)
selector_args = options[:new_args][:selector]
selector = mock('LabelSelector')
Krane::LabelSelector.expects(:parse).with(selector_args).returns(selector)
response = mock('GlobalDeployTask')
response.expects(:run!).with(options[:run_args]).returns(true)
Krane::GlobalDeployTask.expects(:new).with(options[:new_args].merge(selector: selector)).returns(response)
Krane::GlobalDeployTask.expects(:new).with do |args|
args.except(:selector) == options[:new_args].except(:selector) &&
args[:selector].to_s == options[:new_args][:selector]
end.returns(response)
end

def krane_global_deploy!(flags: '')
flags += ' -f /tmp' unless flags.include?('-f')
flags += ' --selector name:web' unless flags.include?('--selector')
flags += ' --selector name=web' unless flags.include?('--selector')
krane = Krane::CLI::Krane.new(
[task_config.context],
flags.split
Expand All @@ -64,7 +64,7 @@ def default_options(new_args = {}, run_args = {})
context: task_config.context,
filenames: ['/tmp'],
global_timeout: 300,
selector: 'name:web',
selector: 'name=web',
}.merge(new_args),
run_args: {
verify_result: true,
Expand Down
32 changes: 31 additions & 1 deletion test/helpers/fixture_deploy_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ def deploy_fixtures(set, subset: nil, **args) # extra args are passed through to
def deploy_global_fixtures(set, subset: nil, **args)
fixtures = load_fixtures(set, subset)
raise "Cannot deploy empty template set" if fixtures.empty?
args.merge!(selector: "test=#{@namespace}")
destroyable = namespace_gloabls(fixtures)

yield fixtures if block_given?

success = false
Dir.mktmpdir("fixture_dir") do |target_dir|
write_fixtures_to_dir(fixtures, target_dir)
success = global_deploy_dirs_without_profiling(target_dir, args)
success = global_deploy_dirs_without_profiling(target_dir, **args)
end
success
ensure
delete_globals(destroyable)
end

def deploy_raw_fixtures(set, wait: true, bindings: {}, subset: nil, render_erb: false)
Expand Down Expand Up @@ -175,4 +179,30 @@ def build_kubectl(log_failure_by_default: true, timeout: '5s')
Krane::Kubectl.new(task_config: task_config,
log_failure_by_default: log_failure_by_default, default_timeout: timeout)
end

def namespace_gloabls(fixtures)
fixtures.each_with_object({}) do |(_, kinds_map), hash|
kinds_map.each do |kind, resources|
hash[kind] ||= []
resources.each do |resource|
resource["metadata"]["name"] += @namespace
resource["metadata"]["labels"] ||= {}
resource["metadata"]["labels"]["test"] = @namespace
hash[kind] << resource["metadata"]["name"]
end
end
end
end

def delete_globals(kinds_map)
kind_to_client = { "StorageClass" => storage_v1_kubeclient }
kinds_map&.each do |kind, names|
client = kind_to_client[kind]
raise "No known client for #{kind}" unless client
existing = client.send("get_#{kind.underscore.pluralize}").map { |r| r.dig(:metadata, :name) }
(names & existing).each do |name|
client[kind].send("delete_#{kind.underscore}", name)
end
end
end
end
9 changes: 5 additions & 4 deletions test/helpers/resource_cache_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ def stub_kind_get(kind, items: [], times: 1, use_namespace: true)
)
end

def build_resource_cache(global_kinds: %w(Node FakeNode))
config = task_config(namespace: 'test-ns')
config.stubs(:global_kinds).returns(global_kinds) if global_kinds
Krane::ResourceCache.new(config)
def build_resource_cache(task_config: nil)
task_config ||= task_config(namespace: 'test-ns').tap do |config|
config.stubs(:global_kinds).returns(%w(Node FakeNode))
end
Krane::ResourceCache.new(task_config)
end
end
26 changes: 0 additions & 26 deletions test/integration-serial/serial_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -503,32 +503,6 @@ def test_apply_failure_with_sensitive_resources_hides_template_content
refute_logs_match("kind: Deployment") # content of the sensitive template
end

def test_global_deploy_task
selector = 'app=krane'
deploy_global_fixtures('globals', selector: selector)

assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector #{selector}",
"All required parameters and files are present",
"Discovering resources:",
" - StorageClass/testing-storage-class",
"Phase 2: Checking initial resource statuses",
"StorageClass/testing-storage-class Not Found",
"Phase 3: Deploying all resources",
" Deploying StorageClass/testing-storage-class (timeout: 300s)",
"Don't know how to monitor resources of type StorageClass. "\
"Assuming StorageClass/testing-storage-class deployed successfully.",
"Successfully deployed in 0.3s: StorageClass/testing-storage-class",
"Result: SUCCESS",
"Successfully deployed 1 resource",
"Successful resources",
"StorageClass/testing-storage-class",
])
ensure
storage_v1_kubeclient.delete_storage_class("testing-storage-class")
end

def test_global_deploy_black_box_success
setup_template_dir("globals") do |target_dir|
flags = "-f #{target_dir} --selector app=krane"
Expand Down
28 changes: 28 additions & 0 deletions test/integration/global_deploy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true
require 'integration_test_helper'

class SerialDeployTest < Krane::IntegrationTest
include StatsDHelper
def test_global_deploy_task_success
assert_deploy_success(deploy_global_fixtures('globals'))

assert_logs_match_all([
"Phase 1: Initializing deploy",
"Using resource selector test=",
"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+Not Found},
"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.},
"Successfully deployed in 0.3s: StorageClass/testing-storage-class",
"Result: SUCCESS",
"Successfully deployed 1 resource",
"Successful resources",
"StorageClass/testing-storage-class",
])
end
end
4 changes: 2 additions & 2 deletions test/unit/krane/kubernetes_resource/pod_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_deploy_failed_is_true_for_disappeared_unmanaged_pods
template = build_pod_template
pod = Krane::Pod.new(namespace: 'test', context: 'nope', definition: template,
logger: @logger, deploy_started_at: Time.now.utc)
cache = build_resource_cache(global_kinds: nil)
cache = build_resource_cache(task_config: task_config)
cache.expects(:get_instance).raises(Krane::Kubectl::ResourceNotFoundError)
pod.sync(cache)

Expand All @@ -233,7 +233,7 @@ def test_deploy_failed_is_false_for_disappeared_managed_pods
template = build_pod_template
pod = Krane::Pod.new(namespace: 'test', context: 'nope', definition: template,
logger: @logger, deploy_started_at: Time.now.utc, parent: mock)
cache = build_resource_cache(global_kinds: nil)
cache = build_resource_cache(task_config: task_config)
cache.expects(:get_instance).raises(Krane::Kubectl::ResourceNotFoundError)
pod.sync(cache)

Expand Down

0 comments on commit 5994edb

Please sign in to comment.