From d46483c93ecc9a4ef7d80656d23bd0891ddf5ac6 Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Wed, 12 Aug 2020 16:44:20 -0400 Subject: [PATCH] bringing in testkit tools this is a library of test tools ported from an older project, released under Apache license version 2.0. --- makefile | 3 +- scripts/core/variables.sh | 2 +- testkit/doc/testkit_reference.html | 268 +++++++++++++++ testkit/examples/blank_test.sh | 64 ++++ testkit/examples/fred-testkit.config | 30 ++ testkit/library/establish_environment.sh | 94 ++++++ testkit/library/extract_der_from_pfx.sh | 21 ++ testkit/library/file_management.sh | 90 +++++ testkit/library/gen_cert_from_ca.sh | 58 ++++ .../library/generate_csr_from_existing_key.sh | 36 ++ testkit/library/helper_methods.sh | 312 ++++++++++++++++++ testkit/library/process_configuration.sh | 45 +++ testkit/library/produce_test_package.sh | 25 ++ testkit/library/random_ids_manager.sh | 61 ++++ testkit/library/runner_functions.sh | 99 ++++++ .../library/tear_down_test_infrastructure.sh | 67 ++++ testkit/makefile | 9 + testkit/prepare_tools.sh | 129 ++++++++ testkit/readme.txt | 9 + .../shunit/example_shunit_test.sh | 0 {scripts => testkit}/shunit/shunit2 | 0 {scripts => testkit}/shunit/shunit2_test.sh | 0 .../shunit/shunit2_test_asserts.sh | 0 .../shunit/shunit2_test_failures.sh | 0 .../shunit/shunit2_test_helpers | 0 .../shunit/shunit2_test_macros.sh | 0 .../shunit/shunit2_test_misc.sh | 0 .../shunit/shunit2_test_standalone.sh | 0 testkit/test_driver.sh | 158 +++++++++ testkit/testkit.config | 30 ++ testkit/tools/bandwidth/set_tcp_config.sh | 24 ++ testkit/tools/bandwidth/show_tcp_memory.sh | 17 + 32 files changed, 1649 insertions(+), 2 deletions(-) create mode 100644 testkit/doc/testkit_reference.html create mode 100644 testkit/examples/blank_test.sh create mode 100644 testkit/examples/fred-testkit.config create mode 100644 testkit/library/establish_environment.sh create mode 100644 testkit/library/extract_der_from_pfx.sh create mode 100644 testkit/library/file_management.sh create mode 100644 testkit/library/gen_cert_from_ca.sh create mode 100644 testkit/library/generate_csr_from_existing_key.sh create mode 100644 testkit/library/helper_methods.sh create mode 100644 testkit/library/process_configuration.sh create mode 100644 testkit/library/produce_test_package.sh create mode 100644 testkit/library/random_ids_manager.sh create mode 100644 testkit/library/runner_functions.sh create mode 100644 testkit/library/tear_down_test_infrastructure.sh create mode 100644 testkit/makefile create mode 100644 testkit/prepare_tools.sh create mode 100644 testkit/readme.txt rename {scripts => testkit}/shunit/example_shunit_test.sh (100%) rename {scripts => testkit}/shunit/shunit2 (100%) rename {scripts => testkit}/shunit/shunit2_test.sh (100%) rename {scripts => testkit}/shunit/shunit2_test_asserts.sh (100%) rename {scripts => testkit}/shunit/shunit2_test_failures.sh (100%) rename {scripts => testkit}/shunit/shunit2_test_helpers (100%) rename {scripts => testkit}/shunit/shunit2_test_macros.sh (100%) rename {scripts => testkit}/shunit/shunit2_test_misc.sh (100%) rename {scripts => testkit}/shunit/shunit2_test_standalone.sh (100%) create mode 100755 testkit/test_driver.sh create mode 100644 testkit/testkit.config create mode 100644 testkit/tools/bandwidth/set_tcp_config.sh create mode 100644 testkit/tools/bandwidth/show_tcp_memory.sh diff --git a/makefile b/makefile index 458d476f..50018a6d 100644 --- a/makefile +++ b/makefile @@ -8,7 +8,8 @@ BUILD_BEFORE = scripts \ kona \ documentation huffware hypermedia \ production \ - experiments + testkit \ + experiments LAST_TARGETS += end_make include rules.def diff --git a/scripts/core/variables.sh b/scripts/core/variables.sh index 9e3d1312..c9c3d37d 100644 --- a/scripts/core/variables.sh +++ b/scripts/core/variables.sh @@ -294,7 +294,7 @@ define_yeti_variable DEFAULT_FEISTYMEOW_ORG_DIR=/opt/feistymeow.org ############## # set the SHUNIT_PATH so our shunit tests can find the codebase. - define_yeti_variable SHUNIT_PATH="$FEISTY_MEOW_SCRIPTS/shunit" + define_yeti_variable SHUNIT_PATH="$FEISTY_MEOW_SCRIPTS/testkit/shunit" ############## diff --git a/testkit/doc/testkit_reference.html b/testkit/doc/testkit_reference.html new file mode 100644 index 00000000..ea9e9227 --- /dev/null +++ b/testkit/doc/testkit_reference.html @@ -0,0 +1,268 @@ + + + + TestKit Reference Manual + + +

Feisty Meow® TestKit Reference Manual

+
Version 1.0 ― Updated August 12 2020
+

The Feisty Meow® TestKit

+

The TestKit is a collection of scripts that leverages the ShUnit unit + testing environment.  TestKit provides a pattern for creating test + suites using a simple configuration file approach.  Full reporting on + test runs is provided in a convenient tabular format.

+

Generally, writing a test script using the TestKit is a matter of + minutes.  A blank test is provided as a template, and that can be + expanded with whatever test steps are needed.

+

TestKit (and ShUnit) are implemented in the GNU Bash script language, but + a TestKit test script can invoke external applications, written in + whatever programming language or scripting tool is desired, using the + standard POSIX interfaces.
+

+

Getting the TestKit

+

Follow these steps to download and install the Feisty Meow TestKit:
+

+
+

sudo + mkdir /opt/feistymeow.org
+ sudo chown -R $USER /opt/feistymeow.org
+ cd /opt/feistymeow.org
+ git clone git://feistymeow.org/feisty_meow

+
+

Preparing the TestKit on Linux

+

Linux is the easiest environment for running the TestKit, given that the + tests were built using the bash shell within a Linux environment.  If + some of the packages used in the tests are missing (such as expect and gnu + awk), these may need to be installed from the appropriate repository for + your Linux distribution.  Most distributions include these packages + automatically however.

+

Preparing the TestKit on Mac OS X

+

The test suite runs well on modern Macs with Intel CPUs.  Due to + some differences in the versions of a few applications on the Mac, some + GNU tools may need to be installed to run the TestKit.  These are + available via the Brew installer tool.
+

+

+

Preparing the TestKit on MS-Windows

+

The Cygwin Unix emulation system is required to run the TestKit on + Windows.  This package is available at: http://cygwin.com/install.html

+

The default packages selected by Cygwin are the starting point of the + install.  In addition to those packages, the following packages are + also required (see list below).  Rather than using the cygwin setup + program for this task, the next section describes how to install Cygwin + with the apt-cyg tool.  Apt-cyg is the preferred method, since it + involves less interaction with the somewhat clunky Cygwin installer.  + If necessary, it is possible to install all the packages without apt-cyg + just by using the Cygwin setup program.  To find each of these + packages more easily, try switching the “View” button on the Cygwin setup + program to “Full” to get an alphabetized list.

+
+

bc
+ crypt
+ cygutils
+ emacs
+ email
+ expect
+ gcc-g++
+ git
+ gitk
+ gvim
+ inetutils
+ less
+ make
+ mutt
+ ncftp
+ openssh
+ perl
+ procps
+ python
+ sharutils
+ shutdown
+ subversion
+ time
+ unzip
+ util-linux
+ vim
+ wget
+ xinit
+ xterm
+ zip

+
+

 

+

Apt-cyg Installation Process

+

The apt-cyg program brings the convenience of the Debian and Ubuntu + installer application (apt-get) to Cygwin.  This program does require + a couple of additional setup steps.  This material is drawn from the + apt-cyg home page: https://github.com/transcode-open/apt-cyg

+

1.  + Install the basic Cygwin packages with setup.exe (rather than the long + list above), but add these two packages which are not selected by default:

