2017-12-04 05:46:34 +00:00
|
|
|
/*
|
|
|
|
Examplar - An automation and testing framework.
|
|
|
|
|
|
|
|
© SURRO INDUSTRIES and Chris Punches, 2017.
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
2017-12-05 04:25:48 +00:00
|
|
|
|
2017-04-18 23:04:21 +00:00
|
|
|
#include <iostream>
|
2017-12-06 00:25:51 +00:00
|
|
|
#include <unistd.h>
|
2017-12-06 04:09:33 +00:00
|
|
|
#include <getopt.h>
|
2017-04-18 23:04:21 +00:00
|
|
|
|
2020-06-21 00:09:32 +00:00
|
|
|
#include "src/json/json.h"
|
|
|
|
#include "src/loaders/abstract/loaders.h"
|
|
|
|
#include "src/Logger/Logger.h"
|
|
|
|
#include "src/loaders/misc/helpers.h"
|
2017-12-05 07:28:52 +00:00
|
|
|
|
2017-12-06 00:25:51 +00:00
|
|
|
void print_usage()
|
|
|
|
{
|
2017-12-08 03:37:42 +00:00
|
|
|
printf("examplar [ -h | --help ] [ -v | --verbose ] [ -e | --execution-context EXECUTION_CONTEXT ][ -c | --config CONFIG_PATH ]\n\n");
|
2017-12-06 00:25:51 +00:00
|
|
|
}
|
2017-12-05 07:28:52 +00:00
|
|
|
|
2017-12-06 00:25:51 +00:00
|
|
|
int main( int argc, char * argv[] )
|
2017-04-18 23:04:21 +00:00
|
|
|
{
|
2020-06-21 00:09:32 +00:00
|
|
|
int opt;
|
2017-12-06 00:25:51 +00:00
|
|
|
bool verbose = false;
|
|
|
|
bool show_help = false;
|
2017-12-08 03:37:42 +00:00
|
|
|
|
|
|
|
// indicator of whether examplar should use a commandline argument for overriding the context
|
2020-06-21 00:25:45 +00:00
|
|
|
// instead of what's supplied in the test file
|
2017-12-08 03:37:42 +00:00
|
|
|
bool cli_context_supplied = false;
|
|
|
|
|
2017-12-06 00:25:51 +00:00
|
|
|
std::string config_path = "/etc/Examplar/config.json";
|
2017-12-08 03:37:42 +00:00
|
|
|
std::string execution_context;
|
2017-12-06 00:25:51 +00:00
|
|
|
|
|
|
|
// commandline switches:
|
|
|
|
// -h help
|
|
|
|
// -v verbose
|
|
|
|
// -c CONFIG_FILE_PATH -- defaults to '/etc/Examplar/config.json'
|
2017-12-08 03:37:42 +00:00
|
|
|
// -e EXECUTION_CONTEXT -- current working directory when executing unit targets
|
2017-12-06 00:25:51 +00:00
|
|
|
|
2017-12-06 04:09:33 +00:00
|
|
|
while (1)
|
2017-12-06 00:25:51 +00:00
|
|
|
{
|
2017-12-06 04:09:33 +00:00
|
|
|
static struct option long_options[] =
|
2017-12-06 00:25:51 +00:00
|
|
|
{
|
2017-12-08 03:37:42 +00:00
|
|
|
{"verbose", no_argument, 0, 'v'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"config", required_argument, 0, 'c'},
|
|
|
|
{"execution-context", required_argument, 0, 'e'},
|
2017-12-06 04:09:33 +00:00
|
|
|
{0, 0}
|
|
|
|
};
|
2017-12-08 03:37:42 +00:00
|
|
|
|
2017-12-06 04:09:33 +00:00
|
|
|
int option_index = 0;
|
|
|
|
|
2020-06-21 00:09:32 +00:00
|
|
|
opt = getopt_long( argc, argv, "vhec:", long_options, &option_index );
|
2017-12-06 04:09:33 +00:00
|
|
|
|
2020-06-21 00:09:32 +00:00
|
|
|
if ( opt == -1 )
|
2017-12-06 04:09:33 +00:00
|
|
|
break;
|
|
|
|
|
2020-06-21 00:09:32 +00:00
|
|
|
switch ( opt )
|
2017-12-06 04:09:33 +00:00
|
|
|
{
|
|
|
|
case 0:
|
2020-06-21 00:09:32 +00:00
|
|
|
if ( long_options[option_index].flag !=0 )
|
2017-12-06 04:09:33 +00:00
|
|
|
break;
|
2017-12-06 00:25:51 +00:00
|
|
|
case 'h':
|
|
|
|
show_help = true;
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
case 'c':
|
2020-06-21 00:09:32 +00:00
|
|
|
config_path = std::string( optarg );
|
2017-12-06 00:25:51 +00:00
|
|
|
break;
|
2017-12-06 04:40:57 +00:00
|
|
|
case '?':
|
|
|
|
print_usage();
|
2020-06-21 00:09:32 +00:00
|
|
|
exit( 1 );
|
2017-12-08 03:37:42 +00:00
|
|
|
case 'e':
|
|
|
|
cli_context_supplied = true;
|
2020-06-21 00:09:32 +00:00
|
|
|
execution_context = std::string( optarg );
|
2017-12-08 03:37:42 +00:00
|
|
|
break;
|
2017-12-06 00:25:51 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 00:09:32 +00:00
|
|
|
if ( show_help )
|
2017-12-06 00:25:51 +00:00
|
|
|
{
|
|
|
|
print_usage();
|
2020-06-21 00:09:32 +00:00
|
|
|
exit( 0 );
|
2017-12-06 00:25:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 00:09:32 +00:00
|
|
|
int L_LEVEL = E_INFO;
|
|
|
|
if ( verbose )
|
|
|
|
{
|
|
|
|
L_LEVEL = E_DEBUG;
|
2020-06-21 00:25:45 +00:00
|
|
|
std::cout << "Verbosity is DBUG." << std::endl;
|
2020-06-21 00:09:32 +00:00
|
|
|
} else {
|
|
|
|
L_LEVEL = E_INFO;
|
2020-06-21 00:25:45 +00:00
|
|
|
std::cout << "Verbosity is INFO." << std::endl;
|
2020-06-21 00:09:32 +00:00
|
|
|
}
|
2017-12-06 00:25:51 +00:00
|
|
|
|
2021-02-14 00:41:08 +00:00
|
|
|
Logger slog = Logger( L_LEVEL, "_main_" );
|
2017-12-06 00:25:51 +00:00
|
|
|
|
2017-04-29 21:35:03 +00:00
|
|
|
// A Plan is made up of Tasks, and a Suite is made up of Units.
|
|
|
|
// A Plan declares what units are executed and a Suite declares the definitions of those units.
|
2020-06-21 00:09:32 +00:00
|
|
|
Conf configuration = Conf(config_path, L_LEVEL );
|
2017-12-08 03:37:42 +00:00
|
|
|
|
|
|
|
// check if context override
|
2020-06-21 00:09:32 +00:00
|
|
|
|
2017-12-08 03:37:42 +00:00
|
|
|
if ( configuration.has_context_override() )
|
|
|
|
{
|
|
|
|
// if so, set the CWD.
|
|
|
|
chdir( configuration.get_execution_context().c_str() );
|
2020-06-21 00:09:32 +00:00
|
|
|
slog.log( E_DEBUG, "Set execution context: " + get_working_path() );
|
2017-12-08 03:37:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 00:09:32 +00:00
|
|
|
// if the user set this option as a commandline argument
|
|
|
|
if ( cli_context_supplied )
|
|
|
|
{
|
2020-06-21 00:25:45 +00:00
|
|
|
// override the test file's specified execution context
|
2020-06-21 00:09:32 +00:00
|
|
|
configuration.set_execution_context( execution_context );
|
|
|
|
slog.log( E_DEBUG, "Set execution context from commandline: " + execution_context );
|
|
|
|
}
|
2017-12-08 03:37:42 +00:00
|
|
|
|
|
|
|
// load the filepaths to definitions of a plan and definitions of units.
|
2017-04-29 21:35:03 +00:00
|
|
|
std::string definitions_file = configuration.get_units_path();
|
|
|
|
std::string plan_file = configuration.get_plan_path();
|
2017-04-19 06:27:54 +00:00
|
|
|
|
2020-06-29 06:22:11 +00:00
|
|
|
slog.log( E_DEBUG, "* Initialising suite (definition library).");
|
2020-06-21 00:09:32 +00:00
|
|
|
Suite available_definitions = Suite( L_LEVEL );
|
2020-06-29 06:22:11 +00:00
|
|
|
|
|
|
|
slog.log( E_INFO, "* Loading all actionable units into suite." );
|
2020-06-21 00:09:32 +00:00
|
|
|
available_definitions.load_units_file( definitions_file );
|
2017-06-11 19:14:45 +00:00
|
|
|
|
2020-06-29 06:22:11 +00:00
|
|
|
slog.log( E_DEBUG, "* Initialising plan." );
|
2020-06-21 00:09:32 +00:00
|
|
|
Plan plan = Plan( &configuration, L_LEVEL );
|
2020-06-29 06:22:11 +00:00
|
|
|
|
|
|
|
slog.log( E_INFO, "* Loading plan outline.");
|
2020-06-21 00:09:32 +00:00
|
|
|
plan.load_plan_file( plan_file );
|
2017-06-11 19:14:45 +00:00
|
|
|
|
2020-06-29 06:22:11 +00:00
|
|
|
slog.log( E_INFO, "* Loading planned tasks from suite to plan." );
|
2020-06-21 00:09:32 +00:00
|
|
|
plan.load_definitions( available_definitions );
|
2017-04-19 05:50:34 +00:00
|
|
|
|
2020-06-29 06:22:11 +00:00
|
|
|
slog.log( E_INFO, "* Ready to execute all actionable tasks in plan." );
|
2017-06-27 02:27:24 +00:00
|
|
|
|
2017-12-05 06:13:23 +00:00
|
|
|
try
|
|
|
|
{
|
2020-06-21 00:09:32 +00:00
|
|
|
plan.execute();
|
2017-12-04 02:15:41 +00:00
|
|
|
}
|
2017-12-05 06:13:23 +00:00
|
|
|
|
|
|
|
catch ( std::exception& e)
|
|
|
|
{
|
2020-06-29 06:22:11 +00:00
|
|
|
slog.log( E_FATAL, "Caught exception.");
|
2020-06-21 00:09:32 +00:00
|
|
|
slog.log( E_FATAL, e.what() );
|
2017-12-05 06:13:23 +00:00
|
|
|
return 1;
|
2017-12-04 02:15:41 +00:00
|
|
|
}
|
2017-06-27 02:27:24 +00:00
|
|
|
|
2017-04-18 23:04:21 +00:00
|
|
|
return 0;
|
2020-06-21 00:09:32 +00:00
|
|
|
}
|