Skip to content

Commit

Permalink
feature: NPG-2188 added mock farm to vitup.It should allow to start m…
Browse files Browse the repository at this point in the history
…ultiple mocks at one machine + give user some control over starting and stopping particular mock
  • Loading branch information
dkijania committed Jul 13, 2022
1 parent 4f0c891 commit 2350bc4
Show file tree
Hide file tree
Showing 21 changed files with 896 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

110 changes: 110 additions & 0 deletions doc/api/vitup/mock-farm/postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"info": {
"_postman_id": "a54a9a24-ef93-48b5-a447-09a5d61d5075",
"name": "Mock Farm",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "11181958"
},
"item": [
{
"name": "List all active environments",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://127.0.0.1:7070/api/v0/active",
"protocol": "https",
"host": [
"127",
"0",
"0",
"1"
],
"port": "7070",
"path": [
"api",
"v0",
"active"
]
}
},
"response": []
},
{
"name": "Start new environment with name 'marek' and port = 10010",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "https://127.0.0.1:7070/api/v0/start/darek",
"protocol": "https",
"host": [
"127",
"0",
"0",
"1"
],
"port": "7070",
"path": [
"api",
"v0",
"start",
"darek"
]
}
},
"response": []
},
{
"name": "Shutdown existing environment with name 'marek'",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "https://127.0.0.1:7070/api/v0/start/marek/10010",
"protocol": "https",
"host": [
"127",
"0",
"0",
"1"
],
"port": "7070",
"path": [
"api",
"v0",
"start",
"marek",
"10010"
]
}
},
"response": []
},
{
"name": "Shutdown existing environment with name 'marek'",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "https://127.0.0.1:7070/api/v0/shutdown/marek",
"protocol": "https",
"host": [
"127",
"0",
"0",
"1"
],
"port": "7070",
"path": [
"api",
"v0",
"shutdown",
"marek"
]
}
},
"response": []
}
]
}
113 changes: 113 additions & 0 deletions doc/api/vitup/mock-farm/v0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
openapi: 3.0.2

info:
title: Mock farm REST API
description: Mock Farm Rest API v0
version: 0.0.1
contact:
url: 'https://github.com/input-output-hk/vit-testing/'

servers:
- url: 'https://localhost'

tags:
- name: active
- name: start
- name: shutdown

paths:
'/api/v0/active':
get:
description: Lists active mock environments
operationId: Active
tags:
- active
responses:
'200':
description: Success
content:
application/json:
schema:
description: assigned port number
type: string
format: text
'400':
description: Mock env with given ID or port already exists

'/api/v0/start/{env_name}':
post:
description: Starts new mock env with random free port
operationId: StartEnvRandomPort
tags:
- start
parameters:
- name: env_name
in: path
required: true
schema:
description: Environment name
type: string
pattern: '[0-9a-f]+'
responses:
'200':
description: Success
content:
application/json:
schema:
description: assigned port number
type: string
format: text
'400':
description: Mock env with given ID or port already exists

'/api/v0/start/{env_name}/{port}':
post:
description: Starts new mock env with random free port
operationId: StartEnv
tags:
- start
parameters:
- name: env_name
in: path
required: true
schema:
description: Environment name
type: string
pattern: '[0-9a-f]+'
responses:
'200':
description: Success
content:
application/json:
schema:
description: assigned port number
type: string
format: text
'400':
description: Mock env with given ID or port already exists

'/api/v0/shutdown/{env_name}':
post:
description: Shutdown new mock env with random free port
operationId: ShutdownEnv
tags:
- shutdown
parameters:
- name: env_name
in: path
required: true
schema:
description: Environment name
type: string
pattern: '[0-9a-f]+'
responses:
'200':
description: Success
content:
application/json:
schema:
description: assigned port number
type: string
format: text
'404':
description: Mock env with given ID was not found
27 changes: 27 additions & 0 deletions doc/vitup/mock_farm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

## Mock Farm

Mock farm is a simple extension for mock service. It allows to run more that one mock at once and give more control to user in term of starting and stopping particular mock instance.

### Configuration

This section describe configuration file which can be passed as argument for snapshot service:

- `port`: port on which registration-service will be exposed,
- `working_directory`: path to folder which config files will be dumped,
- `mocks-port-range`: range of ports assigned for usage,
- `protocol`: decide whether mock farm should be exposed as http or https,
- `local`: should service be exposed on all network interfaces or only 127.0.0.1,
- `token`: token limiting access to environment. Must be provided in header `API-Token` for each request

Note: it is recommended to run command from `vit-testing/vitup` folder (then no explicit paths are required to be provided).
Configuration file example is available under `vit-testing/vitup/example/mock-farm/config.yaml`

### Start

`vitup start mock-farm --config example\mock\mock-farm\config.yaml`

### Documentation

