Пример #1
0
 def test_warning(self):
     o = OpenWrt({"luci": [{"unrecognized": True}]})
     self.assertEqual(o.render(), '')
Пример #2
0
 def test_parse_text(self):
     native = self._tabs(self._system_uci)
     o = OpenWrt(native=native)
     self.assertDictEqual(o.intermediate_data, self._system_intermediate)
Пример #3
0
 def test_render_wpa_personal(self):
     o = OpenWrt(self._wpa_personal_netjson)
     expected = self._tabs(self._wpa_personal_uci)
     self.assertEqual(o.render(), expected)
Пример #4
0
 def test_parse_simple_bridge(self):
     native = self._tabs(self._simple_bridge_uci)
     o = OpenWrt(native=native)
     self.assertEqual(o.config, self._simple_bridge_netjson)
Пример #5
0
 def test_parse_empty(self):
     OpenWrt(native='')
Пример #6
0
 def test_parse_ula_prefix_id(self):
     o = OpenWrt(native=self._ula_uci_id)
     self.assertEqual(o.config, self._ula_netjson_id)
Пример #7
0
 def test_parse_multiple_ip(self):
     o = OpenWrt(native=self._multi_ip_uci)
     self.assertEqual(o.config, self._multi_ip_netjson)
Пример #8
0
 def test_templates(self):
     loopback_template = {
         "interfaces": [
             {
                 "name": "lo",
                 "type": "loopback",
                 "addresses": [
                     {
                         "address": "127.0.0.1",
                         "mask": 8,
                         "proto": "static",
                         "family": "ipv4"
                     }
                 ]
             }
         ]
     }
     radio_template = {
         "interfaces": [
             {
                 "name": "wlan0",
                 "type": "wireless",
                 "addresses": [
                     {
                         "address": "192.168.1.1",
                         "mask": 24,
                         "proto": "static",
                         "family": "ipv4"
                     }
                 ],
                 "wireless": {
                     "radio": "radio0",
                     "mode": "access_point",
                     "ssid": "MyWifiAP",
                     "hidden": True
                 }
             }
         ],
         "radios": [
             {
                 "name": "radio0",
                 "phy": "phy0",
                 "driver": "mac80211",
                 "protocol": "802.11n",
                 "channel": 3,
                 "channel_width": 20,
                 "tx_power": 3
             }
         ]
     }
     config = {
         "general": {
             "hostname": "test_templates",
         }
     }
     o = OpenWrt(config, templates=[loopback_template, radio_template])
     self.assertEqual(o.config['general']['hostname'], 'test_templates')
     self.assertIn('radios', o.config)
     self.assertEqual(len(o.config['radios']), 1)
     self.assertEqual(o.config['radios'][0]['name'], 'radio0')
     self.assertIn('interfaces', o.config)
     self.assertEqual(len(o.config['interfaces']), 2)
     self.assertEqual(o.config['interfaces'][0]['name'], 'lo')
     self.assertEqual(o.config['interfaces'][1]['name'], 'wlan0')
Пример #9
0
 def test_string_argument(self):
     OpenWrt('{}')
Пример #10
0
    def test_generate(self):
        o = OpenWrt({
            "interfaces": [
                {
                    "name": "wlan0",
                    "type": "wireless",
                    "addresses": [
                        {
                            "address": "192.168.1.1",
                            "mask": 24,
                            "proto": "static",
                            "family": "ipv4"
                        }
                    ],
                    "wireless": {
                        "radio": "radio0",
                        "mode": "access_point",
                        "ssid": "MyWifiAP",
                        "hidden": True
                    }
                }
            ],
            "radios": [
                {
                    "name": "radio0",
                    "phy": "phy0",
                    "driver": "mac80211",
                    "protocol": "802.11n",
                    "channel": 3,
                    "channel_width": 20,
                    "tx_power": 3
                }
            ]
        })
        tar = tarfile.open(fileobj=o.generate(), mode='r')
        self.assertEqual(len(tar.getmembers()), 2)
        # network
        network = tar.getmember('etc/config/network')
        contents = tar.extractfile(network).read().decode()
        expected = self._tabs("""config interface 'wlan0'
    option ifname 'wlan0'
    option ipaddr '192.168.1.1'
    option netmask '255.255.255.0'
    option proto 'static'

""")
        self.assertEqual(contents, expected)
        # wireless
        wireless = tar.getmember('etc/config/wireless')
        contents = tar.extractfile(wireless).read().decode()
        expected = self._tabs("""config wifi-device 'radio0'
    option channel '3'
    option htmode 'HT20'
    option hwmode '11g'
    option phy 'phy0'
    option txpower '3'
    option type 'mac80211'

config wifi-iface 'wifi_wlan0'
    option device 'radio0'
    option hidden '1'
    option ifname 'wlan0'
    option mode 'ap'
    option network 'wlan0'
    option ssid 'MyWifiAP'
""")
        self.assertEqual(contents, expected)
        tar.close()
