Skip to content

Instantly share code, notes, and snippets.

@maxjustus
Created March 21, 2024 20:41
Show Gist options
  • Save maxjustus/1f0a10863596480b2f5c898b660774ee to your computer and use it in GitHub Desktop.
Save maxjustus/1f0a10863596480b2f5c898b660774ee to your computer and use it in GitHub Desktop.
require "minitest/autorun"
require 'yaml'
`make test-render`
def flatten_hash(object, current_path = [], result = {})
case object
when Hash
object.each do |key, value|
flatten_hash(value, current_path + [key], result)
end
when Array
object.each_with_index do |value, index|
flatten_hash(value, current_path + [index], result)
end
else
result[current_path.join('.')] = object
end
result
end
def flatten_hashes(a)
a.map { |x| flatten_hash(x) }
end
RENDERED_YAML = flatten_hashes(YAML.load_stream(File.read("test-render.yaml")).compact)
class MatchResult
attr_reader :matched, :path, :expected, :got
def initialize(matched, path, expected, got)
@matched = matched
@path = path
@expected = expected
@got = got
end
def inspect
"matched: #{@matched}, path: #{@path}, expected: #{@expected}, got: #{@got}"
end
end
def must_match_all_paths(paths_and_values)
results = RENDERED_YAML.map do |x|
paths_and_values.map do |path, value|
v = x[path]
MatchResult.new(v == value, path, value, v)
end
end
if results.any? { |x| x.all?(&:matched) }
return
else
results_with_any_matches = results.select { |x| x.any?(&:matched) }
raise "No match for #{paths_and_values.inspect}\n\nfound partial matches:\n#{results_with_any_matches.map { |x| x.map {|v| " #{v.inspect}"}.join("\n") }.join("\n\n")}\n\n"
end
end
describe 'test-render.yaml' do
it 'uses correct image for app deployment' do
must_match_all_paths({
"kind" =>
"Deployment",
"metadata.name" =>
"release-name-app",
"spec.template.spec.containers.0.image" =>
"test-repository/test-app:yourAppImage-app",
})
end
it 'uses correct image for sidekiq monitor deployment' do
must_match_all_paths({
"kind" =>
"Deployment",
"metadata.name" =>
"release-name-sidekiq-monitor",
"spec.template.spec.containers.0.image" =>
"test-repository/test-app:yourAppImage-app",
})
end
it 'uses correct image for sidekiq worker deployment' do
must_match_all_paths({
"kind" =>
"Deployment",
"metadata.name" =>
"release-name-default-worker",
"spec.template.spec.containers.0.image" =>
"test-repository/test-app:yourAppImage-app",
})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment