-
Notifications
You must be signed in to change notification settings - Fork 77
Add test_image_checker in to checkbox_support to check the image (New) #911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
checkbox-support/checkbox_support/scripts/image_checker.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Copyright 2022 Canonical Ltd. | ||
| # All rights reserved. | ||
| # | ||
| # Written by: | ||
| # Patrick Chang <patrick.chang@canonical.com> | ||
|
|
||
| import argparse | ||
| from os.path import exists | ||
| from checkbox_support.snap_utils.system import on_ubuntucore | ||
|
|
||
|
|
||
| def get_type(): | ||
| """ | ||
| Return the type of image. | ||
| """ | ||
| return "core" if on_ubuntucore() else "classic" | ||
|
|
||
|
|
||
| def get_source(): | ||
| """ | ||
| Return the source of image. | ||
| """ | ||
| is_oem_source = False | ||
|
|
||
| if get_type() == "core": | ||
| try: | ||
| with open("/run/mnt/ubuntu-seed/.disk/info") as file: | ||
| lines = file.readlines() | ||
| # Only one timestamp such as 20221021.4 if it's stock image | ||
| # There're three lines in info file if it's oem image | ||
| is_oem_source = len(lines) > 1 | ||
| except FileNotFoundError as e: | ||
| print(e) | ||
| return "unknown" | ||
| else: | ||
| is_oem_source = exists("/var/lib/ubuntu_dist_channel") | ||
|
|
||
| return 'oem' if is_oem_source else "stock" | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-t", "--type", action="store_true") | ||
| parser.add_argument("-s", "--source", action="store_true") | ||
| args = parser.parse_args() | ||
|
|
||
| if args.type: | ||
| print("type: {}".format(get_type())) | ||
| if args.source: | ||
| print("source: {}".format(get_source())) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
68 changes: 68 additions & 0 deletions
68
checkbox-support/checkbox_support/tests/test_image_checker.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # This file is part of Checkbox. | ||
| # | ||
| # Copyright 2024 Canonical Ltd. | ||
| # Written by: | ||
| # Vincent Liao <vincent.liao@canonical.com> | ||
| # | ||
| # Checkbox is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License version 3, | ||
| # as published by the Free Software Foundation. | ||
| # | ||
| # Checkbox is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with Checkbox. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| import unittest | ||
| from unittest.mock import patch, mock_open | ||
| from checkbox_support.scripts.image_checker import get_type, get_source, main | ||
| from io import StringIO | ||
|
|
||
|
|
||
| class ImageCheckerTest(unittest.TestCase): | ||
|
|
||
| @patch("checkbox_support.scripts.image_checker.on_ubuntucore") | ||
| def test_get_type(self, mock_on_ubuntu_core): | ||
| mock_on_ubuntu_core.return_value = True | ||
| self.assertEqual(get_type(), "core") | ||
|
|
||
| mock_on_ubuntu_core.return_value = False | ||
| self.assertEqual(get_type(), "classic") | ||
|
|
||
| @patch("checkbox_support.scripts.image_checker.exists") | ||
| @patch("checkbox_support.scripts.image_checker.get_type") | ||
| def test_get_source(self, mock_get_type, mock_exists): | ||
| # Test when it is core image | ||
| mock_get_type.return_value = "core" | ||
| mock_oem_info = ("Sun, 20 June 1999 66:66:66 +7777\n" | ||
| "Somethingcool\n" | ||
| "iot-vincent-core-24-x24-2023-10-31-24\n") | ||
| with patch("builtins.open", mock_open(read_data=mock_oem_info)): | ||
| self.assertEqual(get_source(), "oem") | ||
|
|
||
| mock_stock_info = "20231031.6" | ||
| with patch("builtins.open", mock_open(read_data=mock_stock_info)): | ||
| self.assertEqual(get_source(), "stock") | ||
|
|
||
| with patch("builtins.open", side_effect=FileNotFoundError): | ||
| self.assertEqual(get_source(), "unknown") | ||
|
|
||
| # Test when it is classic image | ||
| mock_get_type.return_value = "classic" | ||
| mock_exists.return_value = True | ||
| self.assertEqual(get_source(), "oem") | ||
|
|
||
| mock_exists.return_value = False | ||
| self.assertEqual(get_source(), "stock") | ||
|
|
||
| @patch("sys.argv", ["script_name.py", "--type", "--source"]) | ||
| @patch("checkbox_support.scripts.image_checker.get_source") | ||
| @patch("checkbox_support.scripts.image_checker.get_type") | ||
| @patch("sys.stdout", new_callable=StringIO) | ||
| def test_main(self, mock_stdout, mock_get_type, mock_get_source): | ||
| main() | ||
| mock_get_type.assert_called_with() | ||
| mock_get_source.assert_called_with() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.