Пример #11
0
 def test_config_copy(self):
     config = {'interfaces': []}
     o = OpenWrt(config)
     o.validate()
     self.assertDictEqual(config, {'interfaces': []})
Пример #12
0
    def test_render_default(self):
        o = OpenWrt({
            "luci": [{
                "config_name": "core",
                "config_value": "main",
                "lang": "auto",
                "resourcebase": "/luci-static/resources",
                "mediaurlbase": "/luci-static/bootstrap",
                "number": 4,
                "boolean": True
            }],
            "firewall": [{
                "config_name": "rule",
                "name": "Allow-MLD",
                "src": "wan",
                "proto": "icmp",
                "src_ip": "fe80::/10",
                "family": "ipv6",
                "target": "ACCEPT",
                "icmp_type": ["130/0", "131/0", "132/0", "143/0"]
            }, {
                "config_name": "rule",
                "name": "Rule2",
                "src": "wan",
                "proto": "icmp",
                "src_ip": "192.168.1.1/24",
                "family": "ipv4",
                "target": "ACCEPT",
                "icmp_type": ["130/0", "131/0", "132/0", "143/0"]
            }]
        })
        expected = self._tabs("""package firewall

config rule 'rule_1'
    option family 'ipv6'
    list icmp_type '130/0'
    list icmp_type '131/0'
    list icmp_type '132/0'
    list icmp_type '143/0'
    option name 'Allow-MLD'
    option proto 'icmp'
    option src 'wan'
    option src_ip 'fe80::/10'
    option target 'ACCEPT'

config rule 'rule_2'
    option family 'ipv4'
    list icmp_type '130/0'
    list icmp_type '131/0'
    list icmp_type '132/0'
    list icmp_type '143/0'
    option name 'Rule2'
    option proto 'icmp'
    option src 'wan'
    option src_ip '192.168.1.1/24'
    option target 'ACCEPT'

package luci

config core 'main'
    option boolean '1'
    option lang 'auto'
    option mediaurlbase '/luci-static/bootstrap'
    option number '4'
    option resourcebase '/luci-static/resources'
""")
        self.assertEqual(o.render(), expected)
        # try a second time to ensure that the usage of dict.pop
        # in templates does not cause any issue
        self.assertEqual(o.render(), expected)
Пример #13
0
    def test_parse_default(self):
        native = self._tabs("""package firewall

config rule 'rule_1'
    option family 'ipv6'
    list icmp_type '130/0'
    list icmp_type '131/0'
    list icmp_type '132/0'
    list icmp_type '143/0'
    option name 'Allow-MLD'
    option proto 'icmp'
    option src 'wan'
    option src_ip 'fe80::/10'
    option target 'ACCEPT'

package luci

config core 'main'
    option boolean '1'
    option lang 'auto'
    option mediaurlbase '/luci-static/bootstrap'
    option number '4'
    option resourcebase '/luci-static/resources'

package network

config interface 'eth0'
    option ifname 'eth0'
    option proto 'none'

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 custom 'custom'
    option test '1'
""")
        o = OpenWrt(native=native)
        expected = {
            "luci": [{
                "config_name": "core",
                "config_value": "main",
                "lang": "auto",
                "resourcebase": "/luci-static/resources",
                "mediaurlbase": "/luci-static/bootstrap",
                "number": "4",
                "boolean": "1"
            }],
            "firewall": [{
                "config_name": "rule",
                "name": "Allow-MLD",
                "src": "wan",
                "proto": "icmp",
                "src_ip": "fe80::/10",
                "family": "ipv6",
                "target": "ACCEPT",
                "icmp_type": ["130/0", "131/0", "132/0", "143/0"]
            }],
            "led": [{
                "name": "USB1",
                "sysfs": "tp-link:green:usb1",
                "trigger": "usbdev",
                "dev": "1-1.1",
                "interval": 50,
            }],
            "interfaces": [{
                "name": "eth0",
                "type": "ethernet"
            }],
            "system": [{
                "test": "1",
                "config_name": "custom",
                "config_value": "custom"
            }]
        }
        self.assertDictEqual(o.config, expected)
