diff --git a/Deploy.md b/Deploy.md index e45381c..23677f8 100644 --- a/Deploy.md +++ b/Deploy.md @@ -1,5 +1,8 @@ # deploying the ICRN kernel manager services +## get the most recent changes moved to campus cluster + + ## Changes needed to ICRN containers in dev diff --git a/documentation/catalog_resources/README.md b/documentation/catalog_resources/README.md index f66248d..5a88370 100644 --- a/documentation/catalog_resources/README.md +++ b/documentation/catalog_resources/README.md @@ -13,7 +13,7 @@ The central repository should be organized as follows: ``` central_repository/ ├── icrn_kernel_catalog.json -├── r_kernels/ +├── R/ │ ├── cowsay/ │ │ └── 1.0/ │ │ └── R_cowsay.conda.pack.tar.gz @@ -21,7 +21,7 @@ central_repository/ │ │ └── 1.9/ │ │ └── PEcAn-base-3.tar.gz │ └── ... -├── python_kernels/ +├── Python/ │ ├── numpy/ │ │ └── 1.20/ │ │ └── python_numpy.conda.pack.tar.gz diff --git a/icrn_manager b/icrn_manager index def1071..d568647 100755 --- a/icrn_manager +++ b/icrn_manager @@ -21,15 +21,11 @@ ICRN_USER_KERNEL_BASE=${ICRN_USER_KERNEL_BASE:-${ICRN_USER_BASE}/${icrn_kernels} ICRN_USER_CATALOG=${ICRN_USER_CATALOG:-${ICRN_USER_KERNEL_BASE}/user_catalog.json} if [ ! -e ${ICRN_MANAGER_CONFIG} ]; then - # if manager config json doesn't exist, we need to be in the 'init' call - if [ ! "$2" = "init" ]; then - echo "You must run 'icrn_manager kernels init' prior to leveraging this tool." - exit 1 - fi - # if the config doesn't exist, it will be created and populated during the init call + # Note: Auto-initialization will be handled in kernels() function + # This section sets up default paths for when config doesn't exist yet ICRN_KERNEL_REPOSITORY=$central_catalog_default - ICRN_R_KERNELS=${ICRN_KERNEL_REPOSITORY}"/r_kernels" - ICRN_PYTHON_KERNELS=${ICRN_KERNEL_REPOSITORY}"/python_kernels" + ICRN_R_KERNELS=${ICRN_KERNEL_REPOSITORY}"/R" + ICRN_PYTHON_KERNELS=${ICRN_KERNEL_REPOSITORY}"/Python" ICRN_KERNEL_CATALOG=${ICRN_KERNEL_REPOSITORY}"/icrn_kernel_catalog.json" else ICRN_KERNEL_REPOSITORY=$(jq -r ".\"icrn_central_catalog_path\"" "${ICRN_MANAGER_CONFIG}") @@ -846,6 +842,80 @@ function kernels__init() # create base resources echo "" echo "central catalog location will be: ${central_repository}" echo "" + + # Determine which paths will be affected + ICRN_MANAGER_CONFIG=${ICRN_MANAGER_CONFIG:-${ICRN_USER_BASE}/manager_config.json} + + echo "The following paths will be created or modified:" + echo "" + + local paths_to_create=() + local paths_to_overwrite=() + + # Check ICRN_USER_BASE + if [ ! -e "${ICRN_USER_BASE}/" ]; then + paths_to_create+=("${ICRN_USER_BASE}/ (directory)") + fi + + # Check ICRN_USER_KERNEL_BASE + if [ ! -e "${ICRN_USER_KERNEL_BASE}" ]; then + paths_to_create+=("${ICRN_USER_KERNEL_BASE} (directory)") + fi + + # Check language-specific subdirectories + if [ ! -e "${ICRN_USER_KERNEL_BASE}/r" ]; then + paths_to_create+=("${ICRN_USER_KERNEL_BASE}/r (directory)") + fi + + if [ ! -e "${ICRN_USER_KERNEL_BASE}/python" ]; then + paths_to_create+=("${ICRN_USER_KERNEL_BASE}/python (directory)") + fi + + # Check ICRN_USER_CATALOG + if [ ! -e "${ICRN_USER_CATALOG}" ]; then + paths_to_create+=("${ICRN_USER_CATALOG} (file)") + fi + + # Check ICRN_MANAGER_CONFIG + if [ ! -e "${ICRN_MANAGER_CONFIG}" ]; then + paths_to_create+=("${ICRN_MANAGER_CONFIG} (file)") + elif [ -n "$overwrite" ]; then + paths_to_overwrite+=("${ICRN_MANAGER_CONFIG} (file - will update central catalog path)") + fi + + # Display paths that will be created + if [ ${#paths_to_create[@]} -gt 0 ]; then + echo "Paths that will be CREATED:" + for path in "${paths_to_create[@]}"; do + echo " - $path" + done + echo "" + fi + + # Display paths that will be overwritten/modified + if [ ${#paths_to_overwrite[@]} -gt 0 ]; then + echo "Paths that will be MODIFIED:" + for path in "${paths_to_overwrite[@]}"; do + echo " - $path" + done + echo "" + fi + + # If nothing will be created or modified, inform user + if [ ${#paths_to_create[@]} -eq 0 ] && [ ${#paths_to_overwrite[@]} -eq 0 ]; then + echo "All required paths already exist. No changes will be made." + echo "" + return 0 + fi + + # Ask for confirmation + read -r -p "Do you want to proceed with these changes? [y/N] " response + if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then + echo "Initialization cancelled." + exit 0 + fi + echo "" + # check for existence of #~{HOME}/.icrn/ #~{HOME}/.icrn/icrn_kernels/ @@ -895,8 +965,8 @@ function kernels__init() # create base resources echo "creating ${ICRN_MANAGER_CONFIG}" echo "{ \"icrn_central_catalog_path\": \"${central_repository}\", - \"icrn_r_kernels\": \"r_kernels\", - \"icrn_python_kernels\": \"python_kernels\", + \"icrn_r_kernels\": \"R\", + \"icrn_python_kernels\": \"Python\", \"icrn_kernel_catalog\": \"icrn_kernel_catalog.json\" }" > $ICRN_MANAGER_CONFIG # non-append enables re-pointing of central repo via 'init' later @@ -940,6 +1010,32 @@ function kernels__init() # create base resources echo "" } +# Check if initialization is needed and perform it automatically if required +# +# This function checks if the ICRN Manager has been initialized by verifying +# the existence of the manager configuration file. If not initialized, it +# automatically calls kernels__init with the default central repository path. +# +# Parameters: +# None +# +# Returns: +# 0 if initialization was performed, 1 if already initialized +# +# Side effects: +# - Calls kernels__init() if configuration is missing +# - Creates user directories and configuration files +function check_and_init_if_needed() +{ + if [ ! -e "${ICRN_MANAGER_CONFIG}" ]; then + echo "ICRN Manager not initialized. Auto-initializing with default settings..." + echo "" + kernels__init + return 0 + fi + return 1 +} + # Main launcher function for kernel management subcommands. # # This function routes kernel-related subcommands to their respective handler functions. @@ -961,6 +1057,19 @@ function kernels__init() # create base resources function kernels() # launcher { local cmdname=$1; shift + + # Auto-initialize if needed (skip for 'init' command itself) + if [ "$cmdname" != "init" ]; then + check_and_init_if_needed + # After auto-init, reload config variables since they may have changed + if [ -e "${ICRN_MANAGER_CONFIG}" ]; then + ICRN_KERNEL_REPOSITORY=$(jq -r ".\"icrn_central_catalog_path\"" "${ICRN_MANAGER_CONFIG}") + ICRN_R_KERNELS=${ICRN_KERNEL_REPOSITORY}"/"$(jq -r ".\"icrn_r_kernels\"" "${ICRN_MANAGER_CONFIG}") + ICRN_PYTHON_KERNELS=${ICRN_KERNEL_REPOSITORY}"/"$(jq -r ".\"icrn_python_kernels\"" "${ICRN_MANAGER_CONFIG}") + ICRN_KERNEL_CATALOG=${ICRN_KERNEL_REPOSITORY}"/"$(jq -r ".\"icrn_kernel_catalog\"" "${ICRN_MANAGER_CONFIG}") + fi + fi + if [ -z "$cmdname" ]; then echo "" echo Error: No subcommand specified. diff --git a/tests/README.md b/tests/README.md index b9c521a..52836ad 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,88 +1,260 @@ # ICRN Manager Test Suite -This directory contains the comprehensive test suite for the ICRN Kernel Manager. +This directory contains the comprehensive test suite for the ICRN Kernel Manager. The test suite ensures reliability, correctness, and security of all kernel management operations. ## Quick Start ```bash -# Run all tests +# Run all tests (57 tests total) ./tests/run_tests.sh all # Run specific test categories -./tests/run_tests.sh kernels # Kernel operations -./tests/run_tests.sh update_r_libs # R library management -./tests/run_tests.sh config # Configuration validation -./tests/run_tests.sh help # Help and basic commands +./tests/run_tests.sh kernels # Kernel operations (18 tests) +./tests/run_tests.sh update_r_libs # R library management (6 tests) +./tests/run_tests.sh config # Configuration validation (10 tests) +./tests/run_tests.sh help # Help and basic commands (3 tests) +./tests/run_tests.sh kernel_indexer # Kernel indexing (20 tests) + +# Clean up test artifacts after running tests +./tests/cleanup_tests.sh + +# Clean up including test results log +./tests/cleanup_tests.sh --log ``` +## Test Suite Overview + +The test suite consists of **57 tests** organized into 5 main categories: + +| Category | Tests | Description | +|----------|-------|-------------| +| **Kernel Operations** | 18 | Core kernel management (init, get, use, clean, list, available) | +| **Kernel Indexer** | 20 | Kernel indexing, discovery, and manifest collation | +| **Configuration Validation** | 10 | Config file validation, error handling, JSON structure | +| **R Library Management** | 6 | update_r_libs.sh functionality and .Renviron management | +| **Help & Basic Commands** | 3 | Help commands, usage information, error messages | + ## Test Structure ### Test Files -- `test_common.sh` - Common utilities and test framework -- `test_kernels.sh` - Kernel operations (init, get, use, clean, etc.) -- `test_update_r_libs.sh` - R library management functionality -- `test_config.sh` - Configuration validation and error handling -- `test_help.sh` - Help commands and basic functionality -- `run_tests.sh` - Test runner and orchestration + +- **`test_common.sh`** - Common utilities, test framework, and helper functions +- **`test_kernels.sh`** - Kernel operations (init, get, use, clean, list, available) +- **`test_kernel_indexer.sh`** - Kernel indexing and manifest collation +- **`test_config.sh`** - Configuration validation and error handling +- **`test_update_r_libs.sh`** - R library management functionality +- **`test_help.sh`** - Help commands and basic functionality +- **`run_tests.sh`** - Test runner and orchestration script +- **`cleanup_tests.sh`** - Cleanup script for test artifacts ### Test Categories -#### Kernel Operations (13 tests) -- Initialization and configuration -- Listing available and installed kernels -- Getting and using kernels -- Cleaning and removing kernels -- Error handling for invalid parameters - -#### R Library Management (6 tests) -- Adding kernels to .Renviron files -- Removing kernels from .Renviron files -- Overwriting existing kernel configurations -- File permission and path validation -- Error handling for invalid file paths - -#### Configuration Validation (10 tests) -- Missing configuration file handling -- Invalid catalog and repository validation -- JSON structure validation -- Required field validation - -#### Help and Basic Commands (3 tests) -- Help command functionality -- Invalid command handling -- Usage information display +#### 1. Kernel Operations (18 tests) + +Tests core kernel management functionality: + +- **Initialization**: `kernels init` creates necessary directories and config files +- **Discovery**: `kernels available` lists kernels from central catalog +- **Listing**: `kernels list` shows user's installed kernels +- **Getting Kernels**: `kernels get` downloads and registers kernels (R and Python) +- **Using Kernels**: `kernels use` activates kernels for use +- **Cleaning**: `kernels clean` removes kernel entries from user catalog +- **Error Handling**: Invalid parameters, missing arguments, invalid languages +- **Security**: Path traversal protection, wildcard attack prevention + +**Key Features Tested:** +- Automatic initialization with confirmation prompts +- Overlay directory creation for R kernels +- Python kernel registration (in-place) +- R kernel registration with overlay paths +- Kernel activation and deactivation +- Catalog management + +#### 2. Kernel Indexer (20 tests) + +Tests the kernel indexing service: + +- **Help & Commands**: Help display, invalid commands, missing commands +- **Indexing**: R and Python kernel indexing, manifest creation +- **Discovery**: Kernel discovery, empty directories, invalid structures +- **Filtering**: Filtering by kernel name and version +- **Collation**: Manifest collation by kernels and by packages +- **Error Handling**: Missing manifests, invalid JSON, missing R/Rscript + +**Key Features Tested:** +- Kernel discovery in repository structure +- Package manifest generation +- Collated manifest creation (kernel-centric and package-centric) +- Language filtering +- Error recovery and graceful degradation + +#### 3. Configuration Validation (10 tests) + +Tests configuration file handling and validation: + +- **Missing Config**: Auto-initialization when config is missing +- **Missing Catalog**: Error handling when central catalog is missing +- **Missing Repository**: Error handling when repository directory is missing +- **Parameter Validation**: Invalid language, kernel, and version parameters +- **JSON Structure**: Valid JSON structure for config, user catalog, and central catalog +- **Required Fields**: Catalog contains all required fields for kernels + +**Key Features Tested:** +- Automatic initialization behavior +- Configuration file validation +- Catalog structure validation +- Error messages and user guidance + +#### 4. R Library Management (6 tests) + +Tests the `update_r_libs.sh` script: + +- **Adding Kernels**: Adding kernel paths to .Renviron files +- **Removing Kernels**: Removing kernel configurations from .Renviron +- **Overwriting**: Updating existing kernel configurations +- **Preservation**: Preserving existing .Renviron content +- **Error Handling**: Missing parameters, invalid file paths + +**Key Features Tested:** +- .Renviron file modification +- ICRN additions section management +- R_LIBS and R_LIBS_USER configuration +- Overlay path handling + +#### 5. Help & Basic Commands (3 tests) + +Tests basic command functionality: + +- **Help Command**: Help text display and usage information +- **Invalid Commands**: Error handling for unknown commands +- **Kernels Help**: Help display for kernels subcommand ## Test Environment ### Isolation -- Each test runs in its own isolated environment -- Tests create temporary directories in `./tests/test_env/` -- No shared state between tests -- Automatic cleanup after each test + +Each test runs in a completely isolated environment: + +- **Fresh Environment**: `setup_test_env` creates a new test environment for each test +- **Temporary Directories**: Tests use `./tests/test_env/` for all temporary files +- **No Shared State**: Tests don't interfere with each other +- **Automatic Cleanup**: Test environment is cleaned up after each test ### Mock Data -- Sample R kernels: `cowsay` (1.0), `ggplot2` (3.4.0) -- Sample Python kernel: `numpy` (1.24.0) -- Mock catalog with proper JSON structure -- Valid tar files for testing kernel extraction + +The test suite uses consistent mock data: + +**R Kernels:** +- `cowsay` (version 1.0) +- `ggplot2` (version 3.4.0) + +**Python Kernels:** +- `numpy` (version 1.24.0) + +**Mock Catalog:** +- Valid JSON structure with proper kernel metadata +- Environment locations pointing to test repository +- Proper language organization (R and Python) + +**Mock Kernel Environments:** +- In-place conda environments (not tar files) +- Proper directory structure with `bin/` directories +- Mock activation/deactivation scripts +- Mock Rscript binaries for R kernels ### Prerequisites -- `jq` - JSON processor for test data validation -- `tar` - For testing kernel packaging functionality -- `timeout` - For testing command timeouts (Linux systems) + +The test suite requires the following tools: + +- **`jq`** - JSON processor for test data validation and manipulation +- **`tar`** - For testing kernel packaging functionality (if needed) +- **`timeout`** - For testing command timeouts (Linux systems) +- **`bash`** - Bash shell (version 4.0 or later) + +Install missing dependencies: + +```bash +# On Ubuntu/Debian +sudo apt-get install jq coreutils + +# On RHEL/CentOS +sudo yum install jq coreutils + +# On macOS +brew install jq coreutils +``` + +## Running Tests + +### Basic Usage + +```bash +# Run all tests +./tests/run_tests.sh all + +# Run a specific test suite +./tests/run_tests.sh kernels + +# Run multiple test suites +./tests/run_tests.sh help config + +# Verbose output +./tests/run_tests.sh --verbose kernels +``` + +### Test Output + +The test runner provides: +- **Colored Output**: Green for passes, red for failures +- **Test Descriptions**: Each test shows its purpose +- **Summary**: Total tests, passed, failed, skipped +- **Log File**: Detailed results saved to `test_results.log` + +### Test Results + +Test results are saved to `./tests/test_results.log` with timestamps and detailed information about each test execution. + +## Cleanup + +After running tests, clean up test artifacts: + +```bash +# Remove test environment directory (keeps test_results.log) +./tests/cleanup_tests.sh + +# Remove everything including test results log +./tests/cleanup_tests.sh --log + +# Verbose cleanup output +./tests/cleanup_tests.sh --verbose + +# Show help +./tests/cleanup_tests.sh --help +``` + +The cleanup script removes: +- `test_env/` directory (test environment) +- `test_results.log` (optional, with `--log` flag) ## Writing Tests ### Test Function Structure + +All tests follow a consistent structure: + ```bash test_new_feature() { # Setup fresh test environment for this test setup_test_env set_test_env + # Initialize if needed (with automatic confirmation) + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Test the new functionality + # Use icrn_manager_with_confirm for commands that auto-initialize local output - output=$("$ICRN_MANAGER" new_command 2>&1) + output=$(icrn_manager_with_confirm kernels new_command 2>&1) # Verify expected behavior if echo "$output" | grep -q "expected output"; then @@ -94,8 +266,27 @@ test_new_feature() { } ``` +### Handling Automatic Initialization + +Since initialization now occurs automatically, tests must handle confirmation prompts: + +```bash +# For explicit init calls +echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + +# For commands that auto-initialize (use helper function) +output=$(icrn_manager_with_confirm kernels available 2>&1) + +# For commands that need multiple confirmations (like clean) +output=$(echo -e "y\ny" | "$ICRN_MANAGER" kernels clean R kernel version 2>&1) + +# For timeout/env contexts, pipe directly +output=$(echo "y" | timeout 30 "$ICRN_MANAGER" kernels get R cowsay 1.0 2>&1) +``` + ### Test Registration -Add your test to the appropriate test file and register it: + +Add your test to the appropriate test file and register it at the bottom: ```bash # Run tests when sourced or executed directly @@ -103,37 +294,120 @@ run_test "new_feature" test_new_feature "Description of what the test does" ``` ### Best Practices -- Always use `setup_test_env` and `set_test_env` for isolation -- Test both success and failure scenarios -- Use descriptive test names and descriptions -- Check for specific error messages, not just exit codes -- Clean up any files created during testing + +1. **Always use isolation**: Call `setup_test_env` and `set_test_env` at the start +2. **Handle auto-init**: Use `icrn_manager_with_confirm` or pipe "y" for commands that auto-initialize +3. **Test both paths**: Test both success and failure scenarios +4. **Descriptive names**: Use clear, descriptive test names and descriptions +5. **Check output**: Verify specific error messages, not just exit codes +6. **Clean up**: The test framework handles cleanup, but be aware of what you create +7. **Mock dependencies**: Use mock commands when testing external tool interactions +8. **Security testing**: Include security tests for path traversal, wildcards, etc. + +### Helper Functions + +The test framework provides several helper functions: + +- **`setup_test_env`**: Creates a fresh test environment +- **`set_test_env`**: Sets environment variables for test isolation +- **`icrn_manager_with_confirm`**: Runs icrn_manager with automatic "y" confirmation +- **`print_status`**: Prints colored status messages +- **`run_test`**: Registers and runs a test +- **`skip_test`**: Skips a test with a reason ## Continuous Integration -- GitHub Actions automatically runs the full test suite on pull requests -- Tests run in the Docker development environment -- All tests must pass before merging -- Test results are logged to `./tests/test_results.log` +The test suite is integrated into the CI/CD pipeline: + +- **GitHub Actions**: Automatically runs on all pull requests +- **Docker Environment**: Tests run in containerized environment +- **Required Passing**: All tests must pass before merging +- **Test Logging**: Results are captured and reported ## Troubleshooting ### Common Issues -- **Permission errors**: Ensure test directories are writable -- **Missing dependencies**: Install `jq`, `tar`, and `timeout` -- **Test failures**: Check the test output for specific error messages -- **Environment issues**: Verify the test environment is properly isolated -### Debugging +**Permission Errors** ```bash -# Run with verbose output -bash -x ./tests/run_tests.sh kernels +# Ensure test directories are writable +chmod -R u+w ./tests/test_env/ +``` + +**Missing Dependencies** +```bash +# Check if jq is installed +which jq +# Install missing tools +sudo apt-get install jq coreutils # Ubuntu/Debian +``` + +**Test Failures** +- Check the test output for specific error messages +- Review `test_results.log` for detailed information +- Verify the test environment is properly isolated +- Ensure mock data is correctly set up + +**Environment Issues** +```bash # Check test environment ls -la ./tests/test_env/ # View test logs cat ./tests/test_results.log + +# Run with verbose output +bash -x ./tests/run_tests.sh kernels ``` -For more detailed information, see the [Contributing Guide](../documentation/source/contributing.rst). \ No newline at end of file +### Debugging Tests + +```bash +# Run a single test suite with verbose output +./tests/run_tests.sh --verbose kernels + +# Run with bash debugging +bash -x ./tests/run_tests.sh kernels + +# Inspect test environment after a test +# (modify test to not cleanup, or pause before cleanup) +ls -la ./tests/test_env/user_home/.icrn/ + +# Check test output directly +./tests/run_tests.sh kernels 2>&1 | tee test_output.log +``` + +## Security Testing + +The test suite includes comprehensive security tests: + +- **Path Traversal Protection**: Tests reject `../` in kernel names and versions +- **Wildcard Attack Prevention**: Tests reject wildcards (`*`, `?`) in parameters +- **Input Validation**: Tests validate all user inputs for dangerous characters +- **File System Safety**: Tests ensure operations stay within expected directories + +See `SECURITY_TESTS.md` for detailed security test documentation. + +## Test Statistics + +Current test coverage: +- **Total Tests**: 57 +- **Test Categories**: 5 +- **Test Files**: 6 +- **Average Tests per Category**: ~11 + +## Additional Resources + +- **Main README**: [../README.md](../README.md) - Project overview and usage +- **Contributing Guide**: [../documentation/source/contributing.rst](../documentation/source/contributing.rst) - Contribution guidelines +- **Security Tests**: [SECURITY_TESTS.md](SECURITY_TESTS.md) - Security testing documentation + +## Questions or Issues? + +If you encounter issues with the test suite: +1. Check the troubleshooting section above +2. Review the test output and logs +3. Verify all prerequisites are installed +4. Check the main project documentation +5. Open an issue on the project repository diff --git a/tests/cleanup_tests.sh b/tests/cleanup_tests.sh new file mode 100755 index 0000000..7fedaaf --- /dev/null +++ b/tests/cleanup_tests.sh @@ -0,0 +1,123 @@ +#!/bin/bash + +# Cleanup script for ICRN Manager test suite +# This script removes test artifacts and temporary files created during test execution + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Get script directory +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Test directories and files to clean +TEST_BASE="$SCRIPT_DIR/test_env" +TEST_LOG="$SCRIPT_DIR/test_results.log" + +# Function to print colored output +print_status() { + local status_type=$1 + local message=$2 + case $status_type in + "INFO") + echo -e "${BLUE}ℹ INFO${NC}: $message" + ;; + "SUCCESS") + echo -e "${GREEN}✓ SUCCESS${NC}: $message" + ;; + "WARNING") + echo -e "${YELLOW}⚠ WARNING${NC}: $message" + ;; + "ERROR") + echo -e "${RED}✗ ERROR${NC}: $message" + ;; + esac +} + +# Parse command line arguments +CLEAN_LOG=false +VERBOSE=false + +while [[ $# -gt 0 ]]; do + case $1 in + -l|--log) + CLEAN_LOG=true + shift + ;; + -v|--verbose) + VERBOSE=true + shift + ;; + -h|--help) + echo "Usage: $0 [OPTIONS]" + echo "" + echo "Options:" + echo " -l, --log Also remove test_results.log file" + echo " -v, --verbose Enable verbose output" + echo " -h, --help Show this help message" + echo "" + echo "This script removes the test environment directory (test_env/)" + echo "and optionally the test results log file." + exit 0 + ;; + *) + echo "Unknown option: $1" + echo "Use -h or --help for usage information" + exit 1 + ;; + esac +done + +echo "==========================================" +echo "ICRN Manager Test Cleanup" +echo "==========================================" +echo "" + +# Clean test environment directory +if [ -d "$TEST_BASE" ]; then + print_status "INFO" "Removing test environment directory: $TEST_BASE" + if [ "$VERBOSE" = true ]; then + rm -rfv "$TEST_BASE" + else + rm -rf "$TEST_BASE" + fi + + if [ $? -eq 0 ]; then + print_status "SUCCESS" "Test environment directory removed" + else + print_status "ERROR" "Failed to remove test environment directory" + exit 1 + fi +else + print_status "INFO" "Test environment directory does not exist: $TEST_BASE" +fi + +# Clean test log file if requested +if [ "$CLEAN_LOG" = true ]; then + if [ -f "$TEST_LOG" ]; then + print_status "INFO" "Removing test results log: $TEST_LOG" + rm -f "$TEST_LOG" + + if [ $? -eq 0 ]; then + print_status "SUCCESS" "Test results log removed" + else + print_status "ERROR" "Failed to remove test results log" + exit 1 + fi + else + print_status "INFO" "Test results log does not exist: $TEST_LOG" + fi +else + if [ -f "$TEST_LOG" ]; then + print_status "INFO" "Test results log preserved: $TEST_LOG" + print_status "INFO" "Use -l or --log to remove it" + fi +fi + +echo "" +print_status "SUCCESS" "Cleanup complete!" +echo "" + diff --git a/tests/test_common.sh b/tests/test_common.sh index a3a9c1b..cfe7c25 100644 --- a/tests/test_common.sh +++ b/tests/test_common.sh @@ -156,8 +156,8 @@ setup_test_env() { mkdir -p "$TEST_USER_HOME" # Create mock repository structure - mkdir -p "$TEST_REPO/r_kernels" - mkdir -p "$TEST_REPO/python_kernels" + mkdir -p "$TEST_REPO/R" + mkdir -p "$TEST_REPO/Python" # Create mock catalog with paths pointing to test repository cat > "$TEST_REPO/icrn_kernel_catalog.json" << EOF @@ -165,13 +165,13 @@ setup_test_env() { "R": { "cowsay": { "1.0": { - "environment_location": "$TEST_REPO/r_kernels/cowsay/1.0", + "environment_location": "$TEST_REPO/R/cowsay/1.0", "description": "Test R kernel for cowsay package" } }, "ggplot2": { "3.4.0": { - "environment_location": "$TEST_REPO/r_kernels/ggplot2/3.4.0", + "environment_location": "$TEST_REPO/R/ggplot2/3.4.0", "description": "Test R kernel for ggplot2 package" } } @@ -179,7 +179,7 @@ setup_test_env() { "Python": { "numpy": { "1.24.0": { - "environment_location": "$TEST_REPO/python_kernels/numpy/1.24.0", + "environment_location": "$TEST_REPO/Python/numpy/1.24.0", "description": "Test Python kernel for numpy package" } } @@ -188,38 +188,38 @@ setup_test_env() { EOF # Create mock kernel environment directories (in-place environments, not tar files) - mkdir -p "$TEST_REPO/r_kernels/cowsay/1.0/bin" - mkdir -p "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin" - mkdir -p "$TEST_REPO/python_kernels/numpy/1.24.0/bin" + mkdir -p "$TEST_REPO/R/cowsay/1.0/bin" + mkdir -p "$TEST_REPO/R/ggplot2/3.4.0/bin" + mkdir -p "$TEST_REPO/Python/numpy/1.24.0/bin" # Create R kernel mock with conda environment structure - echo "#!/bin/bash" > "$TEST_REPO/r_kernels/cowsay/1.0/bin/activate" - echo "echo 'Activating R conda environment'" >> "$TEST_REPO/r_kernels/cowsay/1.0/bin/activate" - chmod +x "$TEST_REPO/r_kernels/cowsay/1.0/bin/activate" - echo "#!/bin/bash" > "$TEST_REPO/r_kernels/cowsay/1.0/bin/deactivate" - echo "echo 'Deactivating R conda environment'" >> "$TEST_REPO/r_kernels/cowsay/1.0/bin/deactivate" - chmod +x "$TEST_REPO/r_kernels/cowsay/1.0/bin/deactivate" - echo "#!/bin/bash" > "$TEST_REPO/r_kernels/cowsay/1.0/bin/Rscript" - echo "echo '/mock/r/lib'" >> "$TEST_REPO/r_kernels/cowsay/1.0/bin/Rscript" - chmod +x "$TEST_REPO/r_kernels/cowsay/1.0/bin/Rscript" + echo "#!/bin/bash" > "$TEST_REPO/R/cowsay/1.0/bin/activate" + echo "echo 'Activating R conda environment'" >> "$TEST_REPO/R/cowsay/1.0/bin/activate" + chmod +x "$TEST_REPO/R/cowsay/1.0/bin/activate" + echo "#!/bin/bash" > "$TEST_REPO/R/cowsay/1.0/bin/deactivate" + echo "echo 'Deactivating R conda environment'" >> "$TEST_REPO/R/cowsay/1.0/bin/deactivate" + chmod +x "$TEST_REPO/R/cowsay/1.0/bin/deactivate" + echo "#!/bin/bash" > "$TEST_REPO/R/cowsay/1.0/bin/Rscript" + echo "echo '/mock/r/lib'" >> "$TEST_REPO/R/cowsay/1.0/bin/Rscript" + chmod +x "$TEST_REPO/R/cowsay/1.0/bin/Rscript" - echo "#!/bin/bash" > "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/activate" - echo "echo 'Activating R conda environment'" >> "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/activate" - chmod +x "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/activate" - echo "#!/bin/bash" > "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/deactivate" - echo "echo 'Deactivating R conda environment'" >> "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/deactivate" - chmod +x "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/deactivate" - echo "#!/bin/bash" > "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/Rscript" - echo "echo '/mock/r/lib'" >> "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/Rscript" - chmod +x "$TEST_REPO/r_kernels/ggplot2/3.4.0/bin/Rscript" + echo "#!/bin/bash" > "$TEST_REPO/R/ggplot2/3.4.0/bin/activate" + echo "echo 'Activating R conda environment'" >> "$TEST_REPO/R/ggplot2/3.4.0/bin/activate" + chmod +x "$TEST_REPO/R/ggplot2/3.4.0/bin/activate" + echo "#!/bin/bash" > "$TEST_REPO/R/ggplot2/3.4.0/bin/deactivate" + echo "echo 'Deactivating R conda environment'" >> "$TEST_REPO/R/ggplot2/3.4.0/bin/deactivate" + chmod +x "$TEST_REPO/R/ggplot2/3.4.0/bin/deactivate" + echo "#!/bin/bash" > "$TEST_REPO/R/ggplot2/3.4.0/bin/Rscript" + echo "echo '/mock/r/lib'" >> "$TEST_REPO/R/ggplot2/3.4.0/bin/Rscript" + chmod +x "$TEST_REPO/R/ggplot2/3.4.0/bin/Rscript" # Create Python kernel mock with conda environment structure - echo "#!/bin/bash" > "$TEST_REPO/python_kernels/numpy/1.24.0/bin/activate" - echo "echo 'Activating conda environment'" >> "$TEST_REPO/python_kernels/numpy/1.24.0/bin/activate" - chmod +x "$TEST_REPO/python_kernels/numpy/1.24.0/bin/activate" - echo "#!/bin/bash" > "$TEST_REPO/python_kernels/numpy/1.24.0/bin/deactivate" - echo "echo 'Deactivating conda environment'" >> "$TEST_REPO/python_kernels/numpy/1.24.0/bin/deactivate" - chmod +x "$TEST_REPO/python_kernels/numpy/1.24.0/bin/deactivate" + echo "#!/bin/bash" > "$TEST_REPO/Python/numpy/1.24.0/bin/activate" + echo "echo 'Activating conda environment'" >> "$TEST_REPO/Python/numpy/1.24.0/bin/activate" + chmod +x "$TEST_REPO/Python/numpy/1.24.0/bin/activate" + echo "#!/bin/bash" > "$TEST_REPO/Python/numpy/1.24.0/bin/deactivate" + echo "echo 'Deactivating conda environment'" >> "$TEST_REPO/Python/numpy/1.24.0/bin/deactivate" + chmod +x "$TEST_REPO/Python/numpy/1.24.0/bin/deactivate" # Create mock conda-unpack command echo '#!/bin/bash' > "$TEST_BASE/conda-unpack" @@ -248,6 +248,13 @@ set_test_env() { export ICRN_USER_CATALOG="$TEST_USER_HOME/.icrn/icrn_kernels/user_catalog.json" } +# Function to run icrn_manager with automatic confirmation for prompts +# This handles the case where initialization occurs automatically and prompts for confirmation +# Usage: icrn_manager_with_confirm [args...] +icrn_manager_with_confirm() { + echo "y" | "$ICRN_MANAGER" "$@" +} + # Function to print test summary print_test_summary() { echo "" diff --git a/tests/test_config.sh b/tests/test_config.sh old mode 100644 new mode 100755 index 1756d96..ed84d1e --- a/tests/test_config.sh +++ b/tests/test_config.sh @@ -12,19 +12,30 @@ test_config_validation_missing_config() { # Remove any existing config rm -rf "$ICRN_USER_BASE" - # Test that commands fail without config + # Test that commands auto-initialize (with confirmation prompt) + # Since initialization now happens automatically, we need to provide confirmation local output - output=$("$ICRN_MANAGER" kernels list 2>&1) - - # Check if it fails with appropriate error - the script will try to list but fail on missing files - if echo "$output" | grep -q "You must run.*kernels init" || \ - echo "$output" | grep -q "Could not open file" || \ - echo "$output" | grep -q "No such file or directory"; then + output=$(icrn_manager_with_confirm kernels list 2>&1) + + # With auto-initialization, the command should succeed after initializing + # With auto-initialization, the command should succeed after initializing + # Check if auto-initialization occurred and command succeeded + # Note: The output may show initialization messages, but the command should complete + if echo "$output" | grep -q "ICRN Manager not initialized" && \ + echo "$output" | grep -q "Auto-initializing"; then + # Verify that initialization actually happened + if [ -f "$ICRN_USER_BASE/manager_config.json" ]; then + return 0 + fi + fi + # If auto-init didn't trigger (maybe already initialized), that's also acceptable + # as long as the command didn't fail with a missing config error + if ! echo "$output" | grep -q "You must run.*kernels init" && \ + ! echo "$output" | grep -q "Could not open file.*manager_config.json"; then return 0 - else - echo "Missing config output: $output" - return 1 fi + echo "Missing config output: $output" + return 1 } test_config_validation_missing_catalog() { @@ -32,15 +43,15 @@ test_config_validation_missing_catalog() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Remove the catalog file rm -f "$ICRN_KERNEL_CATALOG" # Test that commands fail without catalog local output - output=$("$ICRN_MANAGER" kernels available 2>&1) + output=$(icrn_manager_with_confirm kernels available 2>&1) # Check if it fails with appropriate error - the script will try to read the catalog but fail # Note: The script might still succeed if it can read the catalog from cache or another location @@ -61,15 +72,15 @@ test_config_validation_missing_repository() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Remove the repository directory rm -rf "$TEST_REPO" # Test that commands fail without repository local output - output=$("$ICRN_MANAGER" kernels available 2>&1) + output=$(icrn_manager_with_confirm kernels available 2>&1) # Check if it fails with appropriate error - the script will try to read the catalog but fail if echo "$output" | grep -q "Couldn't locate.*kernel repository" || \ @@ -88,12 +99,12 @@ test_config_validation_language_param() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test with invalid language parameter local output - output=$("$ICRN_MANAGER" kernels get InvalidKernel 1.0 2>&1) + output=$(icrn_manager_with_confirm kernels get InvalidKernel 1.0 2>&1) # Check if it fails with appropriate error - the script will fail on missing parameters if echo "$output" | grep -q "Unsupported language" || \ @@ -111,12 +122,12 @@ test_config_validation_kernel_param() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test with non-existent kernel local output - output=$("$ICRN_MANAGER" kernels get R NonExistentKernel 1.0 2>&1) + output=$(icrn_manager_with_confirm kernels get R NonExistentKernel 1.0 2>&1) # Check if it fails with appropriate error if echo "$output" | grep -q "ERROR: could not find target kernel"; then @@ -132,12 +143,12 @@ test_config_validation_version_param() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test with non-existent version local output - output=$("$ICRN_MANAGER" kernels get R cowsay 999.0 2>&1) + output=$(icrn_manager_with_confirm kernels get R cowsay 999.0 2>&1) # Check if it fails with appropriate error if echo "$output" | grep -q "ERROR: could not find target kernel" || \ @@ -154,8 +165,8 @@ test_config_json_structure() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test that config file has valid JSON structure if [ -f "$ICRN_MANAGER_CONFIG" ] && jq -e . "$ICRN_MANAGER_CONFIG" >/dev/null 2>&1; then @@ -173,8 +184,8 @@ test_user_catalog_json_structure() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test that user catalog has valid JSON structure if [ -f "$ICRN_USER_CATALOG" ] && jq -e . "$ICRN_USER_CATALOG" >/dev/null 2>&1; then @@ -192,8 +203,8 @@ test_catalog_json_structure() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test that central catalog has valid JSON structure if [ -f "$ICRN_KERNEL_CATALOG" ] && jq -e . "$ICRN_KERNEL_CATALOG" >/dev/null 2>&1; then @@ -211,8 +222,8 @@ test_catalog_required_fields() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test that catalog has required fields for each kernel local has_required_fields=true diff --git a/tests/test_help.sh b/tests/test_help.sh old mode 100644 new mode 100755 diff --git a/tests/test_kernels.sh b/tests/test_kernels.sh old mode 100644 new mode 100755 index 18346d3..a4ae06e --- a/tests/test_kernels.sh +++ b/tests/test_kernels.sh @@ -12,9 +12,9 @@ test_kernels_init() { # Clean any existing config rm -rf "$ICRN_USER_BASE" - # Run init + # Run init with automatic confirmation local output - output=$("$ICRN_MANAGER" kernels init "$TEST_REPO" 2>&1) + output=$(echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" 2>&1) # Check if init was successful if [ -f "$ICRN_USER_BASE/manager_config.json" ] && \ @@ -32,11 +32,11 @@ test_kernels_available() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 local output - output=$("$ICRN_MANAGER" kernels available 2>&1) + output=$(icrn_manager_with_confirm kernels available 2>&1) # Check if available shows the expected kernels if echo "$output" | grep -q "Language" && \ @@ -56,11 +56,11 @@ test_kernels_list_empty() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 local output - output=$("$ICRN_MANAGER" kernels list 2>&1) + output=$(icrn_manager_with_confirm kernels list 2>&1) # Check if list shows empty catalog if echo "$output" | grep -q "checked out kernels" && \ @@ -77,14 +77,14 @@ test_kernels_get_python_success() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Add mock conda-unpack to PATH export PATH="$TEST_BASE:$PATH" local output - output=$(timeout 30 "$ICRN_MANAGER" kernels get Python numpy 1.24.0 2>&1) + output=$(echo "y" | timeout 30 "$ICRN_MANAGER" kernels get Python numpy 1.24.0 2>&1) # Check if Python kernel registration succeeds (in-place, no unpacking) if echo "$output" | grep -q "Updating user's catalog with Python numpy and 1.24.0" && \ @@ -101,11 +101,11 @@ test_kernels_get_r_success() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 local output - output=$(timeout 30 "$ICRN_MANAGER" kernels get R cowsay 1.0 2>&1) + output=$(echo "y" | timeout 30 "$ICRN_MANAGER" kernels get R cowsay 1.0 2>&1) # Check if R kernel registration succeeds (in-place, no unpacking) if echo "$output" | grep -q "registering R library" && \ @@ -123,11 +123,11 @@ test_kernels_get_invalid_language() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 local output - output=$("$ICRN_MANAGER" kernels get Invalid cowsay 1.0 2>&1) + output=$(icrn_manager_with_confirm kernels get Invalid cowsay 1.0 2>&1) # Check if it fails with unsupported language error if echo "$output" | grep -q "Unsupported language"; then @@ -148,11 +148,11 @@ test_kernels_get_missing_params() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 local output - output=$("$ICRN_MANAGER" kernels get 2>&1) + output=$(icrn_manager_with_confirm kernels get 2>&1) # Check if it fails with usage message if echo "$output" | grep -q "usage: icrn_manager kernels get "; then @@ -168,16 +168,16 @@ test_kernels_clean() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Add a test entry to user catalog local user_catalog="$ICRN_USER_BASE/icrn_kernels/user_catalog.json" jq '.R.test_kernel.test_version = {"absolute_path": "/tmp/test"}' "$user_catalog" > "$user_catalog.tmp" && mv "$user_catalog.tmp" "$user_catalog" - # Test clean with automatic confirmation + # Test clean with automatic confirmation (needs two "y" responses: one for auto-init, one for clean) local output - output=$(echo "y" | "$ICRN_MANAGER" kernels clean R test_kernel test_version 2>&1) + output=$(echo -e "y\ny" | "$ICRN_MANAGER" kernels clean R test_kernel test_version 2>&1) # Check if clean was successful if echo "$output" | grep -q "Desired kernel to scrub from user catalog" && \ @@ -194,11 +194,11 @@ test_kernels_clean_missing_params() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 local output - output=$(echo "y" | timeout 5 "$ICRN_MANAGER" kernels clean 2>&1) + output=$(echo -e "y\ny" | timeout 5 "$ICRN_MANAGER" kernels clean 2>&1) local exit_code=$? # Check if it fails with usage message or if timeout occurred @@ -217,11 +217,11 @@ test_kernels_use_missing_params() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 local output - output=$("$ICRN_MANAGER" kernels use 2>&1) + output=$(icrn_manager_with_confirm kernels use 2>&1) # Check if it fails with usage message (new format includes "none" option) if echo "$output" | grep -q "usage: icrn_manager kernels use \[version number\]" && \ @@ -238,15 +238,15 @@ test_kernels_use_none() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Create a test .Renviron file local test_renviron="$TEST_USER_HOME/.Renviron" echo "R_LIBS=/usr/lib/R/library" > "$test_renviron" local output - output=$("$ICRN_MANAGER" kernels use R none 2>&1) + output=$(icrn_manager_with_confirm kernels use R none 2>&1) # Check if it handles "none" correctly if echo "$output" | grep -q "Desired kernel: none for R" && \ @@ -267,12 +267,12 @@ test_kernels_get_wildcard_attack() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test wildcard attack in get command local output - output=$("$ICRN_MANAGER" kernels get R "malicious*" "1.0" 2>&1) + output=$(icrn_manager_with_confirm kernels get R "malicious*" "1.0" 2>&1) # Check if it rejects wildcards if echo "$output" | grep -q "Invalid characters in kernel name or version" && \ @@ -289,12 +289,12 @@ test_kernels_get_path_traversal_attack() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test path traversal attack in get command local output - output=$("$ICRN_MANAGER" kernels get R "kernel" "../../../etc" 2>&1) + output=$(icrn_manager_with_confirm kernels get R "kernel" "../../../etc" 2>&1) # Check if it rejects path separators if echo "$output" | grep -q "Invalid characters in kernel name or version" && \ @@ -311,8 +311,8 @@ test_kernels_use_python_success() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Create a mock Python kernel environment local python_kernel_dir="$ICRN_USER_KERNEL_BASE/python/test-python-1.0" @@ -358,7 +358,7 @@ test_kernels_use_python_success() { # Test Python kernel use local output - output=$("$ICRN_MANAGER" kernels use Python test_python 1.0 2>&1) + output=$(icrn_manager_with_confirm kernels use Python test_python 1.0 2>&1) # Check if Python kernel use succeeds if echo "$output" | grep -q "Found. Activating Python kernel" && \ @@ -375,8 +375,8 @@ test_kernels_use_python_none() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Add a test Python kernel to user catalog local user_catalog="$ICRN_USER_CATALOG" @@ -395,7 +395,7 @@ test_kernels_use_python_none() { # Test Python kernel removal with mock jupyter in PATH local output - output=$(env PATH="$TEST_BASE:$PATH" "$ICRN_MANAGER" kernels use Python none 2>&1) + output=$(echo "y" | env PATH="$TEST_BASE:$PATH" "$ICRN_MANAGER" kernels use Python none 2>&1) # Check if Python kernel removal succeeds and only removes catalog kernels # Note: The mock jupyter should return test_kernel-1.0 in the kernelspec list @@ -415,8 +415,8 @@ test_kernels_use_with_overlay() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Create a mock R kernel with overlay local user_catalog="$ICRN_USER_CATALOG" @@ -444,7 +444,7 @@ test_kernels_use_with_overlay() { # Test using kernel with overlay local output - output=$(PATH="$TEST_BASE:$PATH" "$ICRN_MANAGER" kernels use R cowsay 1.0 2>&1) + output=$(PATH="$TEST_BASE:$PATH" icrn_manager_with_confirm kernels use R cowsay 1.0 2>&1) # Check if it uses overlay path if echo "$output" | grep -q "Found. Linking and Activating" || \ @@ -461,11 +461,11 @@ test_kernels_get_creates_overlay_directory() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 local output - output=$(timeout 30 "$ICRN_MANAGER" kernels get R cowsay 1.0 2>&1) + output=$(echo "y" | timeout 30 "$ICRN_MANAGER" kernels get R cowsay 1.0 2>&1) # Check if overlay directory was created local overlay_dir="$ICRN_USER_KERNEL_BASE/r/cowsay-1.0" @@ -484,12 +484,12 @@ test_kernels_get_security_path_traversal() { setup_test_env set_test_env - # Initialize the environment first - "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 + # Initialize the environment first with automatic confirmation + echo "y" | "$ICRN_MANAGER" kernels init "$TEST_REPO" >/dev/null 2>&1 # Test path traversal in kernel name local output - output=$("$ICRN_MANAGER" kernels get R "../../etc" "1.0" 2>&1) + output=$(icrn_manager_with_confirm kernels get R "../../etc" "1.0" 2>&1) # Check if it rejects path separators if echo "$output" | grep -q "Invalid characters in kernel name or version"; then diff --git a/tests/test_results.log b/tests/test_results.log deleted file mode 100644 index 0b86b1f..0000000 --- a/tests/test_results.log +++ /dev/null @@ -1,39 +0,0 @@ -ICRN Manager Test Results - 2025-10-13 15:35:23 -========================================== -[2025-10-13 15:35:23] PASS: help_command - Help command shows usage information -[2025-10-13 15:35:23] PASS: invalid_command - Invalid command fails gracefully -[2025-10-13 15:35:23] PASS: kernels_help - Kernels command without subcommand shows help -[2025-10-13 15:35:23] PASS: kernels_init - Kernels init creates necessary directories and config -[2025-10-13 15:35:23] PASS: kernels_available - Kernels available shows catalog contents -[2025-10-13 15:35:23] PASS: kernels_list_empty - Kernels list shows empty user catalog initially -[2025-10-13 15:35:23] PASS: kernels_get_python_success - Kernels get Python succeeds with proper registration -[2025-10-13 15:35:23] PASS: kernels_get_r_success - Kernels get R succeeds with proper registration -[2025-10-13 15:35:23] PASS: kernels_get_invalid_language - Kernels get fails with invalid language -[2025-10-13 15:35:23] PASS: kernels_get_missing_params - Kernels get fails with missing parameters -[2025-10-13 15:35:23] PASS: kernels_clean - Kernels clean removes entries from user catalog -[2025-10-13 15:35:23] PASS: kernels_clean_missing_params - Kernels clean fails with missing parameters -[2025-10-13 15:35:23] PASS: kernels_use_missing_params - Kernels use fails with missing parameters -[2025-10-13 15:35:23] PASS: kernels_use_none - Kernels use handles 'none' parameter for R -[2025-10-13 15:35:23] PASS: kernels_use_python_success - Kernels use Python succeeds with proper kernel installation -[2025-10-13 15:35:23] PASS: kernels_use_python_none - Kernels use handles 'none' parameter for Python -[2025-10-13 15:35:23] PASS: kernels_use_with_overlay - Kernels use correctly uses overlay path -[2025-10-13 15:35:23] PASS: kernels_get_creates_overlay_directory - Kernels get creates overlay directory -[2025-10-13 15:35:23] PASS: kernels_get_security_path_traversal - Kernels get rejects path traversal attempts -[2025-10-13 15:35:23] PASS: kernels_get_wildcard_attack - Kernels get rejects wildcard attacks -[2025-10-13 15:35:23] PASS: kernels_get_path_traversal_attack - Kernels get rejects path traversal attacks -[2025-10-13 15:35:32] PASS: update_r_libs_add - Update R libs adds kernel to .Renviron -[2025-10-13 15:35:32] PASS: update_r_libs_remove - Update R libs removes kernels from .Renviron -[2025-10-13 15:35:32] PASS: update_r_libs_overwrite - Update R libs overwrites existing kernel -[2025-10-13 15:35:32] PASS: update_r_libs_missing_params - Update R libs fails with missing parameters -[2025-10-13 15:35:32] PASS: update_r_libs_invalid_file - Update R libs handles invalid file paths -[2025-10-13 15:35:32] PASS: update_r_libs_preserve_content - Update R libs preserves existing .Renviron content -[2025-10-13 15:35:33] PASS: config_validation_missing_config - Commands fail without config file -[2025-10-13 15:35:33] PASS: config_validation_missing_catalog - Commands fail without central catalog -[2025-10-13 15:35:33] PASS: config_validation_missing_repository - Commands fail without repository -[2025-10-13 15:35:33] PASS: config_validation_language_param - Commands fail with invalid language parameter -[2025-10-13 15:35:33] PASS: config_validation_kernel_param - Commands fail with invalid kernel parameter -[2025-10-13 15:35:33] PASS: config_validation_version_param - Commands fail with invalid version parameter -[2025-10-13 15:35:33] PASS: config_json_structure - Config file has valid JSON structure -[2025-10-13 15:35:33] PASS: user_catalog_json_structure - User catalog has valid JSON structure -[2025-10-13 15:35:33] PASS: catalog_json_structure - Central catalog has valid JSON structure -[2025-10-13 15:35:33] PASS: catalog_required_fields - Catalog has required fields for all kernels diff --git a/tests/test_update_r_libs.sh b/tests/test_update_r_libs.sh old mode 100644 new mode 100755 diff --git a/update_r_libs.sh b/update_r_libs.sh index f37a9d0..ed3ca39 100755 --- a/update_r_libs.sh +++ b/update_r_libs.sh @@ -38,7 +38,7 @@ ICRN_BASE=${ICRN_BASE:-${HOME}/${icrn_base}} ICRN_KERNEL_BASE=${ICRN_KERNEL_BASE:-${ICRN_BASE}/${icrn_kernels}} ICRN_USER_CATALOG=${ICRN_USER_CATALOG:-${ICRN_KERNEL_BASE}/user_catalog.json} ICRN_KERNEL_REPOSITORY="/u/hdpriest/icrn_temp_repository" -ICRN_R_KERNELS=${ICRN_KERNEL_REPOSITORY}"/r_kernels/" +ICRN_R_KERNELS=${ICRN_KERNEL_REPOSITORY}"/R/" ICRN_KERNEL_CATALOG=${ICRN_KERNEL_REPOSITORY}"/icrn_kernel_catalog.json" update_r_libs_path()