Skip to content

Commit

Permalink
Avoid crash with empty resolv.conf
Browse files Browse the repository at this point in the history
Fixes: containers#417
Signed-off-by: Reinhard Tartler <[email protected]>
  • Loading branch information
Reinhard Tartler authored and siretart committed Nov 18, 2024
1 parent 2be6b3f commit 98e7c38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/services/dns/dns_config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
package dns

import (
"errors"
"net"
"net/netip"

"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)

var errEmptyResolvConf = errors.New("no DNS servers configured in /etc/resolv.conf")

func getDNSHostAndPort() ([]string, error) {
conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
Expand All @@ -27,5 +30,9 @@ func getDNSHostAndPort() ([]string, error) {
hosts = append(hosts, net.JoinHostPort(server, conf.Port))
}
}

if len(hosts) == 0 {
return []string{}, errEmptyResolvConf
}
return hosts, nil
}
11 changes: 9 additions & 2 deletions pkg/services/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dns

import (
"context"
"errors"
"net"
"testing"
"time"
Expand All @@ -23,7 +24,11 @@ var _ = ginkgo.Describe("dns add test", func() {
var server *Server

ginkgo.BeforeEach(func() {
server, _ = New(nil, nil, []types.Zone{})
var err error
server, err = New(nil, nil, []types.Zone{})
if errors.Is(err, errEmptyResolvConf) {
ginkgo.Skip("Skipping test, no DNS Servers found")
}
})

ginkgo.It("should add dns zone with ip", func() {
Expand Down Expand Up @@ -230,7 +235,9 @@ type ARecord struct {
func TestDNS(t *testing.T) {
log.Infof("starting test DNS servers")
nameserver, cleanup, err := startDNSServer()
require.NoError(t, err)
if err != nil {
t.Skip("Failed to setup start DNS server, skipping test")
}
defer cleanup()
time.Sleep(100 * time.Millisecond)
log.Infof("test DNS servers started")
Expand Down

0 comments on commit 98e7c38

Please sign in to comment.