Skip to content

Commit 551ead3

Browse files
committed
continue with implementation of messages in result file
1 parent 3614709 commit 551ead3

5 files changed

Lines changed: 159 additions & 119 deletions

File tree

src/tasks/constants.cr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ CONFIG_FILE = "cnf-testsuite.yml"
99
BASE_CONFIG = "./config.yml"
1010
COMMON_MANIFEST_FILE_PATH = "#{CNF_DIR}/common_manifest.yml"
1111
DEPLOYMENT_MANIFEST_FILE_NAME = "deployment_manifest.yml"
12-
PASSED = "passed"
13-
FAILED = "failed"
14-
SKIPPED = "skipped"
15-
NA = "na"
16-
ERROR = "error"
1712
# todo move to helm module
1813
# CHART_YAML = "Chart.yaml"
1914
DEFAULT_POINTSFILENAME = "points_v1.yml"

src/tasks/utils/cnf_manager/points.cr

Lines changed: 110 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,111 @@ module CNFManager
99
Failed
1010
Skipped
1111
NA
12-
Neutral
1312
Pass5
1413
Pass3
1514
Error
1615

1716
def to_basic
1817
case self
19-
when Pass5, Pass3
20-
ret = CNFManager::ResultStatus::Passed
21-
when Neutral
22-
ret = CNFManager::ResultStatus::Failed
23-
else
24-
ret = self
18+
when Pass5, Pass3 then CNFManager::ResultStatus::Passed
19+
else self
20+
end
21+
end
22+
23+
def to_string
24+
case self.to_basic
25+
when Passed then "passed"
26+
when Failed then "failed"
27+
when Skipped then "skipped"
28+
when NA the "na"
29+
when Error then "error"
30+
else ""
2531
end
2632
end
2733
end
2834

29-
struct TestCaseResult
30-
property state, result_message
35+
class TestCaseResult
36+
property testcase : String
37+
property status : CNFManager::ResultStatus
38+
property result_message : String?
39+
property result_description : Array(String)
40+
property start_time : Time
41+
property end_time : Time
42+
43+
def initialize(@testcase : String = "",
44+
@status : CNFManager::ResultStatus = CNFManager::ResultStatus::Skipped,
45+
@result_message : String? = nil,
46+
@result_description : Array(String) = [] of String)
47+
end
48+
49+
def update(status : CNFManager::ResultStatus, message : String?)
50+
@status = status
51+
@result_message = message
52+
end
53+
54+
def passed(message : String?)
55+
@status = CNFManager::ResultStatus::Passed
56+
@result_message = message
57+
end
58+
59+
def failed(message : String?)
60+
@status = CNFManager::ResultStatus::Failed
61+
@result_message = message
62+
end
63+
64+
def skipped(message : String?)
65+
@status = CNFManager::ResultStatus::Skipped
66+
@result_message = message
67+
end
68+
69+
def error(message : String?)
70+
@status = CNFManager::ResultStatus::Error
71+
@result_message = message
72+
end
73+
74+
def append_description(text : String)
75+
@result_description << text
76+
end
77+
78+
def set_start_time()
79+
@start_time = Time.utc
80+
end
81+
82+
def set_end_time()
83+
@end_time = Time.utc
84+
end
85+
86+
def duration() : Time::Span
87+
@end_time - @start_time
88+
end
3189

32-
def initialize(@state : CNFManager::ResultStatus, @result_message : String? = nil)
90+
def points() :
91+
92+
def set_testcase(testcase : String)
93+
@testcase = testcase
94+
end
95+
96+
def decorated_result_message() : String
97+
tc_emoji = CNFManager::Points.emoji_by_task(@testcase)
98+
cat_emoji = CNFManager::Points.task_emoji_by_task(@testcase)↲
99+
case @status.to_basic↲
100+
when CNFManager::ResultStatus::Passed
101+
" #{cat_emoji}PASSED: [#{@testcase}] #{@result_message} #{tc_emoji}"
102+
when CNFManager::ResultStatus::Failed
103+
" #{cat_emoji}FAILED: [#{@testcase}] #{@result_message} #{tc_emoji}"
104+
when CNFManager::ResultStatus::Skipped
105+
"⏭️ #{cat_emoji}SKIPPED: [#{@testcase}] #{@result_message} #{tc_emoji}"
106+
when CNFManager::ResultStatus::NA
107+
"⏭️ #{cat_emoji}N/A: [#{@testcase}] #{@result_message} #{tc_emoji}"
108+
when CNFManager::ResultStatus::Error
109+
"💥 #{cat_emoji}ERROR: [#{@testcase}] #{@result_message}"
110+
else:
111+
""
112+
end
113+
end
114+
115+
def self.empty
116+
new
33117
end
34118
end
35119

