Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dturn committed Aug 27, 2019
1 parent 0795c72 commit 3ee3e37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
8 changes: 5 additions & 3 deletions lib/kubernetes-deploy/task_config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module KubernetesDeploy
class TaskConfigValidator
DEFAULT_VALIDATIONS = %i(
validate_kubeconfig
validate_context_exists
validate_context_exists_in_kubeconfig
validate_context_reachable
validate_namespace_exists
validate_server_version
).freeze
Expand Down Expand Up @@ -38,7 +39,7 @@ def validate_kubeconfig
@errors += @kubeclient_builder.validate_config_files
end

def validate_context_exists
def validate_context_exists_in_kubeconfig
unless context.present?
return @errors << "Context can not be blank"
end
Expand All @@ -52,9 +53,10 @@ def validate_context_exists
else
"Something went wrong. #{err} "
end
return
end
end

def validate_context_reachable
_, err, st = @kubectl.run("get", "namespaces", "-o", "name",
use_namespace: false, log_failure: false)

Expand Down
26 changes: 17 additions & 9 deletions test/integration/task_config_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ def test_valid_configuration
assert_predicate(validator(context: KubeclientHelper::TEST_CONTEXT, namespace: 'default'), :valid?)
end

def test_invalid_kubeconfig
assert_match(/Context test-context missing from/, validator.errors.join("\n"))
end

def test_only_is_respected
validator = KubernetesDeploy::TaskConfigValidator.new(task_config, nil, nil, only: [])
assert_predicate(validator, :valid?)
end

def test_context_does_not_exists
assert_match("Context test-context missing from your kubeconfig file(s)",
def test_invalid_kubeconfig
bad_file = "/IM_NOT_A_REAL_FILE.yml"
builder = KubernetesDeploy::KubeclientBuilder.new(kubeconfig: bad_file)
assert_match("Kube config not found at #{bad_file}",
validator(kubeclient_builder: builder, only: [:validate_kubeconfig]).errors.join("\n"))
end

def test_context_does_not_exists_in_kubeconfig
assert_match(/Context #{task_config.context} missing from your kubeconfig file/,
validator.errors.join("\n"))
end

def test_context_not_reachable
assert_match(/Something went wrong connectting to #{task_config.context}/,
validator(only: [:validate_context_reachable]).errors.join("\n"))
end

def test_namespace_does_not_exists
assert_match(/Cloud not find Namespace: test-namespace in Context: #{KubeclientHelper::TEST_CONTEXT}/,
validator(context: KubeclientHelper::TEST_CONTEXT).errors.join("\n"))
Expand All @@ -39,12 +47,12 @@ def test_invalid_server_version

private

def validator(context: nil, namespace: nil, logger: nil)
def validator(context: nil, namespace: nil, logger: nil, kubeclient_builder: nil, only: nil)
config = task_config(context: context, namespace: namespace, logger: logger)
kubectl = KubernetesDeploy::Kubectl.new(namespace: config.namespace,
context: config.context, logger: config.logger, log_failure_by_default: true)
kubeclient_builder = KubernetesDeploy::KubeclientBuilder.new
KubernetesDeploy::TaskConfigValidator.new(config, kubectl, kubeclient_builder)
kubeclient_builder ||= KubernetesDeploy::KubeclientBuilder.new
KubernetesDeploy::TaskConfigValidator.new(config, kubectl, kubeclient_builder, only: only)
end

def task_config(context: nil, namespace: nil, logger: nil)
Expand Down

0 comments on commit 3ee3e37

Please sign in to comment.