Skip to content

Commit

Permalink
Add app version to inventory
Browse files Browse the repository at this point in the history
Closes #160
  • Loading branch information
Alexandr Sorokin committed May 29, 2024
1 parent 50f73a7 commit dc88ccb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
24 changes: 23 additions & 1 deletion src/task/inventory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::convert::TryFrom;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::PathBuf;

use clap::ArgMatches;
Expand All @@ -21,11 +22,30 @@ use crate::task::Validate;
use crate::{
error::{GeninError, GeninErrorKind},
task::cluster::ins::Role,
APP_VERSION,
};

use super::cluster::ClusterError;
use super::utils::create_file_or_copy;

pub struct HeaderInfo {
title: String,
version: String,
}
impl Default for HeaderInfo {
fn default() -> Self {
Self {
title: "Inventory generated by Genin".to_owned(),
version: APP_VERSION.to_owned(),
}
}
}
impl ToString for HeaderInfo {
fn to_string(&self) -> String {
format!("# {}\n# version: {}\n", self.title, self.version)
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Inventory {
pub all: InventoryParts,
Expand Down Expand Up @@ -268,7 +288,9 @@ impl Inventory {
.to_owned(),
);

let file = create_file_or_copy(path, args.get_flag("force"))?;
let mut file = create_file_or_copy(path, args.get_flag("force"))?;
let info = HeaderInfo::default().to_string();
file.write_all(info.as_bytes()).unwrap();

serde_yaml::to_writer(file, &self)?;

Expand Down
23 changes: 17 additions & 6 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{
process::{Command, Output},
};

const INVENTORY_HEADER_SIZE: usize = 2;

fn create_file(path: &str) {
File::create(path).unwrap();
}
Expand All @@ -21,6 +23,15 @@ fn cleanup_test_dir(path: &str) {
create_test_dir(path);
}

pub fn read_inventory(path: &str) -> String {
read_to_string(path)
.unwrap()
.lines()
.map(String::from)
.collect::<Vec<String>>()[INVENTORY_HEADER_SIZE..]
.join("\n")
}

#[allow(unused)]
pub fn build_result_from_output(output: Output) -> String {
let mut result = Vec::new();
Expand Down Expand Up @@ -196,7 +207,7 @@ fn sequential_upgrade_from_state() {

let cluster_to_cluster_new = format!(
"{cluster_to_cluster_new}\n{}",
read_to_string("tests/.sequential_upgrade_from_state/v1_inventory.yml").unwrap()
read_inventory("tests/.sequential_upgrade_from_state/v1_inventory.yml")
);

insta::assert_display_snapshot!("cluster_to_cluster_new", cluster_to_cluster_new);
Expand Down Expand Up @@ -225,7 +236,7 @@ fn sequential_upgrade_from_state() {

let cluster_new_to_cluster_v2 = format!(
"{cluster_new_to_cluster_v2}\n{}",
read_to_string("tests/.sequential_upgrade_from_state/v2_inventory.yml").unwrap()
read_inventory("tests/.sequential_upgrade_from_state/v2_inventory.yml")
);

insta::assert_display_snapshot!("cluster_new_to_cluster_v2", cluster_new_to_cluster_v2);
Expand All @@ -252,7 +263,7 @@ fn sequential_upgrade_from_state() {
let cluster_v2_to_cluster_v3 = build_result_from_output(output);
let cluster_v2_to_cluster_v3 = format!(
"{cluster_v2_to_cluster_v3}\n{}",
read_to_string("tests/.sequential_upgrade_from_state/v3_inventory.yml").unwrap()
read_inventory("tests/.sequential_upgrade_from_state/v3_inventory.yml")
);

insta::assert_display_snapshot!("cluster_v2_to_cluster_v3", cluster_v2_to_cluster_v3);
Expand Down Expand Up @@ -282,7 +293,7 @@ fn sequential_upgrade_with_decreasing() {

let cluster_v3_to_cluster_v4 = format!(
"{cluster_v3_to_cluster_v4}\n{}",
read_to_string("tests/.sequential_upgrade_with_decreasing/v1_inventory.yml").unwrap()
read_inventory("tests/.sequential_upgrade_with_decreasing/v1_inventory.yml")
);

insta::assert_display_snapshot!("cluster_v3_to_cluster_v4", cluster_v3_to_cluster_v4);
Expand Down Expand Up @@ -310,7 +321,7 @@ fn sequential_upgrade_with_decreasing() {

let cluster_v4_to_cluster_v5 = format!(
"{cluster_v4_to_cluster_v5}\n{}",
read_to_string("tests/.sequential_upgrade_with_decreasing/v2_inventory.yml").unwrap()
read_inventory("tests/.sequential_upgrade_with_decreasing/v2_inventory.yml")
);

insta::assert_display_snapshot!("cluster_v4_to_cluster_v5", cluster_v4_to_cluster_v5);
Expand Down Expand Up @@ -342,7 +353,7 @@ fn upgrade_consistency_100_times() {

let consistency_100_times = format!(
"{consistency_100_times}\n{}",
read_to_string("tests/.upgrade_consistency_100_times/inventory.yml").unwrap()
read_inventory("tests/.upgrade_consistency_100_times/inventory.yml")
);

insta::assert_display_snapshot!("consistency_100_times", consistency_100_times);
Expand Down

0 comments on commit dc88ccb

Please sign in to comment.