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

Commit

Permalink
start tests overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Apr 13, 2024
1 parent c82fa43 commit 9ed8ee9
Show file tree
Hide file tree
Showing 55 changed files with 2,045 additions and 1,515 deletions.
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
tests/.phpunit.result.cache
32 changes: 18 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"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",
"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"
}
}
}
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
12 changes: 9 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,7 +193,10 @@ private function fetchBearerToken()
}
}

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


return $this->client;
}
}
13 changes: 10 additions & 3 deletions inc/migration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ final class PluginJamfMigration
*/
private $db;

/** @var PluginJamfAPI */
private $api;

/**
* PluginJamfMigration constructor.
* @param string $version
Expand Down Expand Up @@ -307,7 +310,7 @@ private function apply_1_1_0_migration()
// Find all devices that don't have the jamf id recorded, and retrieve it.
$unassigned = $mobiledevice->find(['jamf_items_id' => -1]);
foreach ($unassigned as $item) {
$jamf_item = PluginJamfAPI::getMobileDeviceByUDID($item['udid']);
$jamf_item = $this->api::getMobileDeviceByUDID($item['udid']);
if ($jamf_item !== null && count($jamf_item) === 1) {
$mobiledevice->update([
'id' => $item['id'],
Expand Down Expand Up @@ -552,7 +555,11 @@ public function apply_3_0_0_migration(): void
'FROM' => 'glpi_plugin_jamf_devices'
]);

$jss_mobiledevices = PluginJamfAPI::getAllMobileDevices();
if (count($devices)) {
$jss_mobiledevices = $this->api::getAllMobileDevices();
} else {
$jss_mobiledevices = [];
}
foreach ($devices as $device) {
if ($device['jamf_type'] === 'MobileDevice') {
// We can get the model identifier directly from the list of mobile devices
Expand All @@ -572,7 +579,7 @@ public function apply_3_0_0_migration(): void
}
} else if ($device['jamf_type'] === 'Computer') {
// We need to query the JSS for the computer's model identifier
$computer = PluginJamfAPI::getComputerByID($device['jamf_items_id'], 'hardware');
$computer = $this->api::getComputerByID($device['jamf_items_id'], 'hardware');
if ($computer !== null) {
$this->db->update(
'glpi_plugin_jamf_devices',
Expand Down
6 changes: 6 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="tests/bootstrap.php">
<testsuite name='Jamf Plugin Tests'>
<directory suffix='.php'>./tests/units/</directory>
</testsuite>
</phpunit>
Loading

0 comments on commit 9ed8ee9

Please sign in to comment.