Skip to content

Commit

Permalink
hardcoded-ip-test: Redesign test, add more info on fail
Browse files Browse the repository at this point in the history
Add information about file location, line numbers and lines themselves
if the hardcoded_ip_addresses_in_k8s_runtime_configuration fails

Refs: cnti-testcatalog#2093
Signed-off-by: Konstantin Yarovoy <konstantin.yarovoy@tietoevry.com>
  • Loading branch information
Konstantin Yarovoy committed Jul 1, 2024
1 parent 9497fd6 commit 1808f0f
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/tasks/workload/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,31 @@ task "hardcoded_ip_addresses_in_k8s_runtime_configuration" do |t, args|
helm_install = Helm.install("--namespace hardcoded-ip-test hardcoded-ip-test #{destination_cnf_dir}/#{helm_directory} --dry-run --debug > #{destination_cnf_dir}/helm_chart.yml")
VERBOSE_LOGGING.info "helm_directory: #{helm_directory}" if check_verbose(args)
end

ip_search = File.read_lines("#{destination_cnf_dir}/helm_chart.yml").take_while{|x| x.match(/NOTES:/) == nil}.reduce([] of String) do |acc, x|
(x.match(/([0-9]{1,3}[\.]){3}[0-9]{1,3}/) &&
x.match(/([0-9]{1,3}[\.]){3}[0-9]{1,3}/).try &.[0] != "0.0.0.0" &&
x.match(/([0-9]{1,3}[\.]){3}[0-9]{1,3}/).try &.[0] != "127.0.0.1") ? acc << x : acc

found_violations = [] of NamedTuple(line_number: Int32, line: String)
line_number = 1
File.open("#{destination_cnf_dir}/helm_chart.yml") do |file|
file.each_line do |line|
if line.matches?(/NOTES:/)
break
elsif matches = line.scan(/([0-9]{1,3}[\.]){3}[0-9]{1,3}/)
matches.each do |match|
unless match.to_s == "0.0.0.0" || match.to_s == "127.0.0.1"
found_violations << {line_number: line_number, line: line.strip}
end
end
end
line_number += 1
end
end
unless found_violations.empty?
stdout_failure("Hard-coded IP addresses found in #{destination_cnf_dir}/helm_chart.yml")
found_violations.each do |violation|
stdout_failure(" * Line #{violation[:line_number]}: #{violation[:line]}")
end
end

VERBOSE_LOGGING.info "IPs: #{ip_search}" if check_verbose(args)

if ip_search.empty?
if found_violations.empty?
CNFManager::TestcaseResult.new(CNFManager::ResultStatus::Passed, "No hard-coded IP addresses found in the runtime K8s configuration")
else
CNFManager::TestcaseResult.new(CNFManager::ResultStatus::Failed, "Hard-coded IP addresses found in the runtime K8s configuration")
Expand Down

0 comments on commit 1808f0f

Please sign in to comment.