def test_wifi_iface_list_option(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "open", "basic_rate": ["6000", "9000"] } } ] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' list basic_rate '6000' list basic_rate '9000' option device 'radio0' option ifname 'wlan0' option mode 'ap' option network 'wlan0' option ssid 'open' """) self.assertEqual(o.render(), expected)
def test_macaddr_override(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "mac": "E8:94:F6:33:8C:00", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "open" } } ] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option macaddr 'E8:94:F6:33:8C:00' option mode 'ap' option network 'wlan0' option ssid 'open' """) self.assertEqual(o.render(), expected)
def test_dns(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [ { "address": "192.168.1.1", "mask": 24, "proto": "static", "family": "ipv4" } ] } ], "dns_servers": [ "10.11.12.13", "8.8.8.8" ], "dns_search": [ "netjson.org", "openwisp.org", ] }) expected = self._tabs("""package network config interface 'eth0' option dns '10.11.12.13 8.8.8.8' option dns_search 'netjson.org openwisp.org' option ifname 'eth0' option ipaddr '192.168.1.1/24' option proto 'static' """) self.assertEqual(o.render(), expected)
def test_rules(self): o = OpenWrt({ "ip_rules": [ { "in": "eth0", "out": "eth1", "src": "192.168.1.0/24", "dest": "192.168.2.0/24", "tos": 2, "mark": "0x0/0x1", "invert": True, "lookup": "0", "action": "blackhole" }, { "src": "192.168.1.0/24", "dest": "192.168.3.0/24", "goto": 0 }, { "in": "vpn", "dest": "fdca:1234::/64", "action": "prohibit" }, { "in": "vpn", "src": "fdca:1235::/64", "action": "prohibit" } ] }) expected = self._tabs("""package network config rule option action 'blackhole' option dest '192.168.2.0/24' option in 'eth0' option invert '1' option lookup '0' option mark '0x0/0x1' option out 'eth1' option src '192.168.1.0/24' option tos '2' config rule option dest '192.168.3.0/24' option goto '0' option src '192.168.1.0/24' config rule6 option action 'prohibit' option dest 'fdca:1234::/64' option in 'vpn' config rule6 option action 'prohibit' option in 'vpn' option src 'fdca:1235::/64' """) self.assertEqual(o.render(), expected)
def test_wds_ap(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "wds": True, "ssid": "MyWdsAp" } } ] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' option network 'wlan0' option ssid 'MyWdsAp' option wds '1' """) self.assertEqual(o.render(), expected)
def test_interface_custom_attrs(self): o = OpenWrt({ "interfaces": [ { "name": "mobile0", "type": "wireless", "mtu": 1400, "custom_attr": "yes", "empty": "", "addresses": [ { "proto": "3g", "family": "ipv4" } ] } ] }) expected = self._tabs("""package network config interface 'mobile0' option custom_attr 'yes' option ifname 'mobile0' option mtu '1400' option proto '3g' """) self.assertEqual(o.render(), expected)
def test_custom_proto(self): o = OpenWrt({ "interfaces": [ { "name": "ppp0", "type": "other", "proto": "ppp", "device": "/dev/usb/modem1", "username": "******", "password": "******", "keepalive": 3, "ipv6": True } ] }) expected = self._tabs("""package network config interface 'ppp0' option device '/dev/usb/modem1' option ifname 'ppp0' option ipv6 '1' option keepalive '3' option password 'pwd0123' option proto 'ppp' option username 'user1' """) self.assertEqual(o.render(), expected)
def test_network_dash_conversion(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "open", "network": ["eth0-1"], } } ] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' option network 'eth0_1' option ssid 'open' """) self.assertEqual(o.render(), expected)
def test_multiple_dhcp(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [ { "proto": "dhcp", "family": "ipv4" }, { "proto": "dhcp", "family": "ipv6" } ] } ] }) expected = self._tabs("""package network config interface 'eth0' option ifname 'eth0' option proto 'dhcp' config interface 'eth0_2' option ifname 'eth0' option proto 'dhcpv6' """) self.assertEqual(o.render(), expected)
def test_file_inclusion(self): o = OpenWrt({ "files": [ { "path": "/etc/crontabs/root", "mode": "0644", "contents": '* * * * * echo "test" > /etc/testfile\n' '* * * * * echo "test2" > /etc/testfile2' }, { "path": "/etc/dummy.conf", "mode": "0644", "contents": "testing!" } ] }) output = o.render() self.assertNotIn('package files', output) self.assertIn('* * * * * echo', output) # ensure the additional files are there present in the tar.gz archive tar = tarfile.open(fileobj=o.generate(), mode='r') self.assertEqual(len(tar.getmembers()), 2) # first file crontab = tar.getmember('etc/crontabs/root') contents = tar.extractfile(crontab).read().decode() self.assertEqual(contents, o.config['files'][0]['contents']) self.assertEqual(crontab.mtime, 0) self.assertEqual(crontab.mode, 420) # second file dummy = tar.getmember('etc/dummy.conf') contents = tar.extractfile(dummy).read().decode() self.assertEqual(contents, o.config['files'][1]['contents']) self.assertEqual(dummy.mode, 420) tar.close()
def test_default_addresses(self): """ the following configuration dictionary caused empty output up to 0.4.0 """ o = OpenWrt({ "interfaces": [ { "type": "bridge", "network": "lan", "addresses": [], "name": "br-lan", "bridge_members": [ "eth0", "eth1" ] } ] }) expected = self._tabs("""package network config interface 'lan' option ifname 'eth0 eth1' option proto 'none' option type 'bridge' """) self.assertEqual(o.render(), expected)
def test_loopback(self): o = OpenWrt({ "interfaces": [ { "name": "lo", "type": "loopback", "addresses": [ { "address": "127.0.0.1", "mask": 8, "proto": "static", "family": "ipv4" } ] } ] }) expected = self._tabs("""package network config interface 'lo' option ifname 'lo' option ipaddr '127.0.0.1/8' option proto 'static' """) self.assertEqual(o.render(), expected)
def test_server_bridge_routed(self): c = OpenWrt({ "openvpn": [{ "ca": "ca.pem", "cert": "cert.pem", "dev": "tap0", "dev_type": "tap", "dh": "dh.pem", "enabled": True, "key": "key.pem", "mode": "server", "name": "routed", "proto": "udp", "server": "10.8.0.0 255.255.0.0", "tls_server": True }] }) expected = self._tabs("""package openvpn config openvpn 'routed' option ca 'ca.pem' option cert 'cert.pem' option dev 'tap0' option dev_type 'tap' option dh 'dh.pem' option enabled '1' option key 'key.pem' option mode 'server' option proto 'udp' option server '10.8.0.0 255.255.0.0' option tls_server '1' """) self.assertEqual(c.render(), expected)
def test_enabled_missing(self): c = OpenWrt({ "openvpn": [{ "ca": "ca.pem", "cert": "cert.pem", "dev": "tap0", "dev_type": "tap", "dh": "dh.pem", "key": "key.pem", "mode": "server", "name": "test-properties", "proto": "udp", "tls_server": True }] }) expected = self._tabs("""package openvpn config openvpn 'test_properties' option ca 'ca.pem' option cert 'cert.pem' option dev 'tap0' option dev_type 'tap' option dh 'dh.pem' option enabled '1' option key 'key.pem' option mode 'server' option proto 'udp' option tls_server '1' """) self.assertEqual(c.render(), expected)
def test_led_1(self): o = OpenWrt({ "led": [ { "name": "USB1", "sysfs": "tp-link:green:usb1", "trigger": "usbdev", "dev": "1-1.1", "interval": 50, }, { "name": "WLAN2G", "sysfs": "tp-link:blue:wlan2g", "trigger": "phy0tpt" } ] }) expected = self._tabs("""package system config led 'led_usb1' option dev '1-1.1' option interval '50' option name 'USB1' option sysfs 'tp-link:green:usb1' option trigger 'usbdev' config led 'led_wlan2g' option name 'WLAN2G' option sysfs 'tp-link:blue:wlan2g' option trigger 'phy0tpt' """) self.assertEqual(o.render(), expected)
def test_dns_dhcpv6_ignored(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [ { "proto": "dhcp", "family": "ipv6" } ] } ], "dns_servers": ["10.11.12.13", "8.8.8.8"], "dns_search": ["netjson.org", "openwisp.org"], }) expected = self._tabs("""package network config interface 'eth0' option dns_search 'netjson.org openwisp.org' option ifname 'eth0' option proto 'dhcpv6' """) self.assertEqual(o.render(), expected)
def test_radio_list_option(self): o = OpenWrt({ "radios": [ { "name": "radio0", "phy": "phy0", "driver": "mac80211", "protocol": "802.11n", "channel": 1, "channel_width": 20, "ht_capab": ["SMPS-STATIC", "SHORT-GI-20"] } ] }) expected = self._tabs("""package wireless config wifi-device 'radio0' option channel '1' list ht_capab 'SMPS-STATIC' list ht_capab 'SHORT-GI-20' option htmode 'HT20' option hwmode '11g' option phy 'phy0' option type 'mac80211' """) self.assertEqual(o.render(), expected)
def test_radio_mac80211b(self): o = OpenWrt({ "radios": [ { "name": "radio0", "phy": "phy0", "driver": "mac80211", "protocol": "802.11b", "channel": 3, "channel_width": 20, "tx_power": 3 } ] }) expected = self._tabs("""package wireless config wifi-device 'radio0' option channel '3' option htmode 'NONE' option hwmode '11b' option phy 'phy0' option txpower '3' option type 'mac80211' """) self.assertEqual(o.render(), expected)
def test_radio_ac_and_custom_attrs(self): o = OpenWrt({ "radios": [ { "name": "radio0", "phy": "phy0", "driver": "mac80211", "protocol": "802.11ac", "channel": 132, "channel_width": 80, "tx_power": 8, "diversity": True, "country_ie": True, "empty_setting": "" } ] }) expected = self._tabs("""package wireless config wifi-device 'radio0' option channel '132' option country_ie '1' option diversity '1' option htmode 'VHT80' option hwmode '11a' option phy 'phy0' option txpower '8' option type 'mac80211' """) self.assertEqual(o.render(), expected)
def test_address_list_option(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [ { "proto": "dhcp", "family": "ipv4", "reqopts": ["43", "54"] } ] } ] }) expected = self._tabs("""package network config interface 'eth0' option ifname 'eth0' option proto 'dhcp' list reqopts '43' list reqopts '54' """) self.assertEqual(o.render(), expected)
def test_no_encryption(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "open", "encryption": {"protocol": "none"} } } ] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' option network 'wlan0' option ssid 'open' """) self.assertEqual(o.render(), expected)
def test_ipv4_routes(self): o = OpenWrt({ "interfaces": [ { "name": "eth1", "type": "ethernet", "addresses": [ { "address": "192.168.1.1", "mask": 24, "proto": "static", "family": "ipv4" } ] } ], "routes": [ { "device": "eth1", "destination": "192.168.3.1/24", "next": "192.168.2.1" }, { "device": "eth1", "destination": "192.168.4.1/24", "next": "192.168.2.2", "cost": 2, "source": "192.168.1.10", "table": 2, "onlink": True, "mtu": 1450 } ] }) expected = self._tabs("""package network config interface 'eth1' option ifname 'eth1' option ipaddr '192.168.1.1/24' option proto 'static' config route 'route1' option gateway '192.168.2.1' option interface 'eth1' option netmask '255.255.255.0' option target '192.168.3.1' config route 'route2' option gateway '192.168.2.2' option interface 'eth1' option metric '2' option mtu '1450' option netmask '255.255.255.0' option onlink '1' option source '192.168.1.10' option table '2' option target '192.168.4.1' """) self.assertEqual(o.render(), expected)
def test_no_variables_found(self): config = { "general": { "description": "{{ desc }}", } } o = OpenWrt(config, context={"a": "b"}) output = o.render() self.assertIn("option description '{{ desc }}'", output)
def test_warning(self): o = OpenWrt({ "luci": [ { "unrecognized": True } ] }) self.assertEqual(o.render(), 'package luci\n')
def test_ula_prefix(self): o = OpenWrt({ "general": {"ula_prefix": "fd8e:f40a:6701::/48"} }) expected = self._tabs("""package network config globals 'globals' option ula_prefix 'fd8e:f40a:6701::/48' """) self.assertEqual(o.render(), expected)
def test_template(self): config = {"general": {"hostname": "{{ name }}"}} template = {"general": {"description": "{{ desc }}"}} context = { "name": "test-context-name", "desc": "test.context.desc" } o = OpenWrt(config, context=context, templates=[template]) output = o.render() self.assertIn("option hostname 'test-context-name'", output) self.assertIn("option description 'test.context.desc'", output)
def test_evaluation_order(self): config = { "general": { "timezone": "{{ tz }}", } } context = {"tz": "Europe/Amsterdam"} o = OpenWrt(config, context=context) line = "option timezone '{Europe/Amsterdam}'".format(**timezones) output = o.render() self.assertIn(line, output)
def test_ipv6_routes(self): o = OpenWrt({ "interfaces": [ { "name": "eth1", "type": "ethernet", "addresses": [ { "address": "fd87::1", "mask": 128, "proto": "static", "family": "ipv6" } ] } ], "routes": [ { "device": "eth1", "destination": "fd89::1/128", "next": "fd88::1", "cost": 0 }, { "device": "eth1", "destination": "fd90::1/128", "next": "fd88::2", "cost": 3, "source": "fd87::10" } ] }) expected = self._tabs("""package network config interface 'eth1' option ifname 'eth1' option ip6addr 'fd87::1/128' option proto 'static' config route6 'route1' option gateway 'fd88::1' option interface 'eth1' option metric '0' option target 'fd89::1/128' config route6 'route2' option gateway 'fd88::2' option interface 'eth1' option metric '3' option source 'fd87::10' option target 'fd90::1/128' """) self.assertEqual(o.render(), expected)
def test_multiple_ip(self): o = OpenWrt({ "interfaces": [ { "name": "eth0.1", "type": "ethernet", "autostart": True, "addresses": [ { "address": "192.168.1.1", "mask": 24, "proto": "static", "family": "ipv4" }, { "address": "192.168.2.1", "mask": 24, "proto": "static", "family": "ipv4" }, { "address": "fd87::1", "mask": 128, "proto": "static", "family": "ipv6" } ] } ] }) expected = self._tabs("""package network config interface 'eth0_1' option auto '1' option ifname 'eth0.1' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option proto 'static' config interface 'eth0_1_2' option auto '1' option ifname 'eth0.1' option ipaddr '192.168.2.1' option netmask '255.255.255.0' option proto 'static' config interface 'eth0_1_3' option auto '1' option ifname 'eth0.1' option ip6addr 'fd87::1/128' option proto 'static' """) self.assertEqual(o.render(), expected)
def test_mesh_80211s(self): o = OpenWrt({ "interfaces": [ { "name": "mesh0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "802.11s", "mesh_id": "ninux", "network": ["lan"] } }, { "name": "lan", "type": "bridge", "bridge_members": ["mesh0"], "addresses": [ { "address": "192.168.0.1", "mask": 24, "proto": "static", "family": "ipv4" } ] } ] }) expected = self._tabs("""package network config interface 'mesh0' option ifname 'mesh0' option proto 'none' config interface 'lan' option ifname 'mesh0' option ipaddr '192.168.0.1' option netmask '255.255.255.0' option proto 'static' option type 'bridge' package wireless config wifi-iface 'wifi_mesh0' option device 'radio0' option ifname 'mesh0' option mesh_id 'ninux' option mode 'mesh' option network 'lan' """) self.assertEqual(o.render(), expected)
def test_render_interface_list_option(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "ip6class": ["wan6", "backbone"] } ] }) expected = self._tabs("""package network config interface 'eth0' option ifname 'eth0' list ip6class 'wan6' list ip6class 'backbone' option proto 'none' """) self.assertEqual(o.render(), expected)
def test_render_radio(self): o = OpenWrt({ "radios": [{ "name": "radio0", "phy": "phy0", "driver": "mac80211", "protocol": "802.11n", "channel": 140, "channel_width": 20, "country": "00" }, { "name": "radio1", "phy": "phy1", "driver": "mac80211", "protocol": "802.11n", "channel": 136, "channel_width": 40, "tx_power": 18, "country": "00", "disabled": True }] }) expected = self._tabs("""package wireless config wifi-device 'radio0' option channel '140' option country '00' option htmode 'HT20' option hwmode '11a' option phy 'phy0' option type 'mac80211' config wifi-device 'radio1' option channel '136' option country '00' option disabled '1' option htmode 'HT40' option hwmode '11a' option phy 'phy1' option txpower '18' option type 'mac80211' """) self.assertEqual(o.render(), expected)
def test_render_radio_2ghz_mac80211(self): o = OpenWrt({ "radios": [ { "name": "radio0", "phy": "phy0", "driver": "mac80211", "protocol": "802.11n", "channel": 3, "channel_width": 20, "tx_power": 3, }, { "name": "radio1", "phy": "phy1", "driver": "mac80211", "protocol": "802.11g", "channel": 3, "channel_width": 20, "tx_power": 3, }, ] }) expected = self._tabs("""package wireless config wifi-device 'radio0' option channel '3' option htmode 'HT20' option hwmode '11g' option phy 'phy0' option txpower '3' option type 'mac80211' config wifi-device 'radio1' option channel '3' option htmode 'NONE' option hwmode '11g' option phy 'phy1' option txpower '3' option type 'mac80211' """) self.assertEqual(o.render(), expected)
def test_additional_properties(self): c = OpenWrt({ "openvpn": [{ "ca": "ca.pem", "cert": "cert.pem", "dev": "tap0", "dev_type": "tap", "dh": "dh.pem", "disabled": False, "key": "key.pem", "mode": "server", "name": "test-properties", "proto": "udp", "tls_server": True, "z_falsy": False, "z_list": ["test1", "test2"], "z_number": 5, "z_string": "string", "z_true_val": True, }] }) expected = self._tabs("""package openvpn config openvpn 'test_properties' option ca 'ca.pem' option cert 'cert.pem' option dev 'tap0' option dev_type 'tap' option dh 'dh.pem' option enabled '1' option key 'key.pem' option mode 'server' option proto 'udp' option tls_server '1' option z_falsy '0' list z_list 'test1' list z_list 'test2' option z_number '5' option z_string 'string' option z_true_val '1' """) self.assertEqual(c.render(), expected)
def test_dns_search_override(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "dns_search": ["openwisp.org", "netjson.org"] } ], "dns_search": ["domain.com"] }) expected = self._tabs("""package network config interface 'eth0' list dns_search 'openwisp.org' list dns_search 'netjson.org' option ifname 'eth0' option proto 'none' """) self.assertEqual(o.render(), expected)
def test_render_interface_custom_attrs(self): o = OpenWrt({ "interfaces": [{ "name": "mobile0", "type": "other", "mtu": 1400, "custom_attr": "yes", "empty": "", "proto": "3g", }] }) expected = self._tabs("""package network config interface 'mobile0' option custom_attr 'yes' option ifname 'mobile0' option mtu '1400' option proto '3g' """) self.assertEqual(o.render(), expected)
def test_dns_override(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "dns": ["8.8.8.8", "8.8.4.4"] } ], "dns_servers": ["192.168.3.1", "192.168.3.2"] }) expected = self._tabs("""package network config interface 'eth0' list dns '8.8.8.8' list dns '8.8.4.4' option ifname 'eth0' option proto 'none' """) self.assertEqual(o.render(), expected)
def test_isolate(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "open", "isolate": True } } ] }) self.assertIn("option isolate '1'", o.render()) # try entering an invalid value o.config['interfaces'][0]['wireless']['isolate'] = 'wrong' with self.assertRaises(ValidationError): o.validate()
def test_wpa2_enterprise_client(self): o = OpenWrt({ "interfaces": [{ "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "station", "ssid": "enterprise-client", "bssid": "00:26:b9:20:5f:09", "encryption": { "protocol": "wpa2_enterprise", "cipher": "auto", "eap_type": "tls", "identity": "test-identity", "password": "******", } } }] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option bssid '00:26:b9:20:5f:09' option device 'radio0' option eap_type 'tls' option encryption 'wpa2' option identity 'test-identity' option ifname 'wlan0' option mode 'sta' option network 'wlan0' option password 'test-password' option ssid 'enterprise-client' """) self.assertEqual(o.render(), expected)
def test_render_multiple_ip_and_dhcp(self): o = OpenWrt( { "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [ {"proto": "dhcp", "family": "ipv4"}, { "address": "192.168.1.1", "mask": 24, "proto": "static", "family": "ipv4", }, { "address": "192.168.2.1", "mask": 24, "proto": "static", "family": "ipv4", }, ], } ] } ) expected = self._tabs( """package network config interface 'eth0' option ifname 'eth0' list ipaddr '192.168.1.1/24' list ipaddr '192.168.2.1/24' option proto 'static' config interface 'eth0_2' option ifname 'eth0' option proto 'dhcp' """ ) self.assertEqual(o.render(), expected)
def test_render_disabled(self): c = OpenWrt( { "openvpn": [ { "ca": "ca.pem", "cert": "cert.pem", "dev": "tap0", "dev_type": "tap", "dh": "dh.pem", "disabled": True, "key": "key.pem", "mode": "server", "name": "test_disabled", "proto": "udp", "tls_server": True, } ] } ) self.assertIn("option enabled '0'", c.render())
def test_auto_80211a_channel(self): o = OpenWrt({ "radios": [{ "name": "radio0", "phy": "phy0", "driver": "mac80211", "protocol": "802.11a", "channel": 0, "channel_width": 20, }] }) expected = self._tabs("""package wireless config wifi-device 'radio0' option channel 'auto' option htmode 'NONE' option hwmode '11a' option phy 'phy0' option type 'mac80211' """) self.assertEqual(o.render(), expected)
def test_default_addresses(self): """ the following configuration dictionary caused empty output up to 0.4.0 """ o = OpenWrt({ "interfaces": [{ "type": "bridge", "network": "lan", "addresses": [], "name": "br-lan", "bridge_members": ["eth0", "eth1"], }] }) expected = self._tabs("""package network config interface 'lan' option ifname 'eth0 eth1' option proto 'none' option type 'bridge' """) self.assertEqual(o.render(), expected)
def test_render_invalid_uci_name(self): o = OpenWrt({ "olsrd2": [{ "lan": "10.150.25.0/24 domain=0", "config_value": "lan-hna", "config_name": "olsrv2" }, { "lan": "0.0.0.0/24 domain=1", "config_value": "internet-hna", "config_name": "olsrv2" }], }) expected = self._tabs("""package olsrd2 config olsrv2 'lan_hna' option lan '10.150.25.0/24 domain=0' config olsrv2 'internet_hna' option lan '0.0.0.0/24 domain=1' """) self.assertEqual(o.render(), expected)
def test_render_dhcp(self): o = OpenWrt( { "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [{"proto": "dhcp", "family": "ipv4"}], } ] } ) expected = self._tabs( """package network config interface 'eth0' option ifname 'eth0' option proto 'dhcp' """ ) self.assertEqual(o.render(), expected)
def test_dns_dhcpv6_ignored(self): o = OpenWrt({ "interfaces": [{ "name": "eth0", "type": "ethernet", "addresses": [{ "proto": "dhcp", "family": "ipv6" }], }], "dns_servers": ["10.11.12.13", "8.8.8.8"], "dns_search": ["netjson.org", "openwisp.org"], }) expected = self._tabs("""package network config interface 'eth0' option dns_search 'netjson.org openwisp.org' option ifname 'eth0' option proto 'dhcpv6' """) self.assertEqual(o.render(), expected)
def test_wifi_options_zero(self): """ ensure ack_distance, rts_threshold and frag_threshold are ignored if left empty """ o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "MyWifiAP", "wmm": True, "ack_distance": 0, "rts_threshold": 0, "frag_threshold": 0 } } ] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' option network 'wlan0' option ssid 'MyWifiAP' option wmm '1' """) self.assertEqual(o.render(), expected)
def test_igmp(self): o = OpenWrt({ "interfaces": [{ "name": "br-lan", "type": "bridge", "igmp_snooping": True, "bridge_members": ["eth0", "eth1"], }] }) expected = self._tabs("""package network config interface 'br_lan' option ifname 'eth0 eth1' option igmp_snooping '1' option proto 'none' option type 'bridge' """) self.assertEqual(o.render(), expected) # try entering an invalid value o.config['interfaces'][0]['igmp_snooping'] = 'wrong' with self.assertRaises(ValidationError): o.validate()
def test_no_status_file(self): c = OpenWrt( { "openvpn": [ { "ca": "ca.pem", "cert": "cert.pem", "dev": "tap0", "dev_type": "tap", "dh": "dh.pem", "disabled": False, "key": "key.pem", "mode": "server", "name": "test-no-status", "proto": "udp", "status": "", "status_version": 1, "tls_server": True, } ] } ) expected = self._tabs( """package openvpn config openvpn 'test_no_status' option ca 'ca.pem' option cert 'cert.pem' option dev 'tap0' option dev_type 'tap' option dh 'dh.pem' option enabled '1' option key 'key.pem' option mode 'server' option proto 'udp' option tls_server '1' """ ) self.assertEqual(c.render(), expected)
def test_inherit_disabled_from_interface(self): """ see issue #35 https://github.com/openwisp/netjsonconfig/issues/35 """ o = OpenWrt({ "interfaces": [ { "disabled": True, "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "station", "ssid": "mywifi", "bssid": "00:11:22:33:44:55" } } ] }) expected = self._tabs("""package network config interface 'wlan0' option enabled '0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option bssid '00:11:22:33:44:55' option device 'radio0' option disabled '1' option ifname 'wlan0' option mode 'sta' option network 'wlan0' option ssid 'mywifi' """) self.assertEqual(o.render(), expected)
def test_wifi_macfilter(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "MyWifiAP", "macfilter": "deny", "maclist": [ "E8:94:F6:33:8C:1D", "42:6c:8f:95:0f:00" ] } } ] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option macfilter 'deny' list maclist 'E8:94:F6:33:8C:1D' list maclist '42:6c:8f:95:0f:00' option mode 'ap' option network 'wlan0' option ssid 'MyWifiAP' """) self.assertEqual(o.render(), expected)
def test_render_single_ipv6(self): o = OpenWrt({ "interfaces": [{ "name": "eth0", "type": "ethernet", "addresses": [{ "address": "fd87::2", "mask": 64, "proto": "static", "family": "ipv6", }], }] }) expected = self._tabs("""package network config interface 'eth0' option ifname 'eth0' option ip6addr 'fd87::2/64' option proto 'static' """) self.assertEqual(o.render(), expected)
def test_render_address_list_option(self): o = OpenWrt({ "interfaces": [{ "name": "eth0", "type": "ethernet", "addresses": [{ "proto": "dhcp", "family": "ipv4", "reqopts": ["43", "54"] }], }] }) expected = self._tabs("""package network config interface 'eth0' option ifname 'eth0' option proto 'dhcp' list reqopts '43' list reqopts '54' """) self.assertEqual(o.render(), expected)
def test_loopback(self): o = OpenWrt({ "interfaces": [{ "name": "lo", "type": "loopback", "addresses": [{ "address": "127.0.0.1", "mask": 8, "proto": "static", "family": "ipv4" }] }] }) expected = self._tabs("""package network config interface 'lo' option ifname 'lo' option ipaddr '127.0.0.1/8' option proto 'static' """) self.assertEqual(o.render(), expected)
def test_render_empty_bridge(self): o = OpenWrt( { "interfaces": [ { "network": "lan", "name": "br-lan", "type": "bridge", "bridge_members": [], } ] } ) expected = self._tabs( """package network config interface 'lan' option bridge_empty '1' option proto 'none' option type 'bridge' """ ) self.assertEqual(o.render(), expected)
def test_wps_ap(self): o = OpenWrt({ "interfaces": [{ "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "wps-ssid", "encryption": { "protocol": "wps", "wps_label": False, "wps_pushbutton": True, "wps_pin": "" } } }] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option encryption 'psk' option ifname 'wlan0' option mode 'ap' option network 'wlan0' option ssid 'wps-ssid' option wps_label '0' option wps_pushbutton '1' """) self.assertEqual(o.render(), expected)
def test_file_inclusion(self): o = OpenWrt({ "files": [ { "path": "/etc/crontabs/root", "mode": "0644", "contents": '* * * * * echo "test" > /etc/testfile\n' '* * * * * echo "test2" > /etc/testfile2', }, { "path": "/etc/dummy.conf", "mode": "0644", "contents": "testing!" }, ] }) output = o.render() self.assertNotIn('package files', output) self.assertIn('* * * * * echo', output) # ensure the additional files are there present in the tar.gz archive tar = tarfile.open(fileobj=o.generate(), mode='r') self.assertEqual(len(tar.getmembers()), 2) # first file crontab = tar.getmember('etc/crontabs/root') contents = tar.extractfile(crontab).read().decode() self.assertEqual(contents, o.config['files'][0]['contents']) self.assertEqual(crontab.mtime, 0) self.assertEqual(crontab.mode, 420) # second file dummy = tar.getmember('etc/dummy.conf') contents = tar.extractfile(dummy).read().decode() self.assertEqual(contents, o.config['files'][1]['contents']) self.assertEqual(dummy.mode, 420) tar.close()
def test_network_attribute(self): o = OpenWrt({ "interfaces": [{ "name": "eth0", "type": "ethernet", "network": "lan", "addresses": [{ "address": "192.168.1.1", "mask": 24, "proto": "static", "family": "ipv4" }, { "address": "192.168.2.1", "mask": 24, "proto": "static", "family": "ipv4" }] }] }) expected = self._tabs("""package network config interface 'lan' option ifname 'eth0' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option proto 'static' config interface 'lan_2' option ifname 'eth0' option ipaddr '192.168.2.1' option netmask '255.255.255.0' option proto 'static' """) self.assertEqual(o.render(), expected)
def test_wpa2_personal_adhoc(self): o = OpenWrt({ "interfaces": [{ "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "adhoc", "ssid": "encrypted-adhoc", "bssid": "00:26:b9:20:5f:09", "encryption": { "protocol": "wpa2_personal", "cipher": "auto", "key": "passphrase012345" } } }] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option bssid '00:26:b9:20:5f:09' option device 'radio0' option encryption 'psk2' option ifname 'wlan0' option key 'passphrase012345' option mode 'adhoc' option network 'wlan0' option ssid 'encrypted-adhoc' """) self.assertEqual(o.render(), expected)
def test_wpa_enterprise_ap(self): o = OpenWrt({ "interfaces": [{ "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "enterprise", "encryption": { "protocol": "wpa_enterprise", "cipher": "ccmp", "key": "radius_secret", "server": "192.168.0.1" } } }] }) expected = self._tabs("""package network config interface 'wlan0' option ifname 'wlan0' option proto 'none' package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option encryption 'wpa+ccmp' option ifname 'wlan0' option key 'radius_secret' option mode 'ap' option network 'wlan0' option server '192.168.0.1' option ssid 'enterprise' """) self.assertEqual(o.render(), expected)