votequorum-test.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Test the VOTEQUORUM library. Requires that corosync is running and that we are root.
  2. extern crate rust_corosync as corosync;
  3. use corosync::votequorum;
  4. fn quorum_fn(
  5. _handle: &votequorum::Handle,
  6. _context: u64,
  7. quorate: bool,
  8. member_list: Vec<votequorum::Node>,
  9. ) {
  10. println!("TEST votequorum_quorum_fn called. quorate = {}", quorate);
  11. println!(" members: {:?}", member_list);
  12. }
  13. fn nodelist_fn(
  14. _handle: &votequorum::Handle,
  15. _context: u64,
  16. ring_id: votequorum::RingId,
  17. member_list: Vec<corosync::NodeId>,
  18. ) {
  19. println!(
  20. "TEST nodelist_fn called for {}/{}",
  21. ring_id.nodeid, ring_id.seq
  22. );
  23. println!(" members: {:?}", member_list);
  24. }
  25. fn expectedvotes_fn(_handle: &votequorum::Handle, _context: u64, expected_votes: u32) {
  26. println!("TEST expected_votes_fn called: value is {}", expected_votes);
  27. }
  28. fn main() {
  29. // Initialise the model data
  30. let cb = votequorum::Callbacks {
  31. quorum_notification_fn: Some(quorum_fn),
  32. nodelist_notification_fn: Some(nodelist_fn),
  33. expectedvotes_notification_fn: Some(expectedvotes_fn),
  34. };
  35. let handle = match votequorum::initialize(&cb) {
  36. Ok(h) => {
  37. println!("Votequorum initialized.");
  38. h
  39. }
  40. Err(e) => {
  41. println!("Error in VOTEQUORUM init: {}", e);
  42. return;
  43. }
  44. };
  45. // Test context APIs
  46. let set_context: u64 = 0xabcdbeefcafe;
  47. if let Err(e) = votequorum::context_set(handle, set_context) {
  48. println!("Error in VOTEQUORUM context_set: {}", e);
  49. }
  50. // NOTE This will fail on 32 bit systems because void* is not u64
  51. match votequorum::context_get(handle) {
  52. Ok(c) => {
  53. if c != set_context {
  54. println!(
  55. "Error: context_get() returned {:x}, context should be {:x}",
  56. c, set_context
  57. );
  58. }
  59. }
  60. Err(e) => {
  61. println!("Error in VOTEQUORUM context_get: {}", e);
  62. }
  63. }
  64. const QDEVICE_NAME: &str = "RustQdevice";
  65. if let Err(e) = votequorum::qdevice_register(handle, QDEVICE_NAME) {
  66. println!("Error in VOTEQUORUM qdevice_register: {}", e);
  67. }
  68. match votequorum::get_info(handle, corosync::NodeId::from(1u32)) {
  69. Ok(i) => {
  70. println!("Node info for nodeid 1");
  71. println!(" nodeid: {}", i.node_id);
  72. println!(" node_state: {:?}", i.node_state);
  73. println!(" node_votes: {}", i.node_votes);
  74. println!(" node_expected: {}", i.node_expected_votes);
  75. println!(" highest_expected: {}", i.highest_expected);
  76. println!(" quorum: {}", i.quorum);
  77. println!(" flags: {:x}", i.flags);
  78. println!(" qdevice_votes: {}", i.qdevice_votes);
  79. println!(" qdevice_name: {}", i.qdevice_name);
  80. if i.qdevice_name != QDEVICE_NAME {
  81. println!(
  82. "qdevice names do not match: s/b: \"{}\" is: \"{}\"",
  83. QDEVICE_NAME, i.qdevice_name
  84. );
  85. }
  86. }
  87. Err(e) => {
  88. println!(
  89. "Error in VOTEQUORUM get_info: {} (check nodeid 1 has been online)",
  90. e
  91. );
  92. }
  93. }
  94. if let Err(e) = votequorum::qdevice_unregister(handle, QDEVICE_NAME) {
  95. println!("Error in VOTEQUORUM qdevice_unregister: {}", e);
  96. }
  97. if let Err(e) = votequorum::trackstart(handle, 99_u64, corosync::TrackFlags::Changes) {
  98. println!("Error in VOTEQUORUM trackstart: {}", e);
  99. return;
  100. }
  101. // Wait for events
  102. loop {
  103. if votequorum::dispatch(handle, corosync::DispatchFlags::One).is_err() {
  104. break;
  105. }
  106. }
  107. println!("ERROR: Corosync quit");
  108. }