mirror of
https://github.com/centreon/centreon-plugins.git
synced 2025-09-25 18:58:39 +02:00
Ensure my changes work correctly with GitHub integration
This commit is contained in:
parent
a6f5d87659
commit
fefa587f63
@ -60,9 +60,15 @@ gpgcheck=1\n\
|
||||
gpgkey=https://yum-gpg.centreon.com/RPM-GPG-KEY-CES\n'\
|
||||
>> /etc/yum.repos.d/centreon-plugins.repo
|
||||
|
||||
dnf config-manager --add-repo https://packages.centreon.com/rpm-standard/24.10/el8/centreon-24.10.repo
|
||||
dnf update -y
|
||||
dnf install -y centreon-connector-perl
|
||||
dnf install -y centreon-clib
|
||||
dnf clean all --enablerepo=*
|
||||
|
||||
mkdir -p /var/lib/centreon/centplugins/
|
||||
chmod 777 /var/lib/centreon/centplugins/
|
||||
|
||||
dnf clean all
|
||||
|
||||
EOF
|
||||
EOF
|
@ -59,9 +59,18 @@ gpgcheck=1\n\
|
||||
gpgkey=https://yum-gpg.centreon.com/RPM-GPG-KEY-CES\n'\
|
||||
>> /etc/yum.repos.d/centreon-plugins.repo
|
||||
|
||||
# Add Centreon plugins repositories
|
||||
dnf config-manager --add-repo https://packages.centreon.com/rpm-standard/24.10/el9/centreon-24.10.repo
|
||||
dnf update -y
|
||||
dnf clean all --enablerepo=*
|
||||
dnf install -y centreon-connector-perl
|
||||
dnf install -y centreon-clib
|
||||
|
||||
dnf update -y
|
||||
|
||||
mkdir -p /var/lib/centreon/centplugins/
|
||||
chmod 777 /var/lib/centreon/centplugins/
|
||||
|
||||
dnf clean all
|
||||
|
||||
EOF
|
||||
EOF
|
@ -7,8 +7,13 @@ ENV DEBIAN_FRONTEND=noninteractive
|
||||
# fix locale
|
||||
RUN bash -e <<EOF
|
||||
|
||||
useradd snmp
|
||||
mkdir -p /var/lib/snmp/cert_indexes/
|
||||
chown snmp:snmp -R /var/lib/snmp/cert_indexes/
|
||||
mkdir -p /tmp/cache/
|
||||
|
||||
apt-get update
|
||||
apt-get install -y locales libcurl4-openssl-dev curl wget zstd jq
|
||||
apt-get install -y locales libcurl4-openssl-dev curl wget zstd jq lsb-release ca-certificates software-properties-common gnupg2
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
|
||||
apt-get clean
|
||||
@ -17,7 +22,7 @@ EOF
|
||||
|
||||
ENV LANG=en_US.utf8
|
||||
|
||||
RUN bash -e <<EOF
|
||||
RUN bash <<EOF
|
||||
|
||||
# Avoid apt to clean packages cache directory
|
||||
rm -f /etc/apt/apt.conf.d/docker-clean
|
||||
@ -28,7 +33,7 @@ apt-get install -y python3-dev python3-pip
|
||||
rm -rf /usr/lib/python3.11/EXTERNALLY-MANAGED
|
||||
pip3 install robotframework robotframework-examples
|
||||
# Install snmpsim
|
||||
pip3 install snmpsim
|
||||
pip3 install snmpsim-lextudio
|
||||
|
||||
# Install nodejs
|
||||
curl -fsSL https://deb.nodesource.com/setup_21.x | bash - &&\
|
||||
@ -38,15 +43,22 @@ apt-get install -y nodejs
|
||||
npm install -g -D @mockoon/cli
|
||||
|
||||
# Add Centreon plugins repositories
|
||||
echo "deb https://packages.centreon.com/apt-standard-24.10-stable bookworm main" | tee /etc/apt/sources.list.d/centreon.list
|
||||
echo "deb https://packages.centreon.com/apt-plugins-stable/ bookworm main" | tee /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/apt-plugins-testing/ bookworm main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/apt-plugins-unstable/ bookworm main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
wget -O- https://apt-key.centreon.com | gpg --dearmor | tee /etc/apt/trusted.gpg.d/centreon.gpg > /dev/null 2>&1
|
||||
|
||||
# Update
|
||||
apt-get update
|
||||
|
||||
apt-get install -y centreon-clib centreon-connector-perl centreon-plugins
|
||||
|
||||
rm -f /etc/apt/sources.list.d/centreon.list
|
||||
|
||||
mkdir -p /var/lib/centreon/centplugins/
|
||||
chmod 777 /var/lib/centreon/centplugins/
|
||||
|
||||
apt-get clean
|
||||
|
||||
EOF
|
||||
EOF
|
@ -7,8 +7,13 @@ ENV DEBIAN_FRONTEND=noninteractive
|
||||
# fix locale
|
||||
RUN bash -e <<EOF
|
||||
|
||||
useradd snmp
|
||||
mkdir -p /var/lib/snmp/cert_indexes/
|
||||
chown snmp:snmp -R /var/lib/snmp/cert_indexes/
|
||||
mkdir -p /tmp/cache/
|
||||
|
||||
apt-get update
|
||||
apt-get install -y locales libcurl4-openssl-dev curl wget zstd jq
|
||||
apt-get install -y locales libcurl4-openssl-dev curl wget zstd jq lsb-release ca-certificates software-properties-common gnupg2
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
|
||||
apt-get clean
|
||||
@ -25,6 +30,7 @@ rm -f /etc/apt/apt.conf.d/docker-clean
|
||||
apt-get update
|
||||
# Install Robotframework
|
||||
apt-get install -y python3 python3-dev python3-pip
|
||||
rm -rf /usr/lib/python3.11/EXTERNALLY-MANAGED
|
||||
pip3 install robotframework robotframework-examples
|
||||
# Install snmpsim
|
||||
pip3 install snmpsim
|
||||
@ -36,16 +42,20 @@ apt-get install -y nodejs
|
||||
npm install -g -D @mockoon/cli
|
||||
|
||||
# Add Centreon plugins repositories
|
||||
echo "deb https://packages.centreon.com/apt-standard-24.04-stable bullseye main" | tee /etc/apt/sources.list.d/centreon.list
|
||||
echo "deb https://packages.centreon.com/apt-plugins-stable/ bullseye main" | tee /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/apt-plugins-testing/ bullseye main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/apt-plugins-unstable/ bullseye main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
wget -O- https://apt-key.centreon.com | gpg --dearmor | tee /etc/apt/trusted.gpg.d/centreon.gpg > /dev/null 2>&1
|
||||
apt-get update
|
||||
|
||||
apt-get install -y centreon-clib centreon-connector-perl
|
||||
|
||||
rm -f /etc/apt/sources.list.d/centreon.list
|
||||
|
||||
mkdir -p /var/lib/centreon/centplugins/
|
||||
chmod 777 /var/lib/centreon/centplugins/
|
||||
|
||||
apt-get clean
|
||||
|
||||
|
||||
EOF
|
||||
EOF
|
@ -7,8 +7,13 @@ ENV DEBIAN_FRONTEND=noninteractive
|
||||
# fix locale
|
||||
RUN bash -e <<EOF
|
||||
|
||||
useradd snmp
|
||||
mkdir -p /var/lib/snmp/cert_indexes/
|
||||
chown snmp:snmp -R /var/lib/snmp/cert_indexes/
|
||||
mkdir -p /tmp/cache/
|
||||
|
||||
apt-get update
|
||||
apt-get install -y locales libcurl4-openssl-dev curl wget zstd jq
|
||||
apt-get install -y locales libcurl4-openssl-dev curl wget zstd jq lsb-release ca-certificates software-properties-common gnupg2
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
|
||||
apt-get clean
|
||||
@ -25,6 +30,7 @@ rm -f /etc/apt/apt.conf.d/docker-clean
|
||||
apt-get update
|
||||
# Install Robotframework
|
||||
apt-get install -y python3 python3-dev python3-pip
|
||||
rm -rf /usr/lib/python3.11/EXTERNALLY-MANAGED
|
||||
pip3 install robotframework robotframework-examples
|
||||
# Install snmpsim
|
||||
pip3 install snmpsim
|
||||
@ -36,14 +42,21 @@ apt-get install -y nodejs
|
||||
npm install -g -D @mockoon/cli
|
||||
|
||||
# Add Centreon plugins repositories
|
||||
echo "deb https://packages.centreon.com/ubuntu-standard-24.04-stable/ jammy main" | tee -a /etc/apt/sources.list.d/centreon-stable.list
|
||||
echo "deb https://packages.centreon.com/ubuntu-plugins-stable/ jammy main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/ubuntu-plugins-testing/ jammy main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/ubuntu-plugins-unstable/ jammy main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/ubuntu-connectors-stable/ jammy main" | tee /etc/apt/sources.list.d/centreon-connectors.list
|
||||
wget -O- https://apt-key.centreon.com | gpg --dearmor | tee /etc/apt/trusted.gpg.d/centreon.gpg > /dev/null 2>&1
|
||||
apt-get update
|
||||
|
||||
apt-get install -y centreon-clib centreon-connector-perl
|
||||
|
||||
rm -f /etc/apt/sources.list.d/centreon.list
|
||||
|
||||
mkdir -p /var/lib/centreon/centplugins/
|
||||
chmod 777 /var/lib/centreon/centplugins/
|
||||
|
||||
apt-get clean
|
||||
|
||||
EOF
|
||||
EOF
|
@ -7,8 +7,13 @@ ENV DEBIAN_FRONTEND=noninteractive
|
||||
# fix locale
|
||||
RUN bash -e <<EOF
|
||||
|
||||
useradd snmp
|
||||
mkdir -p /var/lib/snmp/cert_indexes/
|
||||
chown snmp:snmp -R /var/lib/snmp/cert_indexes/
|
||||
mkdir -p /tmp/cache/
|
||||
|
||||
apt-get update
|
||||
apt-get install -y locales libcurl4-openssl-dev curl wget zstd jq
|
||||
apt-get install -y locales libcurl4-openssl-dev curl wget zstd jq lsb-release ca-certificates software-properties-common gnupg2
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
|
||||
apt-get clean
|
||||
@ -26,7 +31,7 @@ apt-get update
|
||||
# Install requirements for python virtual envs
|
||||
apt-get install -y python3-venv
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
. .venv/bin/activate
|
||||
# Install Robotframework
|
||||
apt-get install -y python3 python3-dev python3-pip
|
||||
pip3 install robotframework robotframework-examples
|
||||
@ -40,14 +45,18 @@ apt-get install -y nodejs
|
||||
npm install -g -D @mockoon/cli
|
||||
|
||||
# Add Centreon plugins repositories
|
||||
echo "deb https://packages.centreon.com/ubuntu-standard-24.10-stable/ noble main" | tee -a /etc/apt/sources.list.d/centreon-stable.list
|
||||
echo "deb https://packages.centreon.com/ubuntu-plugins-stable/ noble main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/ubuntu-plugins-testing/ noble main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
echo "deb https://packages.centreon.com/ubuntu-plugins-unstable/ noble main" | tee -a /etc/apt/sources.list.d/centreon-plugins.list
|
||||
wget -O- https://apt-key.centreon.com | gpg --dearmor | tee /etc/apt/trusted.gpg.d/centreon.gpg > /dev/null 2>&1
|
||||
apt-get update
|
||||
|
||||
apt-get install -y centreon-clib centreon-connector-perl
|
||||
|
||||
mkdir -p /var/lib/centreon/centplugins/
|
||||
chmod 777 /var/lib/centreon/centplugins/
|
||||
|
||||
apt-get clean
|
||||
|
||||
EOF
|
||||
EOF
|
38
tests/os/linux/snmp/arp-connector.robot
Normal file
38
tests/os/linux/snmp/arp-connector.robot
Normal file
@ -0,0 +1,38 @@
|
||||
*** Settings ***
|
||||
Documentation Check arp table
|
||||
|
||||
Resource ${CURDIR}${/}..${/}..${/}..${/}resources/import.resource
|
||||
|
||||
Suite Setup Ctn Generic Suite Setup
|
||||
Test Timeout 120s
|
||||
|
||||
|
||||
*** Variables ***
|
||||
${CMD} /usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin=os::linux::snmp::plugin
|
||||
|
||||
|
||||
*** Test Cases ***
|
||||
arp-connectors ${tc}
|
||||
[Tags] os linux
|
||||
${command} Catenate
|
||||
... ${CMD}
|
||||
... --mode=arp
|
||||
... --hostname=${HOSTNAME}
|
||||
... --snmp-version=${SNMPVERSION}
|
||||
... --snmp-port=${SNMPPORT}
|
||||
... --snmp-community=os/linux/snmp/network-interfaces
|
||||
... --snmp-timeout=1
|
||||
... ${extra_options}
|
||||
|
||||
Ctn Run Command With Connector And Check Result As Strings ${tc} ${command} ${expected_result}
|
||||
|
||||
Examples: tc extra_options expected_result --
|
||||
... 1 --filter-macaddr OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
||||
... 2 --filter-ipaddr OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
||||
... 3 --warning-total-entries OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
||||
... 4 --critical-total-entries OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
||||
... 5 --critical-duplicate-ipaddr OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
||||
... 6 --critical-duplicate-macaddr OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
||||
... 7 --warning-duplicate-ipaddr OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
||||
... 8 --warning-duplicate-macaddr OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
||||
... 9 ${EMPTY} OK: total entries 3 - duplicate mac address 0 - duplicate ip address 0 | 'arp.total.entries.count'=3;;;0; 'arp.duplicate.macaddr.count'=0;;;0; 'arp.duplicate.ipaddr.count'=0;;;0;
|
38
tests/os/linux/snmp/cpu-connector.robot
Normal file
38
tests/os/linux/snmp/cpu-connector.robot
Normal file
@ -0,0 +1,38 @@
|
||||
*** Settings ***
|
||||
Documentation Check cpu table
|
||||
|
||||
|
||||
Resource ${CURDIR}${/}..${/}..${/}..${/}resources/import.resource
|
||||
|
||||
Suite Setup Ctn Generic Suite Setup
|
||||
Test Timeout 120s
|
||||
|
||||
|
||||
*** Variables ***
|
||||
${CMD} /usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin=os::linux::snmp::plugin
|
||||
|
||||
|
||||
*** Test Cases ***
|
||||
cpu-connector ${tc}
|
||||
[Tags] os linux
|
||||
${command} Catenate
|
||||
... ${CMD}
|
||||
... --mode=cpu
|
||||
... --hostname=${HOSTNAME}
|
||||
... --snmp-version=${SNMPVERSION}
|
||||
... --snmp-port=${SNMPPORT}
|
||||
... --snmp-community=os/linux/snmp/network-interfaces
|
||||
... ${extra_options}
|
||||
|
||||
Ctn Run Command With Connector And Check Result As Strings ${tc} ${command} ${expected_result}
|
||||
|
||||
Examples: tc extra_options expected_result --
|
||||
... 1 --use-ucd='0' OK: 1 CPU(s) average usage is 2.00 % - CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;;;0;100
|
||||
... 2 --warning-average OK: 1 CPU(s) average usage is 2.00 % - CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;;;0;100
|
||||
... 3 --critical-average OK: 1 CPU(s) average usage is 2.00 % - CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;;;0;100
|
||||
... 4 --warning-core OK: 1 CPU(s) average usage is 2.00 % - CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;;;0;100
|
||||
... 5 --critical-core OK: 1 CPU(s) average usage is 2.00 % - CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;;;0;100
|
||||
... 6 --warning-average='0' WARNING: 1 CPU(s) average usage is 2.00 % | 'total_cpu_avg'=2.00%;0:0;;0;100 'cpu'=2.00%;;;0;100
|
||||
... 7 --critical-average='0' CRITICAL: 1 CPU(s) average usage is 2.00 % | 'total_cpu_avg'=2.00%;;0:0;0;100 'cpu'=2.00%;;;0;100
|
||||
... 8 --warning-core='0' WARNING: CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;0:0;;0;100
|
||||
... 9 --critical-core='0' CRITICAL: CPU '0' usage : 2.00 % | 'total_cpu_avg'=2.00%;;;0;100 'cpu'=2.00%;;0:0;0;100
|
183
tests/resources/connector.py
Normal file
183
tests/resources/connector.py
Normal file
@ -0,0 +1,183 @@
|
||||
import subprocess
|
||||
import time
|
||||
import os
|
||||
from robot.api.deco import keyword
|
||||
import re
|
||||
|
||||
connector = None
|
||||
|
||||
class ConnectorLibrary:
|
||||
def __init__(self):
|
||||
self.process = None
|
||||
|
||||
def start_connector(self, command=["/usr/lib64/centreon-connector/centreon_connector_perl", "--log-file=/tmp/connector.log", "--debug"]):
|
||||
if self.process is None or self.process.poll() is not None:
|
||||
self.process = subprocess.Popen(
|
||||
command,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, # Capture stdout!
|
||||
text=True
|
||||
)
|
||||
print("Connector started")
|
||||
|
||||
def send_to_connector(self, idf: int, command: str, timeout: int, command_log="/tmp/connector.commands.log"):
|
||||
now = int(time.time())
|
||||
|
||||
# Log to console
|
||||
print(f"[Connector] Sending command (id={idf}, timeout={timeout}): {command}")
|
||||
|
||||
# Log to file
|
||||
try:
|
||||
with open(command_log, "a") as logf:
|
||||
logf.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} id={idf} timeout={timeout} command={command}\n")
|
||||
except Exception as e:
|
||||
print(f"[Connector] Could not write to command log file: {e}")
|
||||
|
||||
buf = bytearray()
|
||||
buf.extend(b"2\0")
|
||||
buf.extend(f"{idf}\0".encode('utf-8'))
|
||||
buf.extend(f"{timeout}\0".encode('utf-8'))
|
||||
buf.extend(f"{now}\0".encode('utf-8'))
|
||||
buf.extend(f"{command}\0\0\0\0".encode('utf-8'))
|
||||
|
||||
if self.process and self.process.stdin:
|
||||
self.process.stdin.write(buf.decode('utf-8'))
|
||||
self.process.stdin.flush()
|
||||
else:
|
||||
raise RuntimeError("Connector is not running.")
|
||||
|
||||
def write_next_output_to_file(self, output_file="/tmp/connector.output"):
|
||||
"""
|
||||
Reads the next line from the connector's stdout and appends it to output_file.
|
||||
"""
|
||||
if self.process and self.process.stdout:
|
||||
line = self.process.stdout.readline()
|
||||
if line:
|
||||
with open(output_file, "a") as f:
|
||||
f.write(line)
|
||||
return line
|
||||
return None
|
||||
|
||||
def stop_connector(self):
|
||||
if self.process:
|
||||
self.process.terminate()
|
||||
self.process = None
|
||||
print("Connector stopped")
|
||||
|
||||
def wait_for_output_file(output_file="/tmp/connector.output", timeout=10, poll_interval=0.2):
|
||||
end_time = time.time() + timeout
|
||||
while time.time() < end_time:
|
||||
if os.path.exists(output_file):
|
||||
return True
|
||||
time.sleep(poll_interval)
|
||||
raise FileNotFoundError(f"Output file {output_file} not found after {timeout} seconds")
|
||||
|
||||
def read_from_output_file(idf: int, output_file="/tmp/connector.output", wait_timeout=10):
|
||||
wait_for_output_file(output_file, wait_timeout)
|
||||
with open(output_file, "r") as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
if line.strip().startswith(str(idf)):
|
||||
return line.strip().split(" ", 1)[1]
|
||||
return None
|
||||
|
||||
def start_connector():
|
||||
global connector
|
||||
connector = ConnectorLibrary()
|
||||
connector.start_connector()
|
||||
return connector
|
||||
|
||||
def stop_connector():
|
||||
global connector
|
||||
if connector:
|
||||
connector.stop_connector()
|
||||
connector = None
|
||||
else:
|
||||
print("No connector to stop.")
|
||||
|
||||
def send_to_connector(idf: int, command: str, timeout: int = 5, output_file=None, command_log="/tmp/connector.commands.log"):
|
||||
global connector
|
||||
if connector:
|
||||
connector.send_to_connector(idf, command, timeout, command_log)
|
||||
# Capture the output line after sending command
|
||||
output_file = output_file or f"/tmp/connector.output.{idf}"
|
||||
line = connector.write_next_output_to_file(output_file)
|
||||
# Copy per-test file to common output
|
||||
if output_file != "/tmp/connector.output":
|
||||
import shutil
|
||||
shutil.copyfile(output_file, "/tmp/connector.output")
|
||||
return line # Return the actual line written!
|
||||
else:
|
||||
raise RuntimeError("Connector is not running.")
|
||||
|
||||
|
||||
def wait_for_result(idf: int, timeout: int = 5, poll_interval=0.2):
|
||||
end_time = time.time() + timeout
|
||||
while time.time() < end_time:
|
||||
result = read_from_output_file(idf)
|
||||
if result:
|
||||
return result
|
||||
time.sleep(poll_interval)
|
||||
raise TimeoutError(f"No result found for id {idf} within {timeout} seconds.")
|
||||
|
||||
def clean_connector_output(line):
|
||||
if not isinstance(line, str):
|
||||
line = str(line)
|
||||
line = line.strip()
|
||||
print(f"CLEAN DEBUG RAW: {repr(line)}") # This will show hidden chars
|
||||
# If 'OK:' is in the line, just return everything from 'OK:' onward!
|
||||
idx = line.find("OK:")
|
||||
if idx != -1:
|
||||
cleaned = line[idx:]
|
||||
else:
|
||||
# Fallback to regex if 'OK:' not found
|
||||
cleaned = re.sub(r"^[^\w]*", "", line)
|
||||
print(f"CLEANED: {repr(cleaned)}")
|
||||
return cleaned
|
||||
|
||||
def extract_result_from_log(tc, log_path="/tmp/connector.log", output_path=None):
|
||||
"""
|
||||
Find the line with 'reporting check result' and the right check id.
|
||||
Write the 'output:' content to output_path.
|
||||
"""
|
||||
with open(log_path, 'r') as f:
|
||||
for line in f:
|
||||
# Pattern: reporting check result #<id> ...
|
||||
m = re.search(r'reporting check result #(\d+).*output:(.*)', line)
|
||||
if m:
|
||||
found_id, output = m.group(1), m.group(2).strip()
|
||||
if str(found_id) == str(tc):
|
||||
# Write the output to the file
|
||||
output_path = output_path or f"/tmp/connector.output.{tc}"
|
||||
with open(output_path, 'w') as out:
|
||||
out.write(output + "\n")
|
||||
# Also copy to shared output
|
||||
if output_path != "/tmp/connector.output":
|
||||
import shutil
|
||||
shutil.copyfile(output_path, "/tmp/connector.output")
|
||||
return output
|
||||
# If not found, raise or return None
|
||||
raise Exception(f"No result found for id {tc} in log {log_path}")
|
||||
|
||||
@keyword
|
||||
def extract_result_from_log(tc, log_path="/tmp/connector.log", output_path=None):
|
||||
"""
|
||||
Find the line with 'reporting check result' and the right check id.
|
||||
Write the 'output:' content to output_path.
|
||||
"""
|
||||
import re
|
||||
with open(log_path, 'r') as f:
|
||||
for line in f:
|
||||
m = re.search(r'reporting check result #(\d+).*output:(.*)', line)
|
||||
if m:
|
||||
found_id, output = m.group(1), m.group(2).strip()
|
||||
if str(found_id) == str(tc):
|
||||
output_path = output_path or f"/tmp/connector.output.{tc}"
|
||||
with open(output_path, 'w') as out:
|
||||
out.write(output + "\n")
|
||||
# Also copy to shared output
|
||||
if output_path != "/tmp/connector.output":
|
||||
import shutil
|
||||
shutil.copyfile(output_path, "/tmp/connector.output")
|
||||
return output
|
||||
raise Exception(f"No result found for id {tc} in log {log_path}")
|
@ -8,3 +8,6 @@ Library String
|
||||
Library XML
|
||||
Library Collections
|
||||
Resource resources.resource
|
||||
Library BuiltIn
|
||||
Library connector.py
|
||||
Library Process
|
@ -14,6 +14,7 @@ ${SNMPPORT} 2024
|
||||
${SNMPVERSION} 2c
|
||||
${PERCENT} %
|
||||
${MOCKOON_LOG_FILE} /tmp/mockoon.log
|
||||
${TIMEOUT} 30
|
||||
|
||||
*** Keywords ***
|
||||
Start Mockoon
|
||||
@ -53,6 +54,9 @@ Ctn Cleanup Cache
|
||||
Ctn Generic Suite Setup
|
||||
Ctn Cleanup Cache
|
||||
Set Environment Variable TZ UTC
|
||||
Remove Files /tmp/connector.output.*
|
||||
Remove File /tmp/connector.log
|
||||
Start Connector
|
||||
|
||||
Ctn Run Command And Check Result As Regexp
|
||||
[Arguments] ${command} ${expected_result}
|
||||
@ -86,6 +90,19 @@ Ctn Verify Command Output
|
||||
... values=False
|
||||
... collapse_spaces=True
|
||||
|
||||
Ctn Run Command With Connector And Check Result As Strings
|
||||
[Arguments] ${tc} ${command} ${expected_result} ${timeout}=5
|
||||
Remove File /tmp/connector.output
|
||||
Remove File /tmp/connector.command.log
|
||||
Send To Connector ${tc} ${command} ${timeout}
|
||||
# 1. Extract the result from the log and write to /tmp/connector.output
|
||||
Extract Result From Log ${tc}
|
||||
# 2. Read the file
|
||||
${output} Get File /tmp/connector.output
|
||||
${output} Strip String ${output}
|
||||
# 3. Compare with expected
|
||||
Should Be Equal As Strings ${output} ${expected_result}
|
||||
|
||||
Ctn Run Command And Check Result As Json
|
||||
[Arguments] ${command} ${expected}
|
||||
Log To Console ${command}
|
||||
|
Loading…
x
Reference in New Issue
Block a user