@@ -275,8 +359,8 @@ module CNFManager
275359
{max_points, max_passed}
276360
end
277361

278-
def self.upsert_task(task, status, points, start_time, message)
279-
logger = @@logger.for("upsert_task-#{task}")
362+
def self.upsert_task(result : CNFManager::TestCaseResult)
363+
logger = @@logger.for("upsert_task-#{result.testcase}")
280364

281365
# Raise exception when results file does not exists.
282366
CNFManager::Points::Results.ensure_results_file!
@@ -287,36 +371,22 @@ module CNFManager
287371
result_items = result_items.reject { |x| x["name"] == task }
288372

289373
end_time = Time.utc
290-
task_runtime = (end_time - start_time)
291374

292375
# The task result info has to be appeneded to an array of YAML::Any
293376
# So encode it into YAML and parse it back again to assign it.
294377
#
295378
# Only add task timestamps if the env var is set.
296-
if status == "passed"
297-
task_result_info = {
298-
name: task,
299-
status: status,
300-
type: task_type_by_task(task),
301-
points: points,
302-
start_time: start_time,
303-
end_time: end_time,
304-
task_runtime: "#{task_runtime}",
305-
}
306-
result_items << YAML.parse(task_result_info.to_yaml)
307-
else
308-
task_result_info = {
309-
name: task,
310-
status: status,
311-
status_description: message,
312-
type: task_type_by_task(task),
313-
points: points,
314-
start_time: start_time,
315-
end_time: end_time,
316-
task_runtime: "#{task_runtime}",
317-
}
318-
result_items << YAML.parse(task_result_info.to_yaml)
319-
end
379+
task_result_info = {
380+
name: result.testcase,
381+
status: result.status.to_string,
382+
status_description: result.result_description,
383+
type: task_type_by_task(result.testcase),
384+
points: result.points,
385+
start_time: result.start_time,
386+
end_time: result.end_time,
387+
task_runtime: "#{result.duration}",
388+
}
389+
result_items << YAML.parse(task_result_info.to_yaml)
320390

321391
File.open("#{Results.file}", "w") do |f|
322392
YAML.dump({name: results["name"],
@@ -327,24 +397,9 @@ module CNFManager
327397
exit_code: results["exit_code"],
328398
items: result_items}, f)
329399
end
330-
logger.debug { "Task start time: #{start_time}, end time: #{end_time}" }
331-
logger.info { "Task: '#{task}' has status: '#{status}' and is awarded: #{points} points." +
332-
"Runtime: #{task_runtime}" }
333-
end
334-
335-
def self.failed_task(task, msg)
336-
upsert_task(task, FAILED, task_points(task, false), start_time, msg)
337-
stdout_failure "#{msg}"
338-
end
339-
340-
def self.passed_task(task, msg)
341-
upsert_task(task, PASSED, task_points(task), start_time, msg)
342-
stdout_success "#{msg}"
343-
end
344-
345-
def self.skipped_task(task, msg)
346-
upsert_task(task, SKIPPED, task_points(task), start_time, msg)
347-
stdout_success "#{msg}"
400+
logger.debug { "Task start time: #{result.start_time}, end time: #{result.end_time}" }
401+
logger.info { "Test case: '#{result.testcase}' has status: '#{result.status}' and is awarded: #{points} points." +
402+
"Runtime: #{result.duration}" }
348403
end
349404

350405
def self.failed_required_tasks

src/tasks/utils/cnf_manager/task.cr

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module CNFManager
2222
end
2323

