|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! Devices intended for testing purposes. |
| 6 | +//! |
| 7 | +//! These devices do things which are generally unwanted in real life, such as |
| 8 | +//! "intentionally breaking Propolis", "intentionally breaking the guest OS", or |
| 9 | +//! some combination of the two. |
| 10 | +use std::sync::{ |
| 11 | + atomic::{AtomicUsize, Ordering}, |
| 12 | + Arc, |
| 13 | +}; |
| 14 | + |
| 15 | +use crate::inventory::Entity; |
| 16 | +use crate::migrate::*; |
| 17 | + |
| 18 | +use serde::{Deserialize, Serialize}; |
| 19 | +use slog::info; |
| 20 | + |
| 21 | +/// A test device for simulating migration failures. |
| 22 | +pub struct MigrationFailureDevice { |
| 23 | + log: slog::Logger, |
| 24 | + exports: AtomicUsize, |
| 25 | + imports: AtomicUsize, |
| 26 | + fail: MigrationFailures, |
| 27 | +} |
| 28 | + |
| 29 | +pub struct MigrationFailures { |
| 30 | + pub exports: usize, |
| 31 | + pub imports: usize, |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Clone, Default, Deserialize, Serialize)] |
| 35 | +struct MigrationFailurePayloadV1 {} |
| 36 | + |
| 37 | +impl MigrationFailureDevice { |
| 38 | + pub const NAME: &'static str = "test-migration-failure"; |
| 39 | + |
| 40 | + pub fn create(log: &slog::Logger, fail: MigrationFailures) -> Arc<Self> { |
| 41 | + let log = |
| 42 | + log.new(slog::o!("component" => "testdev", "dev" => Self::NAME)); |
| 43 | + info!(log, |
| 44 | + "Injecting simulated migration failures"; |
| 45 | + "fail_exports" => %fail.exports, |
| 46 | + "fail_imports" => %fail.imports, |
| 47 | + ); |
| 48 | + Arc::new(Self { |
| 49 | + log, |
| 50 | + exports: AtomicUsize::new(0), |
| 51 | + imports: AtomicUsize::new(0), |
| 52 | + fail, |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl Entity for MigrationFailureDevice { |
| 58 | + fn type_name(&self) -> &'static str { |
| 59 | + MigrationFailureDevice::NAME |
| 60 | + } |
| 61 | + fn migrate(&self) -> Migrator { |
| 62 | + Migrator::Single(self) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl MigrateSingle for MigrationFailureDevice { |
| 67 | + fn export( |
| 68 | + &self, |
| 69 | + _ctx: &MigrateCtx, |
| 70 | + ) -> Result<PayloadOutput, MigrateStateError> { |
| 71 | + let export_num = self.exports.fetch_add(1, Ordering::Relaxed); |
| 72 | + if export_num < self.fail.exports { |
| 73 | + info!( |
| 74 | + self.log, |
| 75 | + "failing export"; |
| 76 | + "export_num" => %export_num, |
| 77 | + "fail_exports" => %self.fail.exports |
| 78 | + ); |
| 79 | + return Err(MigrateStateError::Io(std::io::Error::new( |
| 80 | + std::io::ErrorKind::Other, |
| 81 | + "somebody set up us the bomb", |
| 82 | + ))); |
| 83 | + } |
| 84 | + |
| 85 | + info!( |
| 86 | + self.log, |
| 87 | + "exporting device"; |
| 88 | + "export_num" => %export_num, |
| 89 | + ); |
| 90 | + Ok(MigrationFailurePayloadV1 {}.into()) |
| 91 | + } |
| 92 | + |
| 93 | + fn import( |
| 94 | + &self, |
| 95 | + mut offer: PayloadOffer, |
| 96 | + _ctx: &MigrateCtx, |
| 97 | + ) -> Result<(), MigrateStateError> { |
| 98 | + let import_num = self.imports.fetch_add(1, Ordering::Relaxed); |
| 99 | + let fail = import_num < self.fail.imports; |
| 100 | + info!( |
| 101 | + self.log, |
| 102 | + "importing device"; |
| 103 | + "import_num" => %import_num, |
| 104 | + "will_fail" => %fail, |
| 105 | + ); |
| 106 | + let MigrationFailurePayloadV1 {} = offer.parse()?; |
| 107 | + if fail { |
| 108 | + info!(self.log, "failing import"); |
| 109 | + return Err(MigrateStateError::ImportFailed( |
| 110 | + "you have no chance to survive, make your time".to_string(), |
| 111 | + )); |
| 112 | + } |
| 113 | + Ok(()) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl Schema<'_> for MigrationFailurePayloadV1 { |
| 118 | + fn id() -> SchemaId { |
| 119 | + ("testdev-migration-failure", 1) |
| 120 | + } |
| 121 | +} |
0 commit comments