def test_interface_property_request_exception_fail(self):
        """Verify ethernet-interfaces endpoint request failure results in AnsibleFailJson exception."""
        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "address": "192.168.1.1",
            "subnet_mask": "255.255.255.0",
            "config_method": "static"
        }
        get_controller = {
            "A": {
                "controllerSlot": 1,
                "controllerRef": "070000000000000000000001",
                "ssh": False
            },
            "B": {
                "controllerSlot": 2,
                "controllerRef": "070000000000000000000002",
                "ssh": True
            }
        }

        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.get_controllers = lambda: get_controller

        with self.assertRaisesRegexp(
                AnsibleFailJson,
                r"Failed to retrieve defined management interfaces."):
            with mock.patch(self.REQ_FUNC, return_value=Exception()):
                mgmt_interface.update_target_interface_info()
    def test_update_target_interface_info_fail(self):
        """Verify return value from update_target_interface_info method."""
        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "3",
            "address": "192.168.1.1",
            "subnet_mask": "255.255.255.1",
            "config_method": "static"
        }
        get_controller = {
            "A": {
                "controllerSlot": 1,
                "controllerRef": "070000000000000000000001",
                "ssh": False
            },
            "B": {
                "controllerSlot": 2,
                "controllerRef": "070000000000000000000002",
                "ssh": True
            }
        }

        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.get_controllers = lambda: get_controller

        with self.assertRaisesRegexp(
                AnsibleFailJson, "Invalid port number! Controller .*? ports:"):
            with mock.patch(self.REQ_FUNC, return_value=(200, self.TEST_DATA)):
                mgmt_interface.update_target_interface_info()
    def test_update_body_enable_interface_setting_fail(self):
        """Validate update_body_enable_interface_setting throws expected exception"""
        initial = {
            "state": "disabled",
            "controller": "A",
            "port": "1",
            "address": "192.168.1.1",
            "subnet_mask": "255.255.255.1",
            "config_method": "static"
        }
        interface_info = {
            "channel":
            1,
            "link_status":
            "up",
            "enabled":
            True,
            "address":
            "10.1.1.10",
            "gateway":
            "10.1.1.1",
            "subnet_mask":
            "255.255.255.0",
            "dns_config_method":
            "stat",
            "dns_servers": [{
                "addressType": "ipv4",
                "ipv4Address": "10.1.0.250"
            }, {
                "addressType": "ipv4",
                "ipv4Address": "10.10.0.20"
            }],
            "ntp_config_method":
            "disabled",
            "ntp_servers":
            None,
            "config_method":
            "configStatic",
            "controllerRef":
            "070000000000000000000001",
            "controllerSlot":
            1,
            "ipv6_enabled":
            False,
            "id":
            "2800070000000000000000000001000000000000",
            "ssh":
            False
        }

        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.interface_info = interface_info
        with self.assertRaisesRegexp(AnsibleFailJson,
                                     "Either IPv4 or IPv6 must be enabled."):
            mgmt_interface.update_body_enable_interface_setting()
    def test_controller_property_fail(self):
        """Verify controllers endpoint request failure causes AnsibleFailJson exception."""
        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "address": "192.168.1.1",
            "subnet_mask": "255.255.255.1",
            "config_method": "static"
        }
        controller_request = [{
            "physicalLocation": {
                "slot": 2
            },
            "controllerRef": "070000000000000000000002",
            "networkSettings": {
                "remoteAccessEnabled": True
            }
        }, {
            "physicalLocation": {
                "slot": 1
            },
            "controllerRef": "070000000000000000000001",
            "networkSettings": {
                "remoteAccessEnabled": False
            }
        }]
        expected = {
            'A': {
                'controllerRef': '070000000000000000000001',
                'controllerSlot': 1,
                'ssh': False
            },
            'B': {
                'controllerRef': '070000000000000000000002',
                'controllerSlot': 2,
                'ssh': True
            }
        }

        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        with self.assertRaisesRegexp(
                AnsibleFailJson,
                r"Failed to retrieve the controller settings."):
            with mock.patch(self.REQ_FUNC, return_value=Exception):
                response = mgmt_interface.get_controllers()
    def test_get_controllers_pass(self):
        """Verify dictionary return from get_controllers."""
        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "address": "192.168.1.1",
            "subnet_mask": "255.255.255.1",
            "config_method": "static"
        }
        controller_request = [{
            "physicalLocation": {
                "slot": 2
            },
            "controllerRef": "070000000000000000000002",
            "networkSettings": {
                "remoteAccessEnabled": True
            }
        }, {
            "physicalLocation": {
                "slot": 1
            },
            "controllerRef": "070000000000000000000001",
            "networkSettings": {
                "remoteAccessEnabled": False
            }
        }]
        expected = {
            'A': {
                'controllerRef': '070000000000000000000001',
                'controllerSlot': 1,
                'ssh': False
            },
            'B': {
                'controllerRef': '070000000000000000000002',
                'controllerSlot': 2,
                'ssh': True
            }
        }

        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()

        with mock.patch(self.REQ_FUNC, return_value=(200, controller_request)):
            response = mgmt_interface.get_controllers()
            self.assertTrue(response == expected)
 def test_update_url_pass(self):
     """Verify update_url returns expected url."""
     initial = {
         "state": "enabled",
         "controller": "A",
         "port": "1",
         "config_method": "dhcp",
         "ssh": False
     }
     self._set_args(initial)
     mgmt_interface = NetAppESeriesMgmtInterface()
     mgmt_interface.url = "https://192.168.1.100:8443/devmgr/v2/"
     mgmt_interface.alt_interface_addresses = ["192.168.1.102"]
     mgmt_interface.update_url()
     self.assertTrue(mgmt_interface.url,
                     "https://192.168.1.102:8443/devmgr/v2/")
    def test_update_body_ntp_server_settings_pass(self):
        """Validate update_body_ntp_server_settings throws expected exception"""
        interface_info = {
            "channel":
            1,
            "link_status":
            "up",
            "enabled":
            True,
            "address":
            "10.1.1.10",
            "gateway":
            "10.1.1.1",
            "subnet_mask":
            "255.255.255.0",
            "dns_config_method":
            "stat",
            "dns_servers": [{
                "addressType": "ipv4",
                "ipv4Address": "10.1.0.250"
            }, {
                "addressType": "ipv4",
                "ipv4Address": "10.10.0.20"
            }],
            "ntp_config_method":
            "dhcp",
            "ntp_servers":
            None,
            "config_method":
            "configStatic",
            "controllerRef":
            "070000000000000000000001",
            "controllerSlot":
            1,
            "ipv6_enabled":
            False,
            "id":
            "2800070000000000000000000001000000000000",
            "ssh":
            False
        }

        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "ntp_config_method": "disabled"
        }
        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.interface_info = interface_info
        self.assertTrue(mgmt_interface.update_body_ntp_server_settings())
        self.assertEquals(
            mgmt_interface.body,
            {"ntpAcquisitionDescriptor": {
                "ntpAcquisitionType": "disabled"
            }})

        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "ntp_config_method": "dhcp"
        }
        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.interface_info = interface_info
        self.assertFalse(mgmt_interface.update_body_ntp_server_settings())
        self.assertEquals(
            mgmt_interface.body,
            {"ntpAcquisitionDescriptor": {
                "ntpAcquisitionType": "dhcp"
            }})

        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "ntp_config_method": "static",
            "ntp_address": "192.168.1.200"
        }
        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.interface_info = interface_info
        self.assertTrue(mgmt_interface.update_body_ntp_server_settings())
        self.assertEquals(
            mgmt_interface.body, {
                "ntpAcquisitionDescriptor": {
                    "ntpAcquisitionType":
                    "stat",
                    "ntpServers": [{
                        "addrType": "ipvx",
                        "ipvxAddress": {
                            "addressType": "ipv4",
                            "ipv4Address": "192.168.1.200"
                        }
                    }]
                }
            })

        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "ntp_config_method": "static",
            "ntp_address": "192.168.1.200",
            "ntp_address_backup": "192.168.1.202"
        }
        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.interface_info = interface_info
        self.assertTrue(mgmt_interface.update_body_ntp_server_settings())
        self.assertEquals(
            mgmt_interface.body, {
                "ntpAcquisitionDescriptor": {
                    "ntpAcquisitionType":
                    "stat",
                    "ntpServers": [{
                        "addrType": "ipvx",
                        "ipvxAddress": {
                            "addressType": "ipv4",
                            "ipv4Address": "192.168.1.200"
                        }
                    }, {
                        "addrType": "ipvx",
                        "ipvxAddress": {
                            "addressType": "ipv4",
                            "ipv4Address": "192.168.1.202"
                        }
                    }]
                }
            })
    def test_update_body_enable_interface_setting_pass(self):
        """Validate update_body_enable_interface_setting updates properly."""
        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "address": "192.168.1.1",
            "subnet_mask": "255.255.255.1",
            "config_method": "static"
        }
        interface_info = {
            "channel":
            1,
            "link_status":
            "up",
            "enabled":
            True,
            "address":
            "10.1.1.10",
            "gateway":
            "10.1.1.1",
            "subnet_mask":
            "255.255.255.0",
            "dns_config_method":
            "stat",
            "dns_servers": [{
                "addressType": "ipv4",
                "ipv4Address": "10.1.0.250"
            }, {
                "addressType": "ipv4",
                "ipv4Address": "10.10.0.20"
            }],
            "ntp_config_method":
            "disabled",
            "ntp_servers":
            None,
            "config_method":
            "configStatic",
            "controllerRef":
            "070000000000000000000001",
            "controllerSlot":
            1,
            "ipv6_enabled":
            True,
            "id":
            "2800070000000000000000000001000000000000",
            "ssh":
            False
        }
        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.interface_info = interface_info
        change_required = mgmt_interface.update_body_enable_interface_setting()
        self.assertFalse(change_required)
        self.assertTrue("ipv4Enabled" in mgmt_interface.body
                        and mgmt_interface.body["ipv4Enabled"])

        initial = {
            "state": "disabled",
            "controller": "A",
            "port": "1",
            "address": "192.168.1.1",
            "subnet_mask": "255.255.255.1",
            "config_method": "static"
        }
        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.interface_info = interface_info
        change_required = mgmt_interface.update_body_enable_interface_setting()
        self.assertTrue(change_required)
        self.assertTrue("ipv4Enabled" in mgmt_interface.body
                        and not mgmt_interface.body["ipv4Enabled"])
    def test_update_target_interface_info_pass(self):
        """Verify return value from interface property."""
        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "address": "192.168.1.1",
            "subnet_mask": "255.255.255.0",
            "config_method": "static"
        }
        get_controller = {
            "A": {
                "controllerSlot": 1,
                "controllerRef": "070000000000000000000001",
                "ssh": False
            },
            "B": {
                "controllerSlot": 2,
                "controllerRef": "070000000000000000000002",
                "ssh": True
            }
        }
        expected = {
            "channel":
            1,
            "link_status":
            "up",
            "enabled":
            True,
            "address":
            "10.1.1.10",
            "gateway":
            "10.1.1.1",
            "subnet_mask":
            "255.255.255.0",
            "dns_config_method":
            "stat",
            "dns_servers": [{
                "addressType": "ipv4",
                "ipv4Address": "10.1.0.250"
            }, {
                "addressType": "ipv4",
                "ipv4Address": "10.10.0.20"
            }],
            "ntp_config_method":
            "disabled",
            "ntp_servers":
            None,
            "config_method":
            "configStatic",
            "controllerRef":
            "070000000000000000000001",
            "controllerSlot":
            1,
            "ipv6_enabled":
            False,
            "id":
            "2800070000000000000000000001000000000000",
            "ssh":
            False
        }

        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.get_controllers = lambda: get_controller

        with mock.patch(self.REQ_FUNC, return_value=(200, self.TEST_DATA)):
            mgmt_interface.update_target_interface_info()
            self.assertEquals(mgmt_interface.interface_info, expected)
    def test_update_pass(self):
        """Verify update successfully completes."""
        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "config_method": "dhcp",
            "ssh": False
        }
        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.update_request_body = lambda: False
        mgmt_interface.is_embedded = lambda: False
        mgmt_interface.use_alternate_address = False
        with self.assertRaisesRegexp(AnsibleExitJson,
                                     "No changes are required."):
            with mock.patch(self.REQ_FUNC, return_value=(200, None)):
                mgmt_interface.update()

        def update_request_body():
            update_request_body.value = not update_request_body.value
            return update_request_body.value

        update_request_body.value = False

        initial = {
            "state": "enabled",
            "controller": "A",
            "port": "1",
            "config_method": "dhcp",
            "ssh": False
        }
        self._set_args(initial)
        mgmt_interface = NetAppESeriesMgmtInterface()
        mgmt_interface.update_request_body = update_request_body
        mgmt_interface.is_embedded = lambda: True
        mgmt_interface.use_alternate_address = False
        with self.assertRaisesRegexp(
                AnsibleExitJson, "The interface settings have been updated."):
            with mock.patch(self.REQ_FUNC, return_value=(200, None)):
                mgmt_interface.update()