Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dturn committed Oct 8, 2019
1 parent 39b72cc commit 81f4b69
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
15 changes: 10 additions & 5 deletions lib/kubernetes-deploy/cluster_resource_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def crds
end
end

def global_resource_names
def global_resource_kinds
@globals ||= fetch_globals.map { |g| g["kind"] }
end

Expand All @@ -26,11 +26,16 @@ def fetch_globals
raw, _, st = kubectl.run("api-resources", "--namespaced=false", output: "wide", attempts: 5)
if st.success?
rows = raw.split("\n")
header = rows.shift.downcase.scan(/[a-z]+[\W]*/).each_with_object({}) do |match, hash|
start = hash.values.map(&:last).max.to_i
hash[match.strip] = [start, start + match.length]
header = rows[0]
resources = rows[1..-1]
full_width_field_names = header.downcase.scan(/[a-z]+[\W]*/)
cursor = 0
fields = full_width_field_names.each_with_object({}) do |name, hash|
start = cursor
cursor = start + name.length
hash[name.strip] = [start, cursor - 1]
end
rows.map { |r| header.map { |k, (s, e)| [k.strip, r[s...e].strip] }.to_h }
resources.map { |r| fields.map { |k, (s, e)| [k.strip, r[s..e].strip] }.to_h }
else
[]
end
Expand Down
8 changes: 4 additions & 4 deletions lib/kubernetes-deploy/deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ def prune_whitelist
wl + cluster_resource_discoverer.crds.select(&:prunable?).map(&:group_version_kind)
end

def global_resource_names
cluster_resource_discoverer.global_resource_names
end

def server_version
kubectl.server_version
end
Expand Down Expand Up @@ -227,6 +223,10 @@ def run!(verify_result: true, allow_protected_ns: false, prune: true)

private

def global_resource_names
cluster_resource_discoverer.global_resource_kinds
end

def kubeclient_builder
@kubeclient_builder ||= KubeclientBuilder.new
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kubernetes-deploy/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def build(namespace:, context:, definition:, logger:, statsd_tags:, crd: nil, gl
type = definition["kind"]
inst = new(**opts)
inst.type = type
inst.global = true if global_names.map(&:downcase).include?(type.downcase)
inst.global = global_names.map(&:downcase).include?(type.downcase)
inst
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/integration/kubernetes_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1663,8 +1663,8 @@ def test_deploy_fails_if_namespace_is_invalid

assert_logs_match_all([
"Could not find Namespace: unknown in Context: minikube",
], in_order: true)
end
], in_order: true)
end

def test_deploy_allow_globals_warns
result = deploy_fixtures("globals")
Expand Down
51 changes: 51 additions & 0 deletions test/unit/cluster_resource_discovery_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true
require 'test_helper'

class ClusterResourceDiscoveryTest < KubernetesDeploy::TestCase
def test_global_resource_kinds_failure
crd = mocked_cluster_resource_discovery(nil, success: false)
kinds = crd.global_resource_kinds
assert_equal(kinds, [])
end

def test_global_resource_kinds_success
crd = mocked_cluster_resource_discovery(full_response)
kinds = crd.global_resource_kinds
%w(MutatingWebhookConfiguration ComponentStatus ComponentStatus).each do |kind|
assert_includes(kinds, kind)
end
end

private

def mocked_cluster_resource_discovery(response, success: true)
KubernetesDeploy::Kubectl.any_instance.stubs(:run).returns([response, "", stub(success?: success)])
KubernetesDeploy::ClusterResourceDiscovery.new(namespace: 'test', context: 'test', logger: nil, namespace_tags: [])
end

# rubocop:disable Metrics/LineLength
def full_response
%(NAME SHORTNAMES APIGROUP NAMESPACED KIND VERBS
componentstatuses cs false ComponentStatus [get list]
namespaces ns false Namespace [create delete get list patch update watch]
nodes no false Node [create delete deletecollection get list patch update watch]
persistentvolumes pv false PersistentVolume [create delete deletecollection get list patch update watch]
mutatingwebhookconfigurations admissionregistration.k8s.io false MutatingWebhookConfiguration [create delete deletecollection get list patch update watch]
validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration [create delete deletecollection get list patch update watch]
customresourcedefinitions crd,crds apiextensions.k8s.io false CustomResourceDefinition [create delete deletecollection get list patch update watch]
apiservices apiregistration.k8s.io false APIService [create delete deletecollection get list patch update watch]
tokenreviews authentication.k8s.io false TokenReview [create]
selfsubjectaccessreviews authorization.k8s.io false SelfSubjectAccessReview [create]
selfsubjectrulesreviews authorization.k8s.io false SelfSubjectRulesReview [create]
subjectaccessreviews authorization.k8s.io false SubjectAccessReview [create]
certificatesigningrequests csr certificates.k8s.io false CertificateSigningRequest [create delete deletecollection get list patch update watch]
podsecuritypolicies psp extensions false PodSecurityPolicy [create delete deletecollection get list patch update watch]
podsecuritypolicies psp policy false PodSecurityPolicy [create delete deletecollection get list patch update watch]
clusterrolebindings rbac.authorization.k8s.io false ClusterRoleBinding [create delete deletecollection get list patch update watch]
clusterroles rbac.authorization.k8s.io false ClusterRole [create delete deletecollection get list patch update watch]
priorityclasses pc scheduling.k8s.io false PriorityClass [create delete deletecollection get list patch update watch]
storageclasses sc storage.k8s.io false StorageClass [create delete deletecollection get list patch update watch]
volumeattachments storage.k8s.io false VolumeAttachment [create delete deletecollection get list patch update watch])
end
# rubocop:enable Metrics/LineLength:
end

0 comments on commit 81f4b69

Please sign in to comment.