github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/plugins/drivers/testutils/dns_testing.go (about)

     1  package testutils
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	dresolvconf "github.com/docker/libnetwork/resolvconf"
     8  	dtypes "github.com/docker/libnetwork/types"
     9  	"github.com/hashicorp/nomad/plugins/drivers"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // TestTaskDNSConfig asserts that a task is running with the given DNSConfig
    14  func TestTaskDNSConfig(t *testing.T, driver *DriverHarness, taskID string, dns *drivers.DNSConfig) {
    15  	t.Run("dns_config", func(t *testing.T) {
    16  		caps, err := driver.Capabilities()
    17  		require.NoError(t, err)
    18  
    19  		// FS isolation is used here as a proxy for network isolation.
    20  		// This is true for the current built-in drivers but it is not necessarily so.
    21  		isolated := caps.FSIsolation != drivers.FSIsolationNone
    22  		usesHostNetwork := caps.FSIsolation != drivers.FSIsolationImage
    23  
    24  		if !isolated {
    25  			t.Skip("dns config not supported on non isolated drivers")
    26  		}
    27  
    28  		// write to a file and check it presence in host
    29  		r := execTask(t, driver, taskID, `cat /etc/resolv.conf`,
    30  			false, "")
    31  		require.Zero(t, r.exitCode)
    32  
    33  		resolvConf := []byte(strings.TrimSpace(r.stdout))
    34  
    35  		if dns != nil {
    36  			if len(dns.Servers) > 0 {
    37  				require.ElementsMatch(t, dns.Servers, dresolvconf.GetNameservers(resolvConf, dtypes.IP))
    38  			}
    39  			if len(dns.Searches) > 0 {
    40  				require.ElementsMatch(t, dns.Searches, dresolvconf.GetSearchDomains(resolvConf))
    41  			}
    42  			if len(dns.Options) > 0 {
    43  				require.ElementsMatch(t, dns.Options, dresolvconf.GetOptions(resolvConf))
    44  			}
    45  		} else {
    46  			systemPath := "/etc/resolv.conf"
    47  			if !usesHostNetwork {
    48  				systemPath = dresolvconf.Path()
    49  			}
    50  
    51  			system, err := dresolvconf.GetSpecific(systemPath)
    52  			require.NoError(t, err)
    53  			require.ElementsMatch(t, dresolvconf.GetNameservers(system.Content, dtypes.IP), dresolvconf.GetNameservers(resolvConf, dtypes.IP))
    54  			require.ElementsMatch(t, dresolvconf.GetSearchDomains(system.Content), dresolvconf.GetSearchDomains(resolvConf))
    55  			require.ElementsMatch(t, dresolvconf.GetOptions(system.Content), dresolvconf.GetOptions(resolvConf))
    56  		}
    57  	})
    58  }