libnetwork/iptables: fix TestFirewalldInit

This test didn't make a lot of sense, because `checkRunning()` depends on
the `connection` package-var being set, which is done by `firewalldInit()`,
so would never be true on its own.

Add a small utility that opens its own D-Bus connection to verify if
firewalld is running, and otherwise skips the tests (preserving any
error in the process).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-08-15 16:36:51 +02:00
parent b325dcbff6
commit d979d2af45
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -7,12 +7,27 @@ import (
"net"
"strconv"
"testing"
"github.com/godbus/dbus/v5"
)
func TestFirewalldInit(t *testing.T) {
if !checkRunning() {
t.Skip("firewalld is not running")
func skipIfNoFirewalld(t *testing.T) {
t.Helper()
conn, err := dbus.SystemBus()
if err != nil {
t.Skipf("cannot connect to D-bus system bus: %v", err)
}
defer conn.Close()
var zone string
err = conn.Object(dbusInterface, dbusPath).Call(dbusInterface+".getDefaultZone", 0).Store(&zone)
if err != nil {
t.Skipf("firewalld is not running: %v", err)
}
}
func TestFirewalldInit(t *testing.T) {
skipIfNoFirewalld(t)
if err := firewalldInit(); err != nil {
t.Fatal(err)
}
@ -71,9 +86,7 @@ func TestReloaded(t *testing.T) {
}
func TestPassthrough(t *testing.T) {
if !firewalldRunning {
t.Skip("firewalld is not running")
}
skipIfNoFirewalld(t)
rule1 := []string{
"-i", "lo",
"-p", "udp",