Пример #1
0
 def test_checksum(self):
     """ ensures checksum of same config doesn't change """
     o = OpenWrt({"general": {"hostname": "test"}})
     # md5 is good enough and won't slow down test execution too much
     checksum1 = md5(o.generate().getvalue()).hexdigest()
     sleep(1)
     checksum2 = md5(o.generate().getvalue()).hexdigest()
     self.assertEqual(checksum1, checksum2)
Пример #2
0
 def test_checksum(self):
     """ ensures checksum of same config doesn't change """
     o = OpenWrt({"general": {"hostname": "test"}})
     # md5 is good enough and won't slow down test execution too much
     checksum1 = md5(o.generate().getvalue()).hexdigest()
     sleep(1)
     checksum2 = md5(o.generate().getvalue()).hexdigest()
     self.assertEqual(checksum1, checksum2)
Пример #3
0
    def test_override_file(self):
        o = OpenWrt(
            {
                "files":
                [{
                    "path": "/etc/crontabs/root",
                    "mode": "0644",
                    "contents": "*/5 * * * * /command1\n*/5 * * * * /command2",
                }]
            },
            templates=[{
                "files": [{
                    "path": "/etc/crontabs/root",
                    "mode": "0644",
                    "contents": "*/5 * * * * /command1",
                }]
            }],
        )
        expected = """
# ---------- files ---------- #

# path: /etc/crontabs/root
# mode: 0644

*/5 * * * * /command1
*/5 * * * * /command2

"""
        self.assertEqual(o.render(), expected)
        # 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()), 1)
Пример #4
0
 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()
Пример #5
0
 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()
Пример #6
0
 def test_file_permissions(self):
     o = OpenWrt({
         "files": [{
             "path": "/tmp/hello.sh",
             "mode": "0755",
             "contents": "echo 'hello world'",
         }]
     })
     tar = tarfile.open(fileobj=o.generate(), mode='r')
     script = tar.getmember('tmp/hello.sh')
     # check permissions
     self.assertEqual(script.mode, 493)
     tar.close()
Пример #7
0
 def test_file_permissions(self):
     o = OpenWrt({
         "files": [
             {
                 "path": "/tmp/hello.sh",
                 "mode": "0755",
                 "contents": "echo 'hello world'",
             }
         ]
     })
     tar = tarfile.open(fileobj=o.generate(), mode='r')
     script = tar.getmember('tmp/hello.sh')
     # check permissions
     self.assertEqual(script.mode, 493)
     tar.close()
Пример #8
0
    def test_generate(self):
        o = OpenWrt(self._config1)
        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()
Пример #9
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/24'
    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()
Пример #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()