def test_group_id_fail(self):
        """Verify group_id produces expected exceptions."""
        with self.assertRaisesRegexp(AnsibleFailJson,
                                     "Failed to get host groups."):
            with mock.patch(self.REQ_FUNC, return_value=Exception()):
                self._set_args({
                    'state':
                    'present',
                    'name':
                    'beegfs_metadata2',
                    'host_type':
                    'linux dm-mp',
                    'force_port':
                    False,
                    'group':
                    'test_group2',
                    'ports': [{
                        'label':
                        'beegfs_metadata2_iscsi_0',
                        'type':
                        'iscsi',
                        'port':
                        'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'
                    }]
                })
                host = Host()
                host.group_id()

        with self.assertRaisesRegexp(AnsibleFailJson,
                                     "No group with the name:"):
            with mock.patch(self.REQ_FUNC,
                            return_value=(200, self.HOST_GROUPS)):
                self._set_args({
                    'state':
                    'present',
                    'name':
                    'beegfs_metadata2',
                    'host_type':
                    'linux dm-mp',
                    'force_port':
                    False,
                    'group':
                    'test_group2',
                    'ports': [{
                        'label':
                        'beegfs_metadata2_iscsi_0',
                        'type':
                        'iscsi',
                        'port':
                        'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'
                    }]
                })
                host = Host()
                host.group_id()
    def test_group_id_pass(self):
        """Verify group_id produces expected results."""
        with mock.patch(self.REQ_FUNC, return_value=(200, self.HOST_GROUPS)):
            self._set_args({
                'state':
                'present',
                'name':
                'beegfs_metadata1',
                'host_type':
                'linux dm-mp',
                'force_port':
                False,
                'ports': [{
                    'label':
                    'beegfs_metadata2_iscsi_0',
                    'type':
                    'iscsi',
                    'port':
                    'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'
                }]
            })
            host = Host()
            self.assertEqual(host.group_id(),
                             "0000000000000000000000000000000000000000")

            self._set_args({
                'state':
                'present',
                'name':
                'beegfs_metadata2',
                'host_type':
                'linux dm-mp',
                'force_port':
                False,
                'group':
                'test_group',
                'ports': [{
                    'label':
                    'beegfs_metadata2_iscsi_0',
                    'type':
                    'iscsi',
                    'port':
                    'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'
                }]
            })
            host = Host()
            self.assertEqual(host.group_id(),
                             "85000000600A098000A4B9D1003637135D483DEB")
 def test_update_host_fail(self):
     """Verify update_host produces expected exceptions."""
     with self.assertRaisesRegexp(AnsibleFailJson,
                                  "Failed to update host."):
         with mock.patch(self.REQ_FUNC,
                         side_effect=[(200, self.EXISTING_HOSTS),
                                      Exception()]):
             self._set_args({
                 'state':
                 'present',
                 'name':
                 'beegfs_metadata1',
                 'host_type':
                 'windows',
                 'force_port':
                 False,
                 'group':
                 'test_group',
                 'ports': [{
                     'label':
                     'beegfs_metadata1_iscsi_0',
                     'type':
                     'iscsi',
                     'port':
                     'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'
                 }]
             })
             host = Host()
             host.build_success_payload = lambda x: {}
             host.group_id = lambda: "85000000600A098000A4B9D1003637135D483DEB"
             host.host_exists()
             self.assertTrue(host.needs_update())
             host.update_host()
    def test_create_host_pass(self):
        """Verify create_host produces expected results."""
        def _assigned_host_ports(apply_unassigning=False):
            return None

        with self.assertRaises(AnsibleExitJson):
            with mock.patch(self.REQ_FUNC,
                            return_value=(200, {
                                'id':
                                '84000000600A098000A4B9D10030370B5D430109'
                            })):
                self._set_args({
                    'state':
                    'present',
                    'name':
                    'beegfs_metadata1',
                    'host_type':
                    'windows',
                    'force_port':
                    True,
                    'group':
                    'test_group',
                    'ports': [{
                        'label':
                        'beegfs_metadata1_iscsi_1',
                        'type':
                        'iscsi',
                        'port':
                        'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'
                    }]
                })
                host = Host()
                host.host_exists = lambda: False
                host.assigned_host_ports = _assigned_host_ports
                host.build_success_payload = lambda x: {}
                host.group_id = lambda: "85000000600A098000A4B9D1003637135D483DEB"
                host.create_host()
    def test_update_host_pass(self):
        """Verify update_host produces expected results."""
        # Change host type
        with self.assertRaises(AnsibleExitJson):
            with mock.patch(self.REQ_FUNC,
                            return_value=(200, self.EXISTING_HOSTS)):
                self._set_args({
                    'state':
                    'present',
                    'name':
                    'beegfs_metadata1',
                    'host_type':
                    'windows',
                    'force_port':
                    True,
                    'ports': [{
                        'label':
                        'beegfs_metadata1_iscsi_1',
                        'type':
                        'iscsi',
                        'port':
                        'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'
                    }]
                })
                host = Host()
                host.build_success_payload = lambda x: {}
                host.host_exists()
                self.assertTrue(host.needs_update())
                host.update_host()

        # Change port iqn
        with self.assertRaises(AnsibleExitJson):
            with mock.patch(self.REQ_FUNC,
                            return_value=(200, self.EXISTING_HOSTS)):
                self._set_args({
                    'state':
                    'present',
                    'name':
                    'beegfs_metadata1',
                    'host_type':
                    'linux dm-mp',
                    'force_port':
                    False,
                    'ports': [{
                        'label': 'beegfs_metadata1_iscsi_1',
                        'type': 'iscsi',
                        'port': 'iqn.not_used'
                    }]
                })
                host = Host()
                host.build_success_payload = lambda x: {}
                host.host_exists()
                self.assertTrue(host.needs_update())
                host.update_host()

        # Change port type to fc
        with self.assertRaises(AnsibleExitJson):
            with mock.patch(self.REQ_FUNC,
                            return_value=(200, self.EXISTING_HOSTS)):
                self._set_args({
                    'state':
                    'present',
                    'name':
                    'beegfs_metadata1',
                    'host_type':
                    'linux dm-mp',
                    'force_port':
                    False,
                    'ports': [{
                        'label': 'beegfs_metadata1_iscsi_1',
                        'type': 'fc',
                        'port': '0x08ef08ef08ef08ef'
                    }]
                })
                host = Host()
                host.build_success_payload = lambda x: {}
                host.host_exists()
                self.assertTrue(host.needs_update())
                host.update_host()

        # Change port name
        with self.assertRaises(AnsibleExitJson):
            with mock.patch(self.REQ_FUNC,
                            return_value=(200, self.EXISTING_HOSTS)):
                self._set_args({
                    'state':
                    'present',
                    'name':
                    'beegfs_metadata1',
                    'host_type':
                    'windows',
                    'force_port':
                    True,
                    'ports': [{
                        'label':
                        'beegfs_metadata1_iscsi_12',
                        'type':
                        'iscsi',
                        'port':
                        'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'
                    }]
                })
                host = Host()
                host.build_success_payload = lambda x: {}
                host.host_exists()
                self.assertTrue(host.needs_update())
                host.update_host()

        # Change group
        with self.assertRaises(AnsibleExitJson):
            with mock.patch(self.REQ_FUNC,
                            return_value=(200, self.EXISTING_HOSTS)):
                self._set_args({
                    'state':
                    'present',
                    'name':
                    'beegfs_metadata1',
                    'host_type':
                    'windows',
                    'force_port':
                    False,
                    'group':
                    'test_group',
                    'ports': [{
                        'label':
                        'beegfs_metadata1_iscsi_0',
                        'type':
                        'iscsi',
                        'port':
                        'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'
                    }]
                })
                host = Host()
                host.build_success_payload = lambda x: {}
                host.group_id = lambda: "85000000600A098000A4B9D1003637135D483DEB"
                host.host_exists()
                self.assertTrue(host.needs_update())
                host.update_host()