diff --git a/.gitignore b/.gitignore index 0e79714..ee5bc9e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ tests/yaml.sh tests/yamlparser.sh.inc tests/test-dbg.sh +tests/vagrant_testcase.yml diff --git a/tests/README.md b/tests/README.md index 39b5e6b..aa32736 100644 --- a/tests/README.md +++ b/tests/README.md @@ -12,16 +12,16 @@ Example to run `t_no_config` tests for all Ubuntu distributions: # ./test.sh t_no_config ubuntu ``` -If you do not want to pre-download all the boxes specified in the `vagrant_config.yml` then you can this before running `test.sh`: +If you want to pre-download all the boxes specified in the `vagrant_config.yml` then you can this before running `test.sh`: ```console -# SKIP_DOWNLOAD=1 ./test.sh +# PRE_DOWNLOAD_BOXES=1 ./test.sh ``` Below is a table listing some environment variables you can set before running `test.sh`: | Environment variable | Value | Description | | --- | --- | --- | -| SKIP_DOWNLOAD | 1 | If you do not want to pre-download all the boxes specified in the `vagrant_config.yml` | +| PRE_DOWNLOAD_BOXES | 1 | If you want to pre-download all the boxes specified in the `vagrant_config.yml` | | ON_FAILURE_KEEP | 1 | If test fails and you do not want it to destroy the VM | | PROVISION_ONLY | 1 | When you have a running VM and you just want to reprovision it | diff --git a/tests/Vagrantfile b/tests/Vagrantfile index 8ba515a..dd555d0 100644 --- a/tests/Vagrantfile +++ b/tests/Vagrantfile @@ -3,25 +3,14 @@ require 'yaml' -current_dir = File.dirname(File.expand_path(__FILE__)) -configs = YAML.load_file("#{current_dir}/vagrant_config.yml") -vagrant_config={} -if ENV.has_key?('CI') - config_key = "CI" - vagrant_config['box'] = ENV['VAGRANT_BOX'] - vagrant_config['prep_yml'] = ENV['VAGRANT_PREP_YML'] - vagrant_config['test_yml'] = ENV['VAGRANT_TEST_YML'] - if ENV.has_key?('VAGRANT_VBGUEST_UPDATE') - vagrant_config['vbguest_update'] = ENV['VAGRANT_VBGUEST_UPDATE'] - end -else - config_key = ENV['CONFIG_KEY'] || configs['configs']['use'] - vagrant_config = configs['configs'][config_key] -end - -if !vagrant_config.has_key?('vbguest_update') - vagrant_config['vbguest_update'] = false +current_dir = File.dirname(File.expand_path(__FILE__)) +testcase_file = "#{current_dir}/vagrant_testcase.yml" +unless File.exist?(testcase_file) + puts "--> Test case file does not exist: #{testcase_file}" + puts "--> Please use 'test.sh' to execute tests." + exit 1 end +vagrant_config = YAML.load_file("#{testcase_file}") Vagrant.configure("2") do |config| config.vm.box = vagrant_config['box'] @@ -38,14 +27,14 @@ Vagrant.configure("2") do |config| unless File.exist?(docker_disk) vb.customize ['createhd', '--filename', docker_disk, '--size', 5 * 1024] end - vb.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', docker_disk] + vb.customize ['storageattach', :id, '--storagectl', vagrant_config['ide_ctl_name'], '--port', 1, '--device', 0, '--type', 'hdd', '--medium', docker_disk] end # Print config in use config.vm.provision "shell", - inline: "echo '" + config_key + "' > /var/tmp/provisioning-config" + inline: "echo '" + vagrant_config['id'] + "' > /var/tmp/provisioning-config" config.vm.provision "shell", - inline: "echo '--> Using configuration key: " + config_key + "'" + inline: "echo '--> Using configuration key: " + vagrant_config['id'] + "'" # Prepare Ansible roles directory config.vm.provision "shell", diff --git a/tests/test.sh b/tests/test.sh index 1b3cf89..774be16 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -1,11 +1,7 @@ #!/usr/bin/env bash # -# Environment variables: -# -# To NOT destroy VM on failure (for debugging): -# ON_FAILURE_KEEP=1 -# Skip pre-download of boxes: -# SKIP_DOWNLOAD=1 +VAGRANT_TESTCASE_FILE=vagrant_testcase.yml + BLDRED='\033[1;31m' BLDGRN='\033[1;32m' @@ -65,6 +61,15 @@ VagrantExists() { echo "0" } +VagrantVersion() { + if [[ "$(VagrantExists)" == "0" ]]; then + $VAGRANT_CMD --version + return $? + else + RedText "[VagrantVersion] vagrant not found!" + fi +} + VagrantUp() { if [[ "$(VagrantExists)" == "0" ]]; then $VAGRANT_CMD up @@ -113,7 +118,7 @@ VagrantBoxAdd() { DownloadBoxes() { Info "Downloading boxes..." exitCode=0 - for box in ${boxes[*]}; do + for box in ${boxes__box[*]}; do if [[ "$box" != *"$LIMIT_BOX"* ]]; then Skip "Download $box" continue @@ -129,11 +134,25 @@ DownloadBoxes() { return $exitCode } +GenerateTestCaseConfig() { + _box_index=$1 + _test_index=$2 + cat << EOF > $VAGRANT_TESTCASE_FILE +box: ${boxes__box[$_box_index]} +ide_ctl_name: ${boxes__ide_ctl_name[$_box_index]} +vbguest_update: ${boxes__vbguest_update[$_box_index]} +id: ${tests__id[$_test_index]} +prep_yml: ${tests__prep_yml[$_test_index]} +test_yml: ${tests__test_yml[$_test_index]} +EOF +} + ExecuteTests() { Info "Starting tests..." exitCode=0 for index in $(seq 0 `expr ${#tests__name[@]} - 1`); do - for box in ${boxes[*]}; do + for box_index in $(seq 0 `expr ${#boxes__box[@]} - 1`); do + box=${boxes__box[$box_index]} test_name=${tests__name[$index]} WSLENV=CI:VAGRANT_BOX:VAGRANT_PREP_YML:VAGRANT_TEST_YML:VAGRANT_VBGUEST_UPDATE CI=1 @@ -164,6 +183,7 @@ ExecuteTests() { continue fi fi + GenerateTestCaseConfig $box_index $index Info "Test: ${tests__name[$index]} [$box]" export WSLENV CI VAGRANT_BOX VAGRANT_PREP_YML VAGRANT_TEST_YML if [[ "$PROVISION_ONLY" == "1" ]]; then @@ -174,21 +194,11 @@ ExecuteTests() { exitCode=$? if [[ "$exitCode" == "0" ]]; then VagrantDestroy + rm $VAGRANT_TESTCASE_FILE Pass "Test: $test_name [$box]" else if [[ "$ON_FAILURE_KEEP" == "1" ]]; then Info "VM is kept for debugging" - cat << EOF > test-dbg.sh -WSLENV=$WSLENV -CI=$CI -VAGRANT_BOX=$VAGRANT_BOX -VAGRANT_PREP_YML=$VAGRANT_PREP_YML -VAGRANT_TEST_YML=$VAGRANT_TEST_YML -VAGRANT_VBGUEST_UPDATE=$VAGRANT_VBGUEST_UPDATE -export WSLENV CI VAGRANT_BOX VAGRANT_PREP_YML VAGRANT_TEST_YML VAGRANT_VBGUEST_UPDATE -vagrant \$@ -EOF - chmod +x test-dbg.sh else VagrantDestroy fi @@ -208,6 +218,9 @@ SetupYamlParser DetectWSL create_variables vagrant_config.yml +if [[ "$DEBUG" != "" ]]; then + parse_yaml vagrant_config.yml +fi if [[ "$1" != "" ]]; then LIMIT_TEST=$1 @@ -216,13 +229,11 @@ if [[ "$1" != "" ]]; then fi fi -if [[ "$SKIP_DOWNLOAD" == "" ]]; then +Info "$(VagrantVersion)" +if [[ "$PRE_DOWNLOAD_BOXES" != "" ]]; then DownloadBoxes downloadResult=$? Info "Download complete!" - if [[ "$DOWNLOAD_ONLY" != "" ]]; then - exit $downloadResult - fi fi ExecuteTests diff --git a/tests/vagrant_config.yml b/tests/vagrant_config.yml index 45f964d..916a9ca 100644 --- a/tests/vagrant_config.yml +++ b/tests/vagrant_config.yml @@ -1,14 +1,48 @@ boxes: - - geerlingguy/centos7 - - geerlingguy/ubuntu1404 - - geerlingguy/ubuntu1604 - - geerlingguy/ubuntu1804 - - geerlingguy/debian8 - - geerlingguy/debian9 - # - generic/fedora25 - # - generic/fedora26 - # - generic/fedora27 - # - generic/fedora28 + - + box: geerlingguy/centos7 + ide_ctl_name: IDE Controller + vbguest_update: false + - + box: geerlingguy/ubuntu1404 + ide_ctl_name: IDE Controller + vbguest_update: false + - + box: geerlingguy/ubuntu1604 + ide_ctl_name: IDE Controller + vbguest_update: false + - + box: geerlingguy/ubuntu1804 + ide_ctl_name: IDE Controller + vbguest_update: false + - + box: geerlingguy/debian8 + ide_ctl_name: IDE Controller + vbguest_update: false + - + box: geerlingguy/debian9 + ide_ctl_name: IDE Controller + vbguest_update: false + # - + # box: fedora/25-cloud-base + # ide_ctl_name: IDE + # vbguest_update: true + # - + # box: fedora/26-cloud-base + # ide_ctl_name: IDE + # vbguest_update: true + # - + # box: fedora/27-cloud-base + # ide_ctl_name: IDE + # vbguest_update: true + # - + # box: fedora/28-cloud-base + # ide_ctl_name: IDE + # vbguest_update: true + # - + # box: fedora/beta-29-cloud-base + # ide_ctl_name: IDE + # vbguest_update: true tests: - @@ -59,10 +93,3 @@ tests: prep_yml: prepare.yml test_yml: test_issue_42.yml skip_boxes: centos - -configs: - use: 'default' - default: - box: 'geerlingguy/centos7' - prep_yml: prepare.yml - test_yml: test_defaults.yml