def test_dhcp_operation(self):
     fake_ctx = fake_ctx = self.node_for_check_create_network()
     fake_client = self.generate_client(
         vdc_networks=['secret_network']
     )
     fake_client._vdc_gateway.is_busy = mock.MagicMock(
         return_value=True
     )
     with mock.patch('vcloud_network_plugin.network.ctx', fake_ctx):
         with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
             self.assertFalse(
                 network._dhcp_operation(
                     fake_client, 'secret_network', network.DELETE_POOL
                 )
             )
    def test__dhcp_operation(self):
        fake_client = self.generate_client()
        # no dhcp
        fake_ctx = self.generate_node_context(properties={
            'network': {
                'edge_gateway': 'gateway'
            },
            'vcloud_config': {
                'vdc': 'vdc_name'
            }
        })
        with mock.patch('vcloud_network_plugin.network.ctx', fake_ctx):
            with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
                network._dhcp_operation(
                    fake_client, '_management_network', network.ADD_POOL
                )
        # wrong dhcp_range
        fake_ctx = self.generate_node_context(properties={
            'network': {
                'dhcp': {
                    'dhcp_range': ""
                },
                'edge_gateway': 'gateway'
            },
            'vcloud_config': {
                'vdc': 'vdc_name'
            }
        })
        with mock.patch('vcloud_network_plugin.network.ctx', fake_ctx):
            with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    network._dhcp_operation(
                        fake_client, '_management_network', network.ADD_POOL
                    )

        fake_ctx = self.generate_node_context(properties={
            'network': {
                'dhcp': {
                    'dhcp_range': "10.1.1.1-10.1.1.255"
                },
                'edge_gateway': 'gateway'
            },
            'vcloud_config': {
                'vdc': 'vdc_name'
            }
        })

        with mock.patch('vcloud_network_plugin.network.ctx', fake_ctx):
            with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
                # returned error/None from server
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    network._dhcp_operation(
                        fake_client, '_management_network', network.ADD_POOL
                    )
                fake_client.get_gateway.assert_called_with(
                    'vdc_name', 'gateway'
                )

                # returned error/None from server delete
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    network._dhcp_operation(
                        fake_client, '_management_network', network.DELETE_POOL
                    )

                # returned busy, try next time
                self.set_gateway_busy(fake_client._vdc_gateway)
                self.prepare_retry(fake_ctx)

                self.assertFalse(network._dhcp_operation(
                    fake_client, '_management_network',
                    network.DELETE_POOL
                ))

                # no such gateway
                fake_client.get_gateway = mock.MagicMock(return_value=None)
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    network._dhcp_operation(
                        fake_client, '_management_network', network.ADD_POOL
                    )
    def test__dhcp_operation(self):
        fake_client = self.generate_client()
        # no dhcp
        fake_ctx = self.generate_node_context(
            properties={
                'network': {
                    'edge_gateway': 'gateway'
                },
                'vcloud_config': {
                    'vdc': 'vdc_name'
                }
            })
        with mock.patch('vcloud_network_plugin.network.ctx', fake_ctx):
            with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
                network._dhcp_operation(fake_client, '_management_network',
                                        network.ADD_POOL)
        # wrong dhcp_range
        fake_ctx = self.generate_node_context(
            properties={
                'network': {
                    'dhcp': {
                        'dhcp_range': ""
                    },
                    'edge_gateway': 'gateway'
                },
                'vcloud_config': {
                    'vdc': 'vdc_name'
                }
            })
        with mock.patch('vcloud_network_plugin.network.ctx', fake_ctx):
            with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    network._dhcp_operation(fake_client, '_management_network',
                                            network.ADD_POOL)

        fake_ctx = self.generate_node_context(
            properties={
                'network': {
                    'dhcp': {
                        'dhcp_range': "10.1.1.1-10.1.1.255"
                    },
                    'edge_gateway': 'gateway'
                },
                'vcloud_config': {
                    'vdc': 'vdc_name'
                }
            })

        with mock.patch('vcloud_network_plugin.network.ctx', fake_ctx):
            with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
                # returned error/None from server
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    network._dhcp_operation(fake_client, '_management_network',
                                            network.ADD_POOL)
                fake_client.get_gateway.assert_called_with(
                    'vdc_name', 'gateway')

                # returned error/None from server delete
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    network._dhcp_operation(fake_client, '_management_network',
                                            network.DELETE_POOL)

                # returned busy, try next time
                self.set_gateway_busy(fake_client._vdc_gateway)
                self.prepare_retry(fake_ctx)

                self.assertFalse(
                    network._dhcp_operation(fake_client, '_management_network',
                                            network.DELETE_POOL))

                # no such gateway
                fake_client.get_gateway = mock.MagicMock(return_value=None)
                with self.assertRaises(cfy_exc.NonRecoverableError):
                    network._dhcp_operation(fake_client, '_management_network',
                                            network.ADD_POOL)