2424
def self.task_runner(args, task : Sam::Task? = nil, check_cnf_installed = true,
25-
&block : (Sam::Args, CNFInstall::Config::Config) -> (
25+
&block : (Sam::Args, CNFInstall::Config::Config, CNFManager::TestCaseResult) -> (
2626
String | Colorize::Object(String) | CNFManager::TestCaseResult?)
2727
)
2828
CNFManager::Points::Results.ensure_results_file!
@@ -36,7 +36,7 @@ module CNFManager
3636
end
3737

3838
def self.all_cnfs_task_runner(args, task : Sam::Task? = nil,
39-
&block : (Sam::Args, CNFInstall::Config::Config) -> (
39+
&block : (Sam::Args, CNFInstall::Config::Config, CNFManager::TestCaseResult) -> (
4040
String | Colorize::Object(String) | CNFManager::TestCaseResult?)
4141
)
4242
cnf_configs = CNFManager.cnf_config_list(false)
@@ -55,68 +55,58 @@ module CNFManager
5555

5656
# TODO give example for calling
5757
def self.single_task_runner(args, task : Sam::Task? = nil,
58-
&block : (Sam::Args, CNFInstall::Config::Config) -> (
58+
&block : (Sam::Args, CNFInstall::Config::Config, CNFManager::TestCaseResult) -> (
5959
String | Colorize::Object(String) | CNFManager::TestCaseResult?)
6060
)
6161
logger = @@logger.for("task_runner")
6262
logger.debug { "Run task with args #{args.inspect}" }
6363

64-
begin
65-
# platform tests don't have a cnf-config
66-
if args.named["cnf-config"]?
67-
config = CNFInstall::Config.parse_cnf_config_from_file(args.named["cnf-config"].as(String))
68-
else
69-
yaml_string = <<-YAML
70-
config_version: v2
71-
deployments:
72-
helm_dirs:
73-
- name: "platform-test-dummy-deployment"
74-
helm_directory: ""
75-
YAML
76-
config = CNFInstall::Config.parse_cnf_config_from_yaml(yaml_string)
77-
end
64+
# platform tests don't have a cnf-config
65+
if args.named["cnf-config"]?
66+
config = CNFInstall::Config.parse_cnf_config_from_file(args.named["cnf-config"].as(String))
67+
else
68+
yaml_string = <<-YAML
69+
config_version: v2
70+
deployments:
71+
helm_dirs:
72+
- name: "platform-test-dummy-deployment"
73+
helm_directory: ""
74+
YAML
75+
config = CNFInstall::Config.parse_cnf_config_from_yaml(yaml_string)
76+
end
7877

79-
test_start_time = Time.utc
80-
if task
81-
test_name = task.as(Sam::Task).name.as(String)
82-
logger.for(test_name).info { "Starting test" }
83-
stdout_info("🎬 Testing: [#{test_name}]")
84-
end
78+
result = CNFManager::TestCaseResult.empty
79+
result.set_start_time()
80+
if task
81+
result.set_testcase(task.as(Sam::Task).name.as(String))
82+
logger.for(result.test_name).info { "Starting test" }
83+
stdout_info("🎬 Testing: [#{result.test_name}]")
84+
end
8585

86-
ret = yield args, config
87-
if ret.is_a?(CNFManager::TestCaseResult)
88-
upsert_decorated_task(test_name, ret.state, ret.result_message, test_start_time)
89-
end
90-
# todo lax mode, never returns 1
91-
if args.raw.includes? "strict"
92-
if CNFManager::Points.failed_required_tasks.size > 0
93-
logger.fatal { "Strict mode exception. Stopping execution." }
94-
stdout_failure "Test Suite failed in strict mode. Stopping execution."
95-
stdout_failure "Failed required tasks: #{CNFManager::Points.failed_required_tasks.inspect}"
96-
update_yml("#{CNFManager::Points::Results.file}", "exit_code", "#{FAILURE_CODE}")
97-
exit FAILURE_CODE
98-
end
99-
end
100-
ret
86+
begin
87+
yield args, config, result
10188
rescue ex
102-
# platform tests don't have a cnf-config
103-
# Set exception key/value in results
104-
# file to -1
105-
test_start_time = Time.utc
89+
result.error("Unexpected error occurred")
10690
logger.error { ex.message }
10791
ex.backtrace.each do |x|
10892
logger.error { x }
10993
end
94+
ensure
95+
result.set_end_time()
96+
upsert_decorated_task(result)
97+
end
11098

111-
update_yml("#{CNFManager::Points::Results.file}", "exit_code", "#{CRITICAL_FAILURE_CODE}")
112-
if args.raw.includes? "strict"
99+
# todo lax mode, never returns 1
100+
if args.raw.includes? "strict"
101+
if result.state in (CNFManager::ResultStatus::Error, CNFManager::ResultStatus::Failed)
113102
logger.fatal { "Strict mode exception. Stopping execution." }
114103
stdout_failure "Test Suite failed in strict mode. Stopping execution."
115-
exit CRITICAL_FAILURE_CODE
116-
else
117-
logger.warn { "Exception caught, continue to the next task" }
118-
upsert_decorated_task(test_name, CNFManager::ResultStatus::Error,
119-
"Unexpected error occurred", test_start_time)
104+
if CNFManager::Points.failed_required_tasks.size > 0
105+
stdout_failure "Failed required tasks: #{CNFManager::Points.failed_required_tasks.inspect}"
106+
end
107+
exit_code = result.state == CNFManager::ResultStatus::Error ? CRITICAL_FAILURE_CODE : FAILURE_CODE
108+
update_yml("#{CNFManager::Points::Results.file}", "exit_code", "#{exit_code}")
109+
exit exit_code
120110
end
121111
end
122112
end

src/tasks/utils/utils.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ def update_yml(yml_file, top_level_key, value)
190190
end
191191
end
192192

193-
def upsert_decorated_task(task, status : CNFManager::ResultStatus, message, start_time)
194-
tc_emoji = CNFManager::Points.emoji_by_task(task)
195-
cat_emoji = CNFManager::Points.task_emoji_by_task(task)
193+
def upsert_decorated_task(result : CNFManager::TestCaseResult)
194+
tc_emoji = CNFManager::Points.emoji_by_task(result.testcase)
195+
cat_emoji = CNFManager::Points.task_emoji_by_task(result.testcase)
196196
case status.to_basic
197197
when CNFManager::ResultStatus::Passed
198198
upsert_passed_task(task, "✔️ #{cat_emoji}PASSED: [#{task}] #{message} #{tc_emoji}", message, start_time)

src/tasks/workload/microservice.cr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ end
751751

752752
desc "To check if the CNF uses a specialized init system"
753753
task "specialized_init_system", ["setup:install_cluster_tools"] do |t, args|
754-
CNFManager::Task.task_runner(args, task: t) do |args, config|
754+
CNFManager::Task.task_runner(args, task: t) do |args, config, result|
755755
error_occurred = false
756756
resources_checked = false
757757

@@ -790,7 +790,7 @@ task "specialized_init_system", ["setup:install_cluster_tools"] do |t, args|
790790

791791
# Report failures
792792
results.each do |info|
793-
stdout_failure("#{info.kind}/#{info.name} container '#{info.container}' uses non-specialized init '#{info.init_cmd}'")
793+
result.append_description("#{info.kind}/#{info.name} has container '#{info.container}' with '#{info.init_cmd}' as init process")
794794
end
795795

796796
# mark this resource as failing
@@ -799,13 +799,13 @@ task "specialized_init_system", ["setup:install_cluster_tools"] do |t, args|
799799
end
800800

801801
if error_occurred
802-
CNFManager::TestCaseResult.new(CNFManager::ResultStatus::Skipped, "An error occurred during container inspection")
802+
result.skipped("An error occurred during container inspection")
803803
elsif !resources_checked
804-
CNFManager::TestCaseResult.new(CNFManager::ResultStatus::Skipped, "Container checks not executed")
804+
result.skipped("Container checks not executed")
805805
elsif !task_response
806-
CNFManager::TestCaseResult.new(CNFManager::ResultStatus::Failed, "Containers do not use specialized init systems (ভ_ভ) ރ")
806+
result.failed("Containers do not use specialized init systems (ভ_ভ) ރ")
807807
else
808-
CNFManager::TestCaseResult.new(CNFManager::ResultStatus::Passed, "Containers use specialized init systems 🖥️")
808+
result.passed("Containers use specialized init systems 🖥️")
809809
end
810810
end
811-
end
811+
end

0 commit comments

Comments
 (0)