|
| 1 | +//! Grafana smoke test validator for remote instances |
| 2 | +//! |
| 3 | +//! This module provides the `GrafanaValidator` which performs a smoke test |
| 4 | +//! on a running Grafana instance to verify it's operational and accessible. |
| 5 | +//! |
| 6 | +//! ## Key Features |
| 7 | +//! |
| 8 | +//! - Validates Grafana web UI is accessible via HTTP |
| 9 | +//! - Checks Grafana returns a successful HTTP response |
| 10 | +//! - Optionally validates admin credentials work (login test) |
| 11 | +//! - Performs validation from inside the VM (not externally exposed by firewall) |
| 12 | +//! |
| 13 | +//! ## Validation Approach |
| 14 | +//! |
| 15 | +//! Grafana is exposed on port 3100 via Docker, but validation is performed |
| 16 | +//! from inside the VM via SSH for consistency with other service validators: |
| 17 | +//! |
| 18 | +//! 1. Connect to VM via SSH |
| 19 | +//! 2. Execute `curl` command to fetch Grafana homepage |
| 20 | +//! 3. Verify successful HTTP response (200 OK) |
| 21 | +//! |
| 22 | +//! This smoke test confirms Grafana is: |
| 23 | +//! - Running and bound to the expected port (3000 internally, 3100 externally) |
| 24 | +//! - Responding to HTTP requests |
| 25 | +//! - Web UI is functional |
| 26 | +//! |
| 27 | +//! ## Port Mapping |
| 28 | +//! |
| 29 | +//! - Internal (container): 3000 (Grafana default) |
| 30 | +//! - External (host): 3100 (docker-compose port mapping) |
| 31 | +//! - Validation uses: 3100 (tests the published port from inside VM) |
| 32 | +//! |
| 33 | +//! ## Future Enhancements |
| 34 | +//! |
| 35 | +//! For more comprehensive validation, consider: |
| 36 | +//! |
| 37 | +//! 1. **Authentication Validation**: |
| 38 | +//! - Test admin login with configured credentials |
| 39 | +//! - Verify authentication works correctly |
| 40 | +//! - Example: `curl -u admin:password http://localhost:3100/api/health` |
| 41 | +//! |
| 42 | +//! 2. **Datasource Validation**: |
| 43 | +//! - Query Grafana API for configured datasources |
| 44 | +//! - Verify Prometheus datasource is configured |
| 45 | +//! - Check datasource connectivity to Prometheus |
| 46 | +//! - Example: `curl http://localhost:3100/api/datasources | jq` |
| 47 | +//! |
| 48 | +//! 3. **Dashboard Availability**: |
| 49 | +//! - Query for available dashboards |
| 50 | +//! - Verify default dashboards are loaded |
| 51 | +//! - Check dashboard functionality |
| 52 | +//! |
| 53 | +//! These enhancements require: |
| 54 | +//! - JSON parsing of Grafana API responses |
| 55 | +//! - Credential management for authentication tests |
| 56 | +//! - More complex error handling |
| 57 | +//! |
| 58 | +//! The current smoke test provides a good baseline validation that can be |
| 59 | +//! extended as needed. |
| 60 | +
|
| 61 | +use std::net::IpAddr; |
| 62 | +use tracing::{info, instrument}; |
| 63 | + |
| 64 | +use crate::adapters::ssh::SshClient; |
| 65 | +use crate::adapters::ssh::SshConfig; |
| 66 | +use crate::infrastructure::remote_actions::{RemoteAction, RemoteActionError}; |
| 67 | + |
| 68 | +/// Default Grafana external port (exposed by docker-compose) |
| 69 | +const DEFAULT_GRAFANA_PORT: u16 = 3100; |
| 70 | + |
| 71 | +/// Action that validates Grafana is running and accessible |
| 72 | +pub struct GrafanaValidator { |
| 73 | + ssh_client: SshClient, |
| 74 | + grafana_port: u16, |
| 75 | +} |
| 76 | + |
| 77 | +impl GrafanaValidator { |
| 78 | + /// Create a new `GrafanaValidator` with the specified SSH configuration |
| 79 | + /// |
| 80 | + /// # Arguments |
| 81 | + /// * `ssh_config` - SSH connection configuration containing credentials and host IP |
| 82 | + /// * `grafana_port` - Port where Grafana is accessible (defaults to 3100 if None) |
| 83 | + #[must_use] |
| 84 | + pub fn new(ssh_config: SshConfig, grafana_port: Option<u16>) -> Self { |
| 85 | + let ssh_client = SshClient::new(ssh_config); |
| 86 | + Self { |
| 87 | + ssh_client, |
| 88 | + grafana_port: grafana_port.unwrap_or(DEFAULT_GRAFANA_PORT), |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl RemoteAction for GrafanaValidator { |
| 94 | + fn name(&self) -> &'static str { |
| 95 | + "grafana-smoke-test" |
| 96 | + } |
| 97 | + |
| 98 | + #[instrument( |
| 99 | + name = "grafana_smoke_test", |
| 100 | + skip(self), |
| 101 | + fields( |
| 102 | + action_type = "validation", |
| 103 | + component = "grafana", |
| 104 | + server_ip = %server_ip, |
| 105 | + grafana_port = self.grafana_port |
| 106 | + ) |
| 107 | + )] |
| 108 | + async fn execute(&self, server_ip: &IpAddr) -> Result<(), RemoteActionError> { |
| 109 | + info!( |
| 110 | + action = "grafana_smoke_test", |
| 111 | + grafana_port = self.grafana_port, |
| 112 | + "Running Grafana smoke test" |
| 113 | + ); |
| 114 | + |
| 115 | + // Perform smoke test: curl Grafana homepage and check for success |
| 116 | + // Using -f flag to make curl fail on HTTP errors (4xx, 5xx) |
| 117 | + // Using -s flag for silent mode (no progress bar) |
| 118 | + // Using -o /dev/null to discard response body (we only care about status code) |
| 119 | + let command = format!( |
| 120 | + "curl -f -s -o /dev/null http://localhost:{} && echo 'success'", |
| 121 | + self.grafana_port |
| 122 | + ); |
| 123 | + |
| 124 | + let output = self.ssh_client.execute(&command).map_err(|source| { |
| 125 | + RemoteActionError::SshCommandFailed { |
| 126 | + action_name: self.name().to_string(), |
| 127 | + source, |
| 128 | + } |
| 129 | + })?; |
| 130 | + |
| 131 | + if !output.trim().contains("success") { |
| 132 | + return Err(RemoteActionError::ValidationFailed { |
| 133 | + action_name: self.name().to_string(), |
| 134 | + message: format!( |
| 135 | + "Grafana smoke test failed. Grafana may not be running or accessible on port {}. \ |
| 136 | + Check that 'docker compose ps' shows Grafana container as running.", |
| 137 | + self.grafana_port |
| 138 | + ), |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + info!( |
| 143 | + action = "grafana_smoke_test", |
| 144 | + status = "success", |
| 145 | + "Grafana is running and responding to HTTP requests" |
| 146 | + ); |
| 147 | + |
| 148 | + Ok(()) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +#[cfg(test)] |
| 153 | +mod tests { |
| 154 | + use super::*; |
| 155 | + |
| 156 | + mod grafana_validator { |
| 157 | + use super::*; |
| 158 | + use std::path::PathBuf; |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn it_should_have_correct_name() { |
| 162 | + use crate::adapters::ssh::SshCredentials; |
| 163 | + use crate::shared::Username; |
| 164 | + use std::net::SocketAddr; |
| 165 | + |
| 166 | + let credentials = SshCredentials::new( |
| 167 | + PathBuf::from("test_key"), |
| 168 | + PathBuf::from("test_key.pub"), |
| 169 | + Username::new("test").unwrap(), |
| 170 | + ); |
| 171 | + let ssh_config = SshConfig::new(credentials, SocketAddr::from(([127, 0, 0, 1], 22))); |
| 172 | + let validator = GrafanaValidator::new(ssh_config, None); |
| 173 | + |
| 174 | + assert_eq!(validator.name(), "grafana-smoke-test"); |
| 175 | + } |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn it_should_use_default_port_when_none_provided() { |
| 179 | + use crate::adapters::ssh::SshCredentials; |
| 180 | + use crate::shared::Username; |
| 181 | + use std::net::SocketAddr; |
| 182 | + |
| 183 | + let credentials = SshCredentials::new( |
| 184 | + PathBuf::from("test_key"), |
| 185 | + PathBuf::from("test_key.pub"), |
| 186 | + Username::new("test").unwrap(), |
| 187 | + ); |
| 188 | + let ssh_config = SshConfig::new(credentials, SocketAddr::from(([127, 0, 0, 1], 22))); |
| 189 | + let validator = GrafanaValidator::new(ssh_config, None); |
| 190 | + |
| 191 | + assert_eq!(validator.grafana_port, DEFAULT_GRAFANA_PORT); |
| 192 | + } |
| 193 | + |
| 194 | + #[test] |
| 195 | + fn it_should_use_custom_port_when_provided() { |
| 196 | + use crate::adapters::ssh::SshCredentials; |
| 197 | + use crate::shared::Username; |
| 198 | + use std::net::SocketAddr; |
| 199 | + |
| 200 | + let credentials = SshCredentials::new( |
| 201 | + PathBuf::from("test_key"), |
| 202 | + PathBuf::from("test_key.pub"), |
| 203 | + Username::new("test").unwrap(), |
| 204 | + ); |
| 205 | + let ssh_config = SshConfig::new(credentials, SocketAddr::from(([127, 0, 0, 1], 22))); |
| 206 | + let custom_port = 4000; |
| 207 | + let validator = GrafanaValidator::new(ssh_config, Some(custom_port)); |
| 208 | + |
| 209 | + assert_eq!(validator.grafana_port, custom_port); |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments