-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrtoolsHCK.rb
More file actions
executable file
·1604 lines (1418 loc) · 48.8 KB
/
Copy pathrtoolsHCK.rb
File metadata and controls
executable file
·1604 lines (1418 loc) · 48.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative 'exceptions'
require_relative 'ether'
require 'securerandom'
require 'winrm'
require 'winrm-fs'
require 'logger'
require 'json'
require 'tempfile'
# rubocop:disable Metrics/ClassLength, Metrics/ParameterLists
# == Description
#
# A ruby class the resembles tool set for HCK\HLK with various purposes which
# covers several actions as explained in the list if actions below.
#
# == Actions list
#
# +list_pools+:: Lists the pools info.
#
# +create_pool+:: Creates a pool.
#
# +delete_pool+:: Deletes a pool.
#
# +move_machine+:: Moves a machine from one pool to another.
#
# +set_machine_state+:: Sets the state of a machine to Ready or
# NotReady.
#
# +delete_machine+:: Deletes a machine
#
# +list_machine_targets+:: Lists the target devices of a machine that are
# available to be tested.
#
# +list_projects+:: Lists the projects info.
#
# +create_project+:: Creates a project.
#
# +delete_project+:: Deletes a project.
#
# +create_project_target+:: Creates a project's target.
#
# +delete_project_target+:: Deletes a project's target.
#
# +list_tests+:: Lists a project target's tests.
#
# +get_test_info+:: Gets a project target's test info.
#
# +queue_test+:: Queue's a test, use get_test_results to get the
# results.
#
# +update_filters+:: Updates the HCK\HLK controller's filters.
#
# +apply_project_filters+:: Applies the filters on a project's test
# results.
#
# +apply_test_result_filters+:: Applies the filters on a test result.
#
# +list_test_results+:: Lists a test results info.
#
# +zip_test_result_logs+:: Zipps a test result's log and fetches the zip.
#
# +create_project_package+:: Creates a project's package.
class RToolsHCK
WINRM_OPERATION_TIMEOUT = 9_999
WINRM_RECIEVE_TIMEOUT = 99_999
WINRM_RETRY_INTERVAL = 60
WINRM_DEFAULT_PORT = 5985
ETHER_DEFAULT_PORT = 4000
private
def handle_exceptions
yield
rescue StandardError => e
log_exception(e, 'error')
raise e
end
def get_exception_stack(exception)
exception.backtrace.select { |line| line.include?(File.dirname(__FILE__)) }
.join("\n -- ")
end
def log_exception(exception, level)
eclass = exception.class
emessage = exception.message
estack = get_exception_stack(exception)
if exception.is_a?(RToolsHCKError)
ewhere = exception.where
logger(level, ewhere) { "(#{eclass}) #{emessage}\n -- #{estack}" }
else
logger(level, eclass) { "#{emessage}\n -- #{estack}" }
end
end
def logger(level, progname = nil, &)
@stdout_logger.public_send(level, progname, &) if @log_to_stdout
@logger&.public_send(level, progname, &)
end
public
# == Description
#
# Initializes new object of type RToolsHCK to be used by establishing a
# Telnet and a Tftp connection with the guest machine.
#
# == Params:
#
# +init_opts+:: Hash that has various initialize options to configure upon
# initializing a RtoolsHCK object:
# :addr - Controller/Studio machine's IP address
# (default: 192.168.100.1)
# (default is based on HLK-Setup-Script settings)
# :port - The port to be used for the WinRM connection
# (default: 5985)
# :ether_port - The port to be used for the toolsHCK Ether connection
# (default: 4000)
# :user - The user name to use in order to connect via WinRM to the
# guest
# (default: Administrator)
# :pass - The password of the user name specified
# (default: PASSWORD)
# :clients_addrs - The clients WinRM connection ip and port (default: 5985) as a hash
# (example: { 'Client' => { addr: '192.168.0.18', port: 4001 }, ... })
# (default: { 'CL1' => { addr: '192.168.100.2' }, 'CL2' => { addr: '192.168.100.3' } })
# (default is based on HLK-Setup-Script settings)
# :json - JSON format the output of the action methods
# (default: true)
# :timeout - The action's timeout in seconds
# (default: 60)
# :log_to_stdout - Log to STDOUT switch
# (default: false)
# :logger - The ruby logger object for logging
# (default: disabled)
# :outp_dir - The path of the directory to fetch the output files to on
# the local machine
# (default: disabled)
# :r_script_file - The toolsHCK.ps1 file path on remote machine
# (default: C:\\toolsHCK.ps1)
#
def initialize(init_opts)
init_opts = validate_init_opts(init_opts)
@log_to_stdout = init_opts[:log_to_stdout]
@stdout_logger = Logger.new($stdout) if @log_to_stdout
@logger = init_opts[:logger]
handle_exceptions { do_initialize(init_opts) }
rescue StandardError
priv_close
raise
end
private
# Actions that can be performed in no_studio mode (client-only actions)
CLIENT_ACTIONS = %i[
start_client_services
get_machine_system_info
machine_shutdown
run_on_machine
upload_to_machine
download_from_machine
exists_on_machine?
delete_on_machine
install_machine_driver_package
close
].freeze
# init_opts initialization defaults
INIT_OPTS_DEFAULTS = {
addr: '192.168.100.1',
port: WINRM_DEFAULT_PORT,
ether_port: ETHER_DEFAULT_PORT,
user: 'Administrator',
pass: 'PASSWORD',
clients_addrs: {
'CL1' => { addr: '192.168.100.2', port: WINRM_DEFAULT_PORT },
'CL2' => { addr: '192.168.100.3', port: WINRM_DEFAULT_PORT }
},
json: true,
timeout: 60,
logger: nil,
log_to_stdout: false,
outp_dir: nil,
r_script_file: 'C:\\toolsHCK.ps1',
no_studio: false
}.freeze
def validate_init_opts(init_opts)
extra_keys = (init_opts.keys - INIT_OPTS_DEFAULTS.keys)
unless extra_keys.empty?
if extra_keys.include?(:winrm_ports)
raise RToolsHCKError.new('initialize'),
'The :winrm_ports option is deprecated, use :clients_addrs instead.'
end
raise RToolsHCKError.new('initialize'),
"Undefined initialization options: #{extra_keys.join(', ')}."
end
INIT_OPTS_DEFAULTS.merge(init_opts)
end
def start_studio_service(service_name)
retries ||= 0
run("Start-Service #{service_name}")
rescue WinrmPSRunError => e
raise e unless (retries += 1) < 4
logger('warn', "#{e.message} Retrying in #{WINRM_RETRY_INTERVAL} seconds...")
sleep(WINRM_RETRY_INTERVAL)
retry
end
def start_studio_services
services = %w[WTTServer HLKsvc DTMService WttChangeScheduler]
services.each { |service_name| start_studio_service(service_name) }
logger('info', 'HLK Services started successfully')
end
def start_client_service(machine, service_name)
machine_run(machine, "Start-Service #{service_name}")
end
public
# == Description
#
# Starts HLK related services at the machine.
#
# == Params:
#
# +machine+:: The name of the machine
def start_client_services(machine)
services = %w[HLKsvc]
services.each { |service_name| start_client_service(machine, service_name) }
logger('debug', "Machine #{machine}: HLK Services started successfully")
end
private
def do_initialize(init_opts)
load_outp_dir(init_opts[:outp_dir])
load_instance_variables(init_opts)
if @no_studio
logger('debug', 'initialize') { "no-studio mode: clients=#{@clients_addrs.keys}" }
else
logger('debug', 'initialize') { "#{@user}:#{@pass}@#{@addr}" }
load_winrm_ps
load_winrm_fs
start_studio_services
load_toolshck
end
@closed = false
end
def load_outp_dir(outp_dir)
@outp_dir = nil
return if outp_dir.nil?
unless File.directory?(outp_dir)
raise RToolsHCKError.new('initialize/outp'),
"can't find the directory outp_dir specefied."
end
@outp_dir = File.expand_path(outp_dir)
logger('debug', 'initialize/outp') { "outp_dir assigned to #{@outp_dir}" }
end
def load_instance_variables(init_opts)
@addr = init_opts[:addr]
@user = init_opts[:user]
@pass = init_opts[:pass]
@port = init_opts[:port]
@ether_port = init_opts[:ether_port]
@clients_addrs = init_opts[:clients_addrs]
@timeout = init_opts[:timeout]
@json = init_opts[:json]
@r_script_file = init_opts[:r_script_file]
@no_studio = init_opts[:no_studio]
end
def winrm_options_factory(addr, port, user, pass)
{
endpoint: "http://#{addr}:#{port}/wsman",
operation_timeout: WINRM_OPERATION_TIMEOUT,
receive_timeout: WINRM_RECIEVE_TIMEOUT,
transport: :plaintext,
user:,
password: pass,
basic_auth_only: true
}
end
def load_winrm_ps
logger('debug', 'initialize/load_winrm_ps') { 'loading winrm shell...' }
@connection_options = winrm_options_factory(@addr, @port || WINRM_DEFAULT_PORT, @user, @pass)
@connection = WinRM::Connection.new(@connection_options)
@winrm_ps = @connection.shell(:powershell)
run('date')
logger('info', 'initialize/load_winrm_ps') { 'winrm shell loaded!' }
end
def check_run_output(run_output, where, cmd)
return if run_output.exitcode.zero?
error = "Running '#{cmd}' failed with exit code #{run_output.exitcode}." \
"#{run_output.stdout.empty? ? '' : "\n -- stdout:\n#{run_output.stdout}"}" \
"#{run_output.stderr.empty? ? '' : "\n -- stderr:\n#{run_output.stderr}"}"
raise WinrmPSRunError.new(where), error
end
def run(cmd)
run_output = @winrm_ps.run(cmd)
where = 'winrm/run'
check_run_output(run_output, where, cmd)
run_output.stdout
end
def machine_connection(machine)
clients_addr = @clients_addrs[machine]
if clients_addr.nil?
raise RToolsHCKError.new('machine_connection'),
"Unknown machine '#{machine}'. Available machines: #{@clients_addrs.keys.join(', ')}"
end
options = winrm_options_factory(
clients_addr[:addr], clients_addr[:port] || WINRM_DEFAULT_PORT,
@user, @pass
)
WinRM::Connection.new(options)
end
def machine_run(machine, cmd)
where = "#{machine}/winrm/run"
machine_connection(machine).shell(:powershell) do |ps_shell|
run_output = ps_shell.run(cmd)
check_run_output(run_output, where, cmd)
run_output.stdout
end
rescue HTTPClient::KeepAliveDisconnected
raise WinrmPSRunError.new(where), "Machine #{machine} reset connection."
end
def load_winrm_fs
logger('debug', 'initialize/load_winrm_fs') do
'creating winrm file manager instance'
end
@winrm_fs = WinRM::FS::FileManager.new(@connection)
logger('info', 'initialize/load_winrm_fs') do
'winrm file manager instance created!'
end
end
# toolsHCK connection timeout in seconds
TOOLSHCK_CONNECTION_TIMEOUT = 60
def load_toolshck
@toolshck_ether = Ether.new(toolshck_ether_init_opts)
end
def toolshck_ether_init_opts
{
winrm_connection_options: @connection_options,
server_addr: @addr,
server_port: @ether_port || ETHER_DEFAULT_PORT,
operation_timeout: @timeout,
connection_timeout: TOOLSHCK_CONNECTION_TIMEOUT,
outp_dir: @outp_dir,
r_script_file: @r_script_file,
log_to_stdout: @log_to_stdout,
logger: @logger
}
end
def guest_basename(path)
path&.split('\\')&.last
end
def guest_dirname(path)
return nil if path.nil?
path.split('\\').first(path.split('\\').size - 1).join('\\')
end
def handle_return(stream)
if @json
JSON.parse(stream)
else
puts(stream)
!stream.include?('WARNING')
end
end
def parse_action_parameters(action, binding)
method(action).parameters.map do |param|
param_str = "#{param[1]} "
param_str + if param[0].equal?(:opt)
"is #{binding.local_variable_get(param[1]) ? 'on' : 'off'}"
else
"= #{binding.local_variable_get(param[1])}"
end
end
end
def log_action_call(action, binding)
action_parameters = parse_action_parameters(action, binding)
action_parameters.push('no parameters') if action_parameters.empty?
logger('debug', "action/#{action}") { action_parameters.join(', ') }
end
def action_exception_handler(exception)
log_exception(exception, 'debug')
if @json
{ 'result' => 'Failure', 'message' => exception.message }
else
puts "WARNING: #{exception.message}"
false
end
end
def handle_action_exceptions(action, &block)
raise RToolsHCKError.new('action'), 'instance is closed.' if @closed
if @no_studio && !CLIENT_ACTIONS.include?(action)
raise RToolsHCKError.new(action.to_s), 'not supported in no_studio mode.'
end
log_action_call(action, block.binding)
handle_exceptions do
yield
# We can have ECONNREFUSED error due to direct connection to
# clients instead of proxy through studio
# We can have EHOSTUNREACH error due to network issues or machine rebooting
# Safety catch this exception, the action wrapper will retry if needed
rescue RToolsHCKError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
action_exception_handler(e)
end
end
public
attr_accessor :json
# == Description
#
# Lists the pools info.
#
def list_pools
handle_action_exceptions(__method__) do
cmd_line = ['listpools']
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Creates a pool.
#
# == Params:
#
# +pool+:: The name of the pool
def create_pool(pool)
handle_action_exceptions(__method__) do
cmd_line = ["createpool '#{pool}'"]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Deletes a pool.
#
# == Params:
#
# +pool+:: The name of the pool
def delete_pool(pool)
handle_action_exceptions(__method__) do
cmd_line = ["deletepool '#{pool}'"]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Moves a machine from one pool to another.
#
# == Params:
#
# +machine+:: The name of the machine
# +from+:: The name of the source pool
# +to+:: The name of the destination pool
def move_machine(machine, from, to)
handle_action_exceptions(__method__) do
cmd_line = ["movemachine '#{machine}' '#{from}' '#{to}'"]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Sets the state of a machine to Ready or NotReady.
#
# == Params:
#
# +machine+:: The name of the machine
# +pool+:: The name of the pool
# +state+:: The state, Ready or NotReady
# +timeout+:: The action's timeout in seconds, 60 by deafult
def set_machine_state(machine, pool, state, timeout = @timeout)
handle_action_exceptions(__method__) do
cmd_line = ["setmachinestate '#{machine}' '#{pool}' '#{state}'"]
cmd_line << "-timeout #{timeout}"
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' '), timeout))
end
end
# == Description
#
# Deletes a machine.
#
# == Params:
#
# +machine+:: The name of the machine
# +pool+:: The name of the pool
def delete_machine(machine, pool)
handle_action_exceptions(__method__) do
cmd_line = ["deletemachine '#{machine}' '#{pool}'"]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Lists the target devices of a machine that are available to be tested.
#
# == Params:
#
# +machine+:: The name of the machine
# +pool+:: The name of the pool
def list_machine_targets(machine, pool)
handle_action_exceptions(__method__) do
cmd_line = ["listmachinetargets '#{machine}' '#{pool}'"]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Lists the projects info.
#
def list_projects
handle_action_exceptions(__method__) do
cmd_line = ['listprojects']
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Creates a project.
#
# == Params:
#
# +project+:: The name of the project
def create_project(project)
handle_action_exceptions(__method__) do
cmd_line = ["createproject '#{project}'"]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Deletes a project.
#
# == Params:
#
# +project+:: The name of the project
def delete_project(project)
handle_action_exceptions(__method__) do
cmd_line = ["deleteproject '#{project}'"]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Creates a project's target.
#
# == Params:
#
# +target+:: The key of the target, use list_machine_targets to get it
# +project+:: The name of the project
# +machine+:: The name of the machine as registered with the HCK\HLK
# controller
# +pool+:: The name of the pool
def create_project_target(target, project, machine, pool)
handle_action_exceptions(__method__) do
cmd_line = [
"createprojecttarget '#{target}' '#{project}' " \
"'#{machine}' '#{pool}'"
]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Deletes a project's target.
#
# == Params:
#
# +target+:: The key of the target, use list_machine_targets to get it
# +project+:: The name of the project
# +machine+:: The name of the machine as registered with the HCK\HLK
# controller
# +pool+:: The name of the pool
def delete_project_target(target, project, machine, pool)
handle_action_exceptions(__method__) do
cmd_line = [
"deleteprojecttarget '#{target}' " \
"'#{project}' '#{machine}' '#{pool}'"
]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
private
def do_upload_playlist_file(l_playlist)
unless File.exist?(l_playlist)
raise RToolsHCKError.new('action/upload_playlist'),
'Playlist file is not valid.'
end
r_path = "#{@winrm_fs.temp_dir}/#{SecureRandom.uuid}.xml"
@winrm_fs.upload(l_playlist, r_path) do |cb, tb, lp, rp|
# TODO: Check transfer
end
r_path
end
def do_list_tests(cmd_line, l_playlist)
unless l_playlist.nil?
r_playlist = do_upload_playlist_file(l_playlist)
cmd_line << "-playlist #{r_playlist}"
end
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
public
# == Description
#
# Lists a project target's tests.
#
# == Params:
#
# +target+:: The key of the target, use list_machine_targets to get it
# +project+:: The name of the project
# +machine+:: The name of the machine as registered with the HCK\HLK
# controller
# +pool+:: The name of the pool
# +test_type+:: Assign to manual or auto, (can be nil)
# +test_status+:: Assign to failed, inqueue, notrun, passed or running,
# (can be nil)
# +playlist+:: Provide a playlist file path to apply, (can be nil)
def list_tests(target,
project,
machine,
pool,
test_type = nil,
test_status = nil,
playlist = nil)
handle_action_exceptions(__method__) do
cmd_line = ["listtests '#{target}' '#{project}' '#{machine}' '#{pool}'"]
cmd_line << 'json' if @json
cmd_line << "-#{test_type}" unless test_type.nil?
cmd_line << "-#{test_status}" unless test_status.nil?
do_list_tests(cmd_line, playlist)
end
end
private
def file_to_outp_dir(r_file_path)
l_file_path = "#{@outp_dir}/#{guest_basename(r_file_path)}"
@winrm_fs.download(r_file_path, l_file_path) do |cb, tb, lp, rp|
# TODO: Check transfer
end
l_file_path
end
def handle_test_results_json(test_results)
logs_zip_guest_path = test_results['content']['logszippath']
test_results['content'].delete('logszippath')
test_results['content']['guestlogszippath'] = logs_zip_guest_path
unless @outp_dir.nil?
logs_zip_host_path = file_to_outp_dir(logs_zip_guest_path)
test_results['content']['hostlogszippath'] = logs_zip_host_path
end
test_results
end
def handle_test_results_normal(test_results, stream)
return false unless test_results
unless @outp_dir.nil?
logs_zip_guest_path = stream.split("\n")
.grep(/^Logs zipped to .*/)
.last.split('Logs zipped to ')
.last
.strip
puts "HOST: Logs zip fetched to #{file_to_outp_dir(logs_zip_guest_path)}"
end
test_results
end
def handle_test_results(test_results, stream)
if @json
return test_results if test_results['result'].eql?('Failure')
handle_test_results_json(test_results)
else
handle_test_results_normal(test_results, stream)
end
end
public
# == Description
#
# Gets a project target's test info.
#
# == Params:
#
# +test+:: The id of the test, use list_tests action to get it
# +target+:: The key of the target, use list_machine_targets to get it
# +project+:: The name of the project
# +machine+:: The name of the machine as registered with the HCK\HLK
# controller
# +pool+:: The name of the pool
def get_test_info(test, target, project, machine, pool)
handle_action_exceptions(__method__) do
cmd_line = [
"gettestinfo '#{test}' '#{target}' '#{project}' '#{machine}' " \
"'#{pool}'"
]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Queues a test, use get_test_results action to get the results.
# (if the test needs two machines to run use -sup flag)
# (if the test needs the IPv6 address of the support machine use -IPv6 flag)
#
# == Params:
#
# +test+:: The id of the test, use list_tests action to get it
# +target+:: The key of the target, use list_machine_targets to get it
# +project+:: The name of the project
# +machine+:: The name of the machine as registered with the HCK\HLK
# controller
# +pool+:: The name of the pool
# +sup+:: The name of the support machine as registered with the
# HCK\HLK controller, (can be nil)
# +parameters+:: Additional parameters in format '{ ParameterName1: Value1, ParameterName2: Value2 }', (can be nil)
# +ipv6+:: The IPv6 address of the support machine, (can be nil)
def queue_test(test,
target,
project,
machine,
pool,
sup = nil,
parameters = nil,
ipv6 = nil)
handle_action_exceptions(__method__) do
cmd_line = [
"queuetest '#{test}' '#{target}' '#{project}' '#{machine}' '#{pool}'"
]
cmd_line << 'json' if @json
cmd_line << "-sup '#{sup}'" unless sup.nil?
cmd_line << "-parameters '#{parameters.to_json}'" unless parameters.nil?
cmd_line << "-IPv6 '#{ipv6}'" unless ipv6.nil?
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
private
def do_upload_and_update_filter(l_filter)
r_filter = run('$env:DTMBIN').strip + SecureRandom.uuid
@winrm_fs.upload(l_filter, r_filter) do |cb, tb, lp, rp|
# TODO: Check transfer
end
run('pushd $env:DTMBIN')
run("updatefilters.exe /S #{guest_basename(r_filter)}")
run('popd')
@winrm_fs.delete(r_filter)
end
public
# == Description
#
# Updates the HCK\HLK controller's filters by giving a local .sql filter file.
#
# == Params:
#
# +l_filters+:: The local filter .sql file path
def update_filters(l_filter)
handle_action_exceptions(__method__) do
raise 'Filters file not valid.' unless File.exist?(l_filter)
do_upload_and_update_filter(l_filter)
@json ? { 'result' => 'Success' } : true
end
end
# == Description
#
# Applies the filters on a project's test results.
#
# == Params:
#
# +project+:: The name of the project
def apply_project_filters(project)
handle_action_exceptions(__method__) do
cmd_line = ["applyprojectfilters '#{project}'"]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Applies the filters on a test result.
#
# == Params:
#
# +result+:: The index of the test result, use list_test_results action
# to get it
# +test+:: The id of the test, use list_tests action to get it
# +target+:: The key of the target, use list_machine_targets to get it
# +project+:: The name of the project
# +machine+:: The name of the machine as registered with the HCK\HLK
# controller
# +pool+:: The name of the pool
def apply_test_result_filters(result,
test,
target,
project,
machine,
pool)
handle_action_exceptions(__method__) do
cmd_line = [
"applytestresultfilters '#{result}' '#{test}' '#{target}' " \
"'#{project}' '#{machine}' '#{pool}'"
]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Lists a test's results info.
#
# == Params:
#
# +test_id+:: The id of the test, use list_tests action to get it
# If id is nil, all tests results will be listed
# +target+:: The key of the target, use list_machine_targets to get it
# +project+:: The name of the project
# +machine+:: The name of the machine as registered with the HCK\HLK
# controller
# +pool+:: The name of the pool
def list_test_results(test_id, target, project, machine, pool)
test_id = if test_id.nil?
'$null'
else
"'#{test_id}'"
end
handle_action_exceptions(__method__) do
cmd_line = [
"listtestresults #{test_id} '#{target}' '#{project}' '#{machine}' " \
"'#{pool}'"
]
cmd_line << 'json' if @json
handle_return(@toolshck_ether.cmd(cmd_line.join(' ')))
end
end
# == Description
#
# Zips a test result's logs to a zip file fetches it to the local machine if
# logs_dir param was used on initialization.
#
# == Params:
#
# +result_index+:: If index_instance_id is false the index of the test result,
# use list_test_results action to get it
# If index_instance_id is true the instance id of the test result
# +test+:: The id of the test, use list_tests action to get it
# +target+:: The key of the target, use list_machine_targets to get it
# +project+:: The name of the project
# +machine+:: The name of the machine as registered with the HCK\HLK
# controller
# +pool+:: The name of the pool
# +index_instance_id+:: If true, the result_index is treated as an instance id
def zip_test_result_logs(result_index:,
test:,
target:,
project:,
machine:,
pool:,
index_instance_id: false)
handle_action_exceptions(__method__) do
cmd_line = [
"ziptestresultlogs '#{result_index}' '#{test}' '#{target}' " \
"'#{project}' '#{machine}' '#{pool}'"
]
cmd_line << 'json' if @json
cmd_line << '-indexinstanceid' if index_instance_id
stream = @toolshck_ether.cmd(cmd_line.join(' '))
test_results = handle_return(stream)
handle_test_results(test_results, stream)
end
end
private
def handle_project_package_json(project_package)
project_package_guest_path = project_package['content']['projectpackagepath']
project_package['content'].delete('projectpackagepath')
project_package['content']['guestprojectpackagepath'] =
project_package_guest_path
unless @outp_dir.nil?
project_package['content']['hostprojectpackagepath'] =
file_to_outp_dir(project_package_guest_path)
end
project_package
end
def parse_project_package_guest_path(stream)
stream.split("\n")
.grep(/^Packaged to .*/)
.last
.split('Packaged to ')
.last
.split('...')
.first
end
def handle_project_package_normal(project_package, stream)
return false unless project_package