diff --git a/netjsonconfig/backends/raspbian/templates/commands.jinja2 b/netjsonconfig/backends/raspbian/templates/commands.jinja2 index e85d6d072..e4d34c74e 100644 --- a/netjsonconfig/backends/raspbian/templates/commands.jinja2 +++ b/netjsonconfig/backends/raspbian/templates/commands.jinja2 @@ -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 %} diff --git a/netjsonconfig/backends/raspbian/templates/hostapd.jinja2 b/netjsonconfig/backends/raspbian/templates/hostapd.jinja2 index 9f715671f..45d6c9b51 100644 --- a/netjsonconfig/backends/raspbian/templates/hostapd.jinja2 +++ b/netjsonconfig/backends/raspbian/templates/hostapd.jinja2 @@ -23,4 +23,5 @@ wpa_pairwise={{ wireless.encryption.wpa_pairwise }} {% endif %} {% endif %} + {% endfor %} diff --git a/tests/raspbian/test_hostapd.py b/tests/raspbian/test_hostapd.py index d027b94ef..deffd2415 100644 --- a/tests/raspbian/test_hostapd.py +++ b/tests/raspbian/test_hostapd.py @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/tests/raspbian/test_radios.py b/tests/raspbian/test_radios.py index 7587ad8a4..98575c9c3 100644 --- a/tests/raspbian/test_radios.py +++ b/tests/raspbian/test_radios.py @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/tests/raspbian/test_system.py b/tests/raspbian/test_system.py index 399326fd9..4603e84e8 100644 --- a/tests/raspbian/test_system.py +++ b/tests/raspbian/test_system.py @@ -10,10 +10,7 @@ def test_general(self): o = Raspbian({ "general": { "hostname": "test-system", - "timezone": "Europe/Rome", - "custom_setting": True, - "empty_setting1": None, - "empty_setting2": "" + "timezone": "Europe/Rome" } }) @@ -21,8 +18,12 @@ def test_general(self): 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) @@ -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)