Coverage for tests/daemon/rpc/test_admin.py: 79%

81 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1from __future__ import annotations 

2 

3import logging 

4import time 

5 

6import pytest 

7from monero import ( 

8 MoneroDaemonRpc, 

9 MoneroDaemonUpdateCheckResult, 

10 MoneroDaemonUpdateDownloadResult, 

11 MoneroPruneResult, 

12 MoneroRpcError, 

13) 

14 

15from utils import DaemonUtils, TestUtils as Utils 

16from .base_test_monero_daemon_rpc import BaseTestMoneroDaemonRpc 

17 

18logger: logging.Logger = logging.getLogger("TestMoneroDaemonRpcAdmin") 

19 

20 

21@pytest.mark.integration 

22class TestMoneroDaemonRpcAdmin(BaseTestMoneroDaemonRpc): 

23 """Rpc daemon admin integration tests.""" 

24 

25 # Can prune the blockchain 

26 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

27 def test_prune_blockchain(self, daemon: MoneroDaemonRpc) -> None: 

28 result: MoneroPruneResult = daemon.prune_blockchain(True) 

29 logger.debug(f"Testing prune result: {result.serialize()}") 

30 

31 if result.is_pruned: 

32 assert result.pruning_seed is not None 

33 assert result.pruning_seed > 0 

34 else: 

35 assert 0 == result.pruning_seed 

36 

37 # Can check for an update 

38 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

39 @pytest.mark.xfail(reason="the update check needs DNSSEC validated TXT records, which the daemon in docker does not get", raises=MoneroRpcError, strict=False) 

40 def test_check_for_update(self, daemon: MoneroDaemonRpc) -> None: 

41 result: MoneroDaemonUpdateCheckResult = daemon.check_for_update() 

42 DaemonUtils.test_update_check_result(result) 

43 

44 # Can download an update 

45 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

46 @pytest.mark.xfail(reason="the update check needs DNSSEC validated TXT records, which the daemon in docker does not get", raises=MoneroRpcError, strict=False) 

47 def test_download_update(self, daemon: MoneroDaemonRpc) -> None: 

48 # download to default path 

49 result: MoneroDaemonUpdateDownloadResult = daemon.download_update() 

50 DaemonUtils.test_update_download_result(result, None) 

51 

52 # download to defined path 

53 path: str = "test_download_" + str(time.time()) + ".tar.bz2" 

54 result = daemon.download_update(path) 

55 DaemonUtils.test_update_download_result(result, path) 

56 

57 # test invalid path 

58 # TODO monerod: an invalid path causes a 500 in daemon rpc rather than a clean error 

59 if result.is_update_available: 

60 try: 

61 daemon.download_update("./ohhai/there") 

62 except Exception as e: 

63 logger.warning(str(e)) 

64 

65 # Can be stopped 

66 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

67 @pytest.mark.skip(reason="test is disabled to not interfere with other tests") 

68 def test_stop(self, daemon: MoneroDaemonRpc) -> None: 

69 # stop the daemon 

70 daemon.stop() 

71 

72 # give the daemon time to shut down 

73 time.sleep(Utils.SYNC_PERIOD_IN_MS / 1000) 

74 

75 # try to interact with the stopped daemon 

76 with pytest.raises(Exception): 

77 daemon.get_height() 

78 

79 # Can flush the blockchain to disk 

80 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

81 def test_save_blockchain(self, daemon: MoneroDaemonRpc) -> None: 

82 daemon.save_blockchain() 

83 

84 # Can flush the daemon's invalid block and tx caches 

85 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

86 def test_flush_cache(self, daemon: MoneroDaemonRpc) -> None: 

87 daemon.flush_cache() 

88 daemon.flush_cache(True) 

89 

90 # Can disable the bootstrap daemon 

91 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

92 def test_remove_bootstrap_daemon(self, daemon: MoneroDaemonRpc) -> None: 

93 daemon.remove_bootstrap_daemon() 

94 

95 # Can set the poll period for block notifications 

96 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

97 def test_set_poll_period_in_ms(self, daemon: MoneroDaemonRpc) -> None: 

98 try: 

99 daemon.set_poll_period_in_ms(1000) 

100 finally: 

101 # restore the configured period so later tests poll at the expected rate 

102 daemon.set_poll_period_in_ms(Utils.DAEMON_POLL_PERIOD_IN_MS) 

103 

104 # Can set the daemon's log level 

105 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

106 def test_set_log_level(self, daemon: MoneroDaemonRpc) -> None: 

107 try: 

108 for i in range(0, 5): 

109 daemon.set_log_level(i) 

110 

111 for level in (-1, 5): 

112 with pytest.raises(Exception) as exc_info: 

113 daemon.set_log_level(level) 

114 assert str(exc_info.value) == "Log level must be an integer between 0 and 4" 

115 finally: 

116 # restore the level from docker-compose so later tests keep full logs 

117 daemon.set_log_level(Utils.DAEMON_LOG_LEVEL) 

118 

119 # Can set the daemon's log categories 

120 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

121 def test_set_log_categories(self, daemon: MoneroDaemonRpc) -> None: 

122 try: 

123 categories: str = daemon.set_log_categories("*:WARNING") 

124 assert isinstance(categories, str) 

125 finally: 

126 # re-applying the configured level also resets categories to its mapping 

127 daemon.set_log_level(Utils.DAEMON_LOG_LEVEL) 

128 

129 # Can toggle the mining hash rate in the daemon log 

130 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") 

131 def test_set_log_hash_rate(self, daemon: MoneroDaemonRpc) -> None: 

132 try: 

133 daemon.set_log_hash_rate(True) 

134 daemon.set_log_hash_rate(False) 

135 except Exception as e: 

136 # daemon rejects the call while not mining 

137 logger.warning(f"set_log_hash_rate: {e}")