def test_interface_get_by_id(dbsession): """Retrieve an interface by the id.""" interface = Interface("eth0") interface.save(dbsession) retrieved = Interface.get_by_id(interface.id) assert retrieved.id == interface.id
def set_interfaces(interfaces: List): """Set interface information into database and configure hardware accordingly. Args: interfaces (List): A list of dictionaries with the required information. """ session = get_session() wifi_ap_present = False for interface in interfaces: try: db_result = (session.query(Interface).filter_by( interface=interface["name"]).one()) except NoResultFound: db_result = Interface(interface["name"]) session.add(db_result) db_result.is_active = True db_result.is_for_fm = interface["is_for_fm"] db_result.state = interface["state"] if interface["state"] == "ap": wifi_ap_present = True if "creds" in interface: add_wifi_network( wifi_name=interface["creds"]["ssid"], wifi_password=interface["creds"]["password"], interface=db_result, ) session.commit() if wifi_ap_present: set_ap_mode() else: set_wpa_mode()
def test_create_wifi_with_interface(dbsession): """Create a WiFi instance with an interface.""" interface = Interface("eth0") interface.save(dbsession) wifi = Wifi() wifi.interface = interface wifi.save(dbsession) assert wifi.interface == interface assert wifi.interface_id == interface.id
def test_create_interface(dbsession): """Create a Interface instance.""" interface = Interface("eth0") interface.save(dbsession) assert interface.interface == "eth0" assert bool(interface.is_active) assert not bool(interface.is_for_fm) assert not bool(interface.is_external) assert interface.state is None assert interface.credentials == []
def test_add_wifi_network_with_interface(dbsession): """Test the add_wifi_netwrok function passing in an Interface.""" interface = Interface("wlan0") interface.state = "dhcp" interface.save(dbsession) wifi = add_wifi_network( wifi_name="TestWiFiName", wifi_password="******", interface=interface ) assert wifi.name == "TestWiFiName" assert wifi.password == "password" assert wifi.interface.id == interface.id assert wifi.mode == "dhcp" assert wifi in interface.credentials
def test_add_wifi_network(dbsession): """Test the add_wifi_network function.""" interface = Interface("wlan0") interface.state = "dhcp" interface.save(dbsession) wifi = add_wifi_network(wifi_name="TestWiFiName", wifi_password="******") dbsession.add(wifi) assert wifi.name == "TestWiFiName" assert wifi.password == "password" assert wifi.interface == interface assert wifi.mode == "dhcp" assert wifi in interface.credentials
def populate_interfaces(tables, dbsession): """Populate interfaces into the database.""" interface = Interface("wlan0") interface.is_active = True interface.is_for_fm = True interface.is_external = True interface.state = "dhcp" interface.save(dbsession)
def refresh_interfaces(): """Refresh all interfaces. Update with current information.""" session = get_session() ap_present = False interfaces = get_interfaces() # update all interfaces.active to be False by default session.query(Interface).update({Interface.is_active: False}) for my_interface in interfaces: try: interface = session.query(Interface).filter_by( interface=my_interface).one() interface.is_active = True # see if there is an interface that is configured for an ap if interface.state == "ap": ap_present = True # must be a new interface so lets add it except NoResultFound: new_interface = Interface(my_interface) new_interface.is_active = True new_interface.is_for_fm = False new_interface.state = "dhcp" session.add(new_interface) session.commit() session.close() if ap_present: set_ap_mode() else: set_wpa_mode()