Skip to content

Commit 4f1c9a7

Browse files
committedJul 12, 2024
ci: remove github services for devcontainers
1 parent e3de49f commit 4f1c9a7

14 files changed

+191
-67
lines changed
 

‎.github/workflows/build.yml

-16
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@ jobs:
1818
build:
1919
name: 'Rust Build'
2020
runs-on: ubuntu-latest
21-
services:
22-
postgres:
23-
image: postgres:12.19-alpine3.20
24-
ports:
25-
- 5432:5432
26-
env:
27-
POSTGRES_DB: demo
28-
POSTGRES_USER: demo
29-
POSTGRES_PASSWORD: demo_password
30-
vault:
31-
image: hashicorp/vault:1.17.1
32-
ports:
33-
- 8200:8200
34-
options: --cap-add=IPC_LOCK
35-
env:
36-
VAULT_DEV_ROOT_TOKEN_ID: 'root-token'
3721
steps:
3822
- name: Check out code
3923
uses: actions/checkout@v4

‎Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ clap = { version = "4.5.8", features = ["derive"] }
88
env_logger = "0.11.3"
99
lazy_static = "1.5.0"
1010
log = "0.4.22"
11+
postgres = "0.19.7"
12+
rand = "0.9.0-alpha.1"
1113
serde = { version = "1.0.203", features = ["derive"] }
1214
serde_yaml = "0.9.34+deprecated"
1315
tokio = { version = "1.38.0", features = ["macros", "rt"] }
1416
vaultrs = "0.7.2"
15-
rand = "0.9.0-alpha.1"
16-
postgres = "0.19.7"
1717

1818
[dev-dependencies]
1919
assert_cmd = "2.0.14"
2020
predicates = "3.1.0"
2121
reqwest = { version = "0.12.5", features = ["json"] }
2222
serde_json = "1.0.120"
23+
testcontainers = { version = "0.20.0", features = ["blocking"] }
24+
testcontainers-modules = { version = "0.8.0", features = ["blocking", "postgres"] }

