Skip to content

Commit

Permalink
complete test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Jul 7, 2021
1 parent 5533d17 commit 366e919
Show file tree
Hide file tree
Showing 38 changed files with 7,594 additions and 1,050 deletions.
18 changes: 9 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ GIT
GEM
remote: https://rubygems.org/
specs:
activesupport (6.1.3.2)
activesupport (6.1.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.7.0)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
async (1.29.1)
console (~> 1.10)
Expand All @@ -41,7 +41,7 @@ GEM
fiber-local (1.0.0)
gem_logger (0.3.0)
activesupport
graphql (1.12.12)
graphql (1.12.13)
graphql-client (0.16.0)
activesupport (>= 3.0)
graphql (~> 1.8)
Expand Down Expand Up @@ -74,19 +74,19 @@ GEM
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0225)
mime-types-data (3.2021.0704)
minitest (5.14.4)
multi_json (1.15.0)
netrc (0.11.0)
nio4r (2.5.7)
pry (0.13.1)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.9.0)
pry-byebug (3.8.0)
byebug (~> 11.0)
pry (~> 0.13.0)
pry (~> 0.10)
public_suffix (4.0.6)
rake (13.0.3)
rake (13.0.4)
recursive-open-struct (1.1.3)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
Expand Down Expand Up @@ -127,7 +127,7 @@ GEM
zeitwerk (2.4.2)

PLATFORMS
ruby
x86_64-darwin-20

DEPENDENCIES
activesupport
Expand Down
3 changes: 0 additions & 3 deletions lib/kubetruth/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ def initialize(*args, **kwargs)

def convert_types(hash)
selector_key_pattern = /_selector$/
template_key_pattern = /_template$/
hash.merge(hash) do |k, v|
case k
when selector_key_pattern
Regexp.new(v)
when template_key_pattern
Kubetruth::Template.new(v)
when /^resource_templates$/
Hash[v.collect {|k, t| [k.to_s, Kubetruth::Template.new(t)] }]
when /^context$/
Expand Down
5 changes: 3 additions & 2 deletions lib/kubetruth/ctapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Kubetruth

def self.ctapi_setup(api_key:, api_url: nil)
unless Kubetruth.const_defined?(:CtApi)
::Logging.logger.root.debug {"Setting up CtApi"}
api_url ||= "https://api.cloudtruth.com/graphql"

clazz = Class.new do
Expand Down Expand Up @@ -93,13 +94,13 @@ def environment_id(environment)

# retry in case environments have been updated upstream since we cached
# them
if env_id.nil? && ! @environments.nil?
if env_id.nil?
logger.debug {"Unknown environment, retrying after clearing cache"}
@environments = nil
env_id = self.environments[environment]
end

raise("Unknown environment: #{environment}") unless env_id
raise Kubetruth::Error.new("Unknown environment: #{environment}") unless env_id
env_id.to_s
end

Expand Down
2 changes: 1 addition & 1 deletion lib/kubetruth/etl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def with_polling(interval, &block)
run_time = Benchmark.measure do
begin
block.call
rescue Kubetruth::Template::Error => e
rescue ::Kubetruth::Error => e
logger.error e.message
rescue => e
logger.log_exception(e, "Failure while applying config transforms")
Expand Down
18 changes: 10 additions & 8 deletions lib/kubetruth/kubeapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ class KubeApi

attr_accessor :namespace

NAMESPACE_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/namespace'
CA_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
TOKEN_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token'

MANAGED_LABEL_KEY = "app.kubernetes.io/managed-by"
MANAGED_LABEL_VALUE = "kubetruth"

def initialize(namespace: nil, token: nil, api_url: nil)
namespace_path = '/var/run/secrets/kubernetes.io/serviceaccount/namespace'
ca_path = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
token_path = '/var/run/secrets/kubernetes.io/serviceaccount/token'

@namespace = namespace.present? ? namespace : File.read(namespace_path).chomp
@namespace = namespace.present? ? namespace : File.read(NAMESPACE_PATH).chomp

@auth_options = {}
if token
@auth_options[:bearer_token] = token
elsif File.exist?(token_path)
@auth_options[:bearer_token_file] = token_path
elsif File.exist?(TOKEN_PATH)
@auth_options[:bearer_token_file] = TOKEN_PATH
end

@ssl_options = {}
if File.exist?(ca_path)
@ssl_options[:ca_file] = ca_path
if File.exist?(CA_PATH)
@ssl_options[:ca_file] = CA_PATH
end

@api_url = api_url || 'https://kubernetes.default.svc'
Expand Down
4 changes: 2 additions & 2 deletions lib/kubetruth/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Template

include GemLogger::LoggerSupport

class Error < ::StandardError
class Error < ::Kubetruth::Error
end

class TemplateHashDrop < Liquid::Drop
Expand Down Expand Up @@ -158,7 +158,7 @@ def render(*args, **kwargs)
rescue Liquid::Error => e
msg = "Template failed to render:\n"
@source.lines.each {|l| msg << (INDENT * 2) << l }
msg << INDENT << "with error message:\n" << (INDENT * 2) << "#{e.message}"
msg << "\n" << INDENT << "with error message:\n" << (INDENT * 2) << "#{e.message}"
if e.is_a?(Liquid::UndefinedVariable)
msg << "\n" << INDENT << "and variable context:\n"
debug_kwargs ||= kwargs.merge(secrets: Hash[secrets.collect {|k, v| [k, "<masked:#{k}>"] }])
Expand Down
603 changes: 603 additions & 0 deletions spec/fixtures/vcr/CtApi/_environment_id/gets_id.yml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

557 changes: 550 additions & 7 deletions spec/fixtures/vcr/CtApi/_environments/gets_environments.yml

Large diffs are not rendered by default.

603 changes: 603 additions & 0 deletions spec/fixtures/vcr/CtApi/_environments/memoizes_environments.yml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

571 changes: 557 additions & 14 deletions spec/fixtures/vcr/CtApi/_parameters/gets_parameters_without_a_search.yml

Large diffs are not rendered by default.

571 changes: 557 additions & 14 deletions spec/fixtures/vcr/CtApi/_parameters/uses_environment_to_get_values.yml

Large diffs are not rendered by default.

571 changes: 557 additions & 14 deletions spec/fixtures/vcr/CtApi/_parameters/uses_project_to_get_values.yml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

615 changes: 579 additions & 36 deletions spec/fixtures/vcr/CtApi/_projects/doesn_t_cache_projects_.yml

Large diffs are not rendered by default.

579 changes: 561 additions & 18 deletions spec/fixtures/vcr/CtApi/_projects/gets_projects.yml

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions spec/fixtures/vcr/CtApi/class_definition/defines_class.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 366e919

Please sign in to comment.