use super::*;
#[test]
fn test_fj216_compute_resource_waves_no_deps() {
let yaml = r#"
version: "1.0"
name: test
machines:
m1:
hostname: m1
addr: 1.2.3.4
resources:
a:
type: file
machine: m1
path: /a
b:
type: file
machine: m1
path: /b
"#;
let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let waves = compute_resource_waves(&config, &["a", "b"]);
assert_eq!(waves.len(), 1);
assert_eq!(waves[0].len(), 2);
}
#[test]
fn test_fj216_compute_resource_waves_with_deps() {
let yaml = r#"
version: "1.0"
name: test
machines:
m1:
hostname: m1
addr: 1.2.3.4
resources:
base:
type: file
machine: m1
path: /base
app:
type: file
machine: m1
path: /app
depends_on: [base]
"#;
let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let waves = compute_resource_waves(&config, &["base", "app"]);
assert_eq!(waves.len(), 2);
assert_eq!(waves[0], vec!["base"]);
assert_eq!(waves[1], vec!["app"]);
}
#[test]
fn test_fj216_compute_resource_waves_subset() {
let yaml = r#"
version: "1.0"
name: test
machines:
m1:
hostname: m1
addr: 1.2.3.4
resources:
a:
type: file
machine: m1
path: /a
b:
type: file
machine: m1
path: /b
depends_on: [a]
c:
type: file
machine: m1
path: /c
"#;
let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let waves = compute_resource_waves(&config, &["b", "c"]);
assert_eq!(waves.len(), 1);
assert_eq!(waves[0].len(), 2);
}
#[test]
fn test_fj216_parallel_resources_policy_default_false() {
let policy = Policy::default();
assert!(!policy.parallel_resources);
}
#[test]
fn test_fj216_parallel_resources_policy_yaml() {
let yaml = r#"
version: "1.0"
name: test
machines:
m1:
hostname: m1
addr: 1.2.3.4
resources: {}
policy:
parallel_resources: true
"#;
let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
assert!(config.policy.parallel_resources);
}
#[test]
fn test_fj224_trigger_forces_reapply() {
let dir = tempfile::tempdir().unwrap();
let state_dir = dir.path().join("state");
std::fs::create_dir_all(&state_dir).unwrap();
let yaml = r#"
version: "1.0"
name: test
machines:
local:
hostname: localhost
addr: 127.0.0.1
resources:
config:
type: file
machine: local
path: /tmp/fj224-config.txt
content: "v1"
app:
type: file
machine: local
path: /tmp/fj224-app.txt
content: "app-content"
depends_on: [config]
triggers: [config]
"#;
let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let cfg1 = ApplyConfig {
config: &config,
state_dir: &state_dir,
force: false,
dry_run: false,
machine_filter: None,
resource_filter: None,
tag_filter: None,
group_filter: None,
timeout_secs: None,
force_unlock: false,
progress: false,
retry: 0,
parallel: None,
resource_timeout: None,
rollback_on_failure: false,
max_parallel: None,
trace: false,
run_id: None,
refresh: false,
force_tag: None,
};
let r1 = apply(&cfg1).unwrap();
assert_eq!(r1[0].resources_converged, 2);
let r2 = apply(&cfg1).unwrap();
assert_eq!(r2[0].resources_converged, 0, "no changes = no converge");
assert_eq!(r2[0].resources_unchanged, 2);
let yaml3 = r#"
version: "1.0"
name: test
machines:
local:
hostname: localhost
addr: 127.0.0.1
resources:
config:
type: file
machine: local
path: /tmp/fj224-config.txt
content: "v2"
app:
type: file
machine: local
path: /tmp/fj224-app.txt
content: "app-content"
depends_on: [config]
triggers: [config]
"#;
let config3: ForjarConfig = serde_yaml_ng::from_str(yaml3).unwrap();
let cfg3 = ApplyConfig {
config: &config3,
state_dir: &state_dir,
force: false,
dry_run: false,
machine_filter: None,
resource_filter: None,
tag_filter: None,
group_filter: None,
timeout_secs: None,
force_unlock: false,
progress: false,
retry: 0,
parallel: None,
resource_timeout: None,
rollback_on_failure: false,
max_parallel: None,
trace: false,
run_id: None,
refresh: false,
force_tag: None,
};
let r3 = apply(&cfg3).unwrap();
assert_eq!(
r3[0].resources_converged, 2,
"config changed + app triggered"
);
}
#[test]
fn test_fj224_trigger_no_fire_when_dep_unchanged() {
let dir = tempfile::tempdir().unwrap();
let state_dir = dir.path().join("state");
std::fs::create_dir_all(&state_dir).unwrap();
let yaml = r#"
version: "1.0"
name: test
machines:
local:
hostname: localhost
addr: 127.0.0.1
resources:
config:
type: file
machine: local
path: /tmp/fj224b-config.txt
content: "stable"
app:
type: file
machine: local
path: /tmp/fj224b-app.txt
content: "app"
depends_on: [config]
triggers: [config]
"#;
let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let cfg = ApplyConfig {
config: &config,
state_dir: &state_dir,
force: false,
dry_run: false,
machine_filter: None,
resource_filter: None,
tag_filter: None,
group_filter: None,
timeout_secs: None,
force_unlock: false,
progress: false,
retry: 0,
parallel: None,
resource_timeout: None,
rollback_on_failure: false,
max_parallel: None,
trace: false,
run_id: None,
refresh: false,
force_tag: None,
};
let r1 = apply(&cfg).unwrap();
assert_eq!(r1[0].resources_converged, 2);
let r2 = apply(&cfg).unwrap();
assert_eq!(r2[0].resources_converged, 0);
assert_eq!(r2[0].resources_unchanged, 2);
}
#[test]
fn test_fj224_trigger_multiple_sources() {
let dir = tempfile::tempdir().unwrap();
let state_dir = dir.path().join("state");
std::fs::create_dir_all(&state_dir).unwrap();
let yaml1 = r#"
version: "1.0"
name: test
machines:
local:
hostname: localhost
addr: 127.0.0.1
resources:
db-config:
type: file
machine: local
path: /tmp/fj224c-db.txt
content: "db-v1"
app-config:
type: file
machine: local
path: /tmp/fj224c-app.txt
content: "app-v1"
service:
type: file
machine: local
path: /tmp/fj224c-svc.txt
content: "svc"
depends_on: [db-config, app-config]
triggers: [db-config, app-config]
"#;
let config1: ForjarConfig = serde_yaml_ng::from_str(yaml1).unwrap();
let cfg1 = ApplyConfig {
config: &config1,
state_dir: &state_dir,
force: false,
dry_run: false,
machine_filter: None,
resource_filter: None,
tag_filter: None,
group_filter: None,
timeout_secs: None,
force_unlock: false,
progress: false,
retry: 0,
parallel: None,
resource_timeout: None,
rollback_on_failure: false,
max_parallel: None,
trace: false,
run_id: None,
refresh: false,
force_tag: None,
};
let r1 = apply(&cfg1).unwrap();
assert_eq!(r1[0].resources_converged, 3);
let yaml2 = yaml1.replace("db-v1", "db-v2");
let config2: ForjarConfig = serde_yaml_ng::from_str(&yaml2).unwrap();
let cfg2 = ApplyConfig {
config: &config2,
state_dir: &state_dir,
force: false,
dry_run: false,
machine_filter: None,
resource_filter: None,
tag_filter: None,
group_filter: None,
timeout_secs: None,
force_unlock: false,
progress: false,
retry: 0,
parallel: None,
resource_timeout: None,
rollback_on_failure: false,
max_parallel: None,
trace: false,
run_id: None,
refresh: false,
force_tag: None,
};
let r2 = apply(&cfg2).unwrap();
assert_eq!(
r2[0].resources_converged, 2,
"db-config + service triggered"
);
}
#[test]
fn test_fj224_trigger_without_depends_on() {
let dir = tempfile::tempdir().unwrap();
let state_dir = dir.path().join("state");
std::fs::create_dir_all(&state_dir).unwrap();
let yaml = r#"
version: "1.0"
name: test
machines:
local:
hostname: localhost
addr: 127.0.0.1
resources:
config:
type: file
machine: local
path: /tmp/fj224d-config.txt
content: "v1"
app:
type: file
machine: local
path: /tmp/fj224d-app.txt
content: "app"
triggers: [config]
"#;
let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let cfg = ApplyConfig {
config: &config,
state_dir: &state_dir,
force: false,
dry_run: false,
machine_filter: None,
resource_filter: None,
tag_filter: None,
group_filter: None,
timeout_secs: None,
force_unlock: false,
progress: false,
retry: 0,
parallel: None,
resource_timeout: None,
rollback_on_failure: false,
max_parallel: None,
trace: false,
run_id: None,
refresh: false,
force_tag: None,
};
let r1 = apply(&cfg).unwrap();
assert_eq!(r1[0].resources_converged, 2);
let yaml2 = r#"
version: "1.0"
name: test
machines:
local:
hostname: localhost
addr: 127.0.0.1
resources:
config:
type: file
machine: local
path: /tmp/fj224d-config.txt
content: "v2"
app:
type: file
machine: local
path: /tmp/fj224d-app.txt
content: "app"
depends_on: [config]
triggers: [config]
"#;
let config2: ForjarConfig = serde_yaml_ng::from_str(yaml2).unwrap();
let cfg2 = ApplyConfig {
config: &config2,
state_dir: &state_dir,
force: false,
dry_run: false,
machine_filter: None,
resource_filter: None,
tag_filter: None,
group_filter: None,
timeout_secs: None,
force_unlock: false,
progress: false,
retry: 0,
parallel: None,
resource_timeout: None,
rollback_on_failure: false,
max_parallel: None,
trace: false,
run_id: None,
refresh: false,
force_tag: None,
};
let r2 = apply(&cfg2).unwrap();
assert_eq!(
r2[0].resources_converged, 2,
"config changed + app triggered"
);
}