+ +

2.   + Download and install the apt-cyg program from within a Cygwin bash prompt:

+
+

lynx -source + rawgit.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg
+ install apt-cyg /bin

+
+

3.       + Install the packages required for the TestKit:

+
+

apt-cyg install bc + crypt cygutils emacs email expect gcc-g++ git gitk gvim \
+   inetutils less make mutt ncftp openssh perl procps python + sharutils \
+   shutdown time unzip util-linux vim xinit xterm zip

+
+

4.       + The installation will run for a while but then should conclude with all + required packages installed.

+

Setting up a Test Suite

+

Tunning tests in TestKit uses a configuration file called + “testkit.config” to define the environment and, optionally, which test + scripts to run.  This file is the main switchboard that defines where + the tests will find users, home directories, queues, containers, and so + forth.  The configuration file can be specified via the environment + variable “TESTKIT_CFG_FILE”.  This variable can be set to any + location, enabling the configuration file to reside in a directory other + than the toolkit folder.  If the variable is not defined, then the + testing config file defaults to “$TESTKIT_ROOT/testkit.config”.

+ The TESTKIT_ROOT variable is frequently referred to in command + examples.  It is set up automatically by the prepare_tools script (see + below). +

Running a Test Suite

+

Once the TestKit configuration file has been established, running a whole + test suite can be accomplished with this command:
+

+
+

 bash {TESTKIT_FOLDER}/test_driver.sh +

+

Where the {TESTKIT_FOLDER} should be replaced with whatever path + the TestKit is stored in.

+

Alternatively, if the TESTKIT_ROOT folder is already established, the + tests can be run with:

+
+

 bash "$TESTKIT_ROOT/test_driver.sh"

+
+

+

What to Expect From the Test Run

+

The test_driver.sh script will output a few informative lines of text + before printing a table of the tests that it intends to run.

+

After the test plan is shown, all of the tests listed will be executed in + the order they are listed in, and they will each produce output.  + Each individual test (usually a separate bash script) produces a summary + at the end of its run with a count of tests and a report of the tests + success or failure.

+

