本文整理汇总了Python中netjsonconfig.OpenWrt.generate方法的典型用法代码示例。如果您正苦于以下问题:Python OpenWrt.generate方法的具体用法?Python OpenWrt.generate怎么用?Python OpenWrt.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netjsonconfig.OpenWrt
的用法示例。
在下文中一共展示了OpenWrt.generate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_checksum
# 需要导入模块: from netjsonconfig import OpenWrt [as 别名]
# 或者: from netjsonconfig.OpenWrt import generate [as 别名]
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: test_file_inclusion
# 需要导入模块: from netjsonconfig import OpenWrt [as 别名]
# 或者: from netjsonconfig.OpenWrt import generate [as 别名]
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()
示例3: test_file_permissions
# 需要导入模块: from netjsonconfig import OpenWrt [as 别名]
# 或者: from netjsonconfig.OpenWrt import generate [as 别名]
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()
示例4: test_generate
# 需要导入模块: from netjsonconfig import OpenWrt [as 别名]
# 或者: from netjsonconfig.OpenWrt import generate [as 别名]
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()