-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
160 additions
and
41 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -74,6 +74,8 @@ jobs: | |
# tomlq (included with yq) is needed to check the package version in Cargo.toml | ||
- name: Install tomlq | ||
run: pip install yq | ||
- name: cargo clippy version | ||
run: cargo clippy --version | ||
- name: Run pre-commit | ||
uses: pre-commit/[email protected] | ||
|
||
|
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,8 +1,31 @@ | ||
mod service_account_missing_test; | ||
mod status_field_populated_test; | ||
|
||
use std::collections::HashMap; | ||
|
||
use rstest::*; | ||
use sk_core::prelude::*; | ||
use sk_store::{ | ||
TracerConfig, | ||
TrackedObjectConfig, | ||
}; | ||
|
||
use super::*; | ||
use crate::validation::validator::Diagnostic; | ||
use crate::validation::AnnotatedTraceEvent; | ||
|
||
#[fixture] | ||
fn test_trace_config() -> TracerConfig { | ||
TracerConfig { | ||
tracked_objects: HashMap::from([ | ||
( | ||
DEPL_GVK.clone(), | ||
TrackedObjectConfig { | ||
pod_spec_template_path: Some("/spec/template".into()), | ||
..Default::default() | ||
}, | ||
), | ||
(SA_GVK.clone(), Default::default()), | ||
]), | ||
} | ||
} |
107 changes: 92 additions & 15 deletions
107
sk-cli/src/validation/rules/tests/service_account_missing_test.rs
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,36 +1,113 @@ | ||
|
||
use assertables::*; | ||
use serde_json::json; | ||
use sk_store::TraceEvent; | ||
use sk_store::{ | ||
TraceEvent, | ||
TracerConfig, | ||
}; | ||
|
||
use super::service_account_missing::ServiceAccountMissing; | ||
use super::*; | ||
|
||
#[rstest] | ||
fn test_status_field_populated(test_deployment: DynamicObject) { | ||
let v = status_field_populated::validator(); | ||
let mut evt = AnnotatedTraceEvent { | ||
#[fixture] | ||
fn depl_event(test_deployment: DynamicObject, #[default("serviceAccount")] sa_key: &str) -> AnnotatedTraceEvent { | ||
AnnotatedTraceEvent { | ||
data: TraceEvent { | ||
ts: 1, | ||
applied_objs: vec![test_deployment.data(json!({"status": {"availableReplicas": 3}}))], | ||
applied_objs: vec![test_deployment.data(json!({"spec": {"template": {"spec": {sa_key: "foobar"}}}}))], | ||
deleted_objs: vec![], | ||
}, | ||
..Default::default() | ||
} | ||
} | ||
|
||
#[fixture] | ||
fn sa_event(test_service_account: DynamicObject) -> AnnotatedTraceEvent { | ||
AnnotatedTraceEvent { | ||
data: TraceEvent { | ||
ts: 0, | ||
applied_objs: vec![test_service_account], | ||
deleted_objs: vec![], | ||
}, | ||
..Default::default() | ||
} | ||
} | ||
|
||
#[rstest] | ||
#[case("serviceAccount")] | ||
#[case("serviceAccountName")] | ||
fn test_service_account_missing(test_deployment: DynamicObject, test_trace_config: TracerConfig, #[case] sa_key: &str) { | ||
let mut v = ServiceAccountMissing::default(); | ||
let mut evt = depl_event(test_deployment, sa_key); | ||
let annotations = v.check_next_event(&mut evt, &test_trace_config).unwrap(); | ||
|
||
assert_eq!(annotations.keys().collect::<Vec<_>>(), vec![&0]); | ||
} | ||
|
||
#[rstest] | ||
fn test_service_account_missing_deleted( | ||
mut depl_event: AnnotatedTraceEvent, | ||
mut sa_event: AnnotatedTraceEvent, | ||
test_service_account: DynamicObject, | ||
test_trace_config: TracerConfig, | ||
) { | ||
let mut v = ServiceAccountMissing::default(); | ||
let mut sa_event_del = AnnotatedTraceEvent { | ||
data: TraceEvent { | ||
ts: 0, | ||
applied_objs: vec![], | ||
deleted_objs: vec![test_service_account], | ||
}, | ||
..Default::default() | ||
}; | ||
let indices = v.check_next_event(&mut evt); | ||
assert_bag_eq!(indices, &[0]); | ||
v.check_next_event(&mut sa_event, &test_trace_config).unwrap(); | ||
v.check_next_event(&mut sa_event_del, &test_trace_config).unwrap(); | ||
let annotations = v.check_next_event(&mut depl_event, &test_trace_config).unwrap(); | ||
|
||
assert_eq!(annotations.keys().collect::<Vec<_>>(), vec![&0]); | ||
} | ||
|
||
#[rstest] | ||
fn test_service_account_not_missing( | ||
mut depl_event: AnnotatedTraceEvent, | ||
mut sa_event: AnnotatedTraceEvent, | ||
test_trace_config: TracerConfig, | ||
) { | ||
let mut v = ServiceAccountMissing::default(); | ||
v.check_next_event(&mut sa_event, &test_trace_config).unwrap(); | ||
let annotations = v.check_next_event(&mut depl_event, &test_trace_config).unwrap(); | ||
|
||
assert_eq!(annotations.keys().collect::<Vec<_>>(), vec![&0]); | ||
} | ||
|
||
#[rstest] | ||
fn test_status_field_not_populated(test_deployment: DynamicObject) { | ||
let v = status_field_populated::validator(); | ||
let mut evt = AnnotatedTraceEvent { | ||
fn test_service_account_not_missing_same_evt( | ||
test_deployment: DynamicObject, | ||
test_service_account: DynamicObject, | ||
test_trace_config: TracerConfig, | ||
) { | ||
let mut v = ServiceAccountMissing::default(); | ||
let mut depl_evt = AnnotatedTraceEvent { | ||
data: TraceEvent { | ||
ts: 1, | ||
applied_objs: vec![test_deployment.data(json!({}))], | ||
applied_objs: vec![ | ||
test_deployment | ||
.data(json!({"spec": {"template": {"spec": {"serviceAccountName": TEST_SERVICE_ACCOUNT}}}})), | ||
test_service_account, | ||
], | ||
deleted_objs: vec![], | ||
}, | ||
..Default::default() | ||
}; | ||
let indices = v.check_next_event(&mut evt); | ||
assert_is_empty!(indices); | ||
let annotations = v.check_next_event(&mut depl_evt, &test_trace_config).unwrap(); | ||
|
||
assert_eq!(annotations.keys().collect::<Vec<_>>(), vec![&0]); | ||
} | ||
|
||
#[rstest] | ||
fn test_service_account_reset(mut depl_event: AnnotatedTraceEvent, test_trace_config: TracerConfig) { | ||
let mut v = ServiceAccountMissing::default(); | ||
v.check_next_event(&mut depl_event, &test_trace_config).unwrap(); | ||
v.reset(); | ||
|
||
assert_is_empty!(v.seen_service_accounts); | ||
} |
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
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