‎DEVELOPMENT.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,15 @@ Cargo makes it easy to run the project's unit and integration tests:
111111
cargo tests
112112
```
113113

114-
**Note that the integration tests need active Vault and PostgreSQL connections, as described [here](#environment-setup).**
114+
**Note that the integration tests make use of [testcontainers](https://testcontainers.com) in order to spin up ArgoCD, Vault and PostgreSQL.**
115115

116-
Cargo will automatically discover and execute the tests defined within the project.
116+
#### A Note for Windows Users
117+
118+
If testcontainers fail to connect to your Docker socket on Windows, add the below environment variable to the test command:
119+
120+
```shell
121+
DOCKER_HOST=tcp://localhost:2375 cargo test
122+
```
117123

118124
### Running the CLI
119125

‎dev/podman.sh

100755100644
File mode changed.

‎src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) fn read_config(config_path: PathBuf) -> Config {
3535
serde_yaml::from_str(&config_data).expect("Failed to parse configuration")
3636
}
3737

38+
#[cfg(test)]
3839
mod tests {
3940
use super::*;
4041

‎src/vault.rs

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ fn get_vault_client(config: &Config) -> VaultClient {
9999
vault_client
100100
}
101101

102+
#[cfg(test)]
102103
mod tests {
103104
use super::*;
104105
use crate::config::PostgresConfig;

‎src/workflow.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::fmt::format;
2-
31
use log::{debug, trace};
4-
use vaultrs::auth::userpass::user::update_password;
52

63
use crate::cli::RotateArgs;
74
use crate::config::Config;
@@ -90,9 +87,9 @@ fn update_passive_user_postgres_password(
9087
debug!("Successfully rotated PostgreSQL password of passive user");
9188
}
9289

90+
#[cfg(test)]
9391
mod tests {
9492
use super::*;
95-
use postgres::Client;
9693

9794
#[test]
9895
fn switch_active_user_user1_active() {

‎tests/common/mod.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::env::temp_dir;
2+
use std::fs::File;
3+
use std::io::Write;
4+
5+
use testcontainers::{
6+
core::{IntoContainerPort, WaitFor},
7+
Container, GenericImage, ImageExt,
8+
};
9+
use testcontainers_modules::postgres::Postgres;
10+
use testcontainers_modules::testcontainers::runners::SyncRunner;
11+
12+
pub(crate) fn postgres_container() -> Container<Postgres> {
13+
Postgres::default()
14+
.with_env_var("POSTGRES_DB", "demo")
15+
.with_env_var("POSTGRES_USER", "demo")
16+
.with_env_var("POSTGRES_PASSWORD", "demo_password")
17+
.start()
18+
.expect("PostgreSQL database started")
19+
}
20+
21+
pub(crate) fn vault_container() -> Container<GenericImage> {
22+
GenericImage::new("hashicorp/vault", "1.17.1")
23+
.with_exposed_port(8200.tcp())
24+
.with_wait_for(WaitFor::message_on_stdout(
25+
"==> Vault server started! Log data will stream in below",
26+
))
27+
.with_env_var("VAULT_DEV_ROOT_TOKEN_ID", "root-token")
28+
.start()
29+
.expect("Vault started")
30+
}
31+
32+
pub(crate) fn write_string_to_tempfile(content: &str) -> String {
33+
let mut dir = temp_dir();
34+
let filename = format!("temp_file_{}", rand::random::<u64>());
35+
36+
dir.push(filename);
37+
38+
let mut file = File::create(dir.clone()).expect("Failed to create tmp file");
39+
40+
file.write_all(content.as_bytes())
41+
.expect("Failed to write into tmp file");
42+
43+
dir.to_string_lossy().to_string()
44+
}

‎tests/init_vault.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,37 @@ use reqwest::{Client, Response};
99
use serde_json::Value;
1010
use tokio::runtime::{Builder, Runtime};
1111

12+
mod common;
13+
1214
lazy_static! {
1315
static ref BIN_PATH: PathBuf = cargo_bin(env!("CARGO_PKG_NAME"));
1416
}
1517

1618
#[test]
1719
fn init_vault_new_path() {
20+
let vault_container = common::vault_container();
21+
22+
let vault_host = vault_container.get_host().unwrap();
23+
let vault_port = vault_container.get_host_port_ipv4(8200).unwrap();
24+
1825
Command::new(&*BIN_PATH)
1926
.arg("init-vault")
2027
.arg("-c")
21-
.arg("tests/resources/init_vault/new_path.yml")
28+
.arg(common::write_string_to_tempfile(
29+
format!(
30+
// language=yaml
31+
"
32+
postgres:
33+
host: 'localhost'
34+
port: 5432
35+
database: 'demo'
36+
vault:
37+
address: 'http://{vault_host}:{vault_port}'
38+
path: 'init/vault/new/path'
39+
"
40+
)
41+
.as_str(),
42+
))
2243
.env("VAULT_TOKEN", "root-token")
2344
.assert()
2445
.success()
@@ -27,10 +48,10 @@ fn init_vault_new_path() {
2748
));
2849

2950
let client = Client::new();
30-
let url = "http://localhost:8200/v1/secret/data/init/vault/new/path";
51+
let url = format!("http://{vault_host}:{vault_port}/v1/secret/data/init/vault/new/path");
3152

3253
let rt: Runtime = create_tokio_runtime();
33-
let json = read_secret_as_json(client, url, rt);
54+
let json = read_secret_as_json(client, url.as_str(), rt);
3455

3556
assert_json_value_equals(&json, "postgresql_active_user", "TBD");
3657
assert_json_value_equals(&json, "postgresql_active_user_password", "TBD");
@@ -42,6 +63,8 @@ fn init_vault_new_path() {
4263

4364
#[test]
4465
fn init_vault_invalid_url() {
66+
common::vault_container();
67+
4568
Command::new(&*BIN_PATH)
4669
.arg("init-vault")
4770
.arg("-c")

‎tests/resources/init_vault/new_path.yml

-7
This file was deleted.

‎tests/resources/rotate/invalid_initialized_secret.yml

-7
This file was deleted.

‎tests/resources/rotate/non_existing_secret.yml

-7
This file was deleted.

‎tests/resources/rotate/secrets.yml

-7
This file was deleted.

0 commit comments

Comments
 (0)