Test properly quota definition

This commit is contained in:
Pierre HUBERT 2023-05-08 14:38:54 +02:00
parent fb2a9f39fb
commit e3860d9f93
2 changed files with 115 additions and 24 deletions

View File

@ -79,6 +79,11 @@ struct MinioAnonymousAccess {
pub permission: String, pub permission: String,
} }
#[derive(Debug, Clone, Deserialize)]
struct MinioQuota {
pub quota: Option<usize>,
}
impl BasicMinioResult { impl BasicMinioResult {
pub fn success(&self) -> bool { pub fn success(&self) -> bool {
self.status == "success" self.status == "success"
@ -215,23 +220,7 @@ impl MinioService {
.await?; .await?;
// Set quota, if requested // Set quota, if requested
if let Some(quota) = &b.quota { self.bucket_set_quota(&b.name, b.quota).await?;
let quota = format!("{}MB", quota);
let res = self
.exec_mc_cmd::<BasicMinioResult>(&[
"quota",
"set",
bucket_name.as_str(),
"--size",
quota.as_str(),
])
.await?;
if res.get(0).map(|r| r.success()) != Some(true) {
return Err(MinioError::SetQuotaFailed.into());
}
}
// Set retention, if requested // Set retention, if requested
if let Some(retention) = &b.retention { if let Some(retention) = &b.retention {
@ -329,6 +318,41 @@ impl MinioService {
.permission .permission
== "download") == "download")
} }
/// Set bucket quota, in bytes
pub async fn bucket_set_quota(&self, bucket: &str, quota: Option<usize>) -> anyhow::Result<()> {
let bucket_name = self.absolute_bucket_name(bucket);
let res = if let Some(quota) = &quota {
let quota = format!("{}B", quota);
self.exec_mc_cmd::<BasicMinioResult>(&[
"quota",
"set",
bucket_name.as_str(),
"--size",
quota.as_str(),
])
.await?
} else {
self.exec_mc_cmd::<BasicMinioResult>(&["quota", "clear", bucket_name.as_str()])
.await?
};
if res.get(0).map(|r| r.success()) != Some(true) {
return Err(MinioError::SetQuotaFailed.into());
}
Ok(())
}
/// Get current bucket quota, in bytes
pub async fn bucket_get_quota(&self, bucket_name: &str) -> anyhow::Result<Option<usize>> {
let bucket_name = self.absolute_bucket_name(bucket_name);
Ok(self
.exec_mc_cmd::<MinioQuota>(&["quota", "info", bucket_name.as_str()])
.await?
.remove(0)
.quota)
}
} }
#[cfg(test)] #[cfg(test)]
@ -492,7 +516,6 @@ mod test {
); );
} }
// With versioning
#[tokio::test] #[tokio::test]
async fn bucket_with_versioning() { async fn bucket_with_versioning() {
let _ = env_logger::builder().is_test(true).try_init(); let _ = env_logger::builder().is_test(true).try_init();
@ -590,10 +613,78 @@ mod test {
.unwrap()); .unwrap());
} }
// TODO : with quota #[tokio::test]
// TODO : without quota async fn bucket_without_qutoa() {
// TODO : with lock let _ = env_logger::builder().is_test(true).try_init();
// TODO : without lock
let srv = MinioTestServer::start().await.unwrap();
let service = srv.as_service();
service
.create_bucket(&MinioBucketSpec {
instance: "".to_string(),
name: TEST_BUCKET_NAME.to_string(),
secret: "".to_string(),
anonymous_read_access: false,
versioning: false,
quota: None,
lock: false,
retention: None,
})
.await
.unwrap();
assert!(service.bucket_exists(TEST_BUCKET_NAME).await.unwrap());
assert_eq!(
service.bucket_get_quota(TEST_BUCKET_NAME).await.unwrap(),
None
);
service
.bucket_set_quota(TEST_BUCKET_NAME, Some(5122600))
.await
.unwrap();
assert_eq!(
service.bucket_get_quota(TEST_BUCKET_NAME).await.unwrap(),
Some(5122600)
);
service
.bucket_set_quota(TEST_BUCKET_NAME, None)
.await
.unwrap();
assert_eq!(
service.bucket_get_quota(TEST_BUCKET_NAME).await.unwrap(),
None
);
}
#[tokio::test]
async fn bucket_with_qutoa() {
let _ = env_logger::builder().is_test(true).try_init();
let srv = MinioTestServer::start().await.unwrap();
let service = srv.as_service();
service
.create_bucket(&MinioBucketSpec {
instance: "".to_string(),
name: TEST_BUCKET_NAME.to_string(),
secret: "".to_string(),
anonymous_read_access: false,
versioning: false,
quota: Some(42300),
lock: false,
retention: None,
})
.await
.unwrap();
assert!(service.bucket_exists(TEST_BUCKET_NAME).await.unwrap());
assert_eq!(
service.bucket_get_quota(TEST_BUCKET_NAME).await.unwrap(),
Some(42300)
);
}
// TODO : with retention // TODO : with retention
// TODO : without retention // TODO : without retention
} }

View File

@ -47,8 +47,8 @@ spec:
default: false default: false
quota: quota:
type: integer type: integer
description: Limits the amount of data in the bucket, in Megabytes. By default it is unlimited description: Limits the amount of data in the bucket, in bytes. By default it is unlimited
example: 100 example: 1000000000
lock: lock:
description: Object locking prevent objects from being deleted. Will be considered as set to true when retention is defined. description: Object locking prevent objects from being deleted. Will be considered as set to true when retention is defined.
type: boolean type: boolean