Skip to content

Commit

Permalink
test: fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Ryota Sakamoto <[email protected]>
  • Loading branch information
ryota-sakamoto committed May 20, 2024
1 parent 1d4ba74 commit bf27a08
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 393 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ aws-config = "1.4.0"
aws-sdk-dynamodb = "1.28.0"
aws-sdk-ec2 = "1.42.0"
aws-smithy-runtime-api = "1.6.0"
aws-smithy-types = { version = "1.1.9", features=["serde-serialize"] }
aws-types = "1.2.1"
chrono = "0.4"
clap = { version = "4.5.4", features = ["derive"] }
Expand Down
26 changes: 15 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,7 @@ pub async fn table_schema(cx: &Context) -> TableSchema {
Some(table_name) => {
// TODO: reduce # of DescribeTable API calls. table_schema function is called every time you do something.
let desc: TableDescription = control::describe_table_api(
cx,
table_name, /* should be equal to 'cx.effective_table_name()' */
cx, table_name, /* should be equal to 'cx.effective_table_name()' */
)
.await;

Expand Down Expand Up @@ -663,7 +662,7 @@ pub fn index_schemas(desc: &TableDescription) -> Option<Vec<IndexSchema>> {
}
}

pub fn bye(code: i32, msg: &str) {
pub fn bye(code: i32, msg: &str) -> ! {
println!("{}", msg);
std::process::exit(code);
}
Expand Down Expand Up @@ -757,7 +756,6 @@ mod tests {
use super::*;
use std::convert::TryInto;
use std::error::Error;
use std::str::FromStr; // to utilize Region::from_str // for unit tests

#[test]
fn test_context_functions() -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -790,29 +788,35 @@ mod tests {
should_strict_for_query: None,
retry: Some(RetryConfig::default().try_into()?),
};
assert_eq!(cx2.effective_region(), Region::from_str("ap-northeast-1")?);
assert_eq!(
cx2.effective_region(),
Region::from_static("ap-northeast-1")
);
assert_eq!(cx2.effective_table_name(), String::from("cfgtbl"));

let cx3 = Context {
overwritten_region: Some(Region::from_str("us-east-1")?), // --region us-east-1
overwritten_table_name: Some(String::from("argtbl")), // --table argtbl
overwritten_region: Some(Region::from_static("us-east-1")), // --region us-east-1
overwritten_table_name: Some(String::from("argtbl")), // --table argtbl
..cx2.clone()
};
assert_eq!(cx3.effective_region(), Region::from_str("us-east-1")?);
assert_eq!(cx3.effective_region(), Region::from_static("us-east-1"));
assert_eq!(cx3.effective_table_name(), String::from("argtbl"));

let cx4 = Context {
overwritten_region: Some(Region::from_str("us-east-1")?), // --region us-east-1
overwritten_region: Some(Region::from_static("us-east-1")), // --region us-east-1
..cx2.clone()
};
assert_eq!(cx4.effective_region(), Region::from_str("us-east-1")?);
assert_eq!(cx4.effective_region(), Region::from_static("us-east-1"));
assert_eq!(cx4.effective_table_name(), String::from("cfgtbl"));

let cx5 = Context {
overwritten_table_name: Some(String::from("argtbl")), // --table argtbl
..cx2.clone()
};
assert_eq!(cx5.effective_region(), Region::from_str("ap-northeast-1")?);
assert_eq!(
cx5.effective_region(),
Region::from_static("ap-northeast-1")
);
assert_eq!(cx5.effective_table_name(), String::from("argtbl"));

Ok(())
Expand Down
19 changes: 15 additions & 4 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ pub fn build_batch_request_items_from_json(
ddbjson_attributes_to_attrvals(raw_item);
write_requests.push(
WriteRequest::builder()
.put_request(PutRequest::builder().set_item(Some(item)).build().unwrap())
.put_request(
PutRequest::builder().set_item(Some(item)).build().unwrap(),
)
.build(),
);
} else {
Expand Down Expand Up @@ -191,7 +193,9 @@ pub fn build_batch_request_items_from_json(
ddbjson_attributes_to_attrvals(raw_key);
write_requests.push(
WriteRequest::builder()
.delete_request(DeleteRequest::builder().set_key(Some(key)).build().unwrap())
.delete_request(
DeleteRequest::builder().set_key(Some(key)).build().unwrap(),
)
.build(),
);
} else {
Expand Down Expand Up @@ -353,7 +357,12 @@ pub async fn batch_write_item(
validate_item_keys(&attrs, &ts)?;
write_requests.push(
WriteRequest::builder()
.delete_request(DeleteRequest::builder().set_key(Some(attrs)).build().unwrap())
.delete_request(
DeleteRequest::builder()
.set_key(Some(attrs))
.build()
.unwrap(),
)
.build(),
);
}
Expand Down Expand Up @@ -554,7 +563,9 @@ fn ddbjson_val_to_attrval(ddb_jsonval: &JsonValue) -> Option<AttributeValue> {
} else if let Some(x) = ddb_jsonval.get("N") {
Some(AttributeValue::N(x.as_str().unwrap().to_string()))
} else if let Some(x) = ddb_jsonval.get("B") {
Some(AttributeValue::B(aws_sdk_dynamodb::primitives::Blob::new(json_binary_val_to_bytes(x))))
Some(AttributeValue::B(aws_sdk_dynamodb::primitives::Blob::new(
json_binary_val_to_bytes(x),
)))
} else if let Some(x) = ddb_jsonval.get("BOOL") {
Some(AttributeValue::Bool(x.as_bool().unwrap()))
} else if let Some(x) = ddb_jsonval.get("SS") {
Expand Down
7 changes: 6 additions & 1 deletion src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ see https://github.com/awslabs/dynein#working-with-dynamodb-items for detail
.collect();
write_requests.push(
WriteRequest::builder()
.put_request(PutRequest::builder().set_item(Some(item_attrval)).build().unwrap())
.put_request(
PutRequest::builder()
.set_item(Some(item_attrval))
.build()
.unwrap(),
)
.build(),
);
if write_requests.len() == 25 {
Expand Down
44 changes: 22 additions & 22 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ pub async fn describe_table(cx: app::Context, target_table_to_desc: Option<Strin
cx
};

let desc: TableDescription = describe_table_api(
&new_context,
new_context.effective_table_name(),
)
.await;
let desc: TableDescription =
describe_table_api(&new_context, new_context.effective_table_name()).await;
debug!(
"Retrieved table to describe is: '{}' table in '{}' region.",
&new_context.effective_table_name(),
Expand All @@ -145,7 +142,9 @@ pub async fn describe_table(cx: app::Context, target_table_to_desc: Option<Strin
};

match new_context.clone().output.as_deref() {
None | Some("yaml") => util::print_table_description(new_context.effective_region().as_ref(), desc),
None | Some("yaml") => {
util::print_table_description(new_context.effective_region().as_ref(), desc)
}
// Some("raw") => println!("{:#?}", desc),
Some(_) => {
println!("ERROR: unsupported output type.");
Expand All @@ -156,10 +155,7 @@ pub async fn describe_table(cx: app::Context, target_table_to_desc: Option<Strin

/// Originally intended to be called by describe_table function, which is called from `$ dy desc`,
/// however it turned out that DescribeTable API result is useful in various logic, separated API into this standalone function.
pub async fn describe_table_api(
cx: &app::Context,
table_name: String,
) -> TableDescription {
pub async fn describe_table_api(cx: &app::Context, table_name: String) -> TableDescription {
let region = cx.effective_region();
let config = cx.effective_sdk_config_with_region(region.as_ref()).await;
let ddb = DynamoDbSdkClient::new(&config);
Expand Down Expand Up @@ -190,7 +186,7 @@ pub async fn create_table(cx: app::Context, name: String, given_keys: Vec<String
Ok(desc) => util::print_table_description(cx.effective_region().as_ref(), desc),
Err(e) => {
debug!("CreateTable API call got an error -- {:#?}", e);
error!("{}", e.to_string());
error!("{}", e.into_service_error());
std::process::exit(1);
}
}
Expand Down Expand Up @@ -253,7 +249,8 @@ pub async fn create_index(cx: app::Context, index_name: String, given_keys: Vec<
.build(),
)
.set_provisioned_throughput(None) // TODO: assign default rcu/wcu if base table is Provisioned mode. currently it works only for OnDemand talbe.
.build().unwrap();
.build()
.unwrap();

let gsi_update = GlobalSecondaryIndexUpdate::builder()
.create(create_gsi_action)
Expand All @@ -274,7 +271,10 @@ pub async fn create_index(cx: app::Context, index_name: String, given_keys: Vec<
}
Ok(res) => {
debug!("Returned result: {:#?}", res);
util::print_table_description(cx.effective_region().as_ref(), res.table_description.unwrap());
util::print_table_description(
cx.effective_region().as_ref(),
res.table_description.unwrap(),
);
}
}
}
Expand All @@ -287,8 +287,7 @@ pub async fn update_table(
rcu: Option<i64>,
) {
// Retrieve TableDescription of the table to update, current (before update) status.
let desc: TableDescription =
describe_table_api(&cx, table_name_to_update.clone()).await;
let desc: TableDescription = describe_table_api(&cx, table_name_to_update.clone()).await;

// Map given string into "Mode" enum. Note that in cmd.rs clap already limits acceptable values.
let switching_to_mode: Option<util::Mode> = match mode_string {
Expand Down Expand Up @@ -330,7 +329,8 @@ pub async fn update_table(
.write_capacity_units
.unwrap()
}))
.build().unwrap(),
.build()
.unwrap(),
),
}
}
Expand All @@ -348,7 +348,8 @@ pub async fn update_table(
ProvisionedThroughput::builder()
.read_capacity_units(rcu.unwrap_or(5))
.write_capacity_units(wcu.unwrap_or(5))
.build().unwrap(),
.build()
.unwrap(),
),
},
};
Expand Down Expand Up @@ -427,7 +428,7 @@ pub async fn delete_table(cx: app::Context, name: String, skip_confirmation: boo
match ddb.delete_table().table_name(name).send().await {
Err(e) => {
debug!("DeleteTable API call got an error -- {:#?}", e);
error!("{}", e.to_string());
error!("{}", e.into_service_error());
std::process::exit(1);
}
Ok(res) => {
Expand Down Expand Up @@ -469,7 +470,7 @@ pub async fn backup(cx: app::Context, all_tables: bool) {
match req.send().await {
Err(e) => {
debug!("CreateBackup API call got an error -- {:#?}", e);
app::bye(1, &e.to_string());
app::bye(1, &e.into_service_error().to_string());
}
Ok(res) => {
debug!("Returned result: {:#?}", res);
Expand Down Expand Up @@ -589,6 +590,7 @@ pub async fn restore(cx: app::Context, backup_name: Option<String>, restore_name
{
Err(e) => {
debug!("RestoreTableFromBackup API call got an error -- {:#?}", e);
app::bye(1, &e.into_service_error().to_string());
/* e.g. ... Possibly see "BackupInUse" error:
[2020-08-14T13:16:07Z DEBUG dy::control] RestoreTableFromBackup API call got an error -- Service( BackupInUse( "Backup is being used to restore another table: arn:aws:dynamodb:us-west-2:111111111111:table/Music/backup/01527492829107-81b9b3dd",))
*/
Expand Down Expand Up @@ -636,9 +638,7 @@ async fn list_backups_api(cx: &app::Context, all_tables: bool) -> Vec<BackupSumm
match req.send().await {
Err(e) => {
debug!("ListBackups API call got an error -- {:#?}", e);
// app::bye(1, &e.to_string()) // it doesn't meet return value requirement.
println!("{}", &e.to_string());
std::process::exit(1);
app::bye(1, &e.into_service_error().to_string());
}
Ok(res) => res
.backup_summaries
Expand Down
Loading

0 comments on commit bf27a08

Please sign in to comment.