Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Automatic Test Overhaul #147

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/actions/composer-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
docker exec --user www-data glpi /bin/bash -c "cd /var/www/glpi/plugins/jamf && vendor/bin/phpunit -c tests/phpunit.xml --testsuite \"PHP Unit Tests\" --do-not-cache --verbose"
31 changes: 31 additions & 0 deletions .github/actions/init_containers-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
set -e -u -x -o pipefail

docker-compose pull --quiet
docker-compose up --no-start
docker-compose start

# Check services health (because the DB container is not ready by the time we try to install GLPI)
for CONTAINER_ID in `docker-compose ps -a -q`; do
CONTAINER_NAME=`/usr/bin/docker inspect --format='{{print .Name}}{{if .Config.Image}} ({{print .Config.Image}}){{end}}' $CONTAINER_ID`
HEALTHY=false
TOTAL_COUNT=0
until [ $HEALTHY = true ]; do
if [ "`/usr/bin/docker inspect --format='{{if .Config.Healthcheck}}{{print .State.Health.Status}}{{else}}{{print \"healthy\"}}{{end}}' $CONTAINER_ID`" == "healthy" ]
then
HEALTHY=true
echo "$CONTAINER_NAME is healthy"
else
if [ $TOTAL_COUNT -eq 15 ]
then
echo "$CONTAINER_NAME fails to start"
exit 1
fi
echo "Waiting for $CONTAINER_NAME to be ready..."
sleep 2
TOTAL_COUNT=$[$TOTAL_COUNT +1]
fi
done
done

sleep 5
8 changes: 8 additions & 0 deletions .github/actions/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

# install glpi database
docker exec --user www-data glpi /bin/bash -c "cd /var/www/glpi && php bin/console db:install -H db -u glpi -p glpi -d glpi -n -r"

# install our plugin
docker exec --user www-data glpi /bin/bash -c "cd /var/www/glpi && php bin/console plugin:install -u glpi jamf"
docker exec --user www-data glpi /bin/bash -c "cd /var/www/glpi && php bin/console plugin:activate jamf"
2 changes: 2 additions & 0 deletions .github/actions/phpstan-checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
docker exec --user www-data glpi /bin/bash -c "cd /var/www/glpi/plugins/jamf && vendor/bin/phpstan analyze -c phpstan.neon --memory-limit 256M"
3 changes: 3 additions & 0 deletions .github/actions/teardown_containers-cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker-compose down --volumes
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Jamf Plugin CI
on:
push:
branches:
- "main"
tags:
- 'v*'
pull_request:
schedule:
- cron: "0 0 * * *"
workflow_dispatch:
jobs:
ci:
name: "GLPI ${{ matrix.glpi-version }} - php:${{ matrix.php-version }} - ${{ matrix.db-image }}"
strategy:
fail-fast: false
matrix:
include:
- { glpi-version: "10.0.x", php-version: "7.4", db-image: "mariadb:10.5" }
- { glpi-version: "10.0.x", php-version: "8.1", db-image: "mariadb:10.5" }
- { glpi-version: "10.0.x", php-version: "8.2", db-image: "mariadb:11.0" }
uses: "glpi-project/plugin-ci-workflows/.github/workflows/continuous-integration.yml@v1"
with:
plugin-key: "jamf"
glpi-version: "${{ matrix.glpi-version }}"
php-version: "${{ matrix.php-version }}"
db-image: "${{ matrix.db-image }}"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
composer.lock
.phpunit.result.cache
34 changes: 20 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
{
"require": {
"php": "^7.4"
},
"require-dev": {
"glpi-project/tools": "^0.7.2"
},
"config": {
"optimize-autoloader": true,
"platform": {
"php": "7.4.0"
"require": {
"php": "~7.4.0|~8.0.0|~8.1.0|~8.2.0"
},
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
"require-dev": {
"glpi-project/tools": "^0.7.2",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "~9.6"
},
"config": {
"optimize-autoloader": true,
"platform": {
"php": "7.4"
},
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
},
"scripts": {
"test": "phpunit -c phpunit.xml",
"phpstan": "phpstan analyse -c phpstan.neon"
}
}
}
2 changes: 1 addition & 1 deletion front/ruleimport.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
Html::displayNotFoundError();
}

$rulecollection = new PluginJamfRuleImportCollection($_SESSION['glpiactive_entity']);
$rulecollection = new PluginJamfRuleImportCollection();

include(GLPI_ROOT . "/front/rule.common.form.php");
2 changes: 1 addition & 1 deletion front/ruleimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
Html::displayNotFoundError();
}

$rulecollection = new PluginJamfRuleImportCollection($_SESSION['glpiactive_entity']);
$rulecollection = new PluginJamfRuleImportCollection();

include(GLPI_ROOT . "/front/rule.common.php");
31 changes: 17 additions & 14 deletions inc/api.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class PluginJamfAPI
*/
private static $connection;

protected static $connection_class = PluginJamfConnection::class;

