Skip to content

Commit

Permalink
headlamp-plugin: Add e2e test for plugin management
Browse files Browse the repository at this point in the history
This patch adds integration test for plugin
management commands.

Signed-off-by: yolossn <sannagaraj@microsoft.com>
  • Loading branch information
yolossn committed Jul 1, 2024
1 parent e88062c commit c0c0111
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 468 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ frontend-test:
plugins-test:
cd plugins/headlamp-plugin && npm install && ./test-headlamp-plugin.js
cd plugins/headlamp-plugin && ./test-plugins-examples.sh
cd plugins/headlamp-plugin && node ./headlamp-plugin-management.test.js
cd plugins/headlamp-plugin && npx jest ./plugin-management-utils.test.js

# IMAGE_BASE can be used to specify a base final image.
# IMAGE_BASE=debian:latest make image
Expand Down
4 changes: 2 additions & 2 deletions plugins/headlamp-plugin/bin/headlamp-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,8 @@ function test(packageFolder) {
return runScriptOnPackages(packageFolder, 'test', script, { UNDER_TEST: 'true' });
}

const headlampPluginBin = fs.realpathSync(process.argv[1]);
console.log('headlampPluginBin path:', headlampPluginBin);
// const headlampPluginBin = fs.realpathSync(process.argv[1]);
// console.log('headlampPluginBin path:', headlampPluginBin);

yargs(process.argv.slice(2))
.command(
Expand Down
76 changes: 76 additions & 0 deletions plugins/headlamp-plugin/headlamp-plugin-management.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const { execSync } = require('child_process');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const envPaths = require('env-paths');

// Helper function to run CLI commands and return the output
function runCommand(command) {
try {
return execSync(command, { encoding: 'utf8' });
} catch (error) {
console.error(`Error running command "${command}":`, error);
process.exit(1);
}
}

// Helper function to get the default plugins directory
function defaultPluginsDir() {
const paths = envPaths('Headlamp', { suffix: '' });
const configDir = fs.existsSync(paths.data) ? paths.data : paths.config;
return path.join(configDir, 'plugins');
}

// create default plugins directory if it doesn't exist
const pluginsDir = defaultPluginsDir();
if (!fs.existsSync(pluginsDir)) {
fs.mkdirSync(pluginsDir, { recursive: true });
}

// List plugins initially
let output = runCommand('node ./bin/headlamp-plugin.js list --json');
console.log('Initial list output:', output);
let plugins = JSON.parse(output);
console.log('Initial plugins:', plugins);

// Ensure the plugin is not installed
const pluginName = 'prometheus';
let pluginExists = plugins.some(plugin => plugin.pluginName === pluginName);
assert.strictEqual(pluginExists, false, 'Plugin should not be initially installed');

// Install the plugin
const pluginURL = 'https://artifacthub.io/packages/headlamp/test-123/prometheus_headlamp_plugin';
output = runCommand(`node ./bin/headlamp-plugin.js install ${pluginURL}`);
console.log('Install output:', output);

// List plugins to verify installation
output = runCommand('node ./bin/headlamp-plugin.js list --json');
plugins = JSON.parse(output);
console.log('Plugins after install:', plugins);
pluginExists = plugins.some(plugin => plugin.pluginName === pluginName);
assert.strictEqual(pluginExists, true, 'Plugin should be installed');

// Update the plugin
output = runCommand(`node ./bin/headlamp-plugin.js update ${pluginName}`);
console.log('Update output:', output);

// List plugins to verify update
output = runCommand('node ./bin/headlamp-plugin.js list --json');
plugins = JSON.parse(output);
console.log('Plugins after update:', plugins);
pluginExists = plugins.some(plugin => plugin.pluginName === pluginName);
assert.strictEqual(pluginExists, true, 'Plugin should still be installed after update');

// Uninstall the plugin
output = runCommand(`node ./bin/headlamp-plugin.js uninstall ${pluginName}`);
console.log('Uninstall output:', output);

// List plugins to verify uninstallation
output = runCommand('node ./bin/headlamp-plugin.js list --json');
console.log('Initial list output:', output);
plugins = JSON.parse(output);
console.log('Plugins after uninstall:', plugins);
pluginExists = plugins.some(plugin => plugin.pluginName === pluginName);
assert.strictEqual(pluginExists, false, 'Plugin should be uninstalled');

console.log('All tests passed successfully.');
Loading

0 comments on commit c0c0111

Please sign in to comment.