-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use yaml format for conversions file and allow multiple random values
- Loading branch information
Showing
10 changed files
with
111 additions
and
110 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "minos" | ||
version = "0.2.2" | ||
version = "0.3.0" | ||
authors = ["Jordi Polo Carres <[email protected]>"] | ||
description = """ | ||
Minos is a command line tool to generate and run scenarios against | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
paths: | ||
"/": | ||
user_uuid: [03b97130-1be2-42f9-bdaf-e1f6a2b9e269] | ||
|
||
teams: | ||
uuid: 03b97130-1be2-42f9-bdaf-e1f6a2b9e269 | ||
roles: | ||
uuid: effb3dfd-26be-4948-8786-4a04ea208461 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,94 @@ | ||
#[derive(Debug, Clone)] | ||
pub struct KnownParam { | ||
pub pattern: String, | ||
pub value: String, | ||
pub context: String, | ||
} | ||
use serde::Deserialize; | ||
use std::collections::BTreeMap; | ||
use rand::seq::SliceRandom; | ||
|
||
impl KnownParam { | ||
fn new(data: &[&str]) -> Self { | ||
KnownParam { | ||
pattern: data[1].to_string(), | ||
value: data[2].to_string(), | ||
context: data[0].to_string(), | ||
} | ||
} | ||
// StringOrArray and Raw to allow either strings or list of strings | ||
#[derive(Deserialize, Debug, PartialEq)] | ||
#[serde(from = "Raw")] | ||
struct StringOrArray(Vec<String>); | ||
|
||
// We want: | ||
// pattern {user_uuid} path: /v1/users/{user_uuid} -> true | ||
// pattern {user_uuid} path: /v1/users/{user_uuid}/onboard -> true | ||
// pattern {user_uuid} path: /v1/users/{user_uuid}/friends/{friend_uuid} -> false | ||
fn path_matches(&self, full_path: &str) -> bool { | ||
(self.context == "path" | ||
|| (self.context != "query" && full_path.contains(&self.context))) | ||
&& full_path.contains(&self.pattern) | ||
&& full_path.matches('}').count() == self.pattern.matches('}').count() | ||
impl From<Raw> for StringOrArray { | ||
fn from(raw: Raw) -> StringOrArray { | ||
match raw { | ||
Raw::String(s) => StringOrArray(vec![s]), | ||
Raw::List(l) => StringOrArray(l), | ||
} | ||
} | ||
} | ||
|
||
fn query_matches(&self, query_param_name: &str) -> bool { | ||
self.context == "query" && query_param_name == self.pattern | ||
} | ||
#[derive(Deserialize)] | ||
#[serde(untagged)] | ||
enum Raw { | ||
String(String), | ||
List(Vec<String>), | ||
} | ||
|
||
pub struct KnownParamCollection { | ||
params: Vec<KnownParam>, | ||
#[derive(Debug, PartialEq, Deserialize)] | ||
pub struct Conversions { | ||
paths: BTreeMap<String, BTreeMap<String, StringOrArray>>, | ||
} | ||
|
||
impl KnownParamCollection { | ||
pub fn new(conversions: &str) -> Self { | ||
KnownParamCollection { | ||
params: KnownParamCollection::read_known_params(conversions), | ||
impl Conversions { | ||
pub fn new(filename: &str) -> Self { | ||
match Self::_read(filename) { | ||
Err(_) => { | ||
println!( | ||
"Conversions file {} not found. Conversions will not be used", | ||
filename | ||
); | ||
Conversions { | ||
paths: BTreeMap::new(), | ||
} | ||
} | ||
Ok(d) => d, | ||
} | ||
} | ||
|
||
// a path may be /users/{uuid}/friends/{uuid2} | ||
pub fn retrieve_known_path(&self, path: &str) -> Option<String> { | ||
let conversion = self.find_by_path(path)?; | ||
Some(str::replace(path, &conversion.pattern, &conversion.value)) | ||
fn _read(conversions: &str) -> Result<Conversions, std::io::Error> { | ||
let filename = shellexpand::tilde(conversions).into_owned(); | ||
let filedata = std::fs::read_to_string(&filename)?; | ||
let deserialized_map: Conversions = serde_yaml::from_str(&filedata).expect(&format!( | ||
"The file {} could not be deserialized as a conversions YAML file.", | ||
conversions | ||
)); | ||
Ok(deserialized_map) | ||
} | ||
|
||
pub fn param_known(&self, name: &str) -> bool { | ||
self.params.iter().any(|param| param.query_matches(name)) | ||
self.paths | ||
.iter() | ||
.any(|(_name, path)| path.get(name).is_some()) | ||
} | ||
|
||
pub fn param_value(&self, name: &str) -> String { | ||
self.params | ||
.iter() | ||
.find(|param| param.query_matches(name)) | ||
.unwrap() | ||
.value | ||
.to_string() | ||
let path = self.get_generic(); | ||
let list = &path.get(name).unwrap().0; // .0 to get the inner object from StringOrArray | ||
list.choose(&mut rand::thread_rng()).unwrap().to_string() | ||
} | ||
|
||
fn find_by_path(&self, path: &str) -> Option<KnownParam> { | ||
self.params | ||
.clone() | ||
.into_iter() | ||
.find(|param| param.path_matches(path)) | ||
} | ||
// a path may be /users/{uuid}/friends/{uuid2} | ||
pub fn retrieve_known_path(&self, pattern: &str) -> Option<String> { | ||
let mut result = String::new(); | ||
|
||
fn read_known_params(conversions: &str) -> Vec<KnownParam> { | ||
match KnownParamCollection::_read_known_params(conversions) { | ||
Err(_) => { | ||
println!( | ||
"Conversions file {} not found. Conversions will not be used", | ||
conversions | ||
); | ||
vec![] | ||
for (path, keys) in &self.paths { | ||
if pattern.contains(path) { | ||
for (key, value) in keys { | ||
let random_value = &value.0.choose(&mut rand::thread_rng()).unwrap(); | ||
result = str::replace(pattern, &format!("{{{}}}", key), random_value) | ||
} | ||
if !result.contains("{") { | ||
return Some(result); | ||
} | ||
} | ||
Ok(d) => d, | ||
} | ||
None | ||
} | ||
fn _read_known_params(conversions: &str) -> Result<Vec<KnownParam>, std::io::Error> { | ||
let filename = shellexpand::tilde(conversions).into_owned(); | ||
let filedata = std::fs::read_to_string(&filename)?; | ||
|
||
Ok(filedata.split('\n').fold(vec![], |mut acc, line| { | ||
let parts: Vec<&str> = line.split(',').collect(); | ||
if parts.len() == 3 { | ||
acc.push(KnownParam::new(&parts)) | ||
} | ||
acc | ||
})) | ||
fn get_generic(&self) -> &BTreeMap<String, StringOrArray> { | ||
self.paths | ||
.iter() | ||
.find(|(name, _param)| **name == "/".to_string()) | ||
.unwrap() | ||
.1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters