From 6752ce8032979853beaf2bb3bb7bd3c506123c7f Mon Sep 17 00:00:00 2001
From: Lucas Alvares Gomes <lucasagomes@gmail.com>
Date: Tue, 19 Jan 2016 13:36:26 +0000
Subject: [PATCH] Extend root device hints to support device name

This patch is extending the root device hints to also look at the device
name. This patch also refactors the tests for root device hints making
it easier to test a different hint per test.

Change-Id: I48d6456c75bbe6ddf16ac6561e5461ca51eb9c37
Partial-Bug: #1526732
---
 ironic_python_agent/hardware.py               |  3 +-
 .../tests/unit/test_hardware.py               | 37 +++++++++++++++----
 ...me-root-device-hints-0cfc8c90d03c8bf0.yaml |  3 ++
 3 files changed, 34 insertions(+), 9 deletions(-)
 create mode 100644 releasenotes/notes/name-root-device-hints-0cfc8c90d03c8bf0.yaml

diff --git a/ironic_python_agent/hardware.py b/ironic_python_agent/hardware.py
index 3f0f0771e..b0d72c11d 100644
--- a/ironic_python_agent/hardware.py
+++ b/ironic_python_agent/hardware.py
@@ -461,7 +461,8 @@ class GenericHardwareManager(HardwareManager):
 
             def check_device_attrs(device):
                 for key in ('model', 'wwn', 'serial', 'vendor',
-                            'wwn_with_extension', 'wwn_vendor_extension'):
+                            'wwn_with_extension', 'wwn_vendor_extension',
+                            'name'):
                     if key not in root_device_hints:
                         continue
 
diff --git a/ironic_python_agent/tests/unit/test_hardware.py b/ironic_python_agent/tests/unit/test_hardware.py
index 81b180fc9..723de60b7 100644
--- a/ironic_python_agent/tests/unit/test_hardware.py
+++ b/ironic_python_agent/tests/unit/test_hardware.py
@@ -304,14 +304,10 @@ class TestGenericHardwareManager(test_base.BaseTestCase):
 
     @mock.patch.object(hardware, 'list_all_block_devices')
     @mock.patch.object(utils, 'parse_root_device_hints')
-    def test_get_os_install_device_root_device_hints(self, mock_root_device,
-                                                     mock_dev):
+    def _get_os_install_device_root_device_hints(self, hints, expected_device,
+                                                 mock_root_device, mock_dev):
         model = 'fastable sd131 7'
-        mock_root_device.return_value = {'model': model,
-                                         'wwn': 'fake-wwn',
-                                         'serial': 'fake-serial',
-                                         'vendor': 'fake-vendor',
-                                         'size': 10}
+        mock_root_device.return_value = hints
         mock_dev.return_value = [
             hardware.BlockDevice(name='/dev/sda',
                                  model='TinyUSB Drive',
@@ -333,10 +329,35 @@ class TestGenericHardwareManager(test_base.BaseTestCase):
                                  serial='fake-serial'),
         ]
 
-        self.assertEqual('/dev/sdb', self.hardware.get_os_install_device())
+        self.assertEqual(expected_device,
+                         self.hardware.get_os_install_device())
         mock_root_device.assert_called_once_with()
         mock_dev.assert_called_once_with()
 
+    def test_get_os_install_device_root_device_hints_model(self):
+        self._get_os_install_device_root_device_hints(
+            {'model': 'fastable sd131 7'}, '/dev/sdb')
+
+    def test_get_os_install_device_root_device_hints_wwn(self):
+        self._get_os_install_device_root_device_hints(
+            {'wwn': 'wwn0'}, '/dev/sda')
+
+    def test_get_os_install_device_root_device_hints_serial(self):
+        self._get_os_install_device_root_device_hints(
+            {'serial': 'serial0'}, '/dev/sda')
+
+    def test_get_os_install_device_root_device_hints_size(self):
+        self._get_os_install_device_root_device_hints(
+            {'size': 10}, '/dev/sdb')
+
+    def test_get_os_install_device_root_device_hints_vendor(self):
+        self._get_os_install_device_root_device_hints(
+            {'vendor': 'fake-vendor'}, '/dev/sdb')
+
+    def test_get_os_install_device_root_device_hints_name(self):
+        self._get_os_install_device_root_device_hints(
+            {'name': '/dev/sdb'}, '/dev/sdb')
+
     @mock.patch.object(hardware, 'list_all_block_devices')
     @mock.patch.object(utils, 'parse_root_device_hints')
     def test_get_os_install_device_root_device_hints_no_device_found(
diff --git a/releasenotes/notes/name-root-device-hints-0cfc8c90d03c8bf0.yaml b/releasenotes/notes/name-root-device-hints-0cfc8c90d03c8bf0.yaml
new file mode 100644
index 000000000..90f8f46ab
--- /dev/null
+++ b/releasenotes/notes/name-root-device-hints-0cfc8c90d03c8bf0.yaml
@@ -0,0 +1,3 @@
+---
+features:
+  - Root device hints extended to support the device name.