|
| 1 | +// Package utils provides hypervisor configuration and validation utilities for two-node cluster testing. |
| 2 | +// |
| 3 | +// Tests requiring hypervisor access should include the [Requires:HypervisorSSHConfig] annotation. |
| 4 | +// |
| 5 | +// Configuration can be provided via command-line flag or environment variable: |
| 6 | +// |
| 7 | +// openshift-tests run openshift/two-node --with-hypervisor-json='{ |
| 8 | +// "IP": "192.168.111.1", |
| 9 | +// "User": "root", |
| 10 | +// "privateKeyPath": "/path/to/private/key" |
| 11 | +// }' |
| 12 | +// |
| 13 | +// Or: |
| 14 | +// |
| 15 | +// export HYPERVISOR_CONFIG='{"IP":"192.168.111.1","User":"root","privateKeyPath":"/path/to/key"}' |
| 16 | +// openshift-tests run openshift/two-node |
| 17 | +// |
| 18 | +// Usage example: |
| 19 | +// |
| 20 | +// if !exutil.HasHypervisorConfig() { |
| 21 | +// utils.PrintHypervisorConfigUsage() |
| 22 | +// return |
| 23 | +// } |
| 24 | +// config := exutil.GetHypervisorConfig() |
| 25 | +// utils.VerifyHypervisorConnectivity(&config, knownHostsPath) |
| 26 | +package utils |
| 27 | + |
| 28 | +import ( |
| 29 | + "fmt" |
| 30 | + "strings" |
| 31 | + |
| 32 | + g "github.com/onsi/ginkgo/v2" |
| 33 | + "k8s.io/klog/v2" |
| 34 | +) |
| 35 | + |
| 36 | +// PrintHypervisorConfigUsage prints usage instructions for configuring hypervisor SSH access. |
| 37 | +// Call this when HasHypervisorConfig() returns false to provide configuration guidance. |
| 38 | +func PrintHypervisorConfigUsage() { |
| 39 | + usageMessage := ` |
| 40 | +================================================================================ |
| 41 | +Two-Node Test Suite - Hypervisor Configuration Required |
| 42 | +================================================================================ |
| 43 | +
|
| 44 | +This test requires hypervisor SSH configuration to manage virtual machines |
| 45 | +and perform node operations. The [Requires:HypervisorSSHConfig] annotation |
| 46 | +indicates this requirement. |
| 47 | +
|
| 48 | +CONFIGURATION METHODS: |
| 49 | +
|
| 50 | +1. Command-Line Flag (recommended for interactive testing): |
| 51 | +
|
| 52 | + openshift-tests run openshift/two-node --with-hypervisor-json='{ |
| 53 | + "IP": "192.168.111.1", |
| 54 | + "User": "root", |
| 55 | + "privateKeyPath": "/path/to/private/key" |
| 56 | + }' |
| 57 | +
|
| 58 | +2. Environment Variable (recommended for CI/CD): |
| 59 | +
|
| 60 | + export HYPERVISOR_CONFIG='{"IP":"192.168.111.1","User":"root","privateKeyPath":"/path/to/key"}' |
| 61 | + openshift-tests run openshift/two-node |
| 62 | +
|
| 63 | +CONFIGURATION FIELDS: |
| 64 | +
|
| 65 | +- IP: IP address or hostname of the hypervisor |
| 66 | +- User: SSH username (typically "root") |
| 67 | +- privateKeyPath: Absolute path to SSH private key file |
| 68 | +
|
| 69 | +TROUBLESHOOTING: |
| 70 | +
|
| 71 | +If configuration fails: |
| 72 | +1. Verify JSON syntax is valid |
| 73 | +2. Check that the private key file exists |
| 74 | +3. Test SSH connectivity: ssh -i <privateKeyPath> <User>@<IP> |
| 75 | +4. Verify virsh is available: ssh <User>@<IP> 'virsh version' |
| 76 | +
|
| 77 | +================================================================================ |
| 78 | +` |
| 79 | + g.GinkgoT().Logf(usageMessage) |
| 80 | +} |
| 81 | + |
| 82 | +// VerifyHypervisorAvailability verifies SSH connectivity to the hypervisor and checks |
| 83 | +// that virsh and libvirt are available. |
| 84 | +func VerifyHypervisorAvailability(sshConfig *SSHConfig, knownHostsPath string) error { |
| 85 | + klog.V(2).Infof("Verifying hypervisor connectivity to %s@%s", sshConfig.User, sshConfig.IP) |
| 86 | + |
| 87 | + // Test basic SSH connectivity |
| 88 | + output, _, err := VerifyConnectivity(sshConfig, knownHostsPath) |
| 89 | + if err != nil { |
| 90 | + klog.ErrorS(err, "Failed to establish SSH connection to hypervisor", |
| 91 | + "user", sshConfig.User, |
| 92 | + "host", sshConfig.IP, |
| 93 | + "output", output) |
| 94 | + klog.ErrorS(nil, "Ensure the hypervisor is accessible and SSH key is correct") |
| 95 | + return fmt.Errorf("failed to establish SSH connection to hypervisor %s@%s: %w", sshConfig.User, sshConfig.IP, err) |
| 96 | + } |
| 97 | + klog.V(2).Infof("SSH connectivity verified: %s", strings.TrimSpace(output)) |
| 98 | + |
| 99 | + // Test virsh availability and basic functionality |
| 100 | + output, err = VerifyVirsh(sshConfig, knownHostsPath) |
| 101 | + if err != nil { |
| 102 | + klog.ErrorS(err, "virsh is not available or not working on hypervisor", |
| 103 | + "user", sshConfig.User, |
| 104 | + "host", sshConfig.IP, |
| 105 | + "output", output) |
| 106 | + klog.ErrorS(nil, "Ensure libvirt and virsh are installed on the hypervisor") |
| 107 | + return fmt.Errorf("virsh is not available or not working on hypervisor %s@%s: %w", sshConfig.User, sshConfig.IP, err) |
| 108 | + } |
| 109 | + klog.V(2).Infof("virsh availability verified: %s", strings.TrimSpace(output)) |
| 110 | + |
| 111 | + // Test libvirt connection by listing VMs |
| 112 | + output, err = VirshListAllVMs(sshConfig, knownHostsPath) |
| 113 | + if err != nil { |
| 114 | + klog.ErrorS(err, "Failed to connect to libvirt on hypervisor", |
| 115 | + "user", sshConfig.User, |
| 116 | + "host", sshConfig.IP, |
| 117 | + "output", output) |
| 118 | + klog.ErrorS(nil, "Ensure libvirtd service is running and user has access") |
| 119 | + return fmt.Errorf("failed to connect to libvirt on hypervisor %s@%s: %w", sshConfig.User, sshConfig.IP, err) |
| 120 | + } |
| 121 | + klog.V(2).Infof("libvirt connection verified, found VMs: %s", strings.TrimSpace(output)) |
| 122 | + |
| 123 | + klog.V(2).Infof("Hypervisor connectivity verification completed successfully") |
| 124 | + return nil |
| 125 | +} |
0 commit comments