Browse code

Detect Homebrew on Mac M1 (Apple Silicon) (#74378) (#74400)

Homebrew's default install location for macOS on ARM is /opt/homebrew.
Source: https://docs.brew.sh/FAQ

On a Mac M1 (Apple Silicon), homebrew will be installed at
/opt/homebrew/bin/brew.

Fernando Correia authored on 2021/04/27 00:20:33
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+---
1
+bugfixes:
2
+  - facts - detect homebrew installed at /opt/homebrew/bin/brew
... ...
@@ -25,6 +25,7 @@ PKG_MGRS = [{'path': '/usr/bin/yum', 'name': 'yum'},
25 25
             {'path': '/opt/tools/bin/pkgin', 'name': 'pkgin'},
26 26
             {'path': '/opt/local/bin/port', 'name': 'macports'},
27 27
             {'path': '/usr/local/bin/brew', 'name': 'homebrew'},
28
+            {'path': '/opt/homebrew/bin/brew', 'name': 'homebrew'},
28 29
             {'path': '/sbin/apk', 'name': 'apk'},
29 30
             {'path': '/usr/sbin/pkg', 'name': 'pkgng'},
30 31
             {'path': '/usr/sbin/swlist', 'name': 'swdepot'},
... ...
@@ -238,6 +238,46 @@ class TestPkgMgrFacts(BaseFactsTest):
238 238
         self.assertIn('pkg_mgr', facts_dict)
239 239
 
240 240
 
241
+class TestMacOSXPkgMgrFacts(BaseFactsTest):
242
+    __test__ = True
243
+    gather_subset = ['!all', 'pkg_mgr']
244
+    valid_subsets = ['pkg_mgr']
245
+    fact_namespace = 'ansible_pkgmgr'
246
+    collector_class = PkgMgrFactCollector
247
+    collected_facts = {
248
+        "ansible_distribution": "MacOSX",
249
+        "ansible_distribution_major_version": "11",
250
+        "ansible_os_family": "Darwin"
251
+    }
252
+
253
+    @patch('ansible.module_utils.facts.system.pkg_mgr.os.path.exists', side_effect=lambda x: x == '/opt/homebrew/bin/brew')
254
+    def test_collect_opt_homebrew(self, p_exists):
255
+        module = self._mock_module()
256
+        fact_collector = self.collector_class()
257
+        facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts)
258
+        self.assertIsInstance(facts_dict, dict)
259
+        self.assertIn('pkg_mgr', facts_dict)
260
+        self.assertEqual(facts_dict['pkg_mgr'], 'homebrew')
261
+
262
+    @patch('ansible.module_utils.facts.system.pkg_mgr.os.path.exists', side_effect=lambda x: x == '/usr/local/bin/brew')
263
+    def test_collect_usr_homebrew(self, p_exists):
264
+        module = self._mock_module()
265
+        fact_collector = self.collector_class()
266
+        facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts)
267
+        self.assertIsInstance(facts_dict, dict)
268
+        self.assertIn('pkg_mgr', facts_dict)
269
+        self.assertEqual(facts_dict['pkg_mgr'], 'homebrew')
270
+
271
+    @patch('ansible.module_utils.facts.system.pkg_mgr.os.path.exists', side_effect=lambda x: x == '/opt/local/bin/port')
272
+    def test_collect_macports(self, p_exists):
273
+        module = self._mock_module()
274
+        fact_collector = self.collector_class()
275
+        facts_dict = fact_collector.collect(module=module, collected_facts=self.collected_facts)
276
+        self.assertIsInstance(facts_dict, dict)
277
+        self.assertIn('pkg_mgr', facts_dict)
278
+        self.assertEqual(facts_dict['pkg_mgr'], 'macports')
279
+
280
+
241 281
 def _sanitize_os_path_apt_get(path):
242 282
     if path == '/usr/bin/apt-get':
243 283
         return True