// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::BTreeMap;
use std::time::Duration;
use command::expand_fields;
use command::GeneralOpt;
use command::OutputFormat;
use common::logutil::get_logger;
use model::Collector;
use model::Queriable;
use render::HasRenderConfigForDump;
use serde_json::Value;
use tempfile::TempDir;
use tmain::Dumper;
use super::*;
#[test]
// Test correctness of system decoration
fn test_dump_sys_content() {
let logger = get_logger();
let mut collector = Collector::new(logger.clone(), Default::default());
collector
.collect_and_update_model()
.expect("Fail to get model");
let mut opts: GeneralOpt = Default::default();
let mut fields = command::expand_fields(command::DEFAULT_SYSTEM_FIELDS, true);
for subquery_id in enum_iterator::all::<model::SingleCpuModelFieldId>() {
fields.push(DumpField::FieldId(model::SystemModelFieldId::Cpus(
model::BTreeMapFieldId::new(Some(31), subquery_id),
)));
}
opts.output_format = Some(OutputFormat::Json);
let system_dumper = system::System::new(&opts, fields.clone());
// update model again to populate cpu and io data
let model = collector
.collect_and_update_model()
.expect("Fail to get model");
let mut system_content: Vec<u8> = Vec::new();
let mut round = 0;
let ctx = CommonFieldContext {
timestamp: 0,
hostname: "h".to_string(),
};
system_dumper
.dump_model(&ctx, &model, &mut system_content, &mut round, false)
.expect("Failed to dump system model");
// verify json correctness
assert!(!system_content.is_empty());
let jval: Value =
serde_json::from_slice(&system_content).expect("Fail parse json of system dump");
for dump_field in fields.iter() {
match dump_field {
DumpField::Common(_) => continue,
DumpField::FieldId(field_id) => {
let rc = model::SystemModel::get_render_config_for_dump(field_id);
assert_eq!(
rc.render(model.system.query(field_id), false),
jval[rc.render_title(false)]
.as_str()
.unwrap_or_else(|| panic!(
"Key not found in Json: {}",
rc.render_title(false)