Skip to content

Commit

Permalink
feat: consistency tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryTravyan committed Mar 28, 2023
1 parent 51fd4ce commit 1669236
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 442 deletions.
10 changes: 3 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@ node_modules
/tmp/*
!/tmp/.keep
.roles
/test/resources/*
!/test/resources/test-*
/test/resources/test-*-copy*:w
/test/outputs/*
!/test/outputs/test-*
/test/outputs/test-*-copy*:w
/test/outputs/*
/.gitlab-ci.yml
/deploy/.terraform.lock.hcl
/deploy/.terraform/
/deploy/.terraform.tfstate.lock.info
/deploy/terraform.tfstate
/deploy/terraform.tfstate.backup
*.genin.yml
*.genin.yaml
./*.genin.yml
./*.genin.yaml
!/docs/examples/*
inventory.yml
inventory.yaml
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "genin"
version = "0.4.16"
version = "0.4.17"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ Download and unzip the archive for the desired architecture.

Universal executable:
```shell
curl -sLO https://binary.picodata.io/repository/raw/genin/bin/genin-0.4.16-x86_64-musl.tar.gz
tar -xvf genin-0.4.16-x86_64-musl.tar.gz ; sudo install genin /usr/local/bin/
curl -sLO https://binary.picodata.io/repository/raw/genin/bin/genin-0.4.17-x86_64-musl.tar.gz
tar -xvf genin-0.4.17-x86_64-musl.tar.gz ; sudo install genin /usr/local/bin/
```

---
Expand Down Expand Up @@ -84,11 +84,11 @@ sudo yum install -y genin
2. If you want to install `rpm` packages directly without
adding our repository.
```shell
sudo rpm -i https://binary.picodata.io/repository/yum/el/8/x86_64/os/genin-0.4.16-1.el8.x86_64.rpm
sudo rpm -i https://binary.picodata.io/repository/yum/el/8/x86_64/os/genin-0.4.17-1.el8.x86_64.rpm
```
RHEL 7.x, CentOS 7.x
```shell
sudo rpm -i https://binary.picodata.io/repository/yum/el/7/x86_64/os/genin-0.4.16-1.el7.x86_64.rpm
sudo rpm -i https://binary.picodata.io/repository/yum/el/7/x86_64/os/genin-0.4.17-1.el7.x86_64.rpm
```

---
Expand Down Expand Up @@ -116,7 +116,7 @@ sudo apt install -y genin
2. Downloading and installing the package directly:
```shell
curl -sLO https://binary.picodata.io/repository/raw/genin/deb/genin-0.4.16.amd64.deb && sudo dpkg -i genin-0.4.16.amd64.deb
curl -sLO https://binary.picodata.io/repository/raw/genin/deb/genin-0.4.17.amd64.deb && sudo dpkg -i genin-0.4.17.amd64.deb
```
---
Expand All @@ -142,7 +142,7 @@ sudo apt install -y genin
2. Downloading and installing the package directly:
```shell
curl -sLO https://binary.picodata.io/repository/raw/genin/deb/genin-0.4.16.amd64.deb && sudo dpkg -i genin-0.4.16.amd64.deb
curl -sLO https://binary.picodata.io/repository/raw/genin/deb/genin-0.4.17.amd64.deb && sudo dpkg -i genin-0.4.17.amd64.deb
```
---
Expand All @@ -162,8 +162,8 @@ brew install genin
Use the following command to grab and install Genin in macOS (10.10+) wihtout
homebrew:
```shell
curl -L https://binary.picodata.io/repository/raw/genin/apple/genin-0.4.16-darwin-amd64.zip -o genin-0.4.16-darwin-amd64.zip
unzip genin-0.4.16-darwin-amd64.zip -d ~/bin/
curl -L https://binary.picodata.io/repository/raw/genin/apple/genin-0.4.17-darwin-amd64.zip -o genin-0.4.17-darwin-amd64.zip
unzip genin-0.4.17-darwin-amd64.zip -d ~/bin/
```
> **Note:** The application can then be found under the `~/bin` directory.
> Make sure the directory is in your `$PATH`.
Expand All @@ -181,8 +181,8 @@ brew install [email protected]
#### Windows
Use the following command to grab and install Genin in Windows 7 64 bit or newer:
```shell
curl.exe -L https://binary.picodata.io/repository/raw/genin/windows/genin-0.4.16-darwin-amd64.zip -o genin-0.4.16-windows-amd64.zip
unzip.exe genin-0.4.16-windows-amd64.zip -d %HOME%/.cargo/bin/
curl.exe -L https://binary.picodata.io/repository/raw/genin/windows/genin-0.4.17-darwin-amd64.zip -o genin-0.4.17-windows-amd64.zip
unzip.exe genin-0.4.17-windows-amd64.zip -d %HOME%/.cargo/bin/
```
> **Note:** The application can then be found under the `.cargo/bin` folder inside
> your user profile folder. Make sure it is in your `%PATH%`.
Expand Down
22 changes: 11 additions & 11 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Genin уже заранее скомпилирован под разные ар

Универсальный исполняемый файл:
```shell
curl -sLO https://binary.picodata.io/repository/raw/genin/bin/genin-0.4.16-x86_64-musl.tar.gz
tar -xvf genin-0.4.16-x86_64-musl.tar.gz ; sudo install genin /usr/local/bin/
curl -sLO https://binary.picodata.io/repository/raw/genin/bin/genin-0.4.17-x86_64-musl.tar.gz
tar -xvf genin-0.4.17-x86_64-musl.tar.gz ; sudo install genin /usr/local/bin/
```

---
Expand Down Expand Up @@ -87,11 +87,11 @@ sudo yum install -y genin
2. Так же вы можете установить пакет `rpm` напрямую без добавления нашего репозитория.
RHEL 8.x, CentOS 8.x, Rockylinux 8.x, recent Fedora version
```shell
sudo rpm -i https://binary.picodata.io/repository/yum/el/8/x86_64/os/genin-0.4.16-1.el8.x86_64.rpm
sudo rpm -i https://binary.picodata.io/repository/yum/el/8/x86_64/os/genin-0.4.17-1.el8.x86_64.rpm
```
RHEL 7.x, CentOS 7.x
```shell
sudo rpm -i https://binary.picodata.io/repository/yum/el/7/x86_64/os/genin-0.4.16-1.el7.x86_64.rpm
sudo rpm -i https://binary.picodata.io/repository/yum/el/7/x86_64/os/genin-0.4.17-1.el7.x86_64.rpm
```
> **Note:** будьте внимательны, так как при выборе не правильной версии ос могут быть ошибки
> при установке `rpm`
Expand All @@ -118,7 +118,7 @@ sudo apt install -y genin

2. Загрузкой и установкой пакета напрямую:
```shell
curl -sLO https://binary.picodata.io/repository/raw/genin/deb/genin-0.4.16.amd64.deb && sudo dpkg -i genin-0.4.16.amd64.deb
curl -sLO https://binary.picodata.io/repository/raw/genin/deb/genin-0.4.17.amd64.deb && sudo dpkg -i genin-0.4.17.amd64.deb
```

---
Expand All @@ -143,7 +143,7 @@ sudo apt install -y genin

2. Загрузкой и установкой пакета напрямую:
```shell
curl -sLO https://binary.picodata.io/repository/raw/genin/deb/genin-0.4.16.amd64.deb && sudo dpkg -i genin-0.4.16.amd64.deb
curl -sLO https://binary.picodata.io/repository/raw/genin/deb/genin-0.4.17.amd64.deb && sudo dpkg -i genin-0.4.17.amd64.deb
```

---
Expand All @@ -163,8 +163,8 @@ brew install genin
Для установки без помощи homebrew используйте следующие команды для загрузки и установки
Genin на macOS (10.10+):
```shell
curl -sLO https://binary.picodata.io/repository/raw/genin/osx/genin-0.4.16-x86_64-macosx.tar.gz
unzip genin-0.4.16-darwin-amd64.zip -d ~/bin/
curl -sLO https://binary.picodata.io/repository/raw/genin/osx/genin-0.4.17-x86_64-macosx.tar.gz
unzip genin-0.4.17-darwin-amd64.zip -d ~/bin/
```

---
Expand All @@ -188,8 +188,8 @@ brew install [email protected]
Используйте следующие команды для скачивания и установки Genin на операционных системах
Windows 7 64 и новее.
```shell
curl.exe -sLO https://binary.picodata.io/repository/raw/genin/win/genin-0.4.16-win64.zip
unzip.exe genin-0.4.16-win64.zip -d %HOME%/.cargo/bin/
curl.exe -sLO https://binary.picodata.io/repository/raw/genin/win/genin-0.4.17-win64.zip
unzip.exe genin-0.4.17-win64.zip -d %HOME%/.cargo/bin/
```
> **Note:** Genin будет распакован в директорию `.cargo/bin` которая находится в домашнем
> каталоге важего пользователя. Перед использованием приложения пожалуйста удостоверьтесь
Expand All @@ -199,7 +199,7 @@ unzip.exe genin-0.4.16-win64.zip -d %HOME%/.cargo/bin/
```
genin --version
```
Если вы видите сообщение `genin 0.4.16` значит установка прошла успешно.
Если вы видите сообщение `genin 0.4.17` значит установка прошла успешно.

---
## Руководство по использованию
Expand Down
3 changes: 3 additions & 0 deletions src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,6 @@ impl<'a> std::fmt::Debug for ErrConfMapping<'a> {
Ok(())
}
}

#[cfg(test)]
mod test;
139 changes: 139 additions & 0 deletions src/task/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use std::{fs::File, io::Read, path::PathBuf};

use clap::{Arg, ArgAction, Command};

use crate::error::{GeninError, GeninErrorKind};

use super::{
cluster::{
fs::{TryMap, CLUSTER_YAML, IO},
Cluster,
},
inventory::Inventory,
};

#[test]
fn build_consistency_100_times() {
let args = Command::new("genin")
.args(&[
Arg::new("source")
.long("source")
.short('s')
.action(ArgAction::Set),
Arg::new("force")
.long("force")
.short('f')
.action(ArgAction::SetTrue),
])
.try_get_matches_from(vec![
"genin",
"--source",
"test/resources/test-cluster.genin.yaml",
])
.unwrap();

let build_inventory = || -> String {
let bytes = IO::from(&args)
.try_into_files(Some(CLUSTER_YAML), None, args.get_flag("force"))
.unwrap()
.deserialize_input::<Cluster>()
.unwrap()
.try_map(|IO { input, .. }| {
Inventory::try_from(&input).map(|inventory| IO {
input: Some(inventory),
output: Some(Vec::new()),
})
})
.unwrap()
.serialize_input()
.unwrap()
.output
.unwrap();

String::from_utf8(bytes).unwrap()
};

let first = build_inventory();

for _ in 0..100 {
assert_eq!(build_inventory(), first);
}
}

#[test]
fn upgrade_consistency_100_times() {
let args = Command::new("genin")
.args(&[
Arg::new("old").long("old").action(ArgAction::Set),
Arg::new("new").long("new").action(ArgAction::Set),
])
.try_get_matches_from(vec![
"genin",
"--old",
"test/resources/test-cluster.genin.yaml",
"--new",
"test/resources/test-upgrade-cluster.genin.yaml",
])
.unwrap();

let upgrade_inventory = || -> String {
let bytes = IO {
input: args
.try_get_one::<String>("old")
.transpose()
.and_then(|r| r.map_or(None, |s| Some(PathBuf::from(s.as_str())))),
output: None,
}
.try_into_files(Some(CLUSTER_YAML), None, false)
.unwrap()
.deserialize_input::<Cluster>()
.unwrap()
.try_map(|IO { input, output }| {
// 1. read source cluster yaml file what should be upgraded
// 2. read cluster yaml which should contains information about upgrade
File::open(args.get_one::<String>("new").unwrap())
.map_err(|err| GeninError::new(GeninErrorKind::IO, err))
.and_then(|mut file| {
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.map_err(|err| GeninError::new(GeninErrorKind::IO, err))?;
Ok(buffer)
})
.and_then(|buffer| {
serde_yaml::from_slice::<Cluster>(&buffer)
.map_err(|err| GeninError::new(GeninErrorKind::Deserialization, err))
})
.and_then(|new| {
input
.ok_or_else(|| {
GeninError::new(GeninErrorKind::EmptyField, "input file is empty")
})
.and_then(|input_cluster| input_cluster.try_upgrade(&new))
})
.map(|upgraded| IO {
input: Some(upgraded),
output,
})
})
.unwrap()
.try_map(|IO { input, .. }| {
Inventory::try_from(&input).map(|inventory| IO {
input: Some(inventory),
output: Some(Vec::new()),
})
})
.unwrap()
.serialize_input()
.unwrap()
.output
.unwrap();

String::from_utf8(bytes).unwrap()
};

let first = upgrade_inventory();

for _ in 0..100 {
assert_eq!(upgrade_inventory(), first);
}
}
Loading

0 comments on commit 1669236

Please sign in to comment.