Beispiel #1
0
func (*UtilsSuite) TestParseResolvConfEmptyFile(c *gc.C) {
	emptyConf := makeResolvConf(c, "", 0644)
	result, err := network.ParseResolvConf(emptyConf)
	c.Check(err, jc.ErrorIsNil)
	// Expected non-nil, but empty result.
	c.Check(result, jc.DeepEquals, &network.DNSConfig{})
}
Beispiel #2
0
func (*UtilsSuite) TestParseResolvConfNotReadablePath(c *gc.C) {
	unreadableConf := makeResolvConf(c, "#empty", 0000)
	result, err := network.ParseResolvConf(unreadableConf)
	expected := fmt.Sprintf("open %s: permission denied", unreadableConf)
	c.Check(err, gc.ErrorMatches, expected)
	c.Check(result, gc.IsNil)
}
Beispiel #3
0
func (*UtilsSuite) TestParseResolvConfEmptyOrMissingPath(c *gc.C) {
	emptyPath := ""
	missingPath := filepath.Join(c.MkDir(), "missing")

	for _, path := range []string{emptyPath, missingPath} {
		result, err := network.ParseResolvConf(path)
		c.Check(err, jc.ErrorIsNil)
		c.Check(result, gc.IsNil)
	}
}
Beispiel #4
0
func (*UtilsSuite) TestParseResolvConfLastSearchWins(c *gc.C) {
	const multiSearchConf = `
search zero five
search one
# this below overrides all of the above
search two three #comment ;also-comment still-comment
`
	fakeConf := makeResolvConf(c, multiSearchConf, 0644)
	result, err := network.ParseResolvConf(fakeConf)
	c.Check(err, jc.ErrorIsNil)
	c.Check(result, jc.DeepEquals, &network.DNSConfig{
		SearchDomains: []string{"two", "three"},
	})
}
Beispiel #5
0
func (*UtilsSuite) TestParseResolvConfCommentsAndWhitespaceHandling(c *gc.C) {
	const exampleConf = `
  ;; comment
# also comment
;# ditto
  #nameserver ;still comment

  search    foo example.com       bar.     ;comment, leading/trailing ignored
nameserver 8.8.8.8 #comment #still the same comment
`
	fakeConf := makeResolvConf(c, exampleConf, 0644)
	result, err := network.ParseResolvConf(fakeConf)
	c.Check(err, jc.ErrorIsNil)
	c.Check(result, jc.DeepEquals, &network.DNSConfig{
		Nameservers:   network.NewAddresses("8.8.8.8"),
		SearchDomains: []string{"foo", "example.com", "bar."},
	})
}
Beispiel #6
0
// finishNetworkConfig populates the ParentInterfaceName, DNSServers, and
// DNSSearchDomains fields on each element, when they are not set. The given
// bridgeDevice is used for ParentInterfaceName, while the DNS config is
// discovered using network.ParseResolvConf(). If interfaces has zero length,
// container.FallbackInterfaceInfo() is used as fallback.
func finishNetworkConfig(bridgeDevice string, interfaces []network.InterfaceInfo) ([]network.InterfaceInfo, error) {
	haveNameservers, haveSearchDomains := false, false
	if len(interfaces) == 0 {
		// Use the fallback network config as a last resort.
		interfaces = container.FallbackInterfaceInfo()
	}

	results := make([]network.InterfaceInfo, len(interfaces))
	for i, info := range interfaces {
		if info.ParentInterfaceName == "" {
			info.ParentInterfaceName = bridgeDevice
		}

		if len(info.DNSServers) > 0 {
			haveNameservers = true
		}

		if len(info.DNSSearchDomains) > 0 {
			haveSearchDomains = true
		}
		results[i] = info
	}

	if !haveNameservers || !haveSearchDomains {
		logger.Warningf("incomplete DNS config found, discovering host's DNS config")
		dnsConfig, err := network.ParseResolvConf(resolvConf)
		if err != nil {
			return nil, errors.Trace(err)
		}

		// Since the result is sorted, the first entry is the primary NIC. Also,
		// results always contains at least one element.
		results[0].DNSServers = dnsConfig.Nameservers
		results[0].DNSSearchDomains = dnsConfig.SearchDomains
		logger.Debugf(
			"setting DNS servers %+v and domains %+v on container interface %q",
			results[0].DNSServers, results[0].DNSSearchDomains, results[0].InterfaceName,
		)
	}

	return results, nil
}
Beispiel #7
0
func (*UtilsSuite) TestParseResolvConfValueFollowedByCommentWithoutWhitespace(c *gc.C) {
	badConf := makeResolvConf(c, "search foo bar#bad rest;is#ignored: still part of the comment", 0644)
	result, err := network.ParseResolvConf(badConf)
	c.Check(err, gc.ErrorMatches, `parsing ".*", line 1: "search": invalid value "bar#bad"`)
	c.Check(result, gc.IsNil)
}
Beispiel #8
0
func (*UtilsSuite) TestParseResolvConfNameserverWithoutValue(c *gc.C) {
	badConf := makeResolvConf(c, "nameserver", 0644)
	result, err := network.ParseResolvConf(badConf)
	c.Check(err, gc.ErrorMatches, `parsing ".*", line 1: "nameserver": required value\(s\) missing`)
	c.Check(result, gc.IsNil)
}
Beispiel #9
0
func (*UtilsSuite) TestParseResolvConfNameserverWithMultipleValues(c *gc.C) {
	badConf := makeResolvConf(c, "nameserver one two 42 ;;; comment still-inside-comment\n", 0644)
	result, err := network.ParseResolvConf(badConf)
	c.Check(err, gc.ErrorMatches, `parsing ".*", line 1: one value expected for "nameserver", got 3`)
	c.Check(result, gc.IsNil)
}