Пример #14
0
 def test_skip_nonlists(self):
     o = OpenWrt({"custom_package": {'unknown': True}})
     self.assertEqual(o.render(), '')
Пример #15
0
 def test_parse_switch(self):
     o = OpenWrt(native=self._switch_uci)
     self.assertEqual(o.config, self._switch_netjson)
Пример #16
0
 def test_find_bridge_skip_error(self):
     o = OpenWrt({'interfaces': ['WRONG']})
     with self.assertRaises(ValidationError):
         o.validate()
Пример #17
0
 def test_render_ula_prefix_id(self):
     o = OpenWrt(self._ula_netjson_id)
     expected = self._tabs(self._ula_uci_id)
     self.assertEqual(o.render(), expected)
Пример #18
0
 def test_type_error(self):
     with self.assertRaises(TypeError):
         OpenWrt([])
     with self.assertRaises(TypeError):
         OpenWrt('NOTJSON[]\{\}')
Пример #19
0
 def test_render_multiple_ip(self):
     o = OpenWrt(self._multi_ip_netjson)
     expected = self._tabs(self._multi_ip_uci)
     self.assertEqual(o.render(), expected)
Пример #20
0
 def test_render_dialup_interface(self):
     result = OpenWrt(self._dialup_interface_netjson).render()
     expected = self._tabs(self._dialup_interface_uci)
     self.assertEqual(result, expected)
Пример #21
0
 def test_render_simple_bridge(self):
     o = OpenWrt(self._simple_bridge_netjson)
     expected = self._tabs(self._simple_bridge_uci)
     self.assertEqual(o.render(), expected)
Пример #22
0
 def test_parse_dialup_interface(self):
     result = OpenWrt(native=self._dialup_interface_uci).config
     expected = self._dialup_interface_netjson
     self.assertDictEqual(result, expected)
Пример #23
0
 def test_render_complex_bridge(self):
     o = OpenWrt(self._complex_bridge_netjson)
     self.assertEqual(o.render(), self._tabs(self._complex_bridge_uci))
Пример #24
0
 def test_render_rules(self):
     o = OpenWrt(self._rules_netjson)
     expected = self._tabs(self._rules_uci)
     self.assertEqual(o.render(), expected)
Пример #25
0
    def test_parse_empty_package(self):
        native = self._tabs("""package network

""")
        o = OpenWrt(native=native)
        self.assertDictEqual(o.intermediate_data, {"network": []})
Пример #26
0
 def test_parse_rules(self):
     o = OpenWrt(native=self._rules_uci)
     self.assertEqual(o.config, self._rules_netjson)
Пример #27
0
 def test_parse_double_quotes(self):
     native = self._tabs(self._system_uci.replace('\'', '"'))
     o = OpenWrt(native=native)
     self.assertDictEqual(o.intermediate_data, self._system_intermediate)
Пример #28
0
 def test_render_switch(self):
     o = OpenWrt(self._switch_netjson)
     expected = self._tabs(self._switch_uci)
     self.assertEqual(o.render(), expected)
Пример #29
0
 def test_parse_wpa_personal(self):
     o = OpenWrt(native=self._wpa_personal_uci)
     self.assertEqual(o.config, self._wpa_personal_netjson)
Пример #30
0
 def test_skip(self):
     o = OpenWrt({"skipme": {"enabled": True}})
     self.assertEqual(o.render(), '')