Compare commits

...

52 Commits

Author SHA1 Message Date
Aaron Whitehouse 4c1de8017a
Merge cd0b9f362b into fdbbe28ac7 2024-04-18 08:03:50 +02:00
Aaron Whitehouse cd0b9f362b Add tests with monitor_dont_crit set but
without monitor_dont_warn
2022-04-02 21:37:54 +01:00
Aaron Whitehouse 135f310881 Add copy of monitor snapshots test with
monitor_dont_warn and monitor_dont_crit set
2022-04-02 21:23:34 +01:00
Aaron Whitehouse fec7cf9e18 Add newline 2022-03-27 22:23:03 +01:00
Aaron Whitehouse b17a0fe104 Add newline at end of sanoid.conf 2022-03-27 22:13:49 +01:00
Aaron Whitehouse 9759fa45c4 Ready to merge 2022-03-27 22:10:03 +01:00
Aaron Whitehouse 9cc6b7f12b Delete redundant set of tests from rename/merge 2022-03-27 21:36:19 +01:00
Aaron Whitehouse 3a4e043c96 Merge branch 'add_vm_tests' of https://github.com/Hooloovoo/sanoid into add_vm_tests 2022-03-27 21:32:37 +01:00
Aaron Whitehouse cdbe07e4ec Create a new set of tests for --monitor-snapshots
these create pools in a number of states and check the output text
and return code
2022-03-27 21:28:21 +01:00
Aaron Whitehouse 5015aecd7e change name of test_two_warnings_daily
to be more accurate
2022-03-27 20:35:24 +01:00
Aaron Whitehouse c7e57b7c74 Fix test_two_warnings 2022-03-23 22:11:45 +00:00
Aaron Whitehouse 81529523fd Fix return code 2022-03-23 22:10:52 +00:00
Aaron Whitehouse c28f1c30dc Fix test_two_warnings_daily text test 2022-03-23 22:09:29 +00:00
Aaron Whitehouse ae4c0c6fa2 Start test_two_warnings_daily 2022-03-23 21:53:39 +00:00
Aaron Whitehouse 38ed37c96d Fix test_two_criticals returncode 2022-03-23 21:45:44 +00:00
Aaron Whitehouse f0fd435fa6 Fix test_two_criticals output test 2022-03-23 21:44:20 +00:00
Aaron Whitehouse a7fd743228 Add test details for two criticals 2022-03-23 21:42:00 +00:00
Aaron Whitehouse 7462a1a7fe Adjust time slightly 2022-03-23 21:29:18 +00:00
Aaron Whitehouse adddd20672 Add initial test for two critical monitoring
errors
2022-03-23 21:27:08 +00:00
Aaron Whitehouse 4c9a9e2eb8 Use sanoid --force-update instead of
manually deleting snapshot cache
2022-03-23 21:23:11 +00:00
Aaron Whitehouse d9da30dadd Fix test for one monitoring warning. 2022-03-23 21:20:18 +00:00
Aaron Whitehouse f870d27e9f Test warning string 2022-03-23 21:16:31 +00:00
Aaron Whitehouse a2fc4b30ac Add print for debugging 2022-03-23 21:03:36 +00:00
Aaron Whitehouse 2561a5ed5d Add more info for debugging 2022-03-23 21:03:11 +00:00
Aaron Whitehouse 0b7c05f4c7 First time-based test (one warning) 2022-03-23 20:55:44 +00:00
Aaron Whitehouse 6710b2d36f Clear snapshot cache before each test 2022-03-17 22:38:41 +00:00
Aaron Whitehouse b992beba19 Add test for monitoring immediately after running 2022-03-17 22:34:47 +00:00
Aaron Whitehouse d0f89c96bb Fix monitoring test after sanoid run 2022-03-17 22:31:15 +00:00
Aaron Whitehouse 02aedfaaa2 Test monitoring after sanoid cron 2022-03-17 22:22:39 +00:00
Aaron Whitehouse 867b755bb1 Delete temp disk images as part of teardown 2022-03-17 22:19:36 +00:00
Aaron Whitehouse 49589e2a17 Comment out zpool export, as now in python 2022-03-17 22:17:17 +00:00
Aaron Whitehouse 33648050dd Clean up monitoring tests zpool creation/teardown 2022-03-17 22:13:13 +00:00
Aaron Whitehouse 8571510685 Fix pool_target issues in test. 2022-03-17 21:57:40 +00:00
Aaron Whitehouse 3e9730d135 debugging 2022-03-17 21:54:36 +00:00
Aaron Whitehouse 890c785d0b Fixes to zpool creation test. 2022-03-17 21:51:29 +00:00
Aaron Whitehouse 37bce97d6e add test 2022-03-17 21:46:07 +00:00
Aaron Whitehouse e556495dea Create zpool, test. 2022-03-17 21:45:49 +00:00
Aaron Whitehouse c8f2b3f283 Fix return code of first test 2022-03-17 21:25:22 +00:00
Aaron Whitehouse 501c9921c4 Test return code for monitoring no pool 2022-03-17 14:34:08 +00:00
Aaron Whitehouse 26777c2c68 Add 2nd section to sanoid.conf for monitoring test 2022-03-17 14:23:18 +00:00
Aaron Whitehouse f93985111f add second section 2022-03-17 14:19:05 +00:00
Aaron Whitehouse c488df5983 Fix test output. 2022-03-17 11:10:49 +00:00
Aaron Whitehouse 8bb8d38cc8 Remove check on test 2022-03-17 11:07:49 +00:00
Aaron Whitehouse c21d8d0168 Fix path to sanoid snapshot cache file 2022-03-17 11:03:29 +00:00
Aaron Whitehouse 123e35e804 Fix --monitor-snapshots command in test 2022-03-17 10:59:44 +00:00
Aaron Whitehouse 1bdb60715c add subprocess import 2022-03-17 10:57:00 +00:00
Aaron Whitehouse 3655c02775 Test output with no zpool. 2022-03-17 10:54:48 +00:00
Aaron Whitehouse b3ee98a234 Test failure. 2022-03-17 10:37:43 +00:00
Aaron Whitehouse 372eadcb37 Add python test 2022-03-17 10:35:13 +00:00
Aaron Whitehouse e26671c97d Add monitoring test framework 2022-03-17 10:28:10 +00:00
Aaron Whitehouse cdbc1915ba Add new monitoring test scaffolding. 2022-03-17 10:27:04 +00:00
Aaron Whitehouse e963dd228f Add tests README with instructions on testing 2022-03-17 10:05:01 +00:00
12 changed files with 695 additions and 2 deletions

