Skip to content

Commit

Permalink
[raspbian] Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritwick DSouza committed Jul 24, 2017
1 parent 2f08f3c commit 745f317
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 19 deletions.
26 changes: 21 additions & 5 deletions netjsonconfig/backends/raspbian/templates/commands.jinja2
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
{% for general in data.general %}
{% if general.get('timezone') %}
run commands:
$ timedatectl set-timezone {{ general.get('timezone') }}
{% if data.general %}
# script: /scripts/general.sh

{% if 'hostname' in data.general[0]%}
/etc/init.d/hostname.sh start
echo "Hostname of device has been modified"
{% endif %}
{% endfor%}
{% if 'timezone' in data.general[0] %}
timedatectl set-timezone {{ data.general[0].timezone }}
echo "Timezone has changed to {{ data.general[0].timezone }}"
{% endif %}

{% endif %}
{% if data.wireless %}
# script: /scripts/ipv4_forwarding.sh

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o {{ data.wireless[0].ifname }} -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i {{ data.wireless[0].ifname }} -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

{% endif %}
1 change: 1 addition & 0 deletions netjsonconfig/backends/raspbian/templates/hostapd.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
wpa_pairwise={{ wireless.encryption.wpa_pairwise }}
{% endif %}
{% endif %}

{% endfor %}
36 changes: 36 additions & 0 deletions tests/raspbian/test_hostapd.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,20 @@ def test_wpa2_personal(self):
wpa_key_mgmt=WPA-PSK
wpa_passphrase=passphrase012345
wpa_pairwise=TKIP CCMP
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -101,11 +110,20 @@ def test_wpa_personal(self):
wpa=1
wpa_key_mgmt=WPA-PSK
wpa_passphrase=passphrase012345
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -217,11 +235,20 @@ def test_encryption_disabled(self):
channel=3
ieee80211n=1
ssid=MyNetwork
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -260,10 +287,19 @@ def test_no_encryption(self):
channel=3
ieee80211n=1
ssid=open
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)
54 changes: 54 additions & 0 deletions tests/raspbian/test_radios.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ def test_radio_multi(self):
channel=11
ieee80211n=1
ssid=myWiFi
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -93,11 +102,20 @@ def test_radio_n_24ghz(self):
channel=3
ieee80211n=1
ssid=myWiFi
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -135,11 +153,20 @@ def test_radio_n_5ghz(self):
channel=36
ieee80211n=1
ssid=myWiFi
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -176,11 +203,20 @@ def test_radio_ac(self):
channel=132
ieee80211ac=1
ssid=myWiFi
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -216,11 +252,20 @@ def test_radio_a(self):
hw_mode=a
channel=0
ssid=myWiFi
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -256,10 +301,19 @@ def test_radio_g(self):
hw_mode=g
channel=0
ssid=myWiFi
# config: /etc/network/interfaces
auto wlan0
iface wlan0 inet manual
# script: /scripts/ipv4_forwarding.sh
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
'''
self.assertEqual(o.render(), expected)
27 changes: 13 additions & 14 deletions tests/raspbian/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ def test_general(self):
o = Raspbian({
"general": {
"hostname": "test-system",
"timezone": "Europe/Rome",
"custom_setting": True,
"empty_setting1": None,
"empty_setting2": ""
"timezone": "Europe/Rome"
}
})

expected = '''# config: /etc/hostname
test-system
run commands:
$ timedatectl set-timezone Europe/Rome
# script: /scripts/general.sh
/etc/init.d/hostname.sh start
echo "Hostname of device has been modified"
timedatectl set-timezone Europe/Rome
echo "Timezone has changed to Europe/Rome"
'''
self.assertEqual(o.render(), expected)
Expand All @@ -33,19 +34,17 @@ def test_ntp(self):
"enabled": True,
"enable_server": False,
"server": [
"0.openwrt.pool.ntp.org",
"1.openwrt.pool.ntp.org",
"2.openwrt.pool.ntp.org",
"3.openwrt.pool.ntp.org"
"0.pool.ntp.org",
"1.pool.ntp.org",
"2.pool.ntp.org"
]
}
})

expected = '''# config: /etc/ntp.conf
server 0.openwrt.pool.ntp.org
server 1.openwrt.pool.ntp.org
server 2.openwrt.pool.ntp.org
server 3.openwrt.pool.ntp.org
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
'''
self.assertEqual(o.render(), expected)

0 comments on commit 745f317

Please sign in to comment.