Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_pkgin.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import unittest
8 from unittest import mock
9
10 from ansible_collections.community.general.plugins.modules import pkgin
11
12
13 class TestPkginQueryPackage(unittest.TestCase):
14 def setUp(self):
15 pkgin.PKGIN_PATH = ""
16
17 @mock.patch("ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule")
18 def test_package_without_version_is_present(self, mock_module):
19 # given
20 package = "py37-conan"
21 parseable_flag_not_supported = 1
22 mock_module.run_command.side_effect = [
23 (parseable_flag_not_supported, "pkgin 0.11.7 for Darwin-18.6.0 x86_64 (using SQLite 3.27.2)", None),
24 (0, f"{package}-1.21.0 = C/C++ package manager", None),
25 ]
26
27 # when
28 command_result = pkgin.query_package(mock_module, package)
29
30 # then
31 self.assertEqual(command_result, pkgin.PackageState.PRESENT)
32
33 @mock.patch("ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule")
34 def test_package_with_version_is_present(self, mock_module):
35 # given
36 package = "py37-conan-1.21.0"
37 parseable_flag_not_supported = 1
38 mock_module.run_command.side_effect = [
39 (parseable_flag_not_supported, "pkgin 0.11.7 for Darwin-18.6.0 x86_64 (using SQLite 3.27.2)", None),
40 (0, f"{package} = C/C++ package manager", None),
41 ]
42
43 # when
44 command_result = pkgin.query_package(mock_module, package)
45
46 # then
47 self.assertEqual(command_result, pkgin.PackageState.PRESENT)
48
49 @mock.patch("ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule")
50 def test_package_found_but_not_installed(self, mock_module):
51 # given
52 package = "cmake"
53 parseable_flag_not_supported = 1
54 mock_module.run_command.side_effect = [
55 (parseable_flag_not_supported, "pkgin 0.11.7 for Darwin-18.6.0 x86_64 (using SQLite 3.27.2)", None),
56 (
57 0,
58 "cmake316-3.16.0nb1 = Cross platform make\ncmake314-3.14.6nb1 = Cross platform make\ncmake-3.14.0 Cross platform make",
59 None,
60 ),
61 ]
62
63 # when
64 command_result = pkgin.query_package(mock_module, package)
65
66 # then
67 self.assertEqual(command_result, pkgin.PackageState.NOT_INSTALLED)
68
69 @mock.patch("ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule")
70 def test_package_found_outdated(self, mock_module):
71 # given
72 package = "cmake316"
73 parseable_flag_not_supported = 1
74 mock_module.run_command.side_effect = [
75 (parseable_flag_not_supported, "pkgin 0.11.7 for Darwin-18.6.0 x86_64 (using SQLite 3.27.2)", None),
76 (0, "cmake316-3.16.0nb1 < Cross platform make", None),
77 ]
78
79 # when
80 command_result = pkgin.query_package(mock_module, package)
81
82 # then
83 self.assertEqual(command_result, pkgin.PackageState.OUTDATED)
84
85 @mock.patch("ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule")
86 def test_package_with_version_found_outdated(self, mock_module):
87 # given
88 package = "cmake316-3.16.0nb1"
89 parseable_flag_not_supported = 1
90 mock_module.run_command.side_effect = [
91 (parseable_flag_not_supported, "pkgin 0.11.7 for Darwin-18.6.0 x86_64 (using SQLite 3.27.2)", None),
92 (0, "cmake316-3.16.0nb1 < Cross platform make", None),
93 ]
94
95 # when
96 command_result = pkgin.query_package(mock_module, package)
97
98 # then
99 self.assertEqual(command_result, pkgin.PackageState.OUTDATED)
100
101 @mock.patch("ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule")
102 def test_package_not_found(self, mock_module):
103 # given
104 package = "cmake320-3.20.0nb1"
105 parseable_flag_not_supported = 1
106 mock_module.run_command.side_effect = [
107 (parseable_flag_not_supported, "pkgin 0.11.7 for Darwin-18.6.0 x86_64 (using SQLite 3.27.2)", None),
108 (1, None, f"No results found for {package}"),
109 ]
110
111 # when
112 command_result = pkgin.query_package(mock_module, package)
113
114 # then
115 self.assertEqual(command_result, pkgin.PackageState.NOT_FOUND)
116
117 @mock.patch("ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule")
118 def test_with_parseable_flag_supported_package_is_present(self, mock_module):
119 # given
120 package = "py37-conan"
121 parseable_flag_supported = 0
122 mock_module.run_command.side_effect = [
123 (parseable_flag_supported, "pkgin 0.11.7 for Darwin-18.6.0 x86_64 (using SQLite 3.27.2)", None),
124 (0, f"{package}-1.21.0;=;C/C++ package manager", None),
125 ]
126
127 # when
128 command_result = pkgin.query_package(mock_module, package)
129
130 # then
131 self.assertEqual(command_result, pkgin.PackageState.PRESENT)
132
133 @mock.patch("ansible_collections.community.general.plugins.modules.pkgin.AnsibleModule")
134 def test_with_parseable_flag_not_supported_package_is_present(self, mock_module):
135 # given
136 package = "py37-conan"
137 parseable_flag_not_supported = 1
138 mock_module.run_command.side_effect = [
139 (parseable_flag_not_supported, "pkgin 0.11.7 for Darwin-18.6.0 x86_64 (using SQLite 3.27.2)", None),
140 (0, f"{package}-1.21.0 = C/C++ package manager", None),
141 ]
142
143 # when
144 command_result = pkgin.query_package(mock_module, package)
145
146 # then
147 self.assertEqual(command_result, pkgin.PackageState.PRESENT)