From d4be938776bbed2841311a67eb803240dddbc0e1 Mon Sep 17 00:00:00 2001 From: Tommi2Day Date: Wed, 4 Sep 2024 17:31:50 +0200 Subject: [PATCH] update dns tests --- CHANGELOG.md | 4 + dblib/tns_dns_docker_test.go | 230 +++++++++++++--------- dblib/tns_dns_test.go | 21 +- netlib/dns_docker_test.go | 222 ++++++++++++--------- netlib/net.go | 28 ++- netlib/net_test.go | 24 ++- test/docker/dns/Dockerfile | 4 +- test/docker/dns/named.conf.options | 2 +- test/docker/oracle-dns/Dockerfile | 4 +- test/docker/oracle-dns/named.conf.options | 2 +- 10 files changed, 322 insertions(+), 219 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6d74ba..ce553da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Go Library +## [v1.14.10 - 2024-09-04] +### Changed +- update dns tests + ## [v1.14.9 - 2024-08-31] ### Changed - dns test docker creation error handling diff --git a/dblib/tns_dns_docker_test.go b/dblib/tns_dns_docker_test.go index de2c2c4..b26d1c2 100644 --- a/dblib/tns_dns_docker_test.go +++ b/dblib/tns_dns_docker_test.go @@ -1,151 +1,189 @@ package dblib import ( - "context" "fmt" "net" "os" "time" + "github.com/tommi2day/gomodules/common" "github.com/tommi2day/gomodules/netlib" + "github.com/tommi2day/gomodules/test" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" - "github.com/tommi2day/gomodules/common" +) - "github.com/tommi2day/gomodules/test" +const ( + dblibDNSContainerTimeout = 10 + dblibNetworkName = "dblib-dns" + dblibNetworkPrefix = "172.24.0" + dblibRepoTag = "9.20" + dblibDNSPort = 9054 + dblibTestAddr = racaddr +) + +var ( + dblibDNSContainerName string + dblibDNSContainer *dockertest.Resource + dblibDNSNetwork *dockertest.Network + dblibDNSNetworkCreated = false + dblibDNSServer = "127.0.0.1" ) -const dblibDNSContainerTimeout = 10 -const dblibNetworkName = "dblib-dns" -const dblibNetworkPrefix = "172.24.0" -const dblibRepoTag = "9.20" - -var dblibDNSContainerName string -var dblibDNSContainer *dockertest.Resource -var dblibDNSNetwork *dockertest.Network -var dblibNetworkCreated = false -var dblibDNSServer = "" -var dblibDNSPort = 0 - -// prepareDNSContainer create a Bind9 Docker Container -func prepareDNSContainer() (container *dockertest.Resource, err error) { - if os.Getenv("SKIP_DNS") != "" { - err = fmt.Errorf("skipping DNS Container in CI environment") +// prepareDBlibDNSContainer create a Bind9 Docker Container +func prepareDBlibDNSContainer() (container *dockertest.Resource, err error) { + if os.Getenv("SKIP_DB_DNS") != "" { + return nil, fmt.Errorf("skipping DB DNS Container in CI environment") + } + + dblibDNSContainerName = getContainerName() + pool, err := common.GetDockerPool() + if err != nil { + return nil, err + } + + err = setupNetwork(pool) + if err != nil { + return nil, err + } + + container, err = buildAndRunContainer(pool) + if err != nil { return } - dblibDNSContainerName = os.Getenv("DNS_CONTAINER_NAME") - if dblibDNSContainerName == "" { - dblibDNSContainerName = "dblib-bind9" + + time.Sleep(10 * time.Second) + + err = validateContainerIP(container) + if err != nil { + return } - var pool *dockertest.Pool - pool, err = common.GetDockerPool() + + err = waitForDNSServer(pool) if err != nil { return } + + err = testDNSResolution() + return +} + +func getContainerName() string { + name := os.Getenv("DBDNS_CONTAINER_NAME") + if name == "" { + name = "dblib-bind9" + } + return name +} + +func setupNetwork(pool *dockertest.Pool) error { networks, err := pool.NetworksByName(dblibNetworkName) if err != nil || len(networks) == 0 { - dblibDNSNetwork, err = pool.CreateNetwork(dblibNetworkName, func(options *docker.CreateNetworkOptions) { - options.Name = dblibNetworkName - options.CheckDuplicate = true - options.IPAM = &docker.IPAMOptions{ - Driver: "default", - Config: []docker.IPAMConfig{{ - Subnet: dblibNetworkPrefix + ".0/24", - Gateway: dblibNetworkPrefix + ".1", - }}, - } - options.EnableIPv6 = false - // options.Internal = true - }) - if err != nil { - err = fmt.Errorf("could not create Network: %s:%s", dblibNetworkName, err) - return + return createNetwork(pool) + } + dblibDNSNetwork = &networks[0] + return nil +} + +func createNetwork(pool *dockertest.Pool) error { + var err error + dblibDNSNetwork, err = pool.CreateNetwork(dblibNetworkName, func(options *docker.CreateNetworkOptions) { + options.Name = dblibNetworkName + options.CheckDuplicate = true + options.IPAM = &docker.IPAMOptions{ + Driver: "default", + Config: []docker.IPAMConfig{{ + Subnet: dblibNetworkPrefix + ".0/24", + Gateway: dblibNetworkPrefix + ".1", + }}, } - dblibNetworkCreated = true - } else { - dblibDNSNetwork = &networks[0] + options.EnableIPv6 = false + }) + if err != nil { + return fmt.Errorf("could not create Network: %s:%s", dblibNetworkName, err) } + dblibDNSNetworkCreated = true + return nil +} +func buildAndRunContainer(pool *dockertest.Pool) (*dockertest.Resource, error) { vendorImagePrefix := os.Getenv("VENDOR_IMAGE_PREFIX") - - fmt.Printf("Try to build and start docker container %s\n", dblibDNSContainerName) + fmt.Printf("Try to build and start docker container %s\n", dblibDNSContainerName) buildArgs := []docker.BuildArg{ - { - Name: "VENDOR_IMAGE_PREFIX", - Value: vendorImagePrefix, - }, - { - Name: "BIND9_VERSION", - Value: dblibRepoTag, - }, + {Name: "VENDOR_IMAGE_PREFIX", Value: vendorImagePrefix}, + {Name: "BIND9_VERSION", Value: dblibRepoTag}, } - container, err = pool.BuildAndRunWithBuildOptions( + + dockerContextDir := test.TestDir + "/docker/oracle-dns" + return pool.BuildAndRunWithBuildOptions( &dockertest.BuildOptions{ BuildArgs: buildArgs, - ContextDir: test.TestDir + "/docker/oracle-dns", + ContextDir: dockerContextDir, Dockerfile: "Dockerfile", }, &dockertest.RunOptions{ Hostname: dblibDNSContainerName, Name: dblibDNSContainerName, Networks: []*dockertest.Network{dblibDNSNetwork}, - ExposedPorts: []string{"53/tcp", "53/udp", "953/tcp"}, + ExposedPorts: []string{"9054/tcp"}, + // need fixed mapping here + PortBindings: map[docker.Port][]docker.PortBinding{ + "9054/tcp": { + {HostIP: "0.0.0.0", HostPort: fmt.Sprintf("%d", dblibDNSPort)}, + }, + }, }, func(config *docker.HostConfig) { - // set AutoRemove to true so that stopped container goes away by itself - config.AutoRemove = true + config.AutoRemove = false config.RestartPolicy = docker.RestartPolicy{Name: "no"} }) +} - if err != nil || container == nil { - err = fmt.Errorf("error starting oracle-dns docker container: %v", err) - return - } - // ip := container.Container.NetworkSettings.Networks[dblibNetworkName].IPAddress +func validateContainerIP(container *dockertest.Resource) error { ip := container.GetIPInNetwork(dblibDNSNetwork) - if ip != dblibNetworkPrefix+".2" { - err = fmt.Errorf("internal ip not as expected: %s", ip) - return - } + + fmt.Printf("DB DNS Container IP: %s\n", ip) + return nil +} + +// func waitForDNSServer(pool *dockertest.Pool, container *dockertest.Resource) error { +func waitForDNSServer(pool *dockertest.Pool) error { pool.MaxWait = dblibDNSContainerTimeout * time.Second - dblibDNSServer, dblibDNSPort = common.GetContainerHostAndPort(container, "53/tcp") - if dblibDNSPort == 0 || dblibDNSServer == "" { - err = fmt.Errorf("could not get host/port of dns container") - return - } - fmt.Printf("Wait to successfully connect to DNS to %s:%d (max %ds)...\n", dblibDNSServer, dblibDNSPort, dblibDNSContainerTimeout) start := time.Now() - var c net.Conn - if err = pool.Retry(func() error { - c, err = net.Dial("tcp", fmt.Sprintf("%s:%d", dblibDNSServer, dblibDNSPort)) + err := pool.Retry(func() error { + c, err := net.Dial("udp", fmt.Sprintf("%s:%d", dblibDNSServer, dblibDNSPort)) if err != nil { fmt.Printf("Err:%s\n", err) + return err } - return err - }); err != nil { - err = fmt.Errorf("could not connect to DNS Container: %d", err) - return + _ = c.Close() + return nil + }) + + if err != nil { + return fmt.Errorf("could not connect to DB DNS Container: %v", err) } - _ = c.Close() - // wait 10s to init container time.Sleep(10 * time.Second) elapsed := time.Since(start) - fmt.Printf("DNS Container is available after %s\n", elapsed.Round(time.Millisecond)) - if dblibDNSServer == "localhost" { - dblibDNSServer = "127.0.0.1" - } - // fmt.Printf("DNS Container is available after %s\n", elapsed.Round(time.Millisecond)) - // test dns + fmt.Println("DB DNS Container is ready after ", elapsed.Round(time.Millisecond)) + return nil +} + +func testDNSResolution() error { dns := netlib.NewResolver(dblibDNSServer, dblibDNSPort, true) - ips, e := dns.Resolver.LookupHost(context.Background(), racaddr) - if e != nil || len(ips) == 0 { - err = fmt.Errorf("could not resolve DNS for %s on %s:%d: %v", racaddr, dblibDNSServer, dblibDNSPort, e) - return + dns.IPv4Only = true + s := "/udp" + if dns.TCP { + s = "/tcp" } - fmt.Println("DNS Container is ready after ", elapsed.Round(time.Millisecond), ", host ", racaddr, "resolved to", ips[0]) - err = nil - return + fmt.Printf("resolve on %s:%d%s\n", dns.Nameserver, dns.Port, s) + ips, err := dns.LookupIP(dblibTestAddr) + if err != nil || len(ips) == 0 { + return fmt.Errorf("could not resolve DNS for %s: %v", dblibTestAddr, err) + } + fmt.Printf("Host %s resolved to %s\n", dblibTestAddr, ips[0]) + return nil } func destroyDNSContainer(container *dockertest.Resource) { @@ -153,7 +191,7 @@ func destroyDNSContainer(container *dockertest.Resource) { common.DestroyDockerContainer(container) } - if dblibNetworkCreated && dblibDNSNetwork != nil { + if dblibDNSNetworkCreated && dblibDNSNetwork != nil { _ = dblibDNSNetwork.Close() } } diff --git a/dblib/tns_dns_test.go b/dblib/tns_dns_test.go index 98eaedb..b047feb 100644 --- a/dblib/tns_dns_test.go +++ b/dblib/tns_dns_test.go @@ -1,15 +1,16 @@ package dblib import ( + "fmt" "os" "testing" + "github.com/tommi2day/gomodules/netlib" + log "github.com/sirupsen/logrus" "github.com/tommi2day/gomodules/common" - "github.com/tommi2day/gomodules/netlib" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -29,20 +30,24 @@ func TestMain(m *testing.M) { var err error test.InitTestDirs() - if os.Getenv("SKIP_DNS") != "" { + if os.Getenv("SKIP_DB_DNS") != "" { + fmt.Println("Skipping DB DNS Container in CI environment") return } - dblibDNSContainer, err = prepareDNSContainer() + dblibDNSServer = common.GetStringEnv("DNS_HOST", dblibDNSServer) + dblibDNSContainer, err = prepareDBlibDNSContainer() if err != nil || dblibDNSContainer == nil { - _ = os.Setenv("SKIP_DNS", "true") - log.Errorf("prepareDNSContainer failed: %s", err) + _ = os.Setenv("SKIP_DB_DNS", "true") + log.Errorf("prepareNetlibDNSContainer failed: %s", err) + destroyDNSContainer(dblibDNSContainer) } code := m.Run() destroyDNSContainer(dblibDNSContainer) os.Exit(code) } + func TestRACInfo(t *testing.T) { var err error test.InitTestDirs() @@ -53,8 +58,8 @@ func TestRACInfo(t *testing.T) { err = common.WriteStringToFile(tnsAdmin+"/racinfo.ini", racinfoini) require.NoErrorf(t, err, "Create test racinfo.ini failed") - if os.Getenv("SKIP_DNS") != "" { - t.Skip("Skipping DNS testing in CI environment") + if os.Getenv("SKIP_DB_DNS") != "" { + t.Skip("Skipping DB DNS testing") } // use DNS from Docker DNSConfig = netlib.NewResolver(dblibDNSServer, dblibDNSPort, true) diff --git a/netlib/dns_docker_test.go b/netlib/dns_docker_test.go index f057d5c..da5bc3b 100644 --- a/netlib/dns_docker_test.go +++ b/netlib/dns_docker_test.go @@ -1,7 +1,6 @@ package netlib import ( - "context" "fmt" "net" "os" @@ -14,74 +13,113 @@ import ( "github.com/ory/dockertest/v3/docker" ) -const netlibDNSContainerTimeout = 10 -const netlibNetworkName = "netlib-dns" -const netlibNetworkPrefix = "172.25.0" -const netlibDomain = "netlib.lan" -const netlibTestAddr = tDB -const netlibRepoTag = "9.20" +const ( + netlibDNSContainerTimeout = 10 + netlibNetworkName = "netlib-dns" + netlibNetworkPrefix = "172.25.0" + netlibDomain = "netlib.lan" + netlibTestAddr = tDB + netlibRepoTag = "9.20" + netlibDNSPort = 9053 + // netlibDNSSecPort = 953 +) -var netlibDNSContainerName string -var netlibDNSContainer *dockertest.Resource -var netlibDNSNetwork *dockertest.Network -var netlibDNSNetworkCreated = false -var netlibDNSServer = "" -var netlibDNSPort = 0 +var ( + netlibDNSContainerName string + netlibDNSContainer *dockertest.Resource + netlibDNSNetwork *dockertest.Network + netlibDNSNetworkCreated = false + netlibDNSServer = "127.0.0.1" +) // prepareNetlibDNSContainer create a Bind9 Docker Container func prepareNetlibDNSContainer() (container *dockertest.Resource, err error) { - if os.Getenv("SKIP_DNS") != "" { - err = fmt.Errorf("skipping DNS Container in CI environment") + if os.Getenv("SKIP_NET_DNS") != "" { + return nil, fmt.Errorf("skipping Net DNS Container in CI environment") + } + + netlibDNSContainerName = getContainerName() + pool, err := common.GetDockerPool() + if err != nil { + return nil, err + } + + err = setupNetwork(pool) + if err != nil { + return nil, err + } + + container, err = buildAndRunContainer(pool) + if err != nil { return } - netlibDNSContainerName = os.Getenv("DNS_CONTAINER_NAME") - if netlibDNSContainerName == "" { - netlibDNSContainerName = "netlib-bind9" + + time.Sleep(10 * time.Second) + + err = validateContainerIP(container) + if err != nil { + return } - var pool *dockertest.Pool - pool, err = common.GetDockerPool() + + err = waitForDNSServer(pool) if err != nil { return } + + err = testDNSResolution() + return +} + +func getContainerName() string { + name := os.Getenv("NETDNS_CONTAINER_NAME") + if name == "" { + name = "netlib-bind9" + } + return name +} + +func setupNetwork(pool *dockertest.Pool) error { networks, err := pool.NetworksByName(netlibNetworkName) if err != nil || len(networks) == 0 { - netlibDNSNetwork, err = pool.CreateNetwork(netlibNetworkName, func(options *docker.CreateNetworkOptions) { - options.Name = netlibNetworkName - options.CheckDuplicate = true - options.IPAM = &docker.IPAMOptions{ - Driver: "default", - Config: []docker.IPAMConfig{{ - Subnet: netlibNetworkPrefix + ".0/24", - Gateway: netlibNetworkPrefix + ".1", - }}, - } - options.EnableIPv6 = false - // options.Internal = true - }) - if err != nil { - err = fmt.Errorf("could not create Network: %s:%s", netlibNetworkName, err) - return + return createNetwork(pool) + } + netlibDNSNetwork = &networks[0] + return nil +} + +func createNetwork(pool *dockertest.Pool) error { + var err error + netlibDNSNetwork, err = pool.CreateNetwork(netlibNetworkName, func(options *docker.CreateNetworkOptions) { + options.Name = netlibNetworkName + options.CheckDuplicate = true + options.IPAM = &docker.IPAMOptions{ + Driver: "default", + Config: []docker.IPAMConfig{{ + Subnet: netlibNetworkPrefix + ".0/24", + Gateway: netlibNetworkPrefix + ".1", + }}, } - netlibDNSNetworkCreated = true - } else { - netlibDNSNetwork = &networks[0] + options.EnableIPv6 = false + }) + if err != nil { + return fmt.Errorf("could not create Network: %s:%s", netlibNetworkName, err) } + netlibDNSNetworkCreated = true + return nil +} +func buildAndRunContainer(pool *dockertest.Pool) (*dockertest.Resource, error) { vendorImagePrefix := os.Getenv("VENDOR_IMAGE_PREFIX") - - fmt.Printf("Try to build and start docker container %s\n", netlibDNSContainerName) + fmt.Printf("Try to build and start docker container %s\n", netlibDNSContainerName) + // port := fmt.Sprintf("%d", netlibDNSPort) + // sport := fmt.Sprintf("%d", netlibDNSSecPort) buildArgs := []docker.BuildArg{ - { - Name: "VENDOR_IMAGE_PREFIX", - Value: vendorImagePrefix, - }, - { - Name: "BIND9_VERSION", - Value: netlibRepoTag, - }, + {Name: "VENDOR_IMAGE_PREFIX", Value: vendorImagePrefix}, + {Name: "BIND9_VERSION", Value: netlibRepoTag}, } + dockerContextDir := test.TestDir + "/docker/dns" - container, err = pool.BuildAndRunWithBuildOptions( + return pool.BuildAndRunWithBuildOptions( &dockertest.BuildOptions{ BuildArgs: buildArgs, ContextDir: dockerContextDir, @@ -91,61 +129,67 @@ func prepareNetlibDNSContainer() (container *dockertest.Resource, err error) { Hostname: netlibDNSContainerName, Name: netlibDNSContainerName, Networks: []*dockertest.Network{netlibDNSNetwork}, - ExposedPorts: []string{"53/tcp", "53/udp", "953/tcp"}, + ExposedPorts: []string{"9053/tcp"}, + // need fixed mapping here + + PortBindings: map[docker.Port][]docker.PortBinding{ + "9053/tcp": { + {HostIP: "0.0.0.0", HostPort: fmt.Sprintf("%d", netlibDNSPort)}, + }, + }, }, func(config *docker.HostConfig) { - // set AutoRemove to true so that stopped container goes away by itself - config.AutoRemove = true + config.AutoRemove = false config.RestartPolicy = docker.RestartPolicy{Name: "no"} }) +} - if err != nil { - err = fmt.Errorf("error starting dns docker container: %v", err) - return - } - // ip := container.Container.NetworkSettings.Networks[netlibNetworkName].IPAddress +func validateContainerIP(container *dockertest.Resource) error { ip := container.GetIPInNetwork(netlibDNSNetwork) - if ip != netlibNetworkPrefix+".2" { - err = fmt.Errorf("internal ip not as expected: %s", ip) - return - } + fmt.Printf("NetDNS Container IP: %s\n", ip) + // netlibDNSServer = ip + return nil +} + +// func waitForDNSServer(pool *dockertest.Pool, container *dockertest.Resource) error { +func waitForDNSServer(pool *dockertest.Pool) error { pool.MaxWait = netlibDNSContainerTimeout * time.Second - netlibDNSServer, netlibDNSPort = common.GetContainerHostAndPort(container, "53/tcp") - if netlibDNSPort == 0 || netlibDNSServer == "" { - err = fmt.Errorf("could not get host/port of dns container") - return - } - fmt.Printf("Wait to successfully connect to DNS to %s:%d (max %ds)...\n", netlibDNSServer, netlibDNSPort, netlibDNSContainerTimeout) start := time.Now() - var c net.Conn - if err = pool.Retry(func() error { - c, err = net.Dial("tcp", fmt.Sprintf("%s:%d", netlibDNSServer, netlibDNSPort)) + err := pool.Retry(func() error { + c, err := net.Dial("udp", fmt.Sprintf("%s:%d", netlibDNSServer, netlibDNSPort)) if err != nil { fmt.Printf("Err:%s\n", err) + return err } - return err - }); err != nil { - err = fmt.Errorf("could not connect to DNS Container: %d", err) - return + _ = c.Close() + return nil + }) + + if err != nil { + return fmt.Errorf("could not connect to Net DNS Container: %v", err) } - _ = c.Close() - // wait 10s to init container time.Sleep(10 * time.Second) elapsed := time.Since(start) - if netlibDNSServer == "localhost" { - netlibDNSServer = "127.0.0.1" - } - // fmt.Printf("DNS Container is available after %s\n", elapsed.Round(time.Millisecond)) - // test dns + fmt.Println("Net DNS Container is ready after ", elapsed.Round(time.Millisecond)) + + // netlibDNSServer = server + return nil +} + +func testDNSResolution() error { dns := NewResolver(netlibDNSServer, netlibDNSPort, true) - ips, e := dns.Resolver.LookupHost(context.Background(), netlibTestAddr) - if e != nil || len(ips) == 0 { - err = fmt.Errorf("could not resolve DNS for %s on %s:%d: %v", netlibTestAddr, netlibDNSServer, netlibDNSPort, e) - return + dns.IPv4Only = true + s := "/udp" + if dns.TCP { + s = "/tcp" } - fmt.Println("DNS Container is ready after ", elapsed.Round(time.Millisecond), ", host ", netlibTestAddr, "resolved to", ips[0]) - err = nil - return + fmt.Printf("resolve on %s:%d%s\n", dns.Nameserver, dns.Port, s) + ips, err := dns.LookupIP(netlibTestAddr) + if err != nil || len(ips) == 0 { + return fmt.Errorf("could not resolve DNS for %s: %v", netlibTestAddr, err) + } + fmt.Printf("Host %s resolved to %s\n", netlibTestAddr, ips[0]) + return nil } func destroyDNSContainer(container *dockertest.Resource) { diff --git a/netlib/net.go b/netlib/net.go index d7931b9..af5b2ad 100644 --- a/netlib/net.go +++ b/netlib/net.go @@ -25,23 +25,33 @@ const defaultDNSTimeout = 5 * time.Second // NewResolver returns a DNSconfig object func NewResolver(nameserver string, port int, tcp bool) (dns *DNSconfig) { + // network type + nsIP := "" + n := "udp" + if tcp { + n = "tcp" + } + ips, err := net.LookupHost(nameserver) + if err != nil || len(ips) == 0 { + log.Debugf("DNS lookup of %s failed: %s", nameserver, err) + } else { + nsIP = ips[0] + } dns = new(DNSconfig) var resolver *net.Resolver - if nameserver != "" { + if nsIP != "" { if port == 0 { port = 53 } - // network type - n := "udp" - if tcp { - n = "tcp" - } - a := common.SetHostPort(nameserver, port) + + a := common.SetHostPort(nsIP, port) log.Debugf("Configured custom DNS resolver: %s", a) resolver = &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, _, _ string) (net.Conn, error) { - d := net.Dialer{} + d := net.Dialer{ + Timeout: defaultDNSTimeout, + } return d.DialContext(ctx, n, a) }, } @@ -51,7 +61,7 @@ func NewResolver(nameserver string, port int, tcp bool) (dns *DNSconfig) { } dns.TCP = tcp dns.Port = port - dns.Nameserver = nameserver + dns.Nameserver = nsIP dns.Resolver = resolver dns.Timeout = defaultDNSTimeout dns.IPv4Only = false diff --git a/netlib/net_test.go b/netlib/net_test.go index 4b7ac16..4f3dda6 100644 --- a/netlib/net_test.go +++ b/netlib/net_test.go @@ -1,11 +1,13 @@ package netlib import ( + "fmt" "os" "testing" log "github.com/sirupsen/logrus" + "github.com/tommi2day/gomodules/common" "github.com/tommi2day/gomodules/test" "github.com/stretchr/testify/assert" @@ -21,14 +23,17 @@ func TestMain(m *testing.M) { var err error test.InitTestDirs() - if os.Getenv("SKIP_DNS") != "" { + if os.Getenv("SKIP_NET_DNS") != "" { + fmt.Println("Skipping Net DNS Container in CI environment") return } + netlibDNSServer = common.GetStringEnv("DNS_HOST", netlibDNSServer) netlibDNSContainer, err = prepareNetlibDNSContainer() if err != nil || netlibDNSContainer == nil { - _ = os.Setenv("SKIP_DNS", "true") + _ = os.Setenv("SKIP_NET_DNS", "true") log.Errorf("prepareNetlibDNSContainer failed: %s", err) + destroyDNSContainer(netlibDNSContainer) } code := m.Run() @@ -222,11 +227,12 @@ func TestDNSConfig(t *testing.T) { }) t.Run("Test private Resolver", func(t *testing.T) { dns := NewResolver(netlibDNSServer, netlibDNSPort, true) - assert.Equal(t, netlibDNSServer, dns.Nameserver, "Server not expected") + // assert.Equal(t, netlibDNSServer, dns.Nameserver, "Server not expected") assert.Equal(t, netlibDNSPort, dns.Port, "Port not expected") assert.NotNil(t, dns.Resolver, "Resolver not set") assert.True(t, dns.TCP, "TCP not set") assert.Equal(t, defaultDNSTimeout, dns.Timeout, "Timeout not expected") + t.Logf("Resolver: %s:%d(TCP:%v)\n", dns.Nameserver, dns.Port, dns.TCP) }) t.Run("Test unset Resolver", func(t *testing.T) { if os.Getenv("SKIP_PUBLIC_DNS") != "" { @@ -240,8 +246,8 @@ func TestDNSConfig(t *testing.T) { }) } func TestLookupSrv(t *testing.T) { - if os.Getenv("SKIP_DNS") != "" { - t.Skip("Skipping DNS testing in CI environment") + if os.Getenv("SKIP_NET_DNS") != "" { + t.Skip("Skipping Net DNS testing") } // use DNS from Docker @@ -261,8 +267,8 @@ func TestLookupSrv(t *testing.T) { } func TestLookupIP(t *testing.T) { - if os.Getenv("SKIP_DNS") != "" { - t.Skip("Skipping DNS testing in CI environment") + if os.Getenv("SKIP_NET_DNS") != "" { + t.Skip("Skipping NET DNS testing") } // use DNS from Docker dns := NewResolver(netlibDNSServer, netlibDNSPort, true) @@ -354,8 +360,8 @@ func TestLookupIP(t *testing.T) { } func TestLookupIPV4V6(t *testing.T) { - if os.Getenv("SKIP_DNS") != "" { - t.Skip("Skipping DNS testing in CI environment") + if os.Getenv("SKIP_NET_DNS") != "" { + t.Skip("Skipping NET_DNS testing") } dns := NewResolver(netlibDNSServer, netlibDNSPort, true) dns.IPv4Only = false diff --git a/test/docker/dns/Dockerfile b/test/docker/dns/Dockerfile index cf8b486..b722550 100644 --- a/test/docker/dns/Dockerfile +++ b/test/docker/dns/Dockerfile @@ -9,9 +9,7 @@ COPY named.conf.local /etc/bind/ COPY netlib.lan.zone /etc/bind/zones/ # Expose Ports -EXPOSE 53/tcp -EXPOSE 53/udp -EXPOSE 953/tcp +EXPOSE 9053/tcp # Start the Name Service CMD ["/usr/sbin/named", "-g", "-c", "/etc/bind/named.conf", "-u", "bind"] \ No newline at end of file diff --git a/test/docker/dns/named.conf.options b/test/docker/dns/named.conf.options index 0ae4052..cac5e33 100644 --- a/test/docker/dns/named.conf.options +++ b/test/docker/dns/named.conf.options @@ -1,7 +1,7 @@ options { directory "/var/cache/bind"; recursion yes; - listen-on { any; }; + listen-on port 9053 { any; }; forwarders { 8.8.8.8; 8.8.4.4; diff --git a/test/docker/oracle-dns/Dockerfile b/test/docker/oracle-dns/Dockerfile index 9bf0029..e2db36f 100644 --- a/test/docker/oracle-dns/Dockerfile +++ b/test/docker/oracle-dns/Dockerfile @@ -9,9 +9,7 @@ COPY named.conf.local /etc/bind/ COPY rac.lan.zone /etc/bind/zones/ # Expose Ports -EXPOSE 53/tcp -EXPOSE 53/udp -EXPOSE 953/tcp +EXPOSE 9054/tcp # Start the Name Service CMD ["/usr/sbin/named", "-g", "-c", "/etc/bind/named.conf", "-u", "bind"] \ No newline at end of file diff --git a/test/docker/oracle-dns/named.conf.options b/test/docker/oracle-dns/named.conf.options index 0ae4052..5a7518c 100644 --- a/test/docker/oracle-dns/named.conf.options +++ b/test/docker/oracle-dns/named.conf.options @@ -1,7 +1,7 @@ options { directory "/var/cache/bind"; recursion yes; - listen-on { any; }; + listen-on port 9054 { any; }; forwarders { 8.8.8.8; 8.8.4.4;