View File

@ -0,0 +1,18 @@
#!/bin/bash
set -x
# this test will create pools in a number of states
# and check the output text and return code of
# sanoid --monitor-snapshots
. ../common/lib.sh
# prepare
setup
checkEnvironment
disableTimeSync
# set timezone
ln -sf /usr/share/zoneinfo/Europe/Vienna /etc/localtime
python3 test_monitoring.py

View File

@ -0,0 +1,37 @@
[sanoid-test-1]
use_template = production
[sanoid-test-2]
use_template = demo
[template_production]
hourly = 36
daily = 30
monthly = 3
yearly = 0
autosnap = yes
autoprune = no
hourly_warn = 90m
hourly_crit = 360m
daily_warn = 28h
daily_crit = 32h
weekly_warn = 0
weekly_crit = 0
monthly_warn = 32d
monthly_crit = 40d
yearly_warn = 0
yearly_crit = 0
[template_demo]
daily = 60
hourly_warn = 290m
hourly_crit = 360m
daily_warn = 28h
daily_crit = 48h
weekly_warn = 0
weekly_crit = 0
monthly_warn = 32d
monthly_crit = 40d
yearly_warn = 0
yearly_crit = 0

View File

@ -0,0 +1,163 @@
#!/usr/bin/env python3
# this software is licensed for use under the Free Software Foundation's GPL v3.0 license, as retrieved
# from http://www.gnu.org/licenses/gpl-3.0.html on 2014-11-17. A copy should also be available in this
# project's Git repository at https://github.com/jimsalterjrs/sanoid/blob/master/LICENSE.
import os
import subprocess
import time
import unittest
sanoid_cmd = os.environ.get("SANOID")
pool_disk_image1 = "/zpool1.img"
pool_name1 = "sanoid-test-1"
pool_disk_image2 = "/zpool2.img"
pool_name2 = "sanoid-test-2"
clk_id = time.CLOCK_REALTIME
starting_time = time.clock_gettime(clk_id)
def monitor_snapshots_command():
"""Runs sanoid --monitor-snapshots and returns a CompletedProcess instance"""
return_info = subprocess.run([sanoid_cmd, "--monitor-snapshots"], capture_output=True)
return return_info
def run_sanoid_cron_command():
"""Runs sanoid and returns a CompletedProcess instance"""
return_info = subprocess.run([sanoid_cmd, "--cron", "--verbose"], capture_output=True, check=True)
return return_info
def advance_time(seconds):
"""Advances the system clock by seconds"""
# Get the current time
clk_id = time.CLOCK_REALTIME
time_seconds = time.clock_gettime(clk_id)
# print("Current unix time is", time_seconds, "or", time.asctime(time.gmtime(time_seconds)), "in GMT")
# Set the clock to the current time plus seconds
time.clock_settime(clk_id, time_seconds + seconds)
# Print the new time
time_seconds = time.clock_gettime(clk_id)
# print("Current unix time is", time_seconds, "or", time.asctime(time.gmtime(time_seconds)), "in GMT")
return time_seconds
class TestMonitoringOutput(unittest.TestCase):
def test_no_zpool(self):
"""Test what happens if there is no zpool at all"""
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"CRIT: sanoid-test-1 has no daily snapshots at all!, CRIT: sanoid-test-1 has no hourly snapshots at all!, CRIT: sanoid-test-1 has no monthly snapshots at all!, CRIT: sanoid-test-2 has no daily snapshots at all!, CRIT: sanoid-test-2 has no hourly snapshots at all!, CRIT: sanoid-test-2 has no monthly snapshots at all!\n")
self.assertEqual(return_info.returncode, 2)
class TestsWithZpool(unittest.TestCase):
"""Tests that require a test zpool"""
def setUp(self):
"""Set up the zpool"""
subprocess.run(["truncate", "-s", "512M", pool_disk_image1], check=True)
subprocess.run(["zpool", "create", "-f", pool_name1, pool_disk_image1], check=True)
subprocess.run(["truncate", "-s", "512M", pool_disk_image2], check=True)
subprocess.run(["zpool", "create", "-f", pool_name2, pool_disk_image2], check=True)
# Clear the snapshot cache in between
subprocess.run([sanoid_cmd, "--force-update"])
def tearDown(self):
"""Clean up on either passed or failed tests"""
subprocess.run(["zpool", "export", pool_name1])
subprocess.run(["rm", "-f", pool_disk_image1])
subprocess.run(["zpool", "export", pool_name2])
subprocess.run(["rm", "-f", pool_disk_image2])
time.clock_settime(clk_id, starting_time)
def test_with_zpool_no_snapshots(self):
"""Test what happens if there is a zpool, but with no snapshots"""
# Run sanoid --monitor-snapshots before doing anything else
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"CRIT: sanoid-test-1 has no daily snapshots at all!, CRIT: sanoid-test-1 has no hourly snapshots at all!, CRIT: sanoid-test-1 has no monthly snapshots at all!, CRIT: sanoid-test-2 has no daily snapshots at all!, CRIT: sanoid-test-2 has no hourly snapshots at all!, CRIT: sanoid-test-2 has no monthly snapshots at all!\n")
self.assertEqual(return_info.returncode, 2)
def test_immediately_after_running_sanoid(self):
"""Test immediately after running sanoid --cron"""
run_sanoid_cron_command()
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"OK: all monitored datasets (sanoid-test-1, sanoid-test-2) have fresh snapshots\n")
self.assertEqual(return_info.returncode, 0)
def test_one_warning_hourly(self):
"""Test one warning on hourly snapshots, no critical warnings, to check output and error status"""
run_sanoid_cron_command()
# Advance 100 mins to trigger the hourly warning on sanoid-test-1 but nothing else
advance_time(100 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# WARN: sanoid-test-1 newest hourly snapshot is 1h 40m 0s old (should be < 1h 30m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
self.assertEqual(return_info.stdout[:49], b"WARN: sanoid-test-1 newest hourly snapshot is 1h ")
self.assertEqual(return_info.stdout[-30:], b"s old (should be < 1h 30m 0s)\n")
self.assertEqual(return_info.returncode, 1)
def test_two_criticals_hourly(self):
"""Test two criticals (hourly), to check output and error status"""
run_sanoid_cron_command()
# Advance 390 mins to trigger the hourly critical on both sanoid-test-1 and sanoid-test-2
advance_time(390 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# CRIT: sanoid-test-1 newest hourly snapshot is 6h 30m 1s old (should be < 6h 0m 0s), CRIT: sanoid-test-2 newest hourly snapshot is 6h 30m 1s old (should be < 6h 0m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
comma_location = return_info.stdout.find(b",")
self.assertEqual(return_info.stdout[:49], b"CRIT: sanoid-test-1 newest hourly snapshot is 6h ")
self.assertEqual(return_info.stdout[comma_location - 28:comma_location], b"s old (should be < 6h 0m 0s)")
self.assertEqual(return_info.stdout[comma_location:comma_location + 51], b", CRIT: sanoid-test-2 newest hourly snapshot is 6h ")
self.assertEqual(return_info.stdout[-29:], b"s old (should be < 6h 0m 0s)\n")
self.assertEqual(return_info.returncode, 2)
def test_two_criticals_hourly_two_warnings_daily(self):
"""Test two criticals (hourly) and two warnings (daily), to check output and error status"""
run_sanoid_cron_command()
# Advance more than 28 hours to trigger the daily warning on both sanoid-test-1 and sanoid-test-2
advance_time(29 * 60 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# CRIT: sanoid-test-1 newest hourly snapshot is 1d 5h 0m 0s old (should be < 6h 0m 0s), CRIT: sanoid-test-2 newest hourly snapshot is 1d 5h 0m 0s old (should be < 6h 0m 0s), WARN: sanoid-test-1 newest daily snapshot is 1d 5h 0m 0s old (should be < 1d 4h 0m 0s), WARN: sanoid-test-2 newest daily snapshot is 1d 5h 0m 0s old (should be < 1d 4h 0m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
print(return_info.stdout)
output_list = return_info.stdout.split(b", ")
self.assertEqual(output_list[0][:49], b"CRIT: sanoid-test-1 newest hourly snapshot is 1d ")
self.assertEqual(output_list[0][-28:], b"s old (should be < 6h 0m 0s)")
self.assertEqual(output_list[1][:49], b"CRIT: sanoid-test-2 newest hourly snapshot is 1d ")
self.assertEqual(output_list[1][-28:], b"s old (should be < 6h 0m 0s)")
self.assertEqual(output_list[2][:48], b"WARN: sanoid-test-1 newest daily snapshot is 1d ")
self.assertEqual(output_list[2][-31:], b"s old (should be < 1d 4h 0m 0s)")
self.assertEqual(output_list[3][:48], b"WARN: sanoid-test-2 newest daily snapshot is 1d ")
self.assertEqual(output_list[3][-32:], b"s old (should be < 1d 4h 0m 0s)\n")
self.assertEqual(return_info.returncode, 2)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,18 @@
#!/bin/bash
set -x
# this test will create pools in a number of states
# and check the output text and return code of
# sanoid --monitor-snapshots
. ../common/lib.sh
# prepare
setup
checkEnvironment
disableTimeSync
# set timezone
ln -sf /usr/share/zoneinfo/Europe/Vienna /etc/localtime
python3 test_monitoring.py

View File

@ -0,0 +1,41 @@
[sanoid-test-1]
use_template = production
[sanoid-test-2]
use_template = demo
[template_production]
hourly = 36
daily = 30
monthly = 3
yearly = 0
autosnap = yes
autoprune = no
hourly_warn = 90m
hourly_crit = 360m
daily_warn = 28h
daily_crit = 32h
weekly_warn = 0
weekly_crit = 0
monthly_warn = 32d
monthly_crit = 40d
yearly_warn = 0
yearly_crit = 0
monitor_dont_warn = true
monitor_dont_crit = Yes
[template_demo]
daily = 60
hourly_warn = 290m
hourly_crit = 360m
daily_warn = 28h
daily_crit = 48h
weekly_warn = 0
weekly_crit = 0
monthly_warn = 32d
monthly_crit = 40d
yearly_warn = 0
yearly_crit = 0
monitor_dont_warn = ON
monitor_dont_crit = 1

View File

@ -0,0 +1,163 @@
#!/usr/bin/env python3
# this software is licensed for use under the Free Software Foundation's GPL v3.0 license, as retrieved
# from http://www.gnu.org/licenses/gpl-3.0.html on 2014-11-17. A copy should also be available in this
# project's Git repository at https://github.com/jimsalterjrs/sanoid/blob/master/LICENSE.
import os
import subprocess
import time
import unittest
sanoid_cmd = os.environ.get("SANOID")
pool_disk_image1 = "/zpool1.img"
pool_name1 = "sanoid-test-1"
pool_disk_image2 = "/zpool2.img"
pool_name2 = "sanoid-test-2"
clk_id = time.CLOCK_REALTIME
starting_time = time.clock_gettime(clk_id)
def monitor_snapshots_command():
"""Runs sanoid --monitor-snapshots and returns a CompletedProcess instance"""
return_info = subprocess.run([sanoid_cmd, "--monitor-snapshots"], capture_output=True)
return return_info
def run_sanoid_cron_command():
"""Runs sanoid and returns a CompletedProcess instance"""
return_info = subprocess.run([sanoid_cmd, "--cron", "--verbose"], capture_output=True, check=True)
return return_info
def advance_time(seconds):
"""Advances the system clock by seconds"""
# Get the current time
clk_id = time.CLOCK_REALTIME
time_seconds = time.clock_gettime(clk_id)
# print("Current unix time is", time_seconds, "or", time.asctime(time.gmtime(time_seconds)), "in GMT")
# Set the clock to the current time plus seconds
time.clock_settime(clk_id, time_seconds + seconds)
# Print the new time
time_seconds = time.clock_gettime(clk_id)
# print("Current unix time is", time_seconds, "or", time.asctime(time.gmtime(time_seconds)), "in GMT")
return time_seconds
class TestMonitoringOutput(unittest.TestCase):
def test_no_zpool(self):
"""Test what happens if there is no zpool at all"""
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"CRIT: sanoid-test-1 has no daily snapshots at all!, CRIT: sanoid-test-1 has no hourly snapshots at all!, CRIT: sanoid-test-1 has no monthly snapshots at all!, CRIT: sanoid-test-2 has no daily snapshots at all!, CRIT: sanoid-test-2 has no hourly snapshots at all!, CRIT: sanoid-test-2 has no monthly snapshots at all!\n")
self.assertEqual(return_info.returncode, 0)
class TestsWithZpool(unittest.TestCase):
"""Tests that require a test zpool"""
def setUp(self):
"""Set up the zpool"""
subprocess.run(["truncate", "-s", "512M", pool_disk_image1], check=True)
subprocess.run(["zpool", "create", "-f", pool_name1, pool_disk_image1], check=True)
subprocess.run(["truncate", "-s", "512M", pool_disk_image2], check=True)
subprocess.run(["zpool", "create", "-f", pool_name2, pool_disk_image2], check=True)
# Clear the snapshot cache in between
subprocess.run([sanoid_cmd, "--force-update"])
def tearDown(self):
"""Clean up on either passed or failed tests"""
subprocess.run(["zpool", "export", pool_name1])
subprocess.run(["rm", "-f", pool_disk_image1])
subprocess.run(["zpool", "export", pool_name2])
subprocess.run(["rm", "-f", pool_disk_image2])
time.clock_settime(clk_id, starting_time)
def test_with_zpool_no_snapshots(self):
"""Test what happens if there is a zpool, but with no snapshots"""
# Run sanoid --monitor-snapshots before doing anything else
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"CRIT: sanoid-test-1 has no daily snapshots at all!, CRIT: sanoid-test-1 has no hourly snapshots at all!, CRIT: sanoid-test-1 has no monthly snapshots at all!, CRIT: sanoid-test-2 has no daily snapshots at all!, CRIT: sanoid-test-2 has no hourly snapshots at all!, CRIT: sanoid-test-2 has no monthly snapshots at all!\n")
self.assertEqual(return_info.returncode, 0)
def test_immediately_after_running_sanoid(self):
"""Test immediately after running sanoid --cron"""
run_sanoid_cron_command()
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"OK: all monitored datasets (sanoid-test-1, sanoid-test-2) have fresh snapshots\n")
self.assertEqual(return_info.returncode, 0)
def test_one_warning_hourly(self):
"""Test one warning on hourly snapshots, no critical warnings, to check output and error status"""
run_sanoid_cron_command()
# Advance 100 mins to trigger the hourly warning on sanoid-test-1 but nothing else
advance_time(100 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# WARN: sanoid-test-1 newest hourly snapshot is 1h 40m 0s old (should be < 1h 30m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
self.assertEqual(return_info.stdout[:49], b"WARN: sanoid-test-1 newest hourly snapshot is 1h ")
self.assertEqual(return_info.stdout[-30:], b"s old (should be < 1h 30m 0s)\n")
self.assertEqual(return_info.returncode, 0)
def test_two_criticals_hourly(self):
"""Test two criticals (hourly), to check output and error status"""
run_sanoid_cron_command()
# Advance 390 mins to trigger the hourly critical on both sanoid-test-1 and sanoid-test-2
advance_time(390 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# CRIT: sanoid-test-1 newest hourly snapshot is 6h 30m 1s old (should be < 6h 0m 0s), CRIT: sanoid-test-2 newest hourly snapshot is 6h 30m 1s old (should be < 6h 0m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
comma_location = return_info.stdout.find(b",")
self.assertEqual(return_info.stdout[:49], b"CRIT: sanoid-test-1 newest hourly snapshot is 6h ")
self.assertEqual(return_info.stdout[comma_location - 28:comma_location], b"s old (should be < 6h 0m 0s)")
self.assertEqual(return_info.stdout[comma_location:comma_location + 51], b", CRIT: sanoid-test-2 newest hourly snapshot is 6h ")
self.assertEqual(return_info.stdout[-29:], b"s old (should be < 6h 0m 0s)\n")
self.assertEqual(return_info.returncode, 0)
def test_two_criticals_hourly_two_warnings_daily(self):
"""Test two criticals (hourly) and two warnings (daily), to check output and error status"""
run_sanoid_cron_command()
# Advance more than 28 hours to trigger the daily warning on both sanoid-test-1 and sanoid-test-2
advance_time(29 * 60 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# CRIT: sanoid-test-1 newest hourly snapshot is 1d 5h 0m 0s old (should be < 6h 0m 0s), CRIT: sanoid-test-2 newest hourly snapshot is 1d 5h 0m 0s old (should be < 6h 0m 0s), WARN: sanoid-test-1 newest daily snapshot is 1d 5h 0m 0s old (should be < 1d 4h 0m 0s), WARN: sanoid-test-2 newest daily snapshot is 1d 5h 0m 0s old (should be < 1d 4h 0m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
print(return_info.stdout)
output_list = return_info.stdout.split(b", ")
self.assertEqual(output_list[0][:49], b"CRIT: sanoid-test-1 newest hourly snapshot is 1d ")
self.assertEqual(output_list[0][-28:], b"s old (should be < 6h 0m 0s)")
self.assertEqual(output_list[1][:49], b"CRIT: sanoid-test-2 newest hourly snapshot is 1d ")
self.assertEqual(output_list[1][-28:], b"s old (should be < 6h 0m 0s)")
self.assertEqual(output_list[2][:48], b"WARN: sanoid-test-1 newest daily snapshot is 1d ")
self.assertEqual(output_list[2][-31:], b"s old (should be < 1d 4h 0m 0s)")
self.assertEqual(output_list[3][:48], b"WARN: sanoid-test-2 newest daily snapshot is 1d ")
self.assertEqual(output_list[3][-32:], b"s old (should be < 1d 4h 0m 0s)\n")
self.assertEqual(return_info.returncode, 0)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,18 @@
#!/bin/bash
set -x
# this test will create pools in a number of states
# and check the output text and return code of
# sanoid --monitor-snapshots
. ../common/lib.sh
# prepare
setup
checkEnvironment
disableTimeSync
# set timezone
ln -sf /usr/share/zoneinfo/Europe/Vienna /etc/localtime
python3 test_monitoring.py

View File

@ -0,0 +1,41 @@
[sanoid-test-1]
use_template = production
[sanoid-test-2]
use_template = demo
[template_production]
hourly = 36
daily = 30
monthly = 3
yearly = 0
autosnap = yes
autoprune = no
hourly_warn = 90m
hourly_crit = 360m
daily_warn = 28h
daily_crit = 32h
weekly_warn = 0
weekly_crit = 0
monthly_warn = 32d
monthly_crit = 40d
yearly_warn = 0
yearly_crit = 0
monitor_dont_warn = FALSE
monitor_dont_crit = yes
[template_demo]
daily = 60
hourly_warn = 290m
hourly_crit = 360m
daily_warn = 28h
daily_crit = 48h
weekly_warn = 0
weekly_crit = 0
monthly_warn = 32d
monthly_crit = 40d
yearly_warn = 0
yearly_crit = 0
monitor_dont_warn = Off
monitor_dont_crit = On

View File

@ -0,0 +1,163 @@
#!/usr/bin/env python3
# this software is licensed for use under the Free Software Foundation's GPL v3.0 license, as retrieved
# from http://www.gnu.org/licenses/gpl-3.0.html on 2014-11-17. A copy should also be available in this
# project's Git repository at https://github.com/jimsalterjrs/sanoid/blob/master/LICENSE.
import os
import subprocess
import time
import unittest
sanoid_cmd = os.environ.get("SANOID")
pool_disk_image1 = "/zpool1.img"
pool_name1 = "sanoid-test-1"
pool_disk_image2 = "/zpool2.img"
pool_name2 = "sanoid-test-2"
clk_id = time.CLOCK_REALTIME
starting_time = time.clock_gettime(clk_id)
def monitor_snapshots_command():
"""Runs sanoid --monitor-snapshots and returns a CompletedProcess instance"""
return_info = subprocess.run([sanoid_cmd, "--monitor-snapshots"], capture_output=True)
return return_info
def run_sanoid_cron_command():
"""Runs sanoid and returns a CompletedProcess instance"""
return_info = subprocess.run([sanoid_cmd, "--cron", "--verbose"], capture_output=True, check=True)
return return_info
def advance_time(seconds):
"""Advances the system clock by seconds"""
# Get the current time
clk_id = time.CLOCK_REALTIME
time_seconds = time.clock_gettime(clk_id)
# print("Current unix time is", time_seconds, "or", time.asctime(time.gmtime(time_seconds)), "in GMT")
# Set the clock to the current time plus seconds
time.clock_settime(clk_id, time_seconds + seconds)
# Print the new time
time_seconds = time.clock_gettime(clk_id)
# print("Current unix time is", time_seconds, "or", time.asctime(time.gmtime(time_seconds)), "in GMT")
return time_seconds
class TestMonitoringOutput(unittest.TestCase):
def test_no_zpool(self):
"""Test what happens if there is no zpool at all"""
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"CRIT: sanoid-test-1 has no daily snapshots at all!, CRIT: sanoid-test-1 has no hourly snapshots at all!, CRIT: sanoid-test-1 has no monthly snapshots at all!, CRIT: sanoid-test-2 has no daily snapshots at all!, CRIT: sanoid-test-2 has no hourly snapshots at all!, CRIT: sanoid-test-2 has no monthly snapshots at all!\n")
self.assertEqual(return_info.returncode, 0)
class TestsWithZpool(unittest.TestCase):
"""Tests that require a test zpool"""
def setUp(self):
"""Set up the zpool"""
subprocess.run(["truncate", "-s", "512M", pool_disk_image1], check=True)
subprocess.run(["zpool", "create", "-f", pool_name1, pool_disk_image1], check=True)
subprocess.run(["truncate", "-s", "512M", pool_disk_image2], check=True)
subprocess.run(["zpool", "create", "-f", pool_name2, pool_disk_image2], check=True)
# Clear the snapshot cache in between
subprocess.run([sanoid_cmd, "--force-update"])
def tearDown(self):
"""Clean up on either passed or failed tests"""
subprocess.run(["zpool", "export", pool_name1])
subprocess.run(["rm", "-f", pool_disk_image1])
subprocess.run(["zpool", "export", pool_name2])
subprocess.run(["rm", "-f", pool_disk_image2])
time.clock_settime(clk_id, starting_time)
def test_with_zpool_no_snapshots(self):
"""Test what happens if there is a zpool, but with no snapshots"""
# Run sanoid --monitor-snapshots before doing anything else
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"CRIT: sanoid-test-1 has no daily snapshots at all!, CRIT: sanoid-test-1 has no hourly snapshots at all!, CRIT: sanoid-test-1 has no monthly snapshots at all!, CRIT: sanoid-test-2 has no daily snapshots at all!, CRIT: sanoid-test-2 has no hourly snapshots at all!, CRIT: sanoid-test-2 has no monthly snapshots at all!\n")
self.assertEqual(return_info.returncode, 0)
def test_immediately_after_running_sanoid(self):
"""Test immediately after running sanoid --cron"""
run_sanoid_cron_command()
return_info = monitor_snapshots_command()
self.assertEqual(return_info.stdout, b"OK: all monitored datasets (sanoid-test-1, sanoid-test-2) have fresh snapshots\n")
self.assertEqual(return_info.returncode, 0)
def test_one_warning_hourly(self):
"""Test one warning on hourly snapshots, no critical warnings, to check output and error status"""
run_sanoid_cron_command()
# Advance 100 mins to trigger the hourly warning on sanoid-test-1 but nothing else
advance_time(100 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# WARN: sanoid-test-1 newest hourly snapshot is 1h 40m 0s old (should be < 1h 30m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
self.assertEqual(return_info.stdout[:49], b"WARN: sanoid-test-1 newest hourly snapshot is 1h ")
self.assertEqual(return_info.stdout[-30:], b"s old (should be < 1h 30m 0s)\n")
self.assertEqual(return_info.returncode, 1)
def test_two_criticals_hourly(self):
"""Test two criticals (hourly), to check output and error status"""
run_sanoid_cron_command()
# Advance 390 mins to trigger the hourly critical on both sanoid-test-1 and sanoid-test-2
advance_time(390 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# CRIT: sanoid-test-1 newest hourly snapshot is 6h 30m 1s old (should be < 6h 0m 0s), CRIT: sanoid-test-2 newest hourly snapshot is 6h 30m 1s old (should be < 6h 0m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
comma_location = return_info.stdout.find(b",")
self.assertEqual(return_info.stdout[:49], b"CRIT: sanoid-test-1 newest hourly snapshot is 6h ")
self.assertEqual(return_info.stdout[comma_location - 28:comma_location], b"s old (should be < 6h 0m 0s)")
self.assertEqual(return_info.stdout[comma_location:comma_location + 51], b", CRIT: sanoid-test-2 newest hourly snapshot is 6h ")
self.assertEqual(return_info.stdout[-29:], b"s old (should be < 6h 0m 0s)\n")
self.assertEqual(return_info.returncode, 0)
def test_two_criticals_hourly_two_warnings_daily(self):
"""Test two criticals (hourly) and two warnings (daily), to check output and error status"""
run_sanoid_cron_command()
# Advance more than 28 hours to trigger the daily warning on both sanoid-test-1 and sanoid-test-2
advance_time(29 * 60 * 60)
return_info = monitor_snapshots_command()
# Output should be something like:
# CRIT: sanoid-test-1 newest hourly snapshot is 1d 5h 0m 0s old (should be < 6h 0m 0s), CRIT: sanoid-test-2 newest hourly snapshot is 1d 5h 0m 0s old (should be < 6h 0m 0s), WARN: sanoid-test-1 newest daily snapshot is 1d 5h 0m 0s old (should be < 1d 4h 0m 0s), WARN: sanoid-test-2 newest daily snapshot is 1d 5h 0m 0s old (should be < 1d 4h 0m 0s)\n
# But we cannot be sure about the exact time as test execution could be different on
# different machines, so ignore the bits that may be different.
print(return_info.stdout)
output_list = return_info.stdout.split(b", ")
self.assertEqual(output_list[0][:49], b"CRIT: sanoid-test-1 newest hourly snapshot is 1d ")
self.assertEqual(output_list[0][-28:], b"s old (should be < 6h 0m 0s)")
self.assertEqual(output_list[1][:49], b"CRIT: sanoid-test-2 newest hourly snapshot is 1d ")
self.assertEqual(output_list[1][-28:], b"s old (should be < 6h 0m 0s)")
self.assertEqual(output_list[2][:48], b"WARN: sanoid-test-1 newest daily snapshot is 1d ")
self.assertEqual(output_list[2][-31:], b"s old (should be < 1d 4h 0m 0s)")
self.assertEqual(output_list[3][:48], b"WARN: sanoid-test-2 newest daily snapshot is 1d ")
self.assertEqual(output_list[3][-32:], b"s old (should be < 1d 4h 0m 0s)\n")
self.assertEqual(return_info.returncode, 1)
if __name__ == '__main__':
unittest.main()

31
tests/README.md Normal file
View File

@ -0,0 +1,31 @@
### Requirements ###
Tests must be run inside a virtual machine. This is for your own safety, as the tests may create and destroy zpools etc.
A VM with 35GB of storage and 8 cores running Ubuntu 20.04 completes the tests in about 5 hours.
#### Packages ####
The tests require the following packages to be installed in the VM (Ubuntu 20.04 package names are used, translate as appropriate):
```
zfsutils-linux
libconfig-inifiles-perl
libcapture-tiny-perl
```
```
apt install zfsutils-linux libconfig-inifiles-perl libcapture-tiny-perl
```
#### Install sanoid within the VM ####
Install sanoid within the VM, for example
```
apt install git
git clone https://github.com/jimsalterjrs/sanoid.git
mkdir /etc/sanoid/
cp sanoid/sanoid.defaults.conf /etc/sanoid/
```
### Run the tests ##
This requires root/sudo privileges.
```
cd sanoid/tests/
./run-tests.sh
```

View File

@ -10,7 +10,7 @@ function setup {
export SANOID="../../sanoid"
# make sure that there is no cache file
rm -f /var/cache/sanoidsnapshots.txt
rm -f /var/cache/sanoid/snapshots.txt
# install needed sanoid configuration files
[ -f sanoid.conf ] && cp sanoid.conf /etc/sanoid/sanoid.conf

View File

@ -1,6 +1,6 @@
#!/bin/bash
# run's all the available tests
# runs all the available tests
for test in $(find . -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -g); do
if [ ! -x "${test}/run.sh" ]; then