From f712d225d4e343269e010a83e8a2092632c3e643 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 28 Feb 2025 16:57:36 +0100
Subject: [PATCH] add first tests

---
 tests/00_start.php                 | 20 ++++++++++
 tests/010_is_executable.php        |  7 ++++
 tests/020_is_executable_in_tmp.php | 17 ++++++++
 tests/030_has_help.php             |  7 ++++
 tests/110_set_metadata.php         | 64 ++++++++++++++++++++++++++++++
 tests/200_cert.php                 |  7 ++++
 tests/200_diskfree.php             |  7 ++++
 tests/200_exec.php                 |  7 ++++
 tests/200_file.php                 |  7 ++++
 tests/200_simple.php               |  7 ++++
 tests/configs/cert.ini             | 22 ++++++++++
 tests/configs/diskfree.ini         | 24 +++++++++++
 tests/configs/exec.ini             | 22 ++++++++++
 tests/configs/file.ini             | 35 ++++++++++++++++
 tests/configs/hello.ini            | 22 ++++++++++
 tests/configs/metadata.ini         | 45 +++++++++++++++++++++
 16 files changed, 320 insertions(+)
 create mode 100755 tests/00_start.php
 create mode 100644 tests/010_is_executable.php
 create mode 100644 tests/020_is_executable_in_tmp.php
 create mode 100644 tests/030_has_help.php
 create mode 100644 tests/110_set_metadata.php
 create mode 100644 tests/200_cert.php
 create mode 100644 tests/200_diskfree.php
 create mode 100644 tests/200_exec.php
 create mode 100644 tests/200_file.php
 create mode 100644 tests/200_simple.php
 create mode 100644 tests/configs/cert.ini
 create mode 100644 tests/configs/diskfree.ini
 create mode 100644 tests/configs/exec.ini
 create mode 100644 tests/configs/file.ini
 create mode 100644 tests/configs/hello.ini
 create mode 100644 tests/configs/metadata.ini

