<commit_before>#!/usr/bin/env perl
#
# !! ONLY FOR INTERNAL TESTING - IT'S VERY, VERY HACKY !!
# !! IT SENDS AN HACKY DHCP-REQUEST BROADCAST DATAGRAM WITH 'Requested IP Address': 01.01.01.01' !!
#
#
# * executed from jenkins
# * uses sudo for install / service management (hint: jenkins sudo - disable 'requiretty' in sudo config)
#
#
# Usage:
#   runner.pl -package-dir <PATH>
#     tests the latest package (with the highest version) under <PATH>
#
#   runner.pl -jenkins-url http://<IP>:<PORT>/job/<JOB>
#     tests the artifacts from the last succesful build
# 
#
#
# CPAN Modules:
#   * YAML::XS: config file parsing
#   * autodie: autodie on error
#   * Test::Most: test infra
#
#
use v5.14;
use strict;
use warnings;
use diagnostics;
use Config;
use local::lib;
use autodie;
use YAML::XS qw/LoadFile Load/;
use Test::Most;
use Socket;
use File::Basename;
use File::Temp qw/tempdir/;
use Getopt::Long;
use LWP::Simple;


# args
my $package_dir_flag; # local directory with packages (--package-dir-flag ../../build-output/<ARCH>)
my $jenkins_url_flag; # jenkins url (--jenkins-url http://<IP>:<PORT>/job/<JOB>)
GetOptions("package-dir:s" => \$package_dir_flag,
           "jenkins-url:s" => \$jenkins_url_flag);


my $last_package_path;
if(defined($package_dir_flag)){
    $last_package_path = find_last_package_from_dir($package_dir_flag);
}elsif(defined($jenkins_url_flag)){
    $last_package_path = find_package_from_jenkins($jenkins_url_flag);
}else{
    say "parameter  -package-dir or -jenkins-url missing";
    exit(1);
}




# if any test fail - STOP
die_on_fail;

# cd script base dir
chdir(dirname(__FILE__));

# load cmds from platform depend config
my $cmds = LoadFile(config_file_name());

# replace <PKG> pattern with the last package in the 'install' cmd
$cmds->{install} =~ s/<PKG>/$last_package_path/;

# ready to test!
isnt(exec_get_ec($cmds->{print_version}), 0, 'lsleases should not be installed');
is(exec_get_ec($cmds->{install}), 0, 'installation should return exit code: 0');
eval{
  if($cmds->{start_after_install}){
    is(exec_get_ec($cmds->{start}), 0, 'startup should return code: 0');
  }
  is(exec_get_ec($cmds->{print_version}), 0, 'lsleases should be installed and running');
  is(exec_get_stdout($cmds->{list_leases}), '', 'should be empty after installation');
  send_dummy_dhcp_request();
  is(exec_get_stdout($cmds->{list_leases}), "1.1.1.1          08:00:27:f2:97:5a  testhost\n", 'dummy should be in the output');
  is(exec_get_ec($cmds->{stop}), 0, 'stopping lsleases service should return code: 0');
  isnt(exec_get_ec($cmds->{list_leases}), 0, 'list leases should return an error if no server instance running');
  is(exec_get_ec($cmds->{manpage}), 0, "manpage should be installed");
  is(exec_get_ec($cmds->{remove}), 0, 'remove should return exit code: 0');
}; 
if($@){
  say "TEST FAILED - CLEANUP";
  exec_get_ec($cmds->{remove});
}
        
done_testing();


