basic.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. use {
  2. assert_cmd::prelude::*,
  3. solana_ledger::{
  4. blockstore, blockstore::Blockstore, create_new_tmp_ledger_auto_delete,
  5. genesis_utils::create_genesis_config, get_tmp_ledger_path_auto_delete,
  6. },
  7. std::{
  8. path::Path,
  9. process::{Command, Output},
  10. },
  11. };
  12. fn run_ledger_tool(args: &[&str]) -> Output {
  13. Command::cargo_bin(env!("CARGO_PKG_NAME"))
  14. .unwrap()
  15. .args(args)
  16. .output()
  17. .unwrap()
  18. }
  19. #[test]
  20. fn bad_arguments() {
  21. // At least a ledger path is required
  22. assert!(!run_ledger_tool(&[]).status.success());
  23. // Invalid ledger path should fail
  24. assert!(!run_ledger_tool(&["-l", "invalid_ledger", "verify"])
  25. .status
  26. .success());
  27. }
  28. fn nominal_test_helper(ledger_path: &str) {
  29. let output = run_ledger_tool(&["-l", ledger_path, "verify"]);
  30. assert!(output.status.success());
  31. let output = run_ledger_tool(&["-l", ledger_path, "print", "-vv"]);
  32. assert!(output.status.success());
  33. }
  34. #[test]
  35. fn nominal_default() {
  36. let genesis_config = create_genesis_config(100).genesis_config;
  37. let (ledger_path, _blockhash) = create_new_tmp_ledger_auto_delete!(&genesis_config);
  38. nominal_test_helper(ledger_path.path().to_str().unwrap());
  39. }
  40. fn insert_test_shreds(ledger_path: &Path, ending_slot: u64) {
  41. let blockstore = Blockstore::open(ledger_path).unwrap();
  42. let (shreds, _) = blockstore::make_many_slot_entries(
  43. /*start_slot:*/ 0,
  44. ending_slot,
  45. /*entries_per_slot:*/ 10,
  46. );
  47. blockstore.insert_shreds(shreds, None, false).unwrap();
  48. }
  49. #[test]
  50. fn ledger_tool_copy_test() {
  51. let genesis_config = create_genesis_config(100).genesis_config;
  52. let (ledger_path, _blockhash) = create_new_tmp_ledger_auto_delete!(&genesis_config);
  53. const LEDGER_TOOL_COPY_TEST_SHRED_COUNT: u64 = 25;
  54. const LEDGER_TOOL_COPY_TEST_ENDING_SLOT: u64 = LEDGER_TOOL_COPY_TEST_SHRED_COUNT + 1;
  55. insert_test_shreds(ledger_path.path(), LEDGER_TOOL_COPY_TEST_ENDING_SLOT);
  56. let ledger_path = ledger_path.path().to_str().unwrap();
  57. let target_ledger_path = get_tmp_ledger_path_auto_delete!();
  58. let target_ledger_path = target_ledger_path.path().to_str().unwrap();
  59. let output = run_ledger_tool(&[
  60. "-l",
  61. ledger_path,
  62. "copy",
  63. "--target-ledger",
  64. target_ledger_path,
  65. "--ending-slot",
  66. &(LEDGER_TOOL_COPY_TEST_ENDING_SLOT).to_string(),
  67. ]);
  68. assert!(output.status.success());
  69. for slot_id in 0..LEDGER_TOOL_COPY_TEST_ENDING_SLOT {
  70. let src_slot_output = run_ledger_tool(&["-l", ledger_path, "slot", &slot_id.to_string()]);
  71. let dst_slot_output =
  72. run_ledger_tool(&["-l", target_ledger_path, "slot", &slot_id.to_string()]);
  73. assert!(src_slot_output.status.success());
  74. assert!(dst_slot_output.status.success());
  75. assert!(!src_slot_output.stdout.is_empty());
  76. }
  77. }