At the end of all the tests in the suite, the table of tests is printed + again with the results for each test.  For example, this is a test + run that had no errors in any test (that's good, since it is our super + simple example test):

+
$ cd + $FEISTY_MEOW_APEX/testkit
+ $ ./test_driver.sh summary
+ ===========================================================
+ Testkit environment loaded.
+ TESTKIT_ROOT=/opt/feistymeow.org/feisty_meow/testkit
+ TESTKIT_CFG_FILE=/opt/feistymeow.org/feisty_meow/testkit/testkit.config
+ TMP=/Users/fred/.tmp
+ TEST_TEMP=/Users/fred/.tmp/testkit_logs_fred
+ ===========================================================
+ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ TestKit running from: /opt/feistymeow.org/feisty_meow/testkit
+ TestKit config file: + /opt/feistymeow.org/feisty_meow/testkit/testkit.config
+ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Full set of tests:
+ 1: /opt/feistymeow.org/feisty_meow/testkit/examples/blank_test.sh
+
+ ======================================================================
+ Wed Aug 12 14:11:00 EDT 2020: Now running test 1: + /opt/feistymeow.org/feisty_meow/testkit/examples/blank_test.sh
+   Test output file: + /Users/fred/.tmp/testkit_logs_fred/run_2020_08_12/test_log.vKf7J3
+ OK: successful test run for test + /opt/feistymeow.org/feisty_meow/testkit/examples/blank_test.sh
+
+
+ Results table for this test run:
+
+ 01: OKAY -- /opt/feistymeow.org/feisty_meow/testkit/examples/blank_test.sh
+
+ Total testing duration: 00:00 hh:mm (1 seconds total)
+ OK: All 1 Tests Ran Successfully.
+
+
+
+
+ dd
+
+
+

The above shows the "summary" view, which does not allow + the individual tests to output to the console.  If the "summary" flag + is not passed, then the output from all the test runs is also shown.

+

Even when the summary view is used, all output files can + be examined after the run.  For example, in the above, the mentioned + output file "test_log.vKf7J3" can be checked to see exactly what happened + during the test.

+

A test with a failure in it will have “FAIL” next to the + test that failed, and the final output line will start with + “FAILURE”.  For example:

+
+

01: FAIL – + AckPfft_Tests/Gorp_Tests/deslagToaster.sh
+ 02: OKAY – AckPfft_Tests/Gorp_Tests/spumeMerchantry.sh
+ 03: OKAY – AckPfft_Tests/Gorp_Tests/octopusLauncher.sh
+ …
+ 22: OKAY -- Snargle_Tests/scramTests/scramForPetunias.sh

+

FAILURE: 1 Tests Failed out of 22 Tests.

+
+

A failed test will also return a non-zero value from the test execution, + enabling the run of a test suite to be tested for success when launched + externally, such as from a continuous integration system.

+

Loading the TestKit Environment

+

If one wishes to run individual tests within the test suite, rather than + the entire suite, this is done by loading the TestKit variables into the + current shell environment, like so:

+
+

cd {TESTKIT_FOLDER}  # replace + with actual location of TestKit.
+ source prepare_tools.sh prepare_tools.sh
+ source $TESTKIT_ROOT/library/process_configuration.sh
+ define_and_export_variables

+ # Show the important variables.
+ var $TESTKIT_ROOT $TESTKIT_CFG_FILE
+

After loading the TestKit environment, one can execute a specific test + and see its results, for example:

+
+

cd examples
+ bash blank_test.sh

+
+

The test will run and output its results to the console (that is, output + is sent to standard out and standard error, to be more precise).

+


+

+

+ + diff --git a/testkit/examples/blank_test.sh b/testkit/examples/blank_test.sh new file mode 100644 index 00000000..47dfd3a6 --- /dev/null +++ b/testkit/examples/blank_test.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +# Test: X +# Author: Y + +export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )" # obtain the script's working directory. +cd "$WORKDIR" + +# this needs to be relative to where the test will actually reside; the ../../../../../etc +# should get to the top of the tools and tests hierarchy. +source "../prepare_tools.sh" "../prepare_tools.sh" +if [ -z "$TEST_TEMP" ]; then + echo The TestKit could not be automatically located. + exit 1 +fi + +if [ -z "$TESTKIT_SENTINEL" ]; then echo Please run prepare_tools.sh before testing.; exit 3; fi +source "$TESTKIT_ROOT/library/establish_environment.sh" + +oneTimeSetUp() +{ + # a test environment initializer method called directly by shunit. + # you can add your own code here that is needed before the test run starts. + true +} + +# this exact test should always be the first one in a test suite. +testSanity() +{ + # make sure the test environment is good. + sanity_test_and_init + assertEquals "sanity test" 0 $? +} + +testCleaningPriorTestRun() +{ + echo taking steps to clean last test... +#if any. +} + +testDoAThing() +{ + echo doing one thing + assertEquals "doing that thing should work" 0 $? +} + +testDoAnotherThing() +{ + echo doing another thing here + assertEquals "doing that other thing should work" 0 $? + + echo "about to cause a failure, to test assertNotEquals..." + false + assertNotEquals "an explicit failure should be seen" 0 $? +} + +oneTimeTearDown() { + echo cleaning up after test now... +#if anything to do. +} + +# load and run shUnit2 +source "$SHUNIT_DIR/shunit2" + diff --git a/testkit/examples/fred-testkit.config b/testkit/examples/fred-testkit.config new file mode 100644 index 00000000..0d26181a --- /dev/null +++ b/testkit/examples/fred-testkit.config @@ -0,0 +1,30 @@ +# This is an example configuration file for the TestKit. + +#### +# this is fred t. hamster's personal testkit config file. +#### + +############## + +# This section defines variables that are used throughout the tests. +# Many of these need to change to suit your particular configuration. + +# The base user name is used for any paths below that refer to the user who +# will be running the tools and tests. This should be changed to the actual +# user account under which the tools and tests will be run, if the default +# value based on USER cannot be relied upon. +BASE_USER=${USER} + +# Used for windows testing; provides the path to the binaries directory of cygwin. +#CYGWIN_BIN_PATH=c:/cygwin/bin + +############## + +# define the tests to run. this is the most convenient place to put this. +# the test suite list obviously will vary a lot based on what is being tested. +TESTKIT_TEST_SUITE=( \ + $TESTKIT_ROOT/examples/blank_test.sh \ +) + +############## + diff --git a/testkit/library/establish_environment.sh b/testkit/library/establish_environment.sh new file mode 100644 index 00000000..b49810a9 --- /dev/null +++ b/testkit/library/establish_environment.sh @@ -0,0 +1,94 @@ +#!/bin/bash + +# Supports the TestKit with a few handy functions and many variables. +# +# Author: Chris Koeritz + +############## + +# pull in the really basic functions... +source "$TESTKIT_ROOT/library/helper_methods.sh" + +############## + +# this check should be called first, in oneTimeSetUp, in every test script that uses shunit. +# it will make sure that important facts are true about the test environment. +# +#hmmm: need to extend this to allow them to add their own sanity checks to it, +# similarly to how we need to add log parsing capability as an extension. +# +function sanity_test_and_init() +{ + if [ -z "$WORKDIR" ]; then + echo "The WORKDIR variable is not set. This should be established by each test, near the top." + exit 1 + fi + # establish this for shunit so tests do not have to run in current directory. + export SHUNIT_PARENT="$WORKDIR/$(basename "$0")" + +#hmmm: add other checks here, including the user provided ones. + + return 0 +} + +############## + +# this is the main source of parameters for the tests. +export TESTKIT_CFG_FILE +if [ ! -f "$TESTKIT_CFG_FILE" ]; then + # well, no config file variable defined, so we go with our default. + # this file must exist or we're stumped. + TESTKIT_CFG_FILE="$TESTKIT_ROOT/testkit.config" +fi +if [ ! -f "$TESTKIT_CFG_FILE" -a -z "$BADNESS" ]; then + echo "----" + echo "This script requires that you prepare a customized file in:" + echo " $TESTKIT_CFG_FILE" + echo " (above is current value of TESTKIT_CFG_FILE variable)" + echo "with the details of your testing configuration." + echo "There are some example config files in the folder:" + echo " $TESTKIT_ROOT/examples" + BADNESS=true +fi + +############## + +# make sure we aren't premature in processing the config file. +if [ -z "$TESTKIT_BOOTSTRAPPING" ]; then + + # read the config file and generate environment variables for all the entries. + source "$TESTKIT_ROOT/library/process_configuration.sh" + define_and_export_variables + check_if_failed "Not all variables could be imported properly from the configuration file '$TESTKIT_CFG_FILE'" + +fi + +############## + +# announce status if in debugging mode. + +if [ ! -z "$DEBUGGING" -a -z "$SHOWED_SETTINGS_ALREADY" ]; then + echo +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + echo TestKit running from: $TESTKIT_ROOT + echo TestKit config file: $TESTKIT_CFG_FILE + echo +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +fi + +############## + +# more steps we're not ready for if still bootstrapping our environment. +if [ -z "$TESTKIT_BOOTSTRAPPING" ]; then + # try to not blast out the above block of info again during this run. + export SHOWED_SETTINGS_ALREADY=true + + # now that we have the environment set up, we can pull in all the functions + # we use for running the tests... + source "$TESTKIT_ROOT/library/runner_functions.sh" + source "$TESTKIT_ROOT/library/random_ids_manager.sh" + #hmmm: random ids are not used yet, are they? are they working? + source "$TESTKIT_ROOT/library/file_management.sh" + +fi + +############## + diff --git a/testkit/library/extract_der_from_pfx.sh b/testkit/library/extract_der_from_pfx.sh new file mode 100644 index 00000000..73733973 --- /dev/null +++ b/testkit/library/extract_der_from_pfx.sh @@ -0,0 +1,21 @@ + +pfx_file="$1"; shift +if [ -z "$pfx_file" -o ! -f "$pfx_file" ]; then + echo "This script requires the full path to a PFX file which will have its" + echo "certificate extracted into a DER formatted file ending in '.cer'." + exit 1 +fi + +intermed_file="$TMP/$(basename "$pfx_file" .pfx).pem" +final_file="$(dirname "$pfx_file")/$(basename "$pfx_file" .pfx).cer" + +echo input PFX file is $pfx_file +echo generating intermediate PEM file... +openssl pkcs12 -in "$pfx_file" -out "$intermed_file" -nodes -nokeys +if [ $? -ne 0 ]; then echo "previous step failed!"; exit 1; fi +echo generated intermediate PEM file $intermed_file +echo generating final file in DER format... +openssl x509 -outform der -in "$intermed_file" -out "$final_file" +if [ $? -ne 0 ]; then echo "previous step failed!"; exit 1; fi +echo final DER file successfully saved as $final_file + diff --git a/testkit/library/file_management.sh b/testkit/library/file_management.sh new file mode 100644 index 00000000..82893a66 --- /dev/null +++ b/testkit/library/file_management.sh @@ -0,0 +1,90 @@ + +# these are the pieces that we'll use to assemble mostly random files. +RANDOM_CHUNK_FILES=($TEST_TEMP/random.0 $TEST_TEMP/random.1 $TEST_TEMP/random.2 $TEST_TEMP/random.3 $TEST_TEMP/random.4) + +# largest chunk of random data we'll actually generate at a time, in each chunk file. +export MAX_CHUNK_FILE=65536 + +# returns the file size for the first argument. +function getFileSize() +{ + local file="$1"; shift + if isMacOSX; then + stat -f%z "$file" + else + stat --printf="%s" "$file" + fi +} + +# outputs the number of seconds since the epoch. +function getTimeStamp() +{ + date +%s +} + +# makes sure the chunk files are all generated. +function prepareRandomChunks() +{ + local i + for ((i = 0; i < ${#RANDOM_CHUNK_FILES[@]}; i++)); do + # make the chunk files if they don't exist. + local currfile="${RANDOM_CHUNK_FILES[$i]}" + if [ ! -f "$currfile" ]; then + local filesize=$MAX_CHUNK_FILE + # pick a value to add or subtract from the constant sized chunk, so we won't always be + # using files at the same boundaries or with a power of 2 size. + local moddy=$(( ($(echo $RANDOM) % 128) - 64 )) + ((filesize -= $moddy)) +#echo creating chunk file $currfile of size $filesize + dd if=/dev/urandom of=$currfile bs=1 count=$filesize &>/dev/null + assertEquals "creating random chunk file $currfile" 0 $? + fi + done +} + +# creates a somewhat random file for testing. this will be assembled out of +# our chunks of random files, so is not truly random, but we've found that the +# random number generator is a HUGE drag on our testing speed. this is still +# pretty random data. the first argument is the file name and the second is +# the desired file size. +function createRandomFile() +{ + local file="$1"; shift + local size="$1"; shift + + prepareRandomChunks + + local stampBefore=$(getTimeStamp) + + # truncate any existing stuff. + echo -n >"$file" + + while [ $(getFileSize "$file") -lt $size ]; do + which_chunker=$(expr $(echo $RANDOM) % ${#RANDOM_CHUNK_FILES[@]}) +#echo choosing chunk file $which_chunker + cat "${RANDOM_CHUNK_FILES[$which_chunker]}" >>"$file" + done + +#echo file size after random chunkings is $(getFileSize "$file") + + local fsizenow="$(getFileSize "$file")" +#echo size now is $fsizenow and desired is $size + if [ $fsizenow -gt $size ]; then +#echo trying to truncate file + truncate -s $size "$file" + fi +#echo file size after truncate is $(getFileSize "$file") and expected size is $size + + local stampAfter=$(getTimeStamp) + local secs=$(($stampAfter - $stampBefore)) + if [ $secs -le 0 ]; then + # even though it claims it took zero time, we know better, but we also don't want to + # divide by zero, so it loses its credit for being very fast here. + secs=1 + fi + local kbs=$(( $size / $secs / 1024)) + + local fsizenow="$(getFileSize "$file")" + assertEquals "Creating random file of $size bytes at ${kbs} kbps in: $file" $size $fsizenow +} + diff --git a/testkit/library/gen_cert_from_ca.sh b/testkit/library/gen_cert_from_ca.sh new file mode 100644 index 00000000..d068ea0e --- /dev/null +++ b/testkit/library/gen_cert_from_ca.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +#hmmm: NOT ported to testkit yet. + +# creates a new certificate based on the grid's signing cert. +# +# Author: Chris Koeritz + +############## + +#### + +export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )" # obtain the script's working directory. + +# pull in the xsede test base support. +source "$WORKDIR/../prepare_tools.sh" "$WORKDIR/../prepare_tools.sh" + +# if that didn't work, complain. +if [ -z "$TESTKIT_SENTINEL" ]; then echo Please run prepare_tools.sh before testing.; exit 3; fi + +# load the bash libraries we need. +source "$TESTKIT_ROOT/library/helper_methods.sh" +#source "$TESTKIT_ROOT/library/runner_functions.sh" +source "$TESTKIT_ROOT/library/security_management.sh" + +#### + +if [ $# -lt 7 ]; then + echo "this script needs 7 parameters:" + echo "1: signing cert PFX file to base new cert on." + echo "2: the password for the signing cert PFX." + echo "3: the alias of the key to use within the signing PFX." + echo "4: output file to create with new certificate in PFX format." + echo "5: password for new certificate PFX file." + echo "6: alias for the certificate within the PFX file." + echo "7: Common Name (CN) of the identity signified by the new certificate." + echo + echo "This is an example run using a signing cert for a container:" + echo + echo 'bash $TESTKIT_ROOT/library/gen_cert_from_ca.sh signing-cert.pfx signer signing-cert $HOME/new_cert.pfx myPassword certalias "Fred Powers"' + exit 1 +fi + +signing_cert="$1"; shift +signing_passwd="$1"; shift +signing_alias="$1"; shift +output_file="$1"; shift +output_passwd="$1"; shift +output_alias="$1"; shift +output_cn="$1"; shift + +create_pfx_using_CA "$signing_cert" "$signing_passwd" "$signing_alias" "$output_file" "$output_passwd" "$output_alias" "$output_cn" +check_if_failed "generating '$output_file' from '$signing_cert'" + +echo "New certificate was generated into: $output_file" + +exit 0 + diff --git a/testkit/library/generate_csr_from_existing_key.sh b/testkit/library/generate_csr_from_existing_key.sh new file mode 100644 index 00000000..bcd8ac8d --- /dev/null +++ b/testkit/library/generate_csr_from_existing_key.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +privkey="$1"; shift +subject="$1"; shift +csrfile="$1"; shift + +function print_instructions() +{ + echo -e "\n\ +This script creates a new CSR (certificate signing request) file for you from\n\ +an existing private key. Getting a new certificate using this CSR ensures\n\ +that previously signed resources can still be considered properly signed, even\n\ +after the original certificate has expired, by using the new certificate for\n\ +validation. After the new CSR file is generated, it must be sent to the\n\ +certificate authority and they can generate a new certificate for you.\n\ +\n\ +The script takes three parameters. The first is the file in which the\n\ +private key is stored in PEM format. The second parameter is the subject\n\ +to use in the certificate (who the certificate is issued to). The third\n\ +parameter is the output file for the generated CSR (certificate signing\n\ +request).\n\ +\n\ +For example:\n\ + $(basename $0) my-private.key \"Julius Orange\" orange-new-cert-request.csr\n\ +" +} + +if [ -z "$privkey" -o -z "$subject" -o -z "$csrfile" -o ! -f "$privkey" ]; then + print_instructions + echo -e "\nThere was a missing parameter or the private key file did not exist." + exit 1 +fi + +openssl req -new -key "$privkey" -nodes -subj "$subject" -out "$csrfile" + + diff --git a/testkit/library/helper_methods.sh b/testkit/library/helper_methods.sh new file mode 100644 index 00000000..7be57812 --- /dev/null +++ b/testkit/library/helper_methods.sh @@ -0,0 +1,312 @@ +#!/bin/bash + +# useful functions that are somewhat general. these are not needed for +# the basic setup of the test environment, but they are used by other +# test and tool functions and also by specific tests. +# +# Author: Chris Koeritz + +# prints out a timestamp with the current date and time up to seconds. +function date_string() +{ + date +"%Y_%b_%e_%H%M_%S" | sed -e 's/ //g' +} + +# prints out the timestamp in a somewhat readable way. +function readable_date_string() +{ + date +"%Y-%m-%d %T" +} + +######## +# (donated by the feisty meow scripts at http://feistymeow.org) + + function is_array() { + [[ "$(declare -p $1)" =~ "declare -a" ]] + } + + function is_alias() { + alias $1 &>/dev/null + return $? + } + + # displays the value of a variable in bash friendly format. + function var() { + HOLDIFS="$IFS" + IFS="" + while true; do + local varname="$1"; shift + if [ -z "$varname" ]; then + break + fi + + if is_alias "$varname"; then +#echo found $varname is alias + local tmpfile="$(mktemp $TMP/aliasout.XXXXXX)" + alias $varname | sed -e 's/.*=//' >$tmpfile + echo "alias $varname=$(cat $tmpfile)" + \rm $tmpfile + elif [ -z "${!varname}" ]; then + echo "$varname undefined" + else + if is_array "$varname"; then +#echo found $varname is array var + local temparray + eval temparray="(\${$varname[@]})" + echo "$varname=(${temparray[@]})" +#hmmm: would be nice to print above with elements enclosed in quotes, so that we can properly +# see ones that have spaces in them. + else +#echo found $varname is simple + echo "$varname=${!varname}" + fi + fi + done + IFS="$HOLDIFS" + } +######## + +# given a file name and a phrase to look for, this replaces all instances of +# it with a piece of replacement text. note that slashes are okay in the two +# text pieces, but we are using pound signs as the regular expression +# separator; phrases including the octothorpe (#) will cause syntax errors. +function replace_phrase_in_file() +{ + local file="$1"; shift + local phrase="$1"; shift + local replacement="$1"; shift + if [ -z "$file" -o -z "$phrase" ]; then + echo "replace_phrase_in_file: needs a filename, a phrase to replace, and the" + echo "text to replace that phrase with." + return 1 + fi + sed -i -e "s%$phrase%$replacement%g" "$file" +} + +# prints an error message (from parameters) and exits if the previous command failed. +function check_if_failed() +{ + if [ $? -ne 0 ]; then + echo Step failed: $* + exit 1 + fi +} + +# takes a first parameter that is the name for a combined error and output log, +# and then runs all the other parameters as a command. +function logged_command() +{ + local my_output="$1"; shift +# echo "logged_command args: $(printf -- "[%s] " "${@}")" + eval "${@}" >>"$my_output" 2>&1 + local retval=$? + if [ $retval == 0 ]; then + # good so far, but check for more subtle ways of failing; if there is + # an occurrence of our fail message in the output, that also indicates + # the command did not succeed. + grep "\[FAILURE\]" $my_output + # we do not want to see that phrase in the log. + if [ $? != 0 ]; then + return 0 # fine exit, can ignore log. + fi + fi + if [[ ! "$my_output" =~ .*fuse_output.* ]]; then + # this was a failure, so we need to see the log. + # fuse errors currently don't count since they are multifarious. + cat "$my_output" + fi + return 1 +} + +# runs an arbitrary command. if the command fails, then the output from it is +# displayed and an error code is returned. otherwise the output is discarded. +function run_any_command() +{ + local my_output="$(mktemp $TEST_TEMP/test_logs/out_run_any_cmd_$(date_string).XXXXXX)" + logged_command "$my_output" "${@}" + local retval=$? + # make the external version of the log file available. if we're multiplexing users, + # this will be clobbered constantly, which is why we used unique names above. + \cp -f "$my_output" "$TESTKIT_OUTPUT_FILE" + # then add the logging results to our huge mongo log of all actions. + echo >> "$CONGLOMERATED_TESTKIT_OUTPUT" + echo "$(readable_date_string) log from: $my_output" >> "$CONGLOMERATED_TESTKIT_OUTPUT" + echo "=======" >> "$CONGLOMERATED_TESTKIT_OUTPUT" + cat "$my_output" >> "$CONGLOMERATED_TESTKIT_OUTPUT" + echo "=======" >> "$CONGLOMERATED_TESTKIT_OUTPUT" + # and now remove the tiny individual log file so we don't litter. + \rm -f "$my_output" + return $retval +} + + +# returns 0 if there should be no problems using fuse, or non-zero if this platform +# does not currently support fuse. +function fuse_supported() +{ + local retval=0 + local platform="$(uname -a | tr A-Z a-z)" + if [[ $platform =~ .*darwin.* ]]; then retval=1; fi + if [[ $platform =~ .*cygwin.* ]]; then retval=1; fi + if [[ $platform =~ .*ming.* ]]; then retval=1; fi + return $retval +} + +# returns 0 if there should be no problems creating links in the file system, +# or non-zero if this platform does not support symbolic links. +function links_supported() +{ + local retval=0 + local platform="$(uname -a | tr A-Z a-z)" + if [[ $platform =~ .*cygwin.* ]]; then retval=1; fi + if [[ $platform =~ .*ming.* ]]; then retval=1; fi + return $retval +} + +# Create a test directory (in the first parameter) with $2 subdirectories, +# each with $3 subdirs, each with $4 files. +fan_out_directories() +{ + local dir_name="$1"; shift + local top_count=$1; shift + local mid_count=$1; shift + local file_count=$1; shift + mkdir "$dir_name" + for (( di=0 ; di<$top_count ; di++ )); do + mkdir "$dir_name"/sub$di + for (( dj=0 ; dj<$mid_count ; dj++ )); do + mkdir "$dir_name"/sub$di/sub$dj + for (( dk=0 ; dk<$file_count ; dk++ )); do + echo "file $di$dj$dk" > "$dir_name"/sub$di/sub$dj/file$di$dj$dk + done + done + done +} +############## + +# copied from open source codebase at: http://feistymeow.org with permission of author (chris koeritz), +# assigned apache ASL license for this usage. + # locates a process given a search pattern to match in the process list. + # supports a single command line flag style parameter of "-u USERNAME"; + # if the -u flag is found, a username is expected afterwards, and only the + # processes of that user are considered. + function psfind() { + local -a patterns=("${@}") +#echo ==== +#echo patterns list is: "${patterns[@]}" +#echo ==== + + local user_flag + if [ "${patterns[0]}" == "-u" ]; then + user_flag="-u ${patterns[1]}" +#echo "found a -u parm and user=${patterns[1]}" + # void the two elements with that user flag so we don't use them as patterns. + unset patterns[0] patterns[1] + else + # select all users. + user_flag="-e" + fi + + local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")" + local -a PIDS_SOUGHT + + if [ "$OS" == "Windows_NT" ]; then + # gets cygwin's (god awful) ps to show windoze processes also. + local EXTRA_DOZER_FLAGS="-W" + # pattern to use for peeling off the process numbers. + local pid_finder_pattern='s/ *\([0-9][0-9]*\) *.*$/\1/p' + + else + # flags which clean up the output on unixes, which apparently cygwin + # doesn't count as. their crappy specialized ps doesn't support this. + local EXTRA_UNIX_FLAGS="-o pid,args" + # pattern to use for peeling off the process numbers. + local pid_finder_pattern='s/^[[:space:]]*\([0-9][0-9]*\).*$/\1/p' + fi + + /bin/ps $EXTRA_DOZER_FLAGS $EXTRA_UNIX_FLAGS $user_flag | tail -n +2 >$PID_DUMP +#echo ==== +#echo got all this stuff in the pid dump file: +#cat $PID_DUMP +#echo ==== + + # search for the pattern the user wants to find, and just pluck the process + # ids out of the results. + local i + for i in "${patterns[@]}"; do + PIDS_SOUGHT+=($(cat $PID_DUMP \ + | grep -i "$i" \ + | sed -n -e "$pid_finder_pattern")) + done +#echo ==== +#echo pids sought list became: +#echo "${PIDS_SOUGHT[@]}" +#echo ==== + + if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then + local PIDS_SOUGHT2=$(printf -- '%s\n' ${PIDS_SOUGHT[@]} | sort | uniq) + PIDS_SOUGHT=() + PIDS_SOUGHT=${PIDS_SOUGHT2[*]} + echo ${PIDS_SOUGHT[*]} + fi + /bin/rm $PID_DUMP + } + + +####### + +# tests the supposed fuse mount that is passed in as the first parameter. +function test_fuse_mount() +{ + local mount_point="$1"; shift + local trunc_mount="$(basename "$(dirname $mount_point)").$(basename "$mount_point")" + + checkMount="$(mount)" +#echo "checkmount is: '$checkMount'" +#echo "mount point seeking is: '$trunc_mount'" + local retval=1 + if [[ "$checkMount" =~ .*$trunc_mount.* ]]; then +#echo found the mount in the list + retval=0 + fi + if [ $retval -ne 0 ]; then + echo "Finding mount point '$trunc_mount' failed." + return 1 + fi + ls -l "$mount_point" &>/dev/null + return $? +} + +####### + +# also borrowed from feisty meow scripts... by consent of author (chris koeritz). + + # is this the Mac OS X operating system? + function isMacOSX() + { + if [ ! -z "$(echo $OSTYPE | grep -i darwin)" ]; then + true + else + false + fi + } + + # switches from a /X/path form to an X:/ form. this also processes cygwin paths. + function unix_to_dos_path() { + # we usually remove dos slashes in favor of forward slashes. + if [ ! -z "$SERIOUS_SLASH_TREATMENT" ]; then + # unless this flag is set, in which case we force dos slashes. + echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/' | sed -e 's/\//\\/g' + else + echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/' + fi + } + + # switches from an X:/ form to an /X/path form. + function dos_to_unix_path() { + # we always remove dos slashes in favor of forward slashes. + echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/' + } + +####### + diff --git a/testkit/library/process_configuration.sh b/testkit/library/process_configuration.sh new file mode 100644 index 00000000..c2d894f3 --- /dev/null +++ b/testkit/library/process_configuration.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# Processes the XSEDE tools config file to turn variables listed in the +# file into exported variables in the environment. +# +# Author: Chris Koeritz + +############## + +# this processes the single file of input parameters at the test root and +# turns it into a collection of environment variables. we then load all those +# variables into the current environment. we will also automatically fill in +# some important variables here that we'll use later. in some cases, we will +# use the existing value if the variable is already set. +define_and_export_variables() +{ + if [ -z "$TESTKIT_SENTINEL" ]; then echo Please run prepare_tools.sh before testing.; return 3; fi + + # create our output folder so we can store logs and temporaries. + mkdir -p "$TEST_TEMP" &>/dev/null + + # start writing the environment file. + echo > $TEST_TEMP/env_file + + # turn each useful line in input file into an environment variable declaration. + while read line; do + # match lines that are comments or blank. + echo "$line" | grep -e '^[#;]' -e '^[ ]*$' &>/dev/null + # only export non-useless lines. + if [ $? != 0 ]; then + echo "$line" | grep '[a-z0-9A-Z]=(' &>/dev/null + if [ $? == 0 ]; then + # it's an array variable so don't try to export it or bash loses it for us. + echo $line >> $TEST_TEMP/env_file + else + echo "export" $line >> $TEST_TEMP/env_file + fi + fi + done < "$TESTKIT_CFG_FILE" + + # now run the environment file to add those settings to our environment. + chmod +x $TEST_TEMP/env_file + source $TEST_TEMP/env_file &>/dev/null +} + diff --git a/testkit/library/produce_test_package.sh b/testkit/library/produce_test_package.sh new file mode 100644 index 00000000..b6a93f62 --- /dev/null +++ b/testkit/library/produce_test_package.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Creates an archive from the test scripts. +# +# Author: Chris Koeritz + +export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )" # obtain the script's working directory. +cd "$WORKDIR" +export SHOWED_SETTINGS_ALREADY=true +if [ -z "$TESTKIT_SENTINEL" ]; then + source ../prepare_tools.sh ../prepare_tools.sh +fi +source "$TESTKIT_ROOT/library/establish_environment.sh" + +pushd "$TESTKIT_ROOT/.." &>/dev/null +justdir="$(basename "$TESTKIT_ROOT")" + +date_string="$(date +"%Y_%b_%e_%H%M" | sed -e 's/ //g')" + +EXCLUDES=(--exclude=".svn" --exclude="docs" --exclude="random*.dat" --exclude=gzip-1.2.4 --exclude=iozone3_397 --exclude="mount-*" --exclude="releases" --exclude="passwords.txt" --exclude="saved_deployment_info.txt" --exclude="generated_certs" --exclude="gridwide_certs" --exclude="testkit.config*" --exclude="inputfile.txt*") + +tar -czf "$HOME/testkit_${date_string}.tar.gz" "$justdir" ${EXCLUDES[*]} + +popd &>/dev/null + diff --git a/testkit/library/random_ids_manager.sh b/testkit/library/random_ids_manager.sh new file mode 100644 index 00000000..ad938818 --- /dev/null +++ b/testkit/library/random_ids_manager.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# an unfinished idea for how to manage identifiers that are randomly +# generated but are also uniquely identified for later use. +# +# Author: Chris Koeritz + +export RANDOM_IDS=() + +# given a name for a randomly assigned number, this will generate a +# new random value and add it to the RANDOM_IDS list. it is an error +# to try to change an existing random id, because the id may already have +# been used in file generation and so forth. +function setup_random_id() +{ + name="$1"; shift + if [ ! -z "${RANDOM_IDS[$name]}" ]; then + echo "FAILURE: trying to reassign already generated random id for '$name'" + return 1 + fi + new_id=$RANDOM-$RANDOM-$RANDOM + RANDOM_IDS[$name]=$new_id + return 0 +} + +# returns the random value assigned under the name. it is an error +# to request one that does not exist yet; this implies the test has +# not properly configured the random ids it will use. +function get_random_id() +{ + name="$1"; shift + if [ -z "${RANDOM_IDS[$name]}" ]; then + echo "FAILURE-to-find-$name" + return 1 + fi + echo "${RANDOM_IDS[$name]}" + return 0 +} + +## +## # test suite +## setup_random_id "george" +## if [ $? -ne 0 ]; then echo TEST failed to set george; fi +## setup_random_id "lucy" +## if [ $? -ne 0 ]; then echo TEST failed to set lucy; fi +## echo "lucy's id is: $(get_random_id lucy)" +## lucy_id=$(get_random_id lucy)= +## if [ $? -ne 0 ]; then echo TEST failed to get lucy; fi +## echo "george's id is: $(get_random_id george)" +## george_id=$(get_random_id george) +## if [ $? -ne 0 ]; then echo TEST failed to get george; fi +## +## setup_random_id "george" &>/dev/null +## if [ $? -eq 0 ]; then echo TEST failed to trap george being reset; fi +## setup_random_id "lucy" &>/dev/null +## if [ $? -eq 0 ]; then echo TEST failed to trap lucy being reset; fi +## get_random_id tony +## if [ $? -eq 0 ]; then echo TEST failed to trap non-existent id request; fi +## + + diff --git a/testkit/library/runner_functions.sh b/testkit/library/runner_functions.sh new file mode 100644 index 00000000..c6d6ade5 --- /dev/null +++ b/testkit/library/runner_functions.sh @@ -0,0 +1,99 @@ +#!/bin/bash + +# assorted useful ways of running executables. +# +# Author: Chris Koeritz + +############## + +# similar to timed_grid, but for arbitrary commands, and fits in with the timing +# calculators. +function timed_command() +{ + echo "[started timer $(readable_date_string)]" + $(\which time) -p -o "$TESTKIT_TIMING_FILE" $* + local retval=$? + echo "[stopped timer $(readable_date_string)]" + return $retval +} + +############## + +# uses the timing file to determine how long the last activity took in +# seconds and then prints out the value. +calculateTimeTaken() +{ + head -n 1 $TESTKIT_TIMING_FILE | awk '{print $2}' +} + +# calculates the bandwidth for a transfer. this takes the elapsed time as +# the first parameter and the size transferred as second parameter. +calculateBandwidth() +{ + local real_time="$1"; shift + local size="$1"; shift + # drop down to kilobytes rather than bytes. + size=$(echo $size / 1024 | $(\which bc) -l) + +#echo "time=$real_time size=$size" + + local total_sec="$(echo "$real_time" | awk -Fm '{print $1}'| awk -F. '{print $1}' )" + local bandwidth="" + if [[ "$total_sec" =~ .*exited.* ]]; then + echo "FAILURE: Test run failed in some way; setting total seconds to very large number." + total_sec="99999999" + fi + if [ $total_sec -eq 0 ]; then + # fake it when we get a math issue where something took less than a second. + total_sec=1 + fi + bandwidth="$(echo "scale=3; $size / $total_sec" | $(\which bc) -l)" + echo "$bandwidth" +} + +# a wrapper for calculateBandwidth that prints out a nicer form of the +# bandwidth. it requires the same parameters as calculateBandwidth. +showBandwidth() +{ + echo " Bandwidth $(calculateBandwidth $*) kbps" +} + +############## + +# connects to a host as a particular user and executes a command there. +function run_command_remotely() +{ + if [ $# -lt 3 ]; then + echo This function connects to a remote host to run a command. It requires + echo at least three parameters: the host to connect to, the user name on + echo that host which supports passwordless logins, and the command to run. + echo The command to run is the third through Nth parameters. + fi + host="$1"; shift + username="$1"; shift + # run our expecter to feed commands in, and the last one needs to be exit so we + # return to the original host. + OUTFILE="$(mktemp $TMP/ssh_run.XXXXXX)" + expect $TESTKIT_ROOT/library/ssh_expecter.tcl "$username" "" "$host" "${@}" >"$OUTFILE" + reval=$? + # make sure we didn't experience a failure on the other side. + grep "YO_FAILURE" $OUTFILE &>/dev/null + if [ $? -eq 0 ]; then + echo Detected failure running command via ssh. + ((retval++)) + fi + +#debugging +echo ========== output from command ============ +cat "$OUTFILE" +echo =========================================== + + rm "$OUTFILE" + return $retval +} + +#testing +#run_command_remotely serene fred "ls /" + +############## + diff --git a/testkit/library/tear_down_test_infrastructure.sh b/testkit/library/tear_down_test_infrastructure.sh new file mode 100644 index 00000000..b1e38b7d --- /dev/null +++ b/testkit/library/tear_down_test_infrastructure.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +# Tears down the demo users previously set up for multi-user testing. +# +# Author: Chris Koeritz + +export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )" # obtain the script's working directory. +cd "$WORKDIR" + +if [ -z "$TESTKIT_SENTINEL" ]; then echo Please run prepare_tools.sh before testing.; exit 3; fi +source "$TESTKIT_ROOT/library/establish_environment.sh" + +progname="$(basename "$0")" + +if [ $# -lt 1 ]; then + echo "$progname: This script needs a single parameter, which is the container" + echo "path to use for the authentication (e.g. $STS_LOC)" + exit 3 +fi + +ADMIN_CONTAINER=$1; shift +echo "container location is $ADMIN_CONTAINER " + +# we test for ten users currently. +user_count=10 + +# login the right power user that can delete other user entries. +testLoginAsAdmin() +{ + if [ -z "$NON_INTERACTIVE" ]; then + login_a_user admin + fi +} + +# now that we're logged in appropriately, delete our corresponding set of users. +testDeleteUsers() +{ + local x + for (( x=0; x < ${#MULTI_USER_LIST[*]}; x++ )); do + username="${MULTI_USER_LIST[$x]}" + echo "Whacking user '$username'..." + passwd="${MULTI_PASSWORD_LIST[$x]}" + # now do the heavy lifting to get that user set up. + silent_grid script "local:'$TESTKIT_ROOT/library/delete_one_user.xml'" "$CONTAINERPATH" "$(basename $username)" "$username" "$SUBMIT_GROUP" + assertEquals "Should delete user '$username' successfully" 0 $? + done +} + +# make sure we don't leave them logged in as an administrator. +testLogoutAgain() +{ + if [ -z "$NON_INTERACTIVE" ]; then + silent_grid logout --all + assertEquals "Final logout of the grid" 0 $? + fi +} + +testLoginNormalUser() +{ + if [ -z "$NON_INTERACTIVE" ]; then + login_a_user normal + fi +} + +# load and run shUnit2 +source "$SHUNIT_DIR/shunit2" + diff --git a/testkit/makefile b/testkit/makefile new file mode 100644 index 00000000..115c1a2e --- /dev/null +++ b/testkit/makefile @@ -0,0 +1,9 @@ +include variables.def + +PROJECT = testkit +FIRST_TARGETS += run_testkit_check + +include rules.def + +run_testkit_check: + $(HIDESH) -c 'bash test_driver.sh' diff --git a/testkit/prepare_tools.sh b/testkit/prepare_tools.sh new file mode 100644 index 00000000..9186522e --- /dev/null +++ b/testkit/prepare_tools.sh @@ -0,0 +1,129 @@ +#!/bin/bash + +# Author: Chris Koeritz +# +# Note: +# We do not want to "exit" from this file at all (nor from any file that it +# invokes either), since this script is intended for use by the bash 'source' +# command. If we exit, that will exit from the calling shell as well, which +# torpedoes whatever one was doing in that shell. +# There is a variable below called BADNESS that indicates when errors +# occurred during processing, and if it's not empty at the end of the script +# then we will consider this a failed run, and we will not set the test's +# sentinel variable which other scripts check to see if the environment +# was loaded properly. + +# make sure whether they have defined the top-level location for us. +if [ ! -z "$1" ]; then + # first attempt is to use the first parameter, if one is provided. this should + # be an absolute path reference to this very file, from which we can deduce the + # starting directory. + GRITTY_TESTING_TOP_LEVEL="$( cd "$( dirname "$1" )" && \pwd )" + # for this case, they also don't need to be stranded in a new shell, because we + # assume they have sourced this file instead of bashing it. + NO_SUBSHELL=true +fi +if [ -z "$GRITTY_TESTING_TOP_LEVEL" ]; then + # otherwise, if they didn't explicitly set the top-level directory, we will + # do it using some unix trickery. + if [[ "$0" =~ .*bash ]]; then + echo "----" + echo "This script was not launched properly with 'source'. The script should" + echo "be started like this: source prepare_tools.sh prepare_tools.sh" + echo "The double entry is required for bash's source command to find the path." + BADNESS=true + fi + GRITTY_TESTING_TOP_LEVEL="$( cd "$( dirname "$0" 2>/dev/null )" && \pwd )" +else + # we assume they are managing this script more closely and do not need (or want) a bash sub-shell. + NO_SUBSHELL=true +fi +GRITTY_TESTING_TOP_LEVEL="$(echo "$GRITTY_TESTING_TOP_LEVEL" | sed -e 's/\/cygdrive\/\(.\)/\1:/')" + +# the top-level directory for tests, i.e. the root of testing hierarchy. +export TESTKIT_ROOT="$GRITTY_TESTING_TOP_LEVEL" + +# a bit of a dance to not pull in code too early... +export TESTKIT_BOOTSTRAPPING=true +source "$TESTKIT_ROOT/library/establish_environment.sh" +unset TESTKIT_BOOTSTRAPPING +# done with dancing, ready to pull in anything else from testkit. + +#source "$TESTKIT_ROOT/library/helper_methods.sh" + +# where the shunit library resides. +export SHUNIT_DIR="$TESTKIT_ROOT/shunit" + +# establish the TMP variable if it's not already set. +export TMP +if [ -z "$TMP" ]; then + TMP="$HOME/tmp" + if [ ! -d "$TMP" ]; then mkdir "$TMP"; fi +fi +TMP="$(echo "$TMP" | sed -e 's/\/cygdrive\/\(.\)/\1:/')" +if [ ! -d "$TMP" ]; then + echo "The TMP directory was set as $TMP but cannot be created or found." + echo "If there is a file at that location, please move or delete it." + exit 1 +fi + +############## + +# commonly used environment variables... + +# TEST_TEMP is a folder where we can generate a collection of junk files. +export TEST_TEMP="$TMP/testkit_logs_${USER}" +if [ ! -d "$TEST_TEMP" ]; then + mkdir -p "$TEST_TEMP" +fi + +# this variable points to the last output from a grid command. +export TESTKIT_OUTPUT_FILE="$TEST_TEMP/testkit_output.log" +export TESTKIT_TIMING_FILE="$TEST_TEMP/testkit_times.log" +export CONGLOMERATED_TESTKIT_OUTPUT="$TEST_TEMP/full_testkit_output.log" + +############## + +# uncomment this to enable extra output. +export DEBUGGING=true + +############## + +# turn this printout off in non-debugging mode or if the terminal setting +# seems to indicate that we're running in a login environment (where any +# echoing to standard out can screw up scp and sftp for that account). +if [ ! -z "$DEBUGGING" -a -z "$SHOWED_SETTINGS_ALREADY" \ + -a -z "$BADNESS" -a -z "$SILENT_RUNNING" -a "${TERM}" != "dumb" \ + -a -z "$PBS_ENVIRONMENT" ]; then + echo "===========================================================" + echo "Testkit environment loaded." + var TESTKIT_ROOT TESTKIT_CFG_FILE TMP TEST_TEMP + echo "===========================================================" +fi + +if [ ! -z "$(uname -a | grep -i darwin)" -a -z "$BADNESS" ]; then + # add in the mac binaries if this is darwin. + export PATH="$TESTKIT_ROOT/bin/macosx:$PATH" +else + # no change, but we want to make sure sub-shells inherit the path. + export PATH="$PATH" +fi + +if [ -z "$NO_SUBSHELL" -a -z "$BADNESS" ]; then + # at this point we go into a new interactive shell, so as to ensure the + # environment parameters stay right. + # the self-location code at the top doesn't work properly if this file is + # sourced into a current environment. + bash +fi + +if [ ! -z "$BADNESS" ]; then + echo + echo "----" + echo "There were errors in setting up the xsede tests--see above messages." + unset TESTKIT_SENTINEL TESTKIT_ROOT GRITTY_TESTING_TOP_LEVEL SHUNIT_DIR BADNESS +else + # if things were successful, we can finally set our indicator for the scripts to check. + export TESTKIT_SENTINEL=initialized +fi + diff --git a/testkit/readme.txt b/testkit/readme.txt new file mode 100644 index 00000000..073558fb --- /dev/null +++ b/testkit/readme.txt @@ -0,0 +1,9 @@ + +Testkit is a platform agnostic set of tools for running unit tests. + +Testkit relies on the ShUnit scripts to run the tests. +We give our thanks to ShUnit. + +NOTE: this product is in active development and may not be suitable for +any purpose yet. +Please check back... diff --git a/scripts/shunit/example_shunit_test.sh b/testkit/shunit/example_shunit_test.sh similarity index 100% rename from scripts/shunit/example_shunit_test.sh rename to testkit/shunit/example_shunit_test.sh diff --git a/scripts/shunit/shunit2 b/testkit/shunit/shunit2 similarity index 100% rename from scripts/shunit/shunit2 rename to testkit/shunit/shunit2 diff --git a/scripts/shunit/shunit2_test.sh b/testkit/shunit/shunit2_test.sh similarity index 100% rename from scripts/shunit/shunit2_test.sh rename to testkit/shunit/shunit2_test.sh diff --git a/scripts/shunit/shunit2_test_asserts.sh b/testkit/shunit/shunit2_test_asserts.sh similarity index 100% rename from scripts/shunit/shunit2_test_asserts.sh rename to testkit/shunit/shunit2_test_asserts.sh diff --git a/scripts/shunit/shunit2_test_failures.sh b/testkit/shunit/shunit2_test_failures.sh similarity index 100% rename from scripts/shunit/shunit2_test_failures.sh rename to testkit/shunit/shunit2_test_failures.sh diff --git a/scripts/shunit/shunit2_test_helpers b/testkit/shunit/shunit2_test_helpers similarity index 100% rename from scripts/shunit/shunit2_test_helpers rename to testkit/shunit/shunit2_test_helpers diff --git a/scripts/shunit/shunit2_test_macros.sh b/testkit/shunit/shunit2_test_macros.sh similarity index 100% rename from scripts/shunit/shunit2_test_macros.sh rename to testkit/shunit/shunit2_test_macros.sh diff --git a/scripts/shunit/shunit2_test_misc.sh b/testkit/shunit/shunit2_test_misc.sh similarity index 100% rename from scripts/shunit/shunit2_test_misc.sh rename to testkit/shunit/shunit2_test_misc.sh diff --git a/scripts/shunit/shunit2_test_standalone.sh b/testkit/shunit/shunit2_test_standalone.sh similarity index 100% rename from scripts/shunit/shunit2_test_standalone.sh rename to testkit/shunit/shunit2_test_standalone.sh diff --git a/testkit/test_driver.sh b/testkit/test_driver.sh new file mode 100755 index 00000000..db058683 --- /dev/null +++ b/testkit/test_driver.sh @@ -0,0 +1,158 @@ +#!/bin/bash + +# This script runs through all the known tests by performing a test sweep. +# It should claim that all tests succeeded for a new build/configuration/etc. +# to be considered successful. +# +# The way to add tests to the full test set is to add full paths to the +# "TESTKIT_TEST_SUITE" variable. This can be done in your personal +# testkit.config file or in an exported variable set prior to running +# this script. +# +# Author: Chris Koeritz + +export TESTKIT_DIR="$( \cd "$(\dirname "$0")" && \pwd )" # obtain the script's working directory. +cd $TESTKIT_DIR + +TIME_START="$(date +"%s")" + +source prepare_tools.sh prepare_tools.sh + +# if that didn't work, complain. +if [ -z "$TESTKIT_SENTINEL" ]; then echo Please run prepare_tools.sh before testing.; exit 3; fi +source "$TESTKIT_ROOT/library/establish_environment.sh" + +verbosity="$1"; shift + +VERBOSE=1 + +if [ "$verbosity" == "--help" -o "$verbosity" == "-help" -o "$verbosity" == "-h" ]; then + echo "$(basename $0): Runs the available suite of tests." + echo + echo " $(basename $0) {summary | [full]}" + echo + echo "By default, the report will be a 'full' listing that includes all test" + echo "run logging. If 'summary' is passed as the first parameter, then only" + echo "the test results will be displayed." + exit 0 +fi + +if [ "$verbosity" == "summary" ]; then + VERBOSE=0 +fi + +############## + +# clean up any conglomerated log file. +\rm -f "$CONGLOMERATED_TESTKIT_OUTPUT" + +############## + +# define the sets of tests we'd like to run. + +NETBADGE_TESTS=( \ + netbadge_integrations/basic_integrations_test.sh \ +) + +############## + +if [ ! -z "$AUTOBUILD_RUNNING" ]; then + # only add some tests for automated, testing, bootstrap builds based on their needs. + +true # placeholder + +fi + +############## + +# now that all tests have been defined, we build up our total list of tests. + +echo Full set of tests: +for ((test_iter=0; $test_iter < ${#TESTKIT_TEST_SUITE[*]}; test_iter++)); do + echo "$(expr $test_iter + 1): ${TESTKIT_TEST_SUITE[$test_iter]}" +done + +############## + +FAIL_COUNT=0 + +REG_TEMP="$TEST_TEMP/run_$(date +"%Y_%m_%d")" +if [ ! -d "$REG_TEMP" ]; then + mkdir "$REG_TEMP" +fi + +# go to the top of the hierarchy. +cd "$TESTKIT_ROOT" + +for ((test_iter=0; $test_iter < ${#TESTKIT_TEST_SUITE[*]}; test_iter++)); do + echo -e "\n======================================================================" + echo -n `date`": " + echo "Now running test $(expr $test_iter + 1): ${TESTKIT_TEST_SUITE[$test_iter]}" + output_file="$(mktemp $REG_TEMP/test_log.XXXXXX)" + echo " Test output file: $output_file" + +# +#hmmm: no real way to check for errors in the general case, unless we define a +# set of sentinels for this. not done yet. +#--> +# echo "==============" >"$output_file" +# echo "Log state prior to test:" >>"$output_file" +# check_logs_for_errors >>"$output_file" +# echo "==============" >>"$output_file" + + if [ $VERBOSE -ne 1 ]; then + bash "${TESTKIT_TEST_SUITE[$test_iter]}" >>"$output_file" 2>&1 + retval=$? + else + bash "${TESTKIT_TEST_SUITE[$test_iter]}" 2>&1 | tee -a "$output_file" + retval=${PIPESTATUS[0]} + fi + + if [ $retval -ne 0 ]; then + ((FAIL_COUNT++)) + echo "FAILURE: exit code $retval for test ${TESTKIT_TEST_SUITE[$test_iter]}" + TEST_RESULTS[$test_iter]="FAIL" + else + echo "OK: successful test run for test ${TESTKIT_TEST_SUITE[$test_iter]}" + TEST_RESULTS[$test_iter]="OKAY" + fi + +#hmmm: same comment re error checking... define some tags to look for! +# echo "==============" >>"$output_file" +# echo "Log state after test:" >>"$output_file" +# check_logs_for_errors >>"$output_file" +# echo "==============" >>"$output_file" + +done + +# final analysis--how did the test run do? + +echo -e "\n\nResults table for this test run:\n" +for ((test_iter=0; $test_iter < ${#TESTKIT_TEST_SUITE[*]}; test_iter++)); do + num=$(expr $test_iter + 1) + if [ $num -lt 10 ]; then num="0$num"; fi + echo "$num: ${TEST_RESULTS[$test_iter]} -- ${TESTKIT_TEST_SUITE[$test_iter]}" +done +echo + +# figure out how long things took. +TIME_END="$(date +"%s")" +duration="$(($TIME_END - $TIME_START))" +# prepare to print duration in hours and minutes. +minutes="$(($duration / 60))" +hours="$(($minutes / 60))" +# grab out the hours we calculated from the minutes sum. +minutes="$(($minutes - $hours * 60))" +if (($minutes < 10)); then minutes="0$minutes"; fi +if (($hours < 10)); then hours="0$hours"; fi +echo "Total testing duration: $hours:$minutes hh:mm ($duration seconds total)" + +if [ $FAIL_COUNT -ne 0 ]; then + echo "FAILURE: $FAIL_COUNT Tests Failed out of ${#TESTKIT_TEST_SUITE[*]} Tests." + exit 1 +else + echo "OK: All ${#TESTKIT_TEST_SUITE[*]} Tests Ran Successfully." + exit 0 +fi + + diff --git a/testkit/testkit.config b/testkit/testkit.config new file mode 100644 index 00000000..0d26181a --- /dev/null +++ b/testkit/testkit.config @@ -0,0 +1,30 @@ +# This is an example configuration file for the TestKit. + +#### +# this is fred t. hamster's personal testkit config file. +#### + +############## + +# This section defines variables that are used throughout the tests. +# Many of these need to change to suit your particular configuration. + +# The base user name is used for any paths below that refer to the user who +# will be running the tools and tests. This should be changed to the actual +# user account under which the tools and tests will be run, if the default +# value based on USER cannot be relied upon. +BASE_USER=${USER} + +# Used for windows testing; provides the path to the binaries directory of cygwin. +#CYGWIN_BIN_PATH=c:/cygwin/bin + +############## + +# define the tests to run. this is the most convenient place to put this. +# the test suite list obviously will vary a lot based on what is being tested. +TESTKIT_TEST_SUITE=( \ + $TESTKIT_ROOT/examples/blank_test.sh \ +) + +############## + diff --git a/testkit/tools/bandwidth/set_tcp_config.sh b/testkit/tools/bandwidth/set_tcp_config.sh new file mode 100644 index 00000000..06d675d7 --- /dev/null +++ b/testkit/tools/bandwidth/set_tcp_config.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# this script modifies the linux kernel for maximum tcp buffer size, which can +# improve long-haul transfers over a wan. + +# new maximum buffer size to set. +new_max=2097152 + +echo "net.core.wmem_max=$new_max" >> /etc/sysctl.conf +echo "net.core.rmem_max=$new_max" >> /etc/sysctl.conf + +echo "net.ipv4.tcp_rmem= 10240 87380 $new_max" >> /etc/sysctl.conf +echo "net.ipv4.tcp_wmem= 10240 87380 $new_max" >> /etc/sysctl.conf + +echo "net.ipv4.tcp_window_scaling = 1" >> /etc/sysctl.conf + +echo "net.ipv4.tcp_timestamps = 1" >> /etc/sysctl.conf + +echo "net.ipv4.tcp_sack = 1" >> /etc/sysctl.conf + +echo "net.ipv4.tcp_no_metrics_save = 1" >> /etc/sysctl.conf + +echo "net.core.netdev_max_backlog = 5000" >> /etc/sysctl.conf + diff --git a/testkit/tools/bandwidth/show_tcp_memory.sh b/testkit/tools/bandwidth/show_tcp_memory.sh new file mode 100644 index 00000000..7e81e94d --- /dev/null +++ b/testkit/tools/bandwidth/show_tcp_memory.sh @@ -0,0 +1,17 @@ + +function show_proc() +{ + echo ---------------------------------------------- + echo $* + $* +} + +show_proc cat /proc/sys/net/ipv4/tcp_mem +show_proc cat /proc/sys/net/core/rmem_default +show_proc cat /proc/sys/net/core/rmem_max +show_proc cat /proc/sys/net/core/wmem_default +show_proc cat /proc/sys/net/core/wmem_max +show_proc cat /proc/sys/net/core/optmem_max +show_proc cat /proc/net/sockstat +show_proc cat /proc/sys/net/ipv4/tcp_max_orphans + -- 2.34.1