- [OpenApi](../api/vitup/mock-farm/v0.yaml)
- [Requests collection](../api/vitup/mock-farm/postman_collection.json)
Empty file added output.text
Empty file.
1 change: 1 addition & 0 deletions vitup/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/prod/
/validate/
/vole.trace
/mock_farm/
1 change: 1 addition & 0 deletions vitup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ voting-hir = { git = "https://github.com/input-output-hk/catalyst-toolbox.git",
valgrind = { path = "../valgrind" }
poldercast = { git = "https://github.com/primetype/poldercast.git", rev = "8305f1560392a9d26673ca996e7646c8834533ef" }
rand = "0.8"
netstat2 = "0.9"
time = { version = "0.3.7", features=["serde","serde-well-known","parsing"]}
fake = { version = "2.2", features=['chrono','http']}
strum = "0.21.0"
Expand Down
13 changes: 13 additions & 0 deletions vitup/example/mock-farm/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"port": 7070,
"working_directory": "./mock_farm",
"mocks-port-range": {
"start": 8080,
"end": 9090
},
"protocol": {
"key_path": "./resources/tls/server.key",
"cert_path": "./resources/tls/server.crt"
},
"local": true
}
9 changes: 7 additions & 2 deletions vitup/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod validate;
use self::time::TimeCommand;
use crate::cli::generate::{CommitteeIdCommandArgs, QrCommandArgs, SnapshotCommandArgs};
use crate::cli::start::AdvancedStartCommandArgs;
use crate::cli::start::MockStartCommandArgs;
use crate::cli::start::{MockFarmCommand, MockStartCommandArgs};
use crate::Result;
use diff::DiffCommand;
use generate::DataCommandArgs;
Expand Down Expand Up @@ -53,8 +53,10 @@ pub enum StartCommand {
Quick(QuickStartCommandArgs),
/// start advanced backend from scratch
Advanced(AdvancedStartCommandArgs),
// start mock env
/// start mock env
Mock(MockStartCommandArgs),
/// start multiple mock environments
MockFarm(MockFarmCommand),
}

impl StartCommand {
Expand All @@ -63,6 +65,9 @@ impl StartCommand {
Self::Quick(quick_start_command) => quick_start_command.exec(),
Self::Advanced(advanced_start_command) => advanced_start_command.exec(),
Self::Mock(mock_start_command) => mock_start_command.exec().map_err(Into::into),
Self::MockFarm(mock_farm_start_command) => {
mock_farm_start_command.exec().map_err(Into::into)
}
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions vitup/src/cli/start/mock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::mode::mock::{read_config, start_rest_server, Configuration, Context};
use crate::mode::mock::{farm, read_config, start_rest_server, Configuration, Context};
use std::sync::Mutex;
use std::{path::PathBuf, sync::Arc};
use structopt::StructOpt;
Expand Down Expand Up @@ -38,6 +38,31 @@ impl MockStartCommandArgs {
}
}

#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct MockFarmCommand {
/// path to config file
#[structopt(long = "config", short = "c")]
pub config: PathBuf,
}

impl MockFarmCommand {
#[tokio::main]
pub async fn exec(self) -> Result<(), Error> {
let control_context = Arc::new(Mutex::new(farm::Context::new(
farm::read_config(&self.config).unwrap(),
)));
tokio::spawn(async move {
farm::start_rest_server(control_context.clone())
.await
.unwrap()
})
.await
.map(|_| ())
.map_err(Into::into)
}
}

#[derive(Debug, Error)]
#[allow(clippy::large_enum_variant)]
pub enum Error {
Expand All @@ -52,5 +77,7 @@ pub enum Error {
#[error(transparent)]
Mock(#[from] crate::mode::mock::ContextError),
#[error(transparent)]
ServerError(crate::mode::mock::RestError),
Farm(#[from] crate::mode::mock::farm::ContextError),
#[error(transparent)]
ServerError(#[from] crate::mode::mock::RestError),
}
2 changes: 1 addition & 1 deletion vitup/src/cli/start/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ mod mock;
mod quick;

pub use advanced::AdvancedStartCommandArgs;
pub use mock::{Error as MockError, MockStartCommandArgs};
pub use mock::{Error as MockError, MockFarmCommand, MockStartCommandArgs};
pub use quick::QuickStartCommandArgs;
6 changes: 3 additions & 3 deletions vitup/src/config/certs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ impl Default for CertificatesBuilder {

impl CertificatesBuilder {
pub fn build<P: AsRef<Path>>(self, working_dir: P) -> Result<Certs, Error> {
let working_dir = working_dir.as_ref().to_path_buf();

std::fs::create_dir_all(working_dir.join("tls"))?;
let mut working_dir = working_dir.as_ref().to_path_buf();
working_dir = working_dir.join("tls");
std::fs::create_dir_all(&working_dir)?;

let key_path = working_dir.join(SERVER_KEY);
let mut key_file =
Expand Down
Loading

0 comments on commit 2350bc4

Please sign in to comment.