/**
* Get data from a JSS Classic API endpoint.
* @param string $endpoint The API endpoint.
Expand All @@ -44,10 +46,10 @@ class PluginJamfAPI
* @throws PluginJamfRateLimitException
* @since 1.0.0
*/
private static function getClassic(string $endpoint, $raw = false, $response_type = 'application/json')
protected static function getClassic(string $endpoint, $raw = false, $response_type = 'application/json')
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$url = static::$connection->getAPIUrl($endpoint);
$client = static::$connection->getClient();
Expand Down Expand Up @@ -87,10 +89,10 @@ private static function getClassic(string $endpoint, $raw = false, $response_typ
* @return int|bool True if successful, or the HTTP return code if it is not 201.
* @since 1.1.0
*/
private static function addClassic(string $endpoint, string $payload)
protected static function addClassic(string $endpoint, string $payload)
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$url = (static::$connection)->getAPIUrl($endpoint);
$client = static::$connection->getClient();
Expand All @@ -117,10 +119,10 @@ private static function addClassic(string $endpoint, string $payload)
* @return int|bool True if successful, or the HTTP return code if it is not 201.
* @since 1.1.0
*/
private static function updateClassic(string $endpoint, array $data)
protected static function updateClassic(string $endpoint, array $data)
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$url = (static::$connection)->getAPIUrl($endpoint);
$client = static::$connection->getClient();
Expand All @@ -147,10 +149,10 @@ private static function updateClassic(string $endpoint, array $data)
* @return int|bool True if successful, or the HTTP return code if it is not 200.
* @since 1.1.0
*/
private static function deleteClassic(string $endpoint)
protected static function deleteClassic(string $endpoint)
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$url = (static::$connection)->getAPIUrl($endpoint);
$client = static::$connection->getClient();
Expand Down Expand Up @@ -363,7 +365,7 @@ public static function testProAPIConnection(): bool
public static function getJamfProVersion(): string
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$response = static::$connection->getClient()->get(static::$connection->getAPIUrl('v1/jamf-pro-version', true))->getBody()->getContents();
return json_decode($response, true)['version'];
Expand All @@ -380,8 +382,9 @@ public static function sendMDMCommand(string $payload_xml, bool $user_auth = fal
*/
public static function getAllMobileDevices()
{
var_dump((new Exception())->getTraceAsString());ob_flush();
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$all_results = [];

Expand Down Expand Up @@ -419,7 +422,7 @@ public static function getAllMobileDevices()
public static function getMobileDeviceByID(int $id, bool $detailed = false)
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$endpoint = "/v2/mobile-devices/{$id}" . ($detailed ? '/detail' : '');
$response = static::$connection->getClient()->get(static::$connection->getAPIUrl($endpoint, true));
Expand All @@ -435,7 +438,7 @@ public static function getMobileDeviceByID(int $id, bool $detailed = false)
public static function getMobileDeviceByUDID(string $udid, string $section = 'general'): ?array
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$query_params = [
'section' => strtoupper($section),
Expand All @@ -457,7 +460,7 @@ public static function getMobileDeviceByUDID(string $udid, string $section = 'ge
public static function getAllComputers()
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$all_results = [];

Expand Down Expand Up @@ -489,7 +492,7 @@ public static function getAllComputers()
public static function getComputerByID(int $id, ?string $section = null): ?array
{
if (!static::$connection) {
static::$connection = new PluginJamfConnection();
static::$connection = new static::$connection_class();
}
$endpoint = "/v1/computer-inventory";
$query_params = [
Expand Down
10 changes: 5 additions & 5 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ class PluginJamfConfig extends CommonDBTM

public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
if (!$withtemplate && $item->getType() === 'Config') {
if (!$withtemplate && $item::getType() === Config::class) {
return _x('plugin_info', 'Jamf plugin', 'jamf');
}
return '';
}

public function showForm($ID = -1, array $options = [])
{
global $CFG_GLPI;
if (!Session::haveRight('config', UPDATE)) {
return false;
}
Expand All @@ -51,14 +50,15 @@ public function showForm($ID = -1, array $options = [])
'config' => $config,
'url' => Toolbox::getItemTypeFormURL('Config'),
]);
return true;
}

public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
{
if ($item->getType() === 'Config') {
$config = new self();
$config->showForm();
if ($item::getType() === Config::class) {
return (new self())->showForm();
}
return false;
}

public static function undiscloseConfigValue($fields)
Expand Down
17 changes: 14 additions & 3 deletions inc/connection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

use GuzzleHttp\Client;
use GuzzleHttp\ClientTrait;

/**
* JamfConnection class
Expand All @@ -33,7 +34,10 @@ class PluginJamfConnection

private ?string $bearer_token = null;

private Client $client;
/**
* @var ClientTrait
*/
protected $client;

/**
* Load connection details from the DB and store them in the $config array.
Expand Down Expand Up @@ -189,8 +193,16 @@ private function fetchBearerToken()
}
}

public function getClient(): Client
/**
* @return ClientTrait
*/
public function getClient()
{
/**
* @var array $CFG_GLPI
*/
global $CFG_GLPI;

if (!isset($this->client)) {
if ($this->bearer_token === null) {
$this->fetchBearerToken();
Expand All @@ -216,7 +228,6 @@ public function getClient(): Client
$this->client = new Client($options);
}


return $this->client;
}
}
2 changes: 1 addition & 1 deletion inc/devicesync.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static function syncAll(): int
$volume = 0;

$config = PluginJamfConfig::getConfig();
$valid_sync_interval = isset($config['sync_interval']) && !empty($config['sync_interval']) && ((int) $config['sync_interval'] >= 1);
$valid_sync_interval = isset($config['sync_interval']) && !empty($config['sync_interval']) && ((int)$config['sync_interval'] >= 1);
if (!$valid_sync_interval) {
$config['sync_interval'] = 8 * 60;
}
Expand Down
Loading