From f149c62b16801790ce1ab8dbaa74f1092db558c6 Mon Sep 17 00:00:00 2001 From: Jeffrey Clark Date: Tue, 9 Jul 2024 03:05:54 -0500 Subject: [PATCH] (maint) fix spec tests when run sequentially running `rake spec` (sequential tests) or `pdk test unit -v` resulted in none of the tests in spec/unit being run. --- spec/tasks/vagrant_spec.rb | 8 ++-- tasks/vagrant.rb | 78 ++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/spec/tasks/vagrant_spec.rb b/spec/tasks/vagrant_spec.rb index c294c74..d768c19 100644 --- a/spec/tasks/vagrant_spec.rb +++ b/spec/tasks/vagrant_spec.rb @@ -2,6 +2,7 @@ require 'rspec' require 'spec_helper' require 'net/ssh' +require_relative '../../tasks/vagrant' describe 'vagrant' do let(:provider) { 'virtualbox' } @@ -25,12 +26,11 @@ allow(File).to receive(:read).with(%r{#{tmpdir}.*\.vagrant}).and_return('some_unique_id') allow(Open3).to receive(:capture3).with(%r{vagrant ssh-config}, any_args).and_return(['', '', 0]).once allow(Net::SSH).to receive(:start).and_return(true) - require_relative '../../tasks/vagrant' end it 'provisions a new vagrant box when action is provision' do - expect { vagrant }.to output(%r{"status":"ok"}).to_stdout - expect { vagrant }.to output(%r{"platform":"generic/debian10"}).to_stdout - expect { vagrant }.to output(%r{"role":"worker1"}).to_stdout + expect { vagrant }.to raise_error(SystemExit).and output( + include('"status":"ok"', '"platform":"generic/debian10"', '"role":"worker1"'), + ).to_stdout end end diff --git a/tasks/vagrant.rb b/tasks/vagrant.rb index 439dafc..bdff1de 100755 --- a/tasks/vagrant.rb +++ b/tasks/vagrant.rb @@ -205,43 +205,47 @@ def tear_down(node_name, inventory) { status: 'ok' } end -params = JSON.parse($stdin.read) -warn params -platform = params['platform'] -action = params['action'] -node_name = params['node_name'] -vars = params['vars'] -inventory = InventoryHelper.open(params['inventory']) -enable_synced_folder = params['enable_synced_folder'].nil? ? ENV.fetch('VAGRANT_ENABLE_SYNCED_FOLDER', nil) : params['enable_synced_folder'] -enable_synced_folder = enable_synced_folder.casecmp('true').zero? if enable_synced_folder.is_a?(String) -provider = params['provider'].nil? ? ENV.fetch('VAGRANT_PROVIDER', nil) : params['provider'] -cpus = params['cpus'].nil? ? ENV.fetch('VAGRANT_CPUS', nil) : params['cpus'] -memory = params['memory'].nil? ? ENV.fetch('VAGRANT_MEMORY', nil) : params['memory'] -hyperv_vswitch = params['hyperv_vswitch'].nil? ? ENV.fetch('VAGRANT_HYPERV_VSWITCH', nil) : params['hyperv_vswitch'] -hyperv_smb_username = params['hyperv_smb_username'].nil? ? ENV.fetch('VAGRANT_HYPERV_SMB_USERNAME', nil) : params['hyperv_smb_username'] -hyperv_smb_password = params['hyperv_smb_password'].nil? ? ENV.fetch('VAGRANT_HYPERV_SMB_PASSWORD', nil) : params['hyperv_smb_password'] -box_url = params['box_url'].nil? ? ENV.fetch('VAGRANT_BOX_URL', nil) : params['box_url'] -password = params['password'].nil? ? ENV.fetch('VAGRANT_PASSWORD', nil) : params['password'] -raise 'specify a node_name when tearing down' if action == 'tear_down' && node_name.nil? -raise 'specify a platform when provisioning' if action == 'provision' && platform.nil? - -unless node_name.nil? ^ platform.nil? - case action - when 'tear_down' - raise 'specify only a node_name, not platform, when tearing down' - when 'provision' - raise 'specify only a platform, not node_name, when provisioning' - else - raise 'specify only one of: node_name, platform' +def vagrant + params = JSON.parse($stdin.read) + warn params + platform = params['platform'] + action = params['action'] + node_name = params['node_name'] + vars = params['vars'] + inventory = InventoryHelper.open(params['inventory']) + enable_synced_folder = params['enable_synced_folder'].nil? ? ENV.fetch('VAGRANT_ENABLE_SYNCED_FOLDER', nil) : params['enable_synced_folder'] + enable_synced_folder = enable_synced_folder.casecmp('true').zero? if enable_synced_folder.is_a?(String) + provider = params['provider'].nil? ? ENV.fetch('VAGRANT_PROVIDER', nil) : params['provider'] + cpus = params['cpus'].nil? ? ENV.fetch('VAGRANT_CPUS', nil) : params['cpus'] + memory = params['memory'].nil? ? ENV.fetch('VAGRANT_MEMORY', nil) : params['memory'] + hyperv_vswitch = params['hyperv_vswitch'].nil? ? ENV.fetch('VAGRANT_HYPERV_VSWITCH', nil) : params['hyperv_vswitch'] + hyperv_smb_username = params['hyperv_smb_username'].nil? ? ENV.fetch('VAGRANT_HYPERV_SMB_USERNAME', nil) : params['hyperv_smb_username'] + hyperv_smb_password = params['hyperv_smb_password'].nil? ? ENV.fetch('VAGRANT_HYPERV_SMB_PASSWORD', nil) : params['hyperv_smb_password'] + box_url = params['box_url'].nil? ? ENV.fetch('VAGRANT_BOX_URL', nil) : params['box_url'] + password = params['password'].nil? ? ENV.fetch('VAGRANT_PASSWORD', nil) : params['password'] + raise 'specify a node_name when tearing down' if action == 'tear_down' && node_name.nil? + raise 'specify a platform when provisioning' if action == 'provision' && platform.nil? + + unless node_name.nil? ^ platform.nil? + case action + when 'tear_down' + raise 'specify only a node_name, not platform, when tearing down' + when 'provision' + raise 'specify only a platform, not node_name, when provisioning' + else + raise 'specify only one of: node_name, platform' + end end -end -begin - result = provision(platform, inventory, enable_synced_folder, provider, cpus, memory, hyperv_vswitch, hyperv_smb_username, hyperv_smb_password, box_url, password, vars) if action == 'provision' - result = tear_down(node_name, inventory) if action == 'tear_down' - puts result.to_json - exit 0 -rescue StandardError => e - puts({ _error: { kind: 'provision/vagrant_failure', msg: e.message } }.to_json) - exit 1 + begin + result = provision(platform, inventory, enable_synced_folder, provider, cpus, memory, hyperv_vswitch, hyperv_smb_username, hyperv_smb_password, box_url, password, vars) if action == 'provision' + result = tear_down(node_name, inventory) if action == 'tear_down' + puts result.to_json + exit 0 + rescue StandardError => e + puts({ _error: { kind: 'provision/vagrant_failure', msg: e.message } }.to_json) + exit 1 + end end + +vagrant if __FILE__ == $PROGRAM_NAME