Skip to content

Commit

Permalink
Merge pull request #420 from siretart/issue/417
Browse files Browse the repository at this point in the history
Avoid crash with empty resolv.conf
  • Loading branch information
openshift-merge-bot[bot] authored Nov 19, 2024
2 parents 2be6b3f + fdcf750 commit 7e0fa97
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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
}
10 changes: 9 additions & 1 deletion 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,6 +235,9 @@ type ARecord struct {
func TestDNS(t *testing.T) {
log.Infof("starting test DNS servers")
nameserver, cleanup, err := startDNSServer()
if errors.Is(err, errEmptyResolvConf) {
t.Skip("Failed to setup start DNS server, skipping test")
}
require.NoError(t, err)
defer cleanup()
time.Sleep(100 * time.Millisecond)
Expand Down

0 comments on commit 7e0fa97

Please sign in to comment.