# Testing Guide - Proxmox Provider

This document provides guidance for testing the Crossplane Proxmox provider.

## Unit Tests

### Running Unit Tests

```bash
# Run all unit tests
go test ./pkg/...

# Run tests for specific package
go test ./pkg/utils/...
go test ./pkg/proxmox/...
go test ./pkg/controller/virtualmachine/...

# Run with coverage
go test -cover ./pkg/...

# Generate coverage report
go test -coverprofile=coverage.out ./pkg/...
go tool cover -html=coverage.out
```

### Test Files

- `pkg/utils/parsing_test.go` - Parsing utility tests
- `pkg/utils/validation_test.go` - Validation function tests
- `pkg/proxmox/networks_test.go` - Network API tests
- `pkg/proxmox/client_tenant_test.go` - Tenant tag format tests
- `pkg/controller/virtualmachine/errors_test.go` - Error categorization tests

## Integration Tests

Integration tests require a Proxmox test environment.

### Prerequisites

1. Proxmox VE cluster with API access
2. Valid API credentials
3. Test node with available resources
4. Test storage pools
5. Network bridges configured

### Running Integration Tests

```bash
# Run integration tests
go test -tags=integration ./pkg/controller/virtualmachine/...

# Skip integration tests (run unit tests only)
go test -short ./pkg/...
```

### Integration Test Scenarios

1. **VM Creation with Template Cloning**
   - Requires: Template VM (VMID 100-999999999)
   - Tests: Template clone functionality

2. **VM Creation with Cloud Image Import**
   - Requires: Cloud image in storage, importdisk API support
   - Tests: Image import functionality

3. **VM Creation with Pre-imported Images**
   - Requires: Pre-imported image in storage
   - Tests: Image reference functionality

4. **Multi-Site Deployment**
   - Requires: Multiple Proxmox sites configured
   - Tests: Site selection and validation

5. **Network Bridge Validation**
   - Requires: Network bridges on test nodes
   - Tests: Network existence validation

6. **Error Recovery**
   - Tests: Retry logic and error handling

7. **Cloud-init Configuration**
   - Requires: Cloud-init support
   - Tests: UserData writing and configuration

## Manual Testing

### Prerequisites

- Kubernetes cluster with Crossplane installed
- Proxmox provider deployed
- ProviderConfig configured
- Valid credentials

### Test Scenarios

#### 1. Tenant Tags

```bash
# Create VM with tenant ID
kubectl apply -f - <<EOF
apiVersion: proxmox.sankofa.nexus/v1alpha1
kind: ProxmoxVM
metadata:
  name: test-vm-tenant
  labels:
    tenant-id: "test-tenant-123"
spec:
  forProvider:
    node: "test-node"
    name: "test-vm"
    cpu: 2
    memory: "4Gi"
    disk: "50Gi"
    storage: "local-lvm"
    network: "vmbr0"
    image: "100"
    site: "test-site"
  providerConfigRef:
    name: proxmox-provider-config
EOF

# Verify tenant tag in Proxmox
# Should see tag: tenant_test-tenant-123
```

#### 2. API Adapter Authentication

Test the TypeScript API adapter authentication:

```bash
# Verify authentication header format
# Should use: Authorization: PVEAPIToken ${token}
# NOT: Authorization: PVEAPIToken=${token}
```

#### 3. Proxmox Version Testing

Test on different Proxmox versions:
- PVE 6.x
- PVE 7.x
- PVE 8.x

Verify importdisk API detection works correctly.

#### 4. Node Configuration Testing

- Test with multiple nodes
- Test node health checks
- Test node parameterization in compositions

#### 5. Error Scenarios

Test various error conditions:
- Node unavailable
- Storage full
- Network bridge missing
- Invalid credentials
- Quota exceeded

## Test Data Setup

### Creating Test Templates

1. Create a VM in Proxmox
2. Install OS and configure
3. Convert to template
4. Note the VMID

### Creating Test Images

1. Download cloud image (e.g., Ubuntu cloud image)
2. Upload to Proxmox storage
3. Note the storage and path

### Network Bridges

Ensure test nodes have:
- `vmbr0` (default bridge)
- Additional bridges for testing

## Troubleshooting Tests

### Common Issues

1. **Authentication Failures**
   - Verify credentials in ProviderConfig
   - Check API token format
   - Verify Proxmox API access

2. **Network Connectivity**
   - Verify network bridges exist
   - Check node connectivity
   - Verify firewall rules

3. **Storage Issues**
   - Verify storage pools exist
   - Check available space
   - Verify storage permissions

4. **Test Environment**
   - Verify test namespace exists
   - Check RBAC permissions
   - Verify CRDs are installed

## Continuous Integration

Tests should be run in CI/CD pipeline:

```yaml
# Example CI configuration
test:
  unit:
    - go test -v -short ./pkg/...
  integration:
    - go test -v -tags=integration ./pkg/controller/virtualmachine/...
  coverage:
    - go test -coverprofile=coverage.out ./pkg/...
```

## Best Practices

1. **Isolation**: Use separate test namespaces
2. **Cleanup**: Always clean up test resources
3. **Idempotency**: Tests should be repeatable
4. **Mocking**: Use mocks for external dependencies
5. **Coverage**: Aim for >80% code coverage