#
# sends an dummy dhcp request
#
sub send_dummy_dhcp_request{
    my $dummy_host_name = "testhost";
    my $host_name_option = sprintf("%02x%s", length($dummy_host_name), unpack("H*", $dummy_host_name));
    my $dummy_dhcp_request = "010106002ccab3380000000000000000000000000000000000000000080027f2975a" .
		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" .
		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" .
		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" .
		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" .
		"638253633501033204". "01010101" ."0c".$host_name_option."3712011c02030f06770c2c2f1a792a79f921fc2aff0000000" .
		"000000000000000000000000000";

    
    socket(SOCKET, AF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "Couldn't create raw socket: $!";<commit_msg>test infra: stop service before removing on failure<commit_after>#!/usr/bin/env perl
#
# !! ONLY FOR INTERNAL TESTING - IT'S VERY, VERY HACKY !!
# !! IT SENDS AN HACKY DHCP-REQUEST BROADCAST DATAGRAM WITH 'Requested IP Address': 01.01.01.01' !!
#
#
# * executed from jenkins
# * uses sudo for install / service management (hint: jenkins sudo - disable 'requiretty' in sudo config)
#
#
# Usage:
#   runner.pl -package-dir <PATH>
#     tests the latest package (with the highest version) under <PATH>
#
#   runner.pl -jenkins-url http://<IP>:<PORT>/job/<JOB>
#     tests the artifacts from the last succesful build
# 
#
#
# CPAN Modules:
#   * YAML::XS: config file parsing
#   * autodie: autodie on error
#   * Test::Most: test infra
#
#
use v5.14;
use strict;
use warnings;
use diagnostics;
use Config;
use local::lib;
use autodie;
use YAML::XS qw/LoadFile Load/;
use Test::Most;
use Socket;
use File::Basename;
use File::Temp qw/tempdir/;
use Getopt::Long;
use LWP::Simple;


# args
my $package_dir_flag; # local directory with packages (--package-dir-flag ../../build-output/<ARCH>)
my $jenkins_url_flag; # jenkins url (--jenkins-url http://<IP>:<PORT>/job/<JOB>)
GetOptions("package-dir:s" => \$package_dir_flag,
           "jenkins-url:s" => \$jenkins_url_flag);


my $last_package_path;
if(defined($package_dir_flag)){
    $last_package_path = find_last_package_from_dir($package_dir_flag);
}elsif(defined($jenkins_url_flag)){
    $last_package_path = find_package_from_jenkins($jenkins_url_flag);
}else{
    say "parameter  -package-dir or -jenkins-url missing";
    exit(1);
}




# if any test fail - STOP
die_on_fail;

# cd script base dir
chdir(dirname(__FILE__));

# load cmds from platform depend config
my $cmds = LoadFile(config_file_name());

# replace <PKG> pattern with the last package in the 'install' cmd
$cmds->{install} =~ s/<PKG>/$last_package_path/;

# ready to test!
isnt(exec_get_ec($cmds->{print_version}), 0, 'lsleases should not be installed');
is(exec_get_ec($cmds->{install}), 0, 'installation should return exit code: 0');
eval{
  if($cmds->{start_after_install}){
    is(exec_get_ec($cmds->{start}), 0, 'startup should return code: 0');
  }
  is(exec_get_ec($cmds->{print_version}), 0, 'lsleases should be installed and running');
  is(exec_get_stdout($cmds->{list_leases}), '', 'should be empty after installation');
  send_dummy_dhcp_request();
  is(exec_get_stdout($cmds->{list_leases}), "1.1.1.1          08:00:27:f2:97:5a  testhost\n", 'dummy should be in the output');
  is(exec_get_ec($cmds->{stop}), 0, 'stopping lsleases service should return code: 0');
  isnt(exec_get_ec($cmds->{list_leases}), 0, 'list leases should return an error if no server instance running');
  is(exec_get_ec($cmds->{manpage}), 0, "manpage should be installed");
  is(exec_get_ec($cmds->{remove}), 0, 'remove should return exit code: 0');
}; 
if($@){
  say "TEST FAILED - CLEANUP";
  exec_get_ec($cmds->{stop});
  exec_get_ec($cmds->{remove});
}
        
done_testing();


#
# sends an dummy dhcp request
#
sub send_dummy_dhcp_request{
    my $dummy_host_name = "testhost";
    my $host_name_option = sprintf("%02x%s", length($dummy_host_name), unpack("H*", $dummy_host_name));
    my $dummy_dhcp_request = "010106002ccab3380000000000000000000000000000000000000000080027f2975a" .
		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" .
		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" .
		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" .
		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" .
		"638253633501033204". "01010101" ."0c".$host_name_option."3712011c02030f06770c2c2f1a792a79f921fc2aff0000000" .
		"000000000000000000000000000";

    
    socket(SOCKET, AF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "Couldn't create raw socket: $!";
{'fraction_non_alphanumeric': 0.1378132118451025, 'fraction_numerical': 0.15084788661098456, 'mean_word_length': 5.477868852459016, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 14, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2641484', 'n_tokens_mistral': 3758, 'n_tokens_neox': 2623, 'n_words': 624}