def _initialize_faucetizer(self): forch_config = dict_proto(yaml.safe_load(self.FORCH_CONFIG), ForchConfig) self._faucetizer = Faucetizer( forch_config.orchestration, self._temp_structural_config_file, self._temp_behavioral_config_file) self._faucetizer.reload_structural_config() self._faucetizer.reload_segments_to_vlans(self._segments_vlans_file)
def _initialize_faucetizer(self): self._orch_config = str_proto(self.ORCH_CONFIG, OrchestrationConfig) self._faucetizer = Faucetizer( self._orch_config, self._temp_structural_config_file, self._temp_behavioral_config_file) self._faucetizer.reload_structural_config() if self._temp_segments_vlans_file: self._faucetizer.reload_segments_to_vlans(self._temp_segments_vlans_file)
class FaucetizerTestBase(unittest.TestCase): """Base class for Faucetizer unit tests""" ORCH_CONFIG = '' FAUCET_STRUCTURAL_CONFIG = '' FAUCET_BEHAVIORAL_CONFIG = '' SEGMENTS_TO_VLANS = '' def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._faucetizer = None self._orch_config = None self._temp_dir = None self._temp_structural_config_file = None self._temp_behavioral_config_file = None self._temp_segments_vlans_file = None def _setup_config_files(self): self._temp_dir = tempfile.mkdtemp() _, self._temp_structural_config_file = tempfile.mkstemp( dir=self._temp_dir) _, self._temp_behavioral_config_file = tempfile.mkstemp( dir=self._temp_dir) with open(self._temp_structural_config_file, 'w') as structural_config_file: structural_config_file.write(self.FAUCET_STRUCTURAL_CONFIG) if self.SEGMENTS_TO_VLANS: _, self._temp_segments_vlans_file = tempfile.mkstemp( dir=self._temp_dir) with open(self._temp_segments_vlans_file, 'w') as segments_vlans_file: segments_vlans_file.write(self.SEGMENTS_TO_VLANS) def _cleanup_config_files(self): shutil.rmtree(self._temp_dir) def _initialize_faucetizer(self): self._orch_config = str_proto(self.ORCH_CONFIG, OrchestrationConfig) self._faucetizer = Faucetizer(self._orch_config, self._temp_structural_config_file, self._temp_behavioral_config_file) self._faucetizer.reload_structural_config() if self._temp_segments_vlans_file: self._faucetizer.reload_segments_to_vlans( self._temp_segments_vlans_file) def _process_device_placement(self, placement_tuple): self._faucetizer.process_device_placement( placement_tuple[0], dict_proto(placement_tuple[1], DevicePlacement), placement_tuple[2]) def _process_device_behavior(self, behavior_tuple): self._faucetizer.process_device_behavior( behavior_tuple[0], dict_proto(behavior_tuple[1], DeviceBehavior), behavior_tuple[2]) def _update_port_config(self, behavioral_config, **kwargs): port_config = behavioral_config['dps'][kwargs['switch']]['interfaces'][ kwargs['port']] port_config['native_vlan'] = kwargs.get('native_vlan') if 'role' in kwargs: port_config['acls_in'] = [f'role_{kwargs["role"]}'] if 'tail_acl' in kwargs: port_config.setdefault('acls_in', []).append(kwargs['tail_acl']) def _get_base_behavioral_config(self): base_behavioral_config = yaml.safe_load(self.FAUCET_BEHAVIORAL_CONFIG) if self._orch_config.unauthenticated_vlan: vlans_config = base_behavioral_config.setdefault('vlans', {}) vlans_config[self._orch_config.unauthenticated_vlan] = { 'acls_in': [f'uniform_{self._orch_config.unauthenticated_vlan}'], 'description': 'unauthenticated VLAN' } return base_behavioral_config def _verify_behavioral_config(self, expected_behavioral_config): with open(self._temp_behavioral_config_file ) as temp_behavioral_config_file: faucetizer_behavioral_config = yaml.safe_load( temp_behavioral_config_file) self.assertEqual(faucetizer_behavioral_config, expected_behavioral_config)
class FaucetizerTestBase(UnitTestBase): """Base class for Faucetizer unit tests""" FORCH_CONFIG = """ orchestration: unauthenticated_vlan: 100 """ FAUCET_STRUCTURAL_CONFIG = """ dps: t1sw1: dp_id: 111 interfaces: 1: output_only: true 4: description: TESTING tagged_vlans: [272] 6: stack: {dp: t2sw1, port: 6} 7: stack: {dp: t2sw2, port: 7} 23: lacp: 3 t2sw1: dp_id: 121 interfaces: 1: description: HOST max_hosts: 1 2: description: HOST max_hosts: 1 6: stack: {dp: t1sw1, port: 6} t2sw2: dp_id: 122 interfaces: 1: description: HOST max_hosts: 1 2: description: HOST max_hosts: 1 7: stack: {dp: t1sw1, port: 7} acls: role_red: - rule: dl_type: 0x800 actions: allow: True role_green: - rule: actions: allow: False tail_acl: - rule: actions: allow: True """ FAUCET_BEHAVIORAL_CONFIG = """ dps: t1sw1: dp_id: 111 interfaces: 1: output_only: true 4: description: TESTING tagged_vlans: [272] 6: stack: {dp: t2sw1, port: 6} 7: stack: {dp: t2sw2, port: 7} 23: lacp: 3 t2sw1: dp_id: 121 interfaces: 1: description: HOST max_hosts: 1 native_vlan: 100 2: description: HOST max_hosts: 1 native_vlan: 100 6: stack: {dp: t1sw1, port: 6} t2sw2: dp_id: 122 interfaces: 1: description: HOST max_hosts: 1 native_vlan: 100 2: description: HOST max_hosts: 1 native_vlan: 100 7: stack: {dp: t1sw1, port: 7} acls: role_red: - rule: cookie: 1 dl_type: 2048 actions: allow: True role_green: - rule: cookie: 2 actions: allow: False tail_acl: - rule: cookie: 3 actions: allow: True """ SEGMENTS_TO_VLANS = """ segments_to_vlans: SEG_A: 200 SEG_B: 300 SEG_C: 400 SEG_X: 1500 SEG_Y: 1600 SEG_Z: 1700 """ SEQUESTER_SEGMENT = 'TESTING' def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) os.environ['FORCH_LOG'] = _DEFAULT_FORCH_LOG self._faucetizer = None self._segments_vlans_file = None def _setup_config_files(self): super()._setup_config_files() _, self._segments_vlans_file = tempfile.mkstemp( dir=self._forch_config_dir) with open(self._segments_vlans_file, 'w') as segments_vlans_file: segments_vlans_file.write(self.SEGMENTS_TO_VLANS) def _initialize_faucetizer(self): forch_config = dict_proto(yaml.safe_load(self.FORCH_CONFIG), ForchConfig) self._faucetizer = Faucetizer(forch_config.orchestration, self._structural_config_file, self._behavioral_config_file, sequester_segment=self.SEQUESTER_SEGMENT) self._faucetizer.reload_structural_config() self._faucetizer.reload_segments_to_vlans(self._segments_vlans_file) def _process_device_placement(self, placement_tuple): self._faucetizer.process_device_placement( placement_tuple[0], dict_proto(placement_tuple[1], DevicePlacement), placement_tuple[2]) def _process_device_behavior(self, behavior_tuple): self._faucetizer.process_device_behavior( behavior_tuple[0], dict_proto(behavior_tuple[1], DeviceBehavior), behavior_tuple[2]) def _update_port_config(self, behavioral_config, **kwargs): port_config = behavioral_config['dps'][kwargs['switch']]['interfaces'][ kwargs['port']] port_config['native_vlan'] = kwargs.get('native_vlan') if 'role' in kwargs: port_config['acls_in'] = [f'role_{kwargs["role"]}'] if 'tail_acl' in kwargs: port_config.setdefault('acls_in', []).append(kwargs['tail_acl']) if 'tagged_vlans' in kwargs: port_config['tagged_vlans'] = kwargs['tagged_vlans'] def _verify_behavioral_config(self, expected_behavioral_config): with open(self._behavioral_config_file) as behavioral_config_file: faucetizer_behavioral_config = yaml.safe_load( behavioral_config_file) self.assertEqual(faucetizer_behavioral_config, expected_behavioral_config) def setUp(self): """setup fixture for each test method""" self._setup_config_files() self._initialize_faucetizer() def tearDown(self): """cleanup after each test method finishes""" self._faucetizer = None self._cleanup_config_files()