Bug: allow `leap test --continue` to run on additional nodes if there was an ssh...
[leap_platform.git] / lib / leap_cli / commands / test.rb
1 module LeapCli; module Commands
2
3   desc 'Run tests.'
4   command [:test, :t] do |test|
5     test.desc 'Run the test suit on FILTER nodes.'
6     test.arg_name 'FILTER', :optional => true
7     test.command :run do |run|
8       run.switch 'continue', :desc => 'Continue over errors and failures (default is --no-continue).', :negatable => true
9       run.action do |global_options,options,args|
10         do_test_run(global_options, options, args)
11       end
12     end
13
14     test.desc 'Creates files needed to run tests.'
15     test.command :init do |init|
16       init.action do |global_options,options,args|
17         generate_test_client_openvpn_configs
18       end
19     end
20
21     test.default_command :run
22   end
23
24   private
25
26   def do_test_run(global_options, options, args)
27     require 'leap_cli/ssh'
28     test_order = File.join(Path.platform, 'tests/order.rb')
29     if File.exist?(test_order)
30       require test_order
31     end
32     manager.filter!(args).names_in_test_dependency_order.each do |node_name|
33       node = manager.nodes[node_name]
34       begin
35         SSH::remote_command(node, options) do |ssh, host|
36           ssh.stream(test_cmd(options), :raise_error => true, :log_wrap => true)
37         end
38       rescue LeapCli::SSH::TimeoutError, SSHKit::Runner::ExecuteError, SSHKit::Command::Failed
39         if options[:continue]
40           exit_status(1)
41         else
42           bail!
43         end
44       end
45     end
46   end
47
48   def test_cmd(options)
49     if options[:continue]
50       "#{Leap::Platform.leap_dir}/bin/run_tests --continue"
51     else
52       "#{Leap::Platform.leap_dir}/bin/run_tests"
53     end
54   end
55
56   #
57   # generates a whole bunch of openvpn configs that can be used to connect to different openvpn gateways
58   #
59   def generate_test_client_openvpn_configs
60     assert_config! 'provider.ca.client_certificates.unlimited_prefix'
61     assert_config! 'provider.ca.client_certificates.limited_prefix'
62     template = read_file! Path.find_file(:test_client_openvpn_template)
63     manager.environment_names.each do |env|
64       vpn_nodes = manager.nodes[:environment => env][:services => 'openvpn']['openvpn.allow_limited' => true]
65       if vpn_nodes.any?
66         generate_test_client_cert(provider.ca.client_certificates.limited_prefix) do |key, cert|
67           write_file! [:test_openvpn_config, [env, 'limited'].compact.join('_')], Util.erb_eval(template, binding)
68         end
69       end
70       vpn_nodes = manager.nodes[:environment => env][:services => 'openvpn']['openvpn.allow_unlimited' => true]
71       if vpn_nodes.any?
72         generate_test_client_cert(provider.ca.client_certificates.unlimited_prefix) do |key, cert|
73           write_file! [:test_openvpn_config, [env, 'unlimited'].compact.join('_')], Util.erb_eval(template, binding)
74         end
75       end
76     end
77   end
78
79 end; end