diff --git a/tests/00_start.php b/tests/00_start.php
new file mode 100755
index 0000000..bb0df25
--- /dev/null
+++ b/tests/00_start.php
@@ -0,0 +1,20 @@
+#!/bin/env php
+<?php
+
+require_once __DIR__.'/../inc_functions.php';
+require_once __DIR__.'/../inc_vars.php';
+
+$AMCLI??=__DIR__.'/../src/amcli.php';
+
+echo "\n\nSTART TESTS\n\n";
+foreach(glob("*.php") as $sFile){
+    if($sFile==basename(__FILE__)){
+        // SKIP
+    } else {
+        _h1("$sFile");
+        include $sFile;    
+    }
+
+}
+
+_h1("DONE");
diff --git a/tests/010_is_executable.php b/tests/010_is_executable.php
new file mode 100644
index 0000000..ad0b83b
--- /dev/null
+++ b/tests/010_is_executable.php
@@ -0,0 +1,7 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Check if $AMCLI is executable
+";
+_exec("$AMCLI -V");
diff --git a/tests/020_is_executable_in_tmp.php b/tests/020_is_executable_in_tmp.php
new file mode 100644
index 0000000..a8f5572
--- /dev/null
+++ b/tests/020_is_executable_in_tmp.php
@@ -0,0 +1,17 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Check if $AMCLI is executable in another place, eg.tmp
+";
+
+if(strstr($AMCLI, ".php")){
+    echo "SKIP: not for php script - it is for the binary only.\n";
+    return true;
+}
+
+$tmpfile="/tmp/".basename($AMCLI);
+file_put_contents($tmpfile, file_get_contents($AMCLI));
+chmod($tmpfile, 0700);
+_exec("$tmpfile -V");
+unlink($tmpfile);
diff --git a/tests/030_has_help.php b/tests/030_has_help.php
new file mode 100644
index 0000000..4652a7a
--- /dev/null
+++ b/tests/030_has_help.php
@@ -0,0 +1,7 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Check if $AMCLI shows a help page
+";
+_exec("$AMCLI -h");
diff --git a/tests/110_set_metadata.php b/tests/110_set_metadata.php
new file mode 100644
index 0000000..faad586
--- /dev/null
+++ b/tests/110_set_metadata.php
@@ -0,0 +1,64 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Set application metadata
+";
+
+$sInifile=__DIR__."/configs/metadata.ini";
+
+// --------------------------------------------------------------------
+// FUNCTION
+// --------------------------------------------------------------------
+
+function compare_strings($sKey, $sIniValue, $aResponseValue=null){
+    echo "Values for '$sKey':\n  INI     : $sIniValue\n  Response: $aResponseValue\n            ";
+    if ($sIniValue!=$aResponseValue){
+        _abort("Values differ");
+    }
+    _ok(); echo "\n";
+}
+
+// --------------------------------------------------------------------
+// MAIN
+// --------------------------------------------------------------------
+
+// basic check ... the ini file is processed?
+_exec("$AMCLI --ini='$sInifile'");
+
+// --------------------------------------------------------------------
+
+// yes. Now let's compare ini values and the response
+exec("$AMCLI --ini='$sInifile'", $aOut, $rc);
+
+$aIni=parse_ini_file($sInifile, 1);
+$aResponse=json_decode($aOut[0], 1);
+
+// print_r($aIni);
+// print_r($aResponse);
+
+// --------------------------------------------------------------------
+
+echo "\n";
+compare_strings("host", $aIni["meta"]["host"], $aResponse["meta"]["host"]);
+compare_strings("website", $aIni["meta"]["website"], $aResponse["meta"]["website"]);
+compare_strings("ttl", $aIni["meta"]["ttl"], $aResponse["meta"]["ttl"]);
+
+foreach($aIni["notifications"]["email"]??[] as $sEmail){
+    echo "Check Email '$sEmail'\n";
+    if(array_search($sEmail, $aResponse["meta"]["notifications"]["email"])===false){
+        _abort("Email not found in response");
+    }
+    _ok(); echo "\n";
+}
+
+foreach($aIni["notifications"]["slack"]??[] as $sValue){
+    $aArray = json_decode($sValue, 1);
+    $sChannel = array_keys($aArray)[0];
+    $sWebhook = array_values($aArray)[0];
+
+    echo "Check Slack '$sChannel'\n";
+    compare_strings("Check Slack '$sChannel'", $sWebhook, $aResponse["meta"]["notifications"]['slack'][$sChannel]??'');
+}
+
+// --------------------------------------------------------------------
diff --git a/tests/200_cert.php b/tests/200_cert.php
new file mode 100644
index 0000000..f7f3418
--- /dev/null
+++ b/tests/200_cert.php
@@ -0,0 +1,7 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Run check cert
+";
+_exec("$AMCLI --ini='".__DIR__."/configs/cert.ini'");
diff --git a/tests/200_diskfree.php b/tests/200_diskfree.php
new file mode 100644
index 0000000..f7f3418
--- /dev/null
+++ b/tests/200_diskfree.php
@@ -0,0 +1,7 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Run check cert
+";
+_exec("$AMCLI --ini='".__DIR__."/configs/cert.ini'");
diff --git a/tests/200_exec.php b/tests/200_exec.php
new file mode 100644
index 0000000..499bcd9
--- /dev/null
+++ b/tests/200_exec.php
@@ -0,0 +1,7 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Run check Exec
+";
+_exec("$AMCLI --ini='".__DIR__."/configs/exec.ini'");
diff --git a/tests/200_file.php b/tests/200_file.php
new file mode 100644
index 0000000..5664c8d
--- /dev/null
+++ b/tests/200_file.php
@@ -0,0 +1,7 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Run check File
+";
+_exec("$AMCLI --ini='".__DIR__."/configs/file.ini'");
diff --git a/tests/200_simple.php b/tests/200_simple.php
new file mode 100644
index 0000000..e88d4eb
--- /dev/null
+++ b/tests/200_simple.php
@@ -0,0 +1,7 @@
+#!/usr/bin/env php
+<?php
+
+echo "
+WHAT: Run check Simple
+";
+_exec("$AMCLI --ini='".__DIR__."/configs/simple.ini'");
diff --git a/tests/configs/cert.ini b/tests/configs/cert.ini
new file mode 100644
index 0000000..7186448
--- /dev/null
+++ b/tests/configs/cert.ini
@@ -0,0 +1,22 @@
+; =======================================================================
+;
+; APPMONITOR CLI CLIENT
+;
+; for the checks see its parameters
+; <https://os-docs.iml.unibe.ch/appmonitor/PHP_client/Plugins/Checks/index.html>
+;
+; =======================================================================
+
+
+; -----------------------------------------------------------------------
+; CHECKS
+; -----------------------------------------------------------------------
+
+["Cert plugin"]
+description="SSL certificate of www.example.com"
+function="Cert"
+params='{
+    "url": "https://www.example.com"
+}'
+
+; -----------------------------------------------------------------------
diff --git a/tests/configs/diskfree.ini b/tests/configs/diskfree.ini
new file mode 100644
index 0000000..92a0c1f
--- /dev/null
+++ b/tests/configs/diskfree.ini
@@ -0,0 +1,24 @@
+; =======================================================================
+;
+; APPMONITOR CLI CLIENT
+;
+; for the checks see its parameters
+; <https://os-docs.iml.unibe.ch/appmonitor/PHP_client/Plugins/Checks/index.html>
+;
+; =======================================================================
+
+
+; -----------------------------------------------------------------------
+; CHECKS
+; -----------------------------------------------------------------------
+
+["Free disk space /tmp"]
+description="Check free diskspace in /tmp"
+function="Diskfree"
+params='{
+    "directory": "/tmp",
+    "warning": "1GB",
+    "critical": "500MB"
+}'
+
+; -----------------------------------------------------------------------
diff --git a/tests/configs/exec.ini b/tests/configs/exec.ini
new file mode 100644
index 0000000..af22123
--- /dev/null
+++ b/tests/configs/exec.ini
@@ -0,0 +1,22 @@
+; =======================================================================
+;
+; APPMONITOR CLI CLIENT
+;
+; for the checks see its parameters
+; <https://os-docs.iml.unibe.ch/appmonitor/PHP_client/Plugins/Checks/index.html>
+;
+; =======================================================================
+
+
+; -----------------------------------------------------------------------
+; CHECKS
+; -----------------------------------------------------------------------
+
+["Exec uptime"]
+description="Start uptime command"
+function="Exec"
+params='{
+    "command": "uptime"
+}'
+
+; -----------------------------------------------------------------------
diff --git a/tests/configs/file.ini b/tests/configs/file.ini
new file mode 100644
index 0000000..542bc21
--- /dev/null
+++ b/tests/configs/file.ini
@@ -0,0 +1,35 @@
+; =======================================================================
+;
+; APPMONITOR CLI CLIENT
+;
+; for the checks see its parameters
+; <https://os-docs.iml.unibe.ch/appmonitor/PHP_client/Plugins/Checks/index.html>
+;
+; =======================================================================
+
+
+; -----------------------------------------------------------------------
+; CHECKS
+; -----------------------------------------------------------------------
+
+["Check dir /tmp"]
+description="Check existing dir"
+function="File"
+params='{
+    "filename": "/tmp",
+    "exists": true,
+    "flag": "dir",
+    "readable": true,
+    "writable": true
+}'
+
+["No maintenance"]
+description="Check non existing maintenance flag file"
+function="File"
+params='{
+    "filename": "/some/where/maintenance.txt",
+    "exists": false,
+    "flag": "file"
+}'
+
+; -----------------------------------------------------------------------
diff --git a/tests/configs/hello.ini b/tests/configs/hello.ini
new file mode 100644
index 0000000..17f97bc
--- /dev/null
+++ b/tests/configs/hello.ini
@@ -0,0 +1,22 @@
+; =======================================================================
+;
+; APPMONITOR CLI CLIENT
+;
+; for the checks see its parameters
+; <https://os-docs.iml.unibe.ch/appmonitor/PHP_client/Plugins/Checks/index.html>
+;
+; =======================================================================
+
+
+; -----------------------------------------------------------------------
+; CHECKS
+; -----------------------------------------------------------------------
+
+["hello plugin"]
+description="I sust wann say hello"
+function="hello"
+params='{
+"message": "Here I am"
+}'
+
+; -----------------------------------------------------------------------
diff --git a/tests/configs/metadata.ini b/tests/configs/metadata.ini
new file mode 100644
index 0000000..0909722
--- /dev/null
+++ b/tests/configs/metadata.ini
@@ -0,0 +1,45 @@
+; =======================================================================
+;
+; APPMONITOR CLI CLIENT
+;
+; to understand the entries of metadata see JSON in 
+; <https://os-docs.iml.unibe.ch/appmonitor/Client/Description_of_response.html>
+;
+; for the checks see its parameters
+; <https://os-docs.iml.unibe.ch/appmonitor/PHP_client/Plugins/Checks/index.html>
+;
+; =======================================================================
+
+
+; -----------------------------------------------------------------------
+; METADATA
+; -----------------------------------------------------------------------
+
+[meta]
+host = "www.example.com"
+website = "Company website"
+ttl = 300
+tags[]="monitoring"
+
+
+[notifications]
+email[]="support@example.com"
+email[]="webmaster@example.com"
+
+; for slack use the following format
+; <channelname> + comma + <webhook url>
+slack[]='{ "#support-channel": "https://hooks.slack.com/services/XXXXXX/YYYYYY/ZZZZZ" }'
+
+
+; -----------------------------------------------------------------------
+; CHECKS
+; -----------------------------------------------------------------------
+
+["hello plugin"]
+description="I sust wann say hello"
+function="hello"
+params='{
+"message": "Here I am"
+}'
+
+; -----------------------------------------------------------------------
-- 
GitLab