Esempio n. 1
0
 def test_mirror(self):
     # Add and remove port mirroring.
     mirror_test_yaml_str = """
     {dps: {ovs: {
         dp_id: 1,
         hardware: Open vSwitch,
         interfaces: {
             1: {native_vlan: 100},
             2: {native_vlan: 100},
             3: {output_only: true, mirror: []}}}}}
     """
     mirror_test_yaml = yaml_load(mirror_test_yaml_str)
     response = self.client.set_config_file(
         yaml_load(mirror_test_yaml_str),
         config_filename=self.default_config,
         merge=False)
     assert response is not None
     response = self.client.add_port_mirror('ovs', 2, 3)
     assert response is not None
     assert mirror_test_yaml != self.client.get_config_file(
         config_filename=self.default_config)
     response = self.client.remove_port_mirror('ovs', 2, 3)
     assert response is not None
     assert mirror_test_yaml == self.client.get_config_file(
         config_filename=self.default_config)
     response = self.client.add_port_mirror('ovs', 2, 3)
     assert response is not None
     response = self.client.clear_port_mirror('ovs', 3)
     assert response is not None
     assert mirror_test_yaml == self.client.get_config_file(
         config_filename=self.default_config)
 def get_config_file(self, config_filename=None):
     """Get a YAML config file."""
     response = self._call(
         self._stub.GetConfigFile,
         faucetconfrpc_pb2.GetConfigFileRequest(
             config_filename=config_filename))
     if response is not None:
         return yaml_load(response.config_yaml)
     return None
Esempio n. 3
0
 def test_get(self):
     # Get of non-existent file.
     response = self.client.get_config_file(
         config_filename='nosuchfile.yaml')
     assert response is None
     # Get existing file
     response = self.client.get_config_file(
         config_filename=self.default_config)
     assert response == yaml_load(self.default_test_yaml_str)
Esempio n. 4
0
    def test_get_acl_names(self):
        include_name = os.path.join(self.tmpdir, 'include.yaml')
        include_yaml = yaml_load(
            '{acls: {anotheracl: [{rule: {actions: {allow: 0}}}]}}')
        acl_test_yaml = yaml_load("""
        {dps: {ovs: {
            dp_id: 1,
            hardware: Open vSwitch,
            interfaces: {
                1: {native_vlan: 100, acls_in: []},
                2: {native_vlan: 100},
                3: {output_only: true, mirror: [1]}}}},
         acls: {test: [{rule: {actions: {allow: 0}}}]},
         include: [%s]}
        """ % os.path.basename(include_name))

        with open(os.path.join(self.tmpdir, self.default_config),
                  'w') as test_yaml_file:
            test_yaml_file.write(yaml_dump(acl_test_yaml))  # pytype: disable=wrong-arg-types
        with open(include_name, 'w') as test_yaml_file:
            test_yaml_file.write(yaml_dump(include_yaml))  # pytype: disable=wrong-arg-types
        response = self.client.get_acl_names()
        assert response.acl_name == ["test", "anotheracl"]
