예제 #1
0
 def setUp(self):
     super(TestNeutronDriver, self).setUp()
     self.neutron_client = mock.MagicMock()
     self.neutron_client.list_networks.return_value = network_response
     self.neutron_client.list_ports.return_value = port_response
     self.neutron_client.list_routers.return_value = router_response
     self.neutron_client.list_security_groups.return_value = (
         security_group_response)
     args = helper.datasource_openstack_args()
     self.driver = neutron_driver.NeutronDriver(args=args)
     self.driver.neutron = self.neutron_client
def create_network_group(tablename, full_neutron_tablename=None):
    driver = neutron_driver.NeutronDriver(
        args=helper.datasource_openstack_args())
    if full_neutron_tablename is None:
        full_neutron_tablename = 'neutron:networks'
    network_key_to_index = driver.get_column_map(
        neutron_driver.NeutronDriver.NETWORKS)
    network_id_index = network_key_to_index['id']
    network_max_index = max(network_key_to_index.values())
    network_args = ['x' + str(i) for i in xrange(0, network_max_index + 1)]
    formula = compile.parse1('{}({}) :- {}({})'.format(
        tablename, 'x' + str(network_id_index), full_neutron_tablename,
        ",".join(network_args)))
    return formula
def create_networkXnetwork_group(tablename):
    """Return rule of the form:

    TABLENAME(x,y) :- neutron:network(...,x,...),neutron:network(...,y,...)
    """
    driver = neutron_driver.NeutronDriver(
        args=helper.datasource_openstack_args())
    network_key_to_index = driver.get_column_map(
        neutron_driver.NeutronDriver.NETWORKS)
    network_id_index = network_key_to_index['id']
    network_max_index = max(network_key_to_index.values())
    net1_args = ['x' + str(i) for i in xrange(0, network_max_index + 1)]
    net2_args = ['y' + str(i) for i in xrange(0, network_max_index + 1)]
    formula = compile.parse1(
        '{}({},{}) :- neutron:networks({}), neutron2:networks({})'.format(
            tablename, 'x' + str(network_id_index),
            'y' + str(network_id_index), ",".join(net1_args),
            ",".join(net2_args)))
    return formula
    def setUp(self):
        """Setup polling tests."""
        super(TestDataSourceDriver, self).setUp()
        cage = d6cage.d6Cage()

        # Create mock of Neutron client so we can control data
        mock_factory = mox.Mox()
        neutron_client = mock_factory.CreateMock(
            neutronclient.v2_0.client.Client)
        neutron_client.list_networks().InAnyOrder(1).AndReturn(network1)
        neutron_client.list_ports().InAnyOrder(1).AndReturn(port_response)
        neutron_client.list_routers().InAnyOrder(1).AndReturn(router_response)
        neutron_client.list_security_groups().InAnyOrder(1).AndReturn(
            security_group_response)
        neutron_client.list_networks().InAnyOrder(2).AndReturn(network2)
        neutron_client.list_ports().InAnyOrder(2).AndReturn(port_response)
        neutron_client.list_routers().InAnyOrder(2).AndReturn(router_response)
        neutron_client.list_security_groups().InAnyOrder(2).AndReturn(
            security_group_response)
        mock_factory.ReplayAll()

        # Create modules (without auto-polling)
        cage.loadModule("NeutronDriver",
                        helper.data_module_path("neutron_driver.py"))
        cage.loadModule("PolicyDriver", helper.policy_module_path())
        cage.createservice(name="policy",
                           moduleName="PolicyDriver",
                           args={
                               'd6cage': cage,
                               'rootdir': helper.data_module_path('')
                           })
        args = helper.datasource_openstack_args()
        args['poll_time'] = 0
        args['client'] = neutron_client
        cage.createservice(name="neutron",
                           moduleName="NeutronDriver",
                           args=args)
        policy = cage.service_object('policy')
        policy.create_policy('neutron')
        policy.set_schema('neutron',
                          cage.service_object('neutron').get_schema())
        cage.service_object('neutron').neutron = neutron_client
        policy.debug_mode()

        # insert rule into policy to make testing easier.
        #   (Some of the IDs are auto-generated each time we convert)
        policy.insert(create_network_group('p'))

        # create some garbage data
        args = helper.datasource_openstack_args()
        driver = neutron_driver.NeutronDriver(args=args)
        network_key_to_index = driver.get_column_map(
            neutron_driver.NeutronDriver.NETWORKS)
        network_max_index = max(network_key_to_index.values())
        args1 = ['1'] * (network_max_index + 1)
        args2 = ['2'] * (network_max_index + 1)
        args1 = ",".join(args1)
        args2 = ",".join(args2)
        fake_networks = [
            'neutron:networks({})'.format(args1),
            'neutron:networks({})'.format(args2)
        ]

        # answer to query above for network1
        datalog1 = ('p("240ff9df-df35-43ae-9df5-27fae87f2492") '
                    'p("340ff9df-df35-43ae-9df5-27fae87f2492") '
                    'p("440ff9df-df35-43ae-9df5-27fae87f2492")')

        # answer to query above for network2
        datalog2 = ('p("240ff9df-df35-43ae-9df5-27fae87f2492") '
                    'p("640ff9df-df35-43ae-9df5-27fae87f2492") '
                    'p("540ff9df-df35-43ae-9df5-27fae87f2492")')

        # return value
        self.info = {}
        self.info['cage'] = cage
        self.info['datalog1'] = datalog1
        self.info['datalog2'] = datalog2
        self.info['fake_networks'] = fake_networks
예제 #5
0
    def setUp(self):
        """Setup polling tests."""
        super(TestDataSourceDriver, self).setUp()
        cfg.CONF.set_override(
            'drivers',
            ['congress.datasources.neutron_driver.NeutronDriver'])

        # Create mock of Neutron client so we can control data
        mock_factory = mox.Mox()
        neutron_client = mock_factory.CreateMock(
            neutronclient.v2_0.client.Client)
        neutron_client.list_networks().InAnyOrder(1).AndReturn(network1)
        neutron_client.list_ports().InAnyOrder(1).AndReturn(port_response)
        neutron_client.list_routers().InAnyOrder(1).AndReturn(router_response)
        neutron_client.list_security_groups().InAnyOrder(1).AndReturn(
            security_group_response)
        neutron_client.list_networks().InAnyOrder(2).AndReturn(network2)
        neutron_client.list_ports().InAnyOrder(2).AndReturn(port_response)
        neutron_client.list_routers().InAnyOrder(2).AndReturn(router_response)
        neutron_client.list_security_groups().InAnyOrder(2).AndReturn(
            security_group_response)
        mock_factory.ReplayAll()

        node = helper.make_dsenode_new_partition('neutron_ds_node')

        engine = harness.create_policy_engine()
        node.register_service(engine)

        neutron_args = {
            'name': 'neutron',
            'driver': 'neutron',
            'description': None,
            'type': None,
            'enabled': '1'
            }
        neutron_args['config'] = helper.datasource_openstack_args()
        neutron_args['config']['poll_time'] = 0
        neutron_args['config']['client'] = neutron_client
        neutron_ds = node.create_datasource_service(neutron_args)
        node.register_service(neutron_ds)

        engine.create_policy('neutron')
        engine.set_schema('neutron', neutron_ds.get_schema())
        neutron_ds.neutron = neutron_client
        engine.debug_mode()

        # insert rule into policy to make testing easier.
        #   (Some of the IDs are auto-generated each time we convert)
        engine.insert(create_network_group('p'))

        # create some garbage data
        args = helper.datasource_openstack_args()
        driver = neutron_driver.NeutronDriver(args=args)
        network_key_to_index = driver.get_column_map(
            neutron_driver.NeutronDriver.NETWORKS)
        network_max_index = max(network_key_to_index.values())
        args1 = ['1'] * (network_max_index + 1)
        args2 = ['2'] * (network_max_index + 1)
        args1 = ",".join(args1)
        args2 = ",".join(args2)
        fake_networks = [
            'neutron:networks({})'.format(args1),
            'neutron:networks({})'.format(args2)]

        # answer to query above for network1
        datalog1 = (
            'p("240ff9df-df35-43ae-9df5-27fae87f2492") '
            'p("340ff9df-df35-43ae-9df5-27fae87f2492") '
            'p("440ff9df-df35-43ae-9df5-27fae87f2492")')

        # answer to query above for network2
        datalog2 = (
            'p("240ff9df-df35-43ae-9df5-27fae87f2492") '
            'p("640ff9df-df35-43ae-9df5-27fae87f2492") '
            'p("540ff9df-df35-43ae-9df5-27fae87f2492")')

        # return value
        self.info = {}
        self.info['node'] = node
        self.info['datalog1'] = datalog1
        self.info['datalog2'] = datalog2
        self.info['fake_networks'] = fake_networks