From 9b0ed7c627a4e441defa06ccc5bb1f74a98cc089 Mon Sep 17 00:00:00 2001 From: Pierre Equoy Date: Wed, 18 Oct 2023 19:49:20 +0800 Subject: [PATCH] WIP: rewrite unit tests for snap_update_test script --- .../tests/test_data/snap_update_test_data.py | 10 +- providers/base/tests/test_snap_update_test.py | 432 ++++++++++-------- 2 files changed, 248 insertions(+), 194 deletions(-) diff --git a/providers/base/tests/test_data/snap_update_test_data.py b/providers/base/tests/test_data/snap_update_test_data.py index 94bd777b4..9be308740 100644 --- a/providers/base/tests/test_data/snap_update_test_data.py +++ b/providers/base/tests/test_data/snap_update_test_data.py @@ -1,3 +1,5 @@ +from pathlib import Path + snapd_list_sample = [ { "channel": "22/stable", @@ -296,10 +298,10 @@ ] snapd_seed_glob_data = [ - "/var/lib/snapd/seed/snaps/firefox_2605.snap", - "/var/lib/snapd/seed/snaps/snapd_19267.snap", - "/var/lib/snapd/seed/snaps/pc-kernel_1289.snap", - "/var/lib/snapd/seed/snaps/core22_617.snap", + Path("/var/lib/snapd/seed/snaps/firefox_2605.snap"), + Path("/var/lib/snapd/seed/snaps/snapd_19267.snap"), + Path("/var/lib/snapd/seed/snaps/pc-kernel_1289.snap"), + Path("/var/lib/snapd/seed/snaps/core22_617.snap"), ] snapd_list_firefox_snap = { diff --git a/providers/base/tests/test_snap_update_test.py b/providers/base/tests/test_snap_update_test.py index 14c938789..57be7eeac 100644 --- a/providers/base/tests/test_snap_update_test.py +++ b/providers/base/tests/test_snap_update_test.py @@ -1,7 +1,7 @@ import io import logging import unittest -from unittest.mock import patch, mock_open +from unittest.mock import patch, mock_open, MagicMock import snap_update_test @@ -20,126 +20,149 @@ class SnapUpdateTests(unittest.TestCase): def test_guess_snaps(self, mock_snapd_list): mock_snapd_list.return_value = snapd_list_sample snaps = snap_update_test.guess_snaps() - expected_snaps = {"kernel": "pi-kernel", "snapd": "snapd", "gadget": "pi"} + expected_snaps = ["pi-kernel", "snapd", "pi"] self.assertEqual(snaps, expected_snaps) @patch("snap_update_test.Snapd.list") def test_guess_snaps_nothing(self, mock_snapd_list): mock_snapd_list.return_value = snapd_list_no_kernel_snapd_gadget_snap snaps = snap_update_test.guess_snaps() - self.assertEqual(snaps, {}) + self.assertEqual(snaps, []) - @patch("snap_update_test.glob") - def test_get_snap_base_rev(self, mock_glob): + @patch("snap_update_test.Path.glob") + def test_get_snaps_base_rev(self, mock_glob): mock_glob.return_value = snapd_seed_glob_data - snap_rev = snap_update_test.get_snap_base_rev() + snap_rev = snap_update_test.get_snaps_base_rev() self.assertEqual(len(snap_rev), 4) self.assertEqual(snap_rev["pc-kernel"], "1289") - @patch("snap_update_test.get_snap_base_rev") - @patch("snap_update_test.Snapd.list") - @patch("snap_update_test.Snapd.find") - def test_get_snap_info(self, mock_snapd_find, mock_snapd_list, mock_base_revs): + @patch("builtins.open", new_callable=mock_open) + def test_load_change_info_file_not_found(self, mock_open): + mock_open.side_effect = FileNotFoundError() + logging.disable(logging.CRITICAL) + with self.assertRaises(SystemExit): + snap_update_test.load_change_info("/file/not/found") + + @patch("builtins.open", new_callable=mock_open) + @patch("snap_update_test.json") + def test_load_change_info(self, mock_json, mock_open): + snap_update_test.load_change_info("test") + mock_json.load.assert_called_once() + +class SnapInfoTests(unittest.TestCase): + @patch("snap_update_test.get_snaps_base_rev") + @patch("snap_update_test.Snapd") + def test_init(self, mock_snapd, mock_base_revs): mock_base_revs.return_value = {"firefox": "2605"} - mock_snapd_list.return_value = snapd_list_firefox_snap - mock_snapd_find.return_value = snapd_find_firefox_snap - expected_snap_info = { - "installed_revision": "3026", - "base_revision": "2605", - "name": "firefox", - "type": "app", - "revisions": { - "esr/candidate": "3052", - "esr/stable": "3052", - "latest/beta": "3099", - "latest/candidate": "3068", - "latest/edge": "3102", - "latest/stable": "3068", - }, - "tracking_channel": "latest/stable", - "tracking_prefix": "latest/", - } - - snap_info = snap_update_test.get_snap_info("firefox") - self.assertEqual(snap_info, expected_snap_info) - - @patch("snap_update_test.get_snap_info") + mock_snapd.return_value.list.return_value = snapd_list_firefox_snap + mock_snapd.return_value.find.return_value = snapd_find_firefox_snap + + snap_info = snap_update_test.SnapInfo("firefox") + self.assertEqual(snap_info.installed_revision, "3026") + self.assertEqual(snap_info.base_revision, "2605") + self.assertEqual(snap_info.tracking_channel, "latest/stable") + self.assertEqual(snap_info.tracking_prefix, "latest/") + self.assertEqual(snap_info.stable_revision, "3068") + self.assertEqual(snap_info.candidate_revision, "3068") + self.assertEqual(snap_info.beta_revision, "3099") + self.assertEqual(snap_info.edge_revision, "3102") + @patch("sys.stdout", new_callable=io.StringIO) - def test_print_resource_info(self, mock_stdout, mock_snap_info): - mock_snap_info.return_value = snap_info_pi_kernel - expected_output = ( - "name: pi-kernel\ntype: kernel\n" - "tracking: 22/stable\nbase_rev: 567\nstable_rev: 658\n" - "candidate_rev: 663\nbeta_rev: 663\nedge_rev: 663\n" - "original_installed_rev: 567\n\n" - ) - snap_update_test.print_resource_info() - self.assertEqual(mock_stdout.getvalue(), expected_output) + def test_print_as_resource(self, mock_stdout): + mock_self = MagicMock() + snap_update_test.SnapInfo.print_as_resource(mock_self) + assert "name:" in mock_stdout.getvalue() + assert "type:" in mock_stdout.getvalue() + assert "tracking:" in mock_stdout.getvalue() + assert "base_rev:" in mock_stdout.getvalue() + assert "stable_rev:" in mock_stdout.getvalue() + assert "candidate_rev:" in mock_stdout.getvalue() + assert "beta_rev:" in mock_stdout.getvalue() + assert "edge_rev:" in mock_stdout.getvalue() + assert "original_installed_rev:" in mock_stdout.getvalue() + # Make sure there is a blank line at the end, as this is required by + # Checkbox resource jobs + assert mock_stdout.getvalue().endswith("\n\n") class SnapRefreshRevertTests(unittest.TestCase): - @patch("snap_update_test.Snapd") - @patch("snap_update_test.get_snap_info") - def test_snap_refresh_same_revision(self, mock_snap_info, mock_snapd): - mock_snap_info.return_value = {"installed_revision": "132"} - srr = snap_update_test.SnapRefreshRevert( - name="test", rev="132", info_path="/test/info" - ) + + # @patch("snap_update_test.SnapInfo") + # def test_snap_refresh_same_revision(self, mock_snap_info): + # mock_snap_info.return_value.installed_revision = "132" + # srr = snap_update_test.SnapRefreshRevert( + # name="test", revision="132", info_path="/test/info" + # ) + # logging.disable(logging.CRITICAL) + # with self.assertRaises(SystemExit): + # srr.snap_refresh() + def test_snap_refresh_same_revision(self): + mock_self = MagicMock() + mock_self.revision = "1" + mock_snap_info = MagicMock() + mock_snap_info.installed_revision = "1" + mock_self.snap_info = mock_snap_info logging.disable(logging.CRITICAL) - self.assertEqual(srr.snap_refresh(), 1) + with self.assertRaises(SystemExit): + snap_update_test.SnapRefreshRevert.snap_refresh(mock_self) - @patch("builtins.open", new_callable=mock_open) @patch("snap_update_test.Snapd.refresh") - @patch("snap_update_test.get_snap_info") - def test_snap_refresh_different_revision( - self, mock_snap_info, mock_snapd_refresh, mock_file - ): - mock_snap_info.return_value = { - "installed_revision": "132", - "tracking_channel": "22/beta", - } - mock_snapd_refresh.return_value = {"change": "1"} - srr = snap_update_test.SnapRefreshRevert( - name="test", rev="137", info_path="/test/info" - ) - self.assertEqual(srr.snap_refresh(), 0) + @patch("snap_update_test.save_change_info") + def test_snap_refresh_different_revision(self, mock_save_change_info, mock_snapd_refresh): + mock_self = MagicMock() + mock_self.revision = "1" + mock_snap_info = MagicMock() + mock_snap_info.installed_revision = "2" + mock_self.snap_info = mock_snap_info + snap_update_test.SnapRefreshRevert.snap_refresh(mock_self) + snap_update_test.save_change_info.assert_called_once() + # @patch("builtins.open", new_callable=mock_open) + # @patch("snap_update_test.Snapd.refresh") + # @patch("snap_update_test.SnapInfo") + # def test_snap_refresh_different_revision( + # self, mock_snap_info, mock_snapd_refresh, mock_file + # ): + # mock_snap_info.installed_revision.return_value = "132" + # mock_snap_info.tracking_channel.return_value = "22/beta" + # mock_snapd_refresh.return_value = {"change": "1"} + # srr = snap_update_test.SnapRefreshRevert( + # name="test", revision="137", info_path="/test/info" + # ) + # self.assertEqual(srr.snap_refresh(), 0) - @patch("builtins.open", new_callable=mock_open) - @patch("snap_update_test.Snapd.list") - @patch("snap_update_test.Snapd.change") - @patch("snap_update_test.json.load") - @patch("snap_update_test.get_snap_info") - def test_verify_refresh_ok( - self, - mock_snap_info, - mock_json_load, - mock_snapd_change, - mock_snapd_list, - mock_file, - ): - mock_snap_info.return_value = { - "name": "test-snap", - "installed_revision": "132", - "tracking_channel": "22/beta", - } - mock_json_load.return_value = { - "refresh_id": "1", - "name": "test-snap", - "destination_revision": "2", - } - mock_snapd_change.return_value = "Done" - mock_snapd_list.return_value = {"revision": "2"} - srr = snap_update_test.SnapRefreshRevert( - name="test-snap", rev="2", info_path="/test/info" - ) - self.assertEqual(srr.verify_refresh(), 0) + @patch("snap_update_test.Snapd.revert") + @patch("snap_update_test.load_change_info") + @patch("snap_update_test.save_change_info") + def test_snap_revert(self, mock_save_change_info, mock_load_change_info, mock_snapd_revert): + mock_self = MagicMock() + mock_snap_info = MagicMock() + mock_self.snap_info = mock_snap_info + snap_update_test.SnapRefreshRevert.snap_revert(mock_self) + snap_update_test.load_change_info.assert_called_once() + snap_update_test.save_change_info.assert_called_once() + + # mock_file_data = ( + # '{"name": "test-snap", "original_revision": "1", ' + # '"destination_revision": "2", "change_id": "80"}' + # ) + # mock_snapd_revert.return_value = {"change": 99} + # srr = snap_update_test.SnapRefreshRevert( + # name="test-snap", revision="2", info_path="/test/info" + # ) + # mock_snap_info.name.return_value = "test-snap" + # mock_snap_info.installed_revision.return_value = "132" + # mock_snap_info.tracking_channel.return_value = "22/beta" + # with patch("builtins.open", mock_open(read_data=mock_file_data)) as m: + # srr.snap_revert() + # self.assertTrue(mock_snapd_revert.called) + # m.assert_called_with("/test/info", "w") @patch("builtins.open", new_callable=mock_open) @patch("snap_update_test.Snapd.list") @patch("snap_update_test.Snapd.change") @patch("snap_update_test.json.load") - @patch("snap_update_test.get_snap_info") - def test_verify_refresh_nok( + @patch("snap_update_test.SnapInfo") + def test_verify_invalid( self, mock_snap_info, mock_json_load, @@ -147,103 +170,132 @@ def test_verify_refresh_nok( mock_snapd_list, mock_file, ): - mock_snap_info.return_value = { - "name": "test-snap", - "installed_revision": "132", - "tracking_channel": "22/beta", - } - mock_json_load.return_value = { - "refresh_id": "1", - "name": "test-snap", - "destination_revision": "2", - } - mock_snapd_change.return_value = "Done" - mock_snapd_list.return_value = {"revision": "1"} srr = snap_update_test.SnapRefreshRevert( - name="test-snap", rev="2", info_path="/test/info" + name="test-snap", revision="2", info_path="/test/info" ) + with self.assertRaises(SystemExit): + srr.verify("invalid") - logging.disable(logging.CRITICAL) - self.assertEqual(srr.verify_refresh(), 1) + # @patch("builtins.open", new_callable=mock_open) + # @patch("snap_update_test.Snapd.list") + # @patch("snap_update_test.Snapd.change") + # @patch("snap_update_test.json.load") + # @patch("snap_update_test.SnapInfo.get_snap_info") + # def test_verify_refresh_ok( + # self, + # mock_snap_info, + # mock_json_load, + # mock_snapd_change, + # mock_snapd_list, + # mock_file, + # ): + # mock_snap_info.return_value = { + # "name": "test-snap", + # "installed_revision": "132", + # "tracking_channel": "22/beta", + # } + # mock_json_load.return_value = { + # "refresh_id": "1", + # "name": "test-snap", + # "destination_revision": "2", + # } + # mock_snapd_change.return_value = "Done" + # mock_snapd_list.return_value = {"revision": "2"} + # srr = snap_update_test.SnapRefreshRevert( + # name="test-snap", revision="2", info_path="/test/info" + # ) + # self.assertEqual(srr.verify("refresh"), 0) - @patch("builtins.open", new_callable=mock_open) - @patch("snap_update_test.Snapd.list") - @patch("snap_update_test.Snapd.change") - @patch("snap_update_test.json.load") - @patch("snap_update_test.get_snap_info") - def test_verify_revert_ok( - self, - mock_snap_info, - mock_json_load, - mock_snapd_change, - mock_snapd_list, - mock_file, - ): - mock_snap_info.return_value = { - "name": "test-snap", - "installed_revision": "132", - "tracking_channel": "22/beta", - } - mock_json_load.return_value = { - "revert_id": "1", - "name": "test-snap", - "original_revision": "2", - } - mock_snapd_change.return_value = "Done" - mock_snapd_list.return_value = {"revision": "2"} - srr = snap_update_test.SnapRefreshRevert( - name="test-snap", rev="2", info_path="/test/info" - ) - self.assertEqual(srr.verify_revert(), 0) + # @patch("builtins.open", new_callable=mock_open) + # @patch("snap_update_test.Snapd.list") + # @patch("snap_update_test.Snapd.change") + # @patch("snap_update_test.json.load") + # @patch("snap_update_test.SnapInfo.get_snap_info") + # def test_verify_refresh_nok( + # self, + # mock_snap_info, + # mock_json_load, + # mock_snapd_change, + # mock_snapd_list, + # mock_file, + # ): + # mock_snap_info.return_value = { + # "name": "test-snap", + # "installed_revision": "132", + # "tracking_channel": "22/beta", + # } + # mock_json_load.return_value = { + # "refresh_id": "1", + # "name": "test-snap", + # "destination_revision": "2", + # } + # mock_snapd_change.return_value = "Done" + # mock_snapd_list.return_value = {"revision": "1"} + # srr = snap_update_test.SnapRefreshRevert( + # name="test-snap", revision="2", info_path="/test/info" + # ) - @patch("builtins.open", new_callable=mock_open) - @patch("snap_update_test.Snapd.list") - @patch("snap_update_test.Snapd.change") - @patch("snap_update_test.json.load") - @patch("snap_update_test.get_snap_info") - def test_verify_revert_nok( - self, - mock_snap_info, - mock_json_load, - mock_snapd_change, - mock_snapd_list, - mock_file, - ): - mock_snap_info.return_value = { - "name": "test-snap", - "installed_revision": "132", - "tracking_channel": "22/beta", - } - mock_json_load.return_value = { - "revert_id": "1", - "name": "test-snap", - "original_revision": "2", - } - mock_snapd_change.return_value = "Done" - mock_snapd_list.return_value = {"revision": "1"} - srr = snap_update_test.SnapRefreshRevert( - name="test-snap", rev="2", info_path="/test/info" - ) - logging.disable(logging.CRITICAL) - self.assertEqual(srr.verify_revert(), 1) + # logging.disable(logging.CRITICAL) + # self.assertEqual(srr.verify_refresh(), 1) + + # @patch("builtins.open", new_callable=mock_open) + # @patch("snap_update_test.Snapd.list") + # @patch("snap_update_test.Snapd.change") + # @patch("snap_update_test.json.load") + # @patch("snap_update_test.SnapInfo.get_snap_info") + # def test_verify_revert_ok( + # self, + # mock_snap_info, + # mock_json_load, + # mock_snapd_change, + # mock_snapd_list, + # mock_file, + # ): + # mock_snap_info.return_value = { + # "name": "test-snap", + # "installed_revision": "132", + # "tracking_channel": "22/beta", + # } + # mock_json_load.return_value = { + # "revert_id": "1", + # "name": "test-snap", + # "original_revision": "2", + # } + # mock_snapd_change.return_value = "Done" + # mock_snapd_list.return_value = {"revision": "2"} + # srr = snap_update_test.SnapRefreshRevert( + # name="test-snap", revision="2", info_path="/test/info" + # ) + # self.assertEqual(srr.verify_revert(), 0) + + # @patch("builtins.open", new_callable=mock_open) + # @patch("snap_update_test.Snapd.list") + # @patch("snap_update_test.Snapd.change") + # @patch("snap_update_test.json.load") + # @patch("snap_update_test.SnapInfo.get_snap_info") + # def test_verify_revert_nok( + # self, + # mock_snap_info, + # mock_json_load, + # mock_snapd_change, + # mock_snapd_list, + # mock_file, + # ): + # mock_snap_info.return_value = { + # "name": "test-snap", + # "installed_revision": "132", + # "tracking_channel": "22/beta", + # } + # mock_json_load.return_value = { + # "revert_id": "1", + # "name": "test-snap", + # "original_revision": "2", + # } + # mock_snapd_change.return_value = "Done" + # mock_snapd_list.return_value = {"revision": "1"} + # srr = snap_update_test.SnapRefreshRevert( + # name="test-snap", revision="2", info_path="/test/info" + # ) + # logging.disable(logging.CRITICAL) + # self.assertEqual(srr.verify_revert(), 1) - @patch("snap_update_test.Snapd.revert") - @patch("snap_update_test.get_snap_info") - def test_snap_revert(self, mock_snap_info, mock_snapd_revert): - mock_file_data = ( - '{"name": "test-snap", "original_revision": "10", ' - '"destination_revision": "20", "refresh_id": "80"}' - ) - mock_snapd_revert.return_value = {"change": 99} - srr = snap_update_test.SnapRefreshRevert( - name="test-snap", rev="2", info_path="/test/info" - ) - mock_snap_info.return_value = { - "name": "test-snap", - "installed_revision": "132", - "tracking_channel": "22/beta", - } - with patch("builtins.open", mock_open(read_data=mock_file_data)) as m: - srr.snap_revert() - self.assertTrue(mock_snapd_revert.called) - m.assert_called_with("/test/info", "w")