Skip to content

Commit 5ca0cb2

Browse files
committed
test: Add a config parsing test
1 parent f2b7c31 commit 5ca0cb2

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

objectstore-server/src/config.rs

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ mod tests {
926926

927927
use secrecy::ExposeSecret;
928928

929+
use crate::killswitches::Killswitch;
930+
929931
use super::*;
930932

931933
#[test]
@@ -1077,29 +1079,30 @@ mod tests {
10771079
Ok(())
10781080
});
10791081
}
1082+
10801083
#[test]
10811084
fn configure_auth_with_yaml() {
10821085
let mut tempfile = tempfile::NamedTempFile::new().unwrap();
10831086
tempfile
10841087
.write_all(
10851088
br#"
1086-
auth:
1087-
enforce: true
1088-
keys:
1089-
kid1:
1090-
key_versions:
1091-
- "abcde"
1092-
- "fghij"
1093-
- |
1094-
this is a test
1095-
multiline string
1096-
end of string
1097-
max_permissions:
1098-
- "object.read"
1099-
- "object.write"
1100-
kid2:
1101-
key_versions:
1102-
- "12345"
1089+
auth:
1090+
enforce: true
1091+
keys:
1092+
kid1:
1093+
key_versions:
1094+
- "abcde"
1095+
- "fghij"
1096+
- |
1097+
this is a test
1098+
multiline string
1099+
end of string
1100+
max_permissions:
1101+
- "object.read"
1102+
- "object.write"
1103+
kid2:
1104+
key_versions:
1105+
- "12345"
11031106
"#,
11041107
)
11051108
.unwrap();
@@ -1128,4 +1131,54 @@ mod tests {
11281131
Ok(())
11291132
});
11301133
}
1134+
1135+
#[test]
1136+
fn configure_killswitches_with_yaml() {
1137+
let mut tempfile = tempfile::NamedTempFile::new().unwrap();
1138+
tempfile
1139+
.write_all(
1140+
br#"
1141+
killswitches:
1142+
- usecase: broken_usecase
1143+
- scopes:
1144+
org: "42"
1145+
- scopes:
1146+
org: "42"
1147+
project: "4711"
1148+
- usecase: attachments
1149+
scopes:
1150+
org: "42"
1151+
"#,
1152+
)
1153+
.unwrap();
1154+
1155+
figment::Jail::expect_with(|_jail| {
1156+
let expected = [
1157+
Killswitch {
1158+
usecase: Some("broken_usecase".into()),
1159+
scopes: BTreeMap::new(),
1160+
},
1161+
Killswitch {
1162+
usecase: None,
1163+
scopes: BTreeMap::from([("org".into(), "42".into())]),
1164+
},
1165+
Killswitch {
1166+
usecase: None,
1167+
scopes: BTreeMap::from([
1168+
("org".into(), "42".into()),
1169+
("project".into(), "4711".into()),
1170+
]),
1171+
},
1172+
Killswitch {
1173+
usecase: Some("attachments".into()),
1174+
scopes: BTreeMap::from([("org".into(), "42".into())]),
1175+
},
1176+
];
1177+
1178+
let config = Config::load(Some(tempfile.path())).unwrap();
1179+
assert_eq!(&config.killswitches.0, &expected,);
1180+
1181+
Ok(())
1182+
});
1183+
}
11311184
}

objectstore-server/src/killswitches.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use serde::{Deserialize, Serialize};
55

66
/// A list of killswitches that may disable access to certain object contexts.
77
#[derive(Debug, Default, Deserialize, Serialize)]
8-
pub struct Killswitches(Vec<Killswitch>);
8+
pub struct Killswitches(pub Vec<Killswitch>);
99

1010
impl Killswitches {
11+
/// Returns `true` if any of the contained killswitches matches the given context.
1112
pub fn matches(&self, context: &ObjectContext) -> bool {
1213
self.0.iter().any(|s| s.matches(context))
1314
}
@@ -17,7 +18,7 @@ impl Killswitches {
1718
///
1819
/// Note that at least one of the fields should be set, or else the killswitch will match all
1920
/// contexts and discard all requests.
20-
#[derive(Debug, Deserialize, Serialize)]
21+
#[derive(Debug, PartialEq, Deserialize, Serialize)]
2122
pub struct Killswitch {
2223
/// Optional usecase to match.
2324
///
@@ -34,6 +35,7 @@ pub struct Killswitch {
3435
}
3536

3637
impl Killswitch {
38+
/// Returns `true` if this killswitch matches the given context.
3739
pub fn matches(&self, context: &ObjectContext) -> bool {
3840
if let Some(ref switch_usecase) = self.usecase
3941
&& switch_usecase != &context.usecase

0 commit comments

Comments
 (0)