|
| 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 | +use crate::builder::SitrepBuilderRng; |
| 6 | +use chrono::Utc; |
| 7 | +use nexus_reconfigurator_planning::example; |
| 8 | +use nexus_types::fm::ereport::{ |
| 9 | + Ena, Ereport, EreportData, EreportId, Reporter, |
| 10 | +}; |
| 11 | +use omicron_test_utils::dev; |
| 12 | +use omicron_uuid_kinds::EreporterRestartKind; |
| 13 | +use omicron_uuid_kinds::EreporterRestartUuid; |
| 14 | +use omicron_uuid_kinds::OmicronZoneKind; |
| 15 | +use omicron_uuid_kinds::OmicronZoneUuid; |
| 16 | +use rand::rngs::StdRng; |
| 17 | +use typed_rng::TypedUuidRng; |
| 18 | + |
| 19 | +pub struct FmTest { |
| 20 | + pub logctx: dev::LogContext, |
| 21 | + pub reporters: SimReporters, |
| 22 | + pub sitrep_rng: SitrepBuilderRng, |
| 23 | + pub system_builder: example::ExampleSystemBuilder, |
| 24 | +} |
| 25 | + |
| 26 | +impl FmTest { |
| 27 | + pub fn new(test_name: &str) -> Self { |
| 28 | + let logctx = dev::test_setup_log(test_name); |
| 29 | + let example_system_builder = |
| 30 | + example::ExampleSystemBuilder::new(&logctx.log, test_name); |
| 31 | + Self { |
| 32 | + logctx, |
| 33 | + reporters: SimReporters::new(test_name), |
| 34 | + sitrep_rng: SitrepBuilderRng::from_seed(test_name), |
| 35 | + system_builder: example_system_builder, |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +pub struct SimReporters { |
| 41 | + parent: StdRng, |
| 42 | + collector_id_rng: TypedUuidRng<OmicronZoneKind>, |
| 43 | +} |
| 44 | + |
| 45 | +impl SimReporters { |
| 46 | + fn new(test_name: &str) -> Self { |
| 47 | + let mut parent = typed_rng::from_seed(test_name, "sim-reporters"); |
| 48 | + // TODO(eliza): would be more realistic to pick something from the |
| 49 | + // example system's omicron zones, but these UUIDs are only used for |
| 50 | + // debugging purposes... |
| 51 | + let collector_id_rng = |
| 52 | + TypedUuidRng::from_parent_rng(&mut parent, "collector-ids"); |
| 53 | + Self { parent, collector_id_rng } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn reporter(&mut self, reporter: Reporter) -> SimReporter { |
| 57 | + let collector_id = self.collector_id_rng.next(); |
| 58 | + let mut restart_id_rng = TypedUuidRng::from_parent_rng( |
| 59 | + &mut self.parent, |
| 60 | + ("restart_id", reporter), |
| 61 | + ); |
| 62 | + let restart_id = restart_id_rng.next(); |
| 63 | + SimReporter { |
| 64 | + reporter, |
| 65 | + restart_id, |
| 66 | + ena: Ena(0x1), |
| 67 | + restart_id_rng, |
| 68 | + collector_id, |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +pub struct SimReporter { |
| 74 | + reporter: Reporter, |
| 75 | + restart_id: EreporterRestartUuid, |
| 76 | + ena: Ena, |
| 77 | + restart_id_rng: TypedUuidRng<EreporterRestartKind>, |
| 78 | + |
| 79 | + // TODO(eliza): this is not super realistic, as it will give a new "nexus" |
| 80 | + // to each reporter...but the DEs don't actually care who collected the |
| 81 | + // ereport, and we just need something to put in there. |
| 82 | + collector_id: OmicronZoneUuid, |
| 83 | +} |
| 84 | + |
| 85 | +impl SimReporter { |
| 86 | + #[track_caller] |
| 87 | + pub fn parse_ereport( |
| 88 | + &mut self, |
| 89 | + now: chrono::DateTime<Utc>, |
| 90 | + json: &str, |
| 91 | + ) -> Ereport { |
| 92 | + self.mk_ereport( |
| 93 | + now, |
| 94 | + json.parse().expect("must be called with valid ereport JSON"), |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + pub fn mk_ereport( |
| 99 | + &mut self, |
| 100 | + now: chrono::DateTime<Utc>, |
| 101 | + json: serde_json::Value, |
| 102 | + ) -> Ereport { |
| 103 | + self.ena.0 += 1; |
| 104 | + mk_ereport( |
| 105 | + self.reporter, |
| 106 | + EreportId { ena: self.ena, restart_id: self.restart_id }, |
| 107 | + self.collector_id, |
| 108 | + now, |
| 109 | + json, |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + pub fn restart(&mut self) { |
| 114 | + self.ena = Ena(0x1); |
| 115 | + self.restart_id = self.restart_id_rng.next(); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +pub fn mk_ereport( |
| 120 | + reporter: Reporter, |
| 121 | + id: EreportId, |
| 122 | + collector_id: OmicronZoneUuid, |
| 123 | + time_collected: chrono::DateTime<Utc>, |
| 124 | + json: serde_json::Value, |
| 125 | +) -> Ereport { |
| 126 | + Ereport { |
| 127 | + reporter, |
| 128 | + data: EreportData { |
| 129 | + id, |
| 130 | + collector_id, |
| 131 | + time_collected, |
| 132 | + class: json["class"] |
| 133 | + .as_str() |
| 134 | + .or_else(|| json["k"].as_str()) |
| 135 | + .map(ToOwned::to_owned), |
| 136 | + serial_number: json["serial_number"] |
| 137 | + .as_str() |
| 138 | + .map(ToOwned::to_owned), |
| 139 | + part_number: json["part_number"].as_str().map(ToOwned::to_owned), |
| 140 | + report: json, |
| 141 | + }, |
| 142 | + } |
| 143 | +} |
0 commit comments