Esempio n. 5
0
    def setUpClass(cls):
        cls.tmpdir = tempfile.mkdtemp()
        cls.default_test_yaml = yaml_load(cls.default_test_yaml_str)
        cls.default_config = 'test.yaml'
        with open(os.path.join(cls.tmpdir, cls.default_config),
                  'w') as test_yaml_file:
            test_yaml_file.write(cls.default_test_yaml_str)  # pytype: disable=wrong-arg-types

        cls.host = 'localhost'
        cls.port = 59999
        cls.prom_port = 59998
        certstrap = shutil.which('certstrap')
        cls.assertTrue(certstrap, 'certstrap not found')
        for cmd in (['init', '--common-name', 'ca', '--passphrase', ''], [
                'request-cert', '--common-name', 'client', '--passphrase', ''
        ], ['sign', 'client', '--CA', 'ca'
            ], ['request-cert', '--common-name', cls.host, '--passphrase',
                ''], ['sign', cls.host, '--CA', 'ca']):
            subprocess.check_call([certstrap, '--depot-path', cls.tmpdir] +
                                  cmd)
        client_key = os.path.join(cls.tmpdir, 'client.key')
        client_cert = os.path.join(cls.tmpdir, 'client.crt')
        server_key = os.path.join(cls.tmpdir, '%s.key' % cls.host)
        server_cert = os.path.join(cls.tmpdir, '%s.crt' % cls.host)
        ca_cert = os.path.join(cls.tmpdir, 'ca.crt')

        cls.server = subprocess.Popen([
            'timeout',
            '10s',
            './faucetconfrpc/faucetconfrpc_server.py',
            '--config_dir=%s' % cls.tmpdir,
            '--default_config=%s' % cls.default_config,
            '--port=%u' % cls.port,
            '--prom_port=%u' % cls.prom_port,
            '--host=%s' % cls.host,
            '--key=%s' % server_key,
            '--cert=%s' % server_cert,
            '--cacert=%s' % ca_cert,
        ],
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.STDOUT)
        cls._wait_for_port(cls.host, cls.port)
        cls._wait_for_port(cls.host, cls.prom_port)
        server_addr = '%s:%u' % (cls.host, cls.port)
        cls.client = FaucetConfRpcClient(client_key, client_cert, ca_cert,
                                         server_addr)
Esempio n. 6
0
    def test_set(self):
        # Replace existing file with new content
        response = self.client.set_config_file(
            self.default_test_yaml_str,
            config_filename=self.default_config,
            merge=False)
        assert response is not None
        assert self.default_test_yaml == self.client.get_config_file(
            self.default_config)
        # Replace existing file with broken content
        response = self.client.set_config_file(
            '{dps: {ovs: {dp_id: NO, hardware: xyz vSwitch, interfaces: {1: {blah: 100}}}}}',
            config_filename=self.default_config,
            merge=False)
        assert response is None
        # File didn't change.
        assert self.default_test_yaml == self.client.get_config_file(
            self.default_config)

        # Merge new content into existing file
        response = self.client.set_config_file(
            self.default_test_yaml_str,
            config_filename=self.default_config,
            merge=True)
        assert response is not None
        assert self.default_test_yaml == self.client.get_config_file(
            config_filename=self.default_config)
        new_test_yaml = yaml_load(
            '{dps: {ovs: {interfaces: {3: {description: test}}}}}')
        response = self.client.set_config_file(
            new_test_yaml, config_filename=self.default_config, merge=True)
        assert response is not None
        new_test_yaml = {
            'dps': {
                'ovs': {
                    'dp_id': 1,
                    'hardware': 'Open vSwitch',
                    'interfaces': {
                        1: {
                            'native_vlan': 100,
                            'acls_in': []
                        },
                        2: {
                            'native_vlan': 100
                        },
                        3: {
                            'output_only': True,
                            'description': 'test',
                            'mirror': [1]
                        }
                    }
                }
            },
            'acls': {
                'test': [{
                    'rule': {
                        'actions': {
                            'allow': 0
                        }
                    }
                }]
            }
        }
        assert new_test_yaml == self.client.get_config_file(
            config_filename=self.default_config)

        # Test replace operation.
        new_test_yaml = yaml_load(
            '{dps: {ovs: {interfaces: {3: {description: replaced, output_only: true}}}}}'
        )
        del_config_yaml_keys = '[dps, ovs, interfaces, 3]'
        response = self.client.set_config_file(
            new_test_yaml,
            config_filename=self.default_config,
            merge=True,
            del_config_yaml_keys=del_config_yaml_keys)
        assert response is not None
        assert (new_test_yaml['dps']['ovs']['interfaces'][3] ==
                self.client.get_config_file(config_filename=self.default_config
                                            )['dps']['ovs']['interfaces'][3])
Esempio n. 7
0
 def setUp(self):
     self.default_test_yaml = yaml_load(self.default_test_yaml_str)
     assert self.client.set_config_file(self.default_test_yaml_str,
                                        config_filename=self.default_config,
                                        merge=False)
Esempio n. 8
0
def yaml_sanity():
    """Ensure YAML libraries present."""
    test_dict = {'test': 'value'}
    test_yaml = yaml_dump(test_dict)
    assert yaml_load(test_yaml) == test_dict