Skip to content

Commit

Permalink
Changing Ubuntu Net manager to write to /etc/resolv.conf.d/base
Browse files Browse the repository at this point in the history
[#123916651]

Signed-off-by: Cornelius Schumacher <[email protected]>
  • Loading branch information
maximilien authored and cornelius committed Jul 2, 2016
1 parent 8a1a382 commit b74fb97
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 8 deletions.
35 changes: 30 additions & 5 deletions platform/net/ubuntu_net_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,27 +353,52 @@ func (net UbuntuNetManager) ifaceNames(dhcpConfigs DHCPInterfaceConfigurations,
func (net UbuntuNetManager) writeResolvConf(networks boshsettings.Networks) error {
buffer := bytes.NewBuffer([]byte{})

const ubuntuResolvConfTemplate = `# Generated by bosh-agent
const resolvConfTemplate = `# Generated by bosh-agent
{{ range .DNSServers }}nameserver {{ . }}
{{ end }}`

t := template.Must(template.New("resolv-conf").Parse(ubuntuResolvConfTemplate))
t := template.Must(template.New("resolv-conf").Parse(resolvConfTemplate))

// Keep DNS servers in the order specified by the network
dnsNetwork, _ := networks.DefaultNetworkFor("dns")

type dnsConfigArg struct {
DNSServers []string
}

dnsServersArg := dnsConfigArg{dnsNetwork.DNS}

err := t.Execute(buffer, dnsServersArg)
if err != nil {
return bosherr.WrapError(err, "Generating config from template")
return bosherr.WrapError(err, "Generating DNS config from template")
}

if len(dnsNetwork.DNS) > 0 {
// Write out base so that releases may overwrite head
err = net.fs.WriteFile("/etc/resolvconf/resolv.conf.d/base", buffer.Bytes())
if err != nil {
return bosherr.WrapError(err, "Writing to /etc/resolvconf/resolv.conf.d/base")
}
} else {
// For the first time before resolv.conf is symlinked to /run/...
// inherit possibly configured resolv.conf

targetPath, err := net.fs.ReadLink("/etc/resolv.conf")
if err != nil {
return bosherr.WrapError(err, "Reading /etc/resolv.conf symlink")
}

if targetPath == "/etc/resolv.conf" {
err := net.fs.CopyFile("/etc/resolv.conf", "/etc/resolvconf/resolv.conf.d/base")
if err != nil {
return bosherr.WrapError(err, "Copying /etc/resolv.conf for backwards compat")
}
}
}

err = net.fs.WriteFile("/etc/resolvconf/resolv.conf.d/head", buffer.Bytes())
err = net.fs.Symlink("/run/resolvconf/resolv.conf", "/etc/resolv.conf")
if err != nil {
return bosherr.WrapError(err, "Writing to /etc/resolvconf/resolv.conf.d/head")
return bosherr.WrapError(err, "Setting up /etc/resolv.conf symlink")
}

_, _, _, err = net.cmdRunner.RunCommand("resolvconf", "-u")
Expand Down
115 changes: 112 additions & 3 deletions platform/net/ubuntu_net_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,118 @@ dns-nameservers 8.8.8.8 9.9.9.9`
})

Context("networks is preconfigured", func() {
var networks boshsettings.Networks
BeforeEach(func() {
dhcpNetwork.Preconfigured = true
staticNetwork.Preconfigured = true
networks = boshsettings.Networks{
"first": dhcpNetwork,
"second": staticNetwork,
}

Expect(networks.IsPreconfigured()).To(BeTrue())
})

Context("when there are configured DNS servers", func() {
BeforeEach(func() {
networks = boshsettings.Networks{
"first": dhcpNetwork,
}
})

It("writes DNS to /etc/resolvconf/resolv.conf.d/base", func() {
err := netManager.SetupNetworking(networks, nil)
Expect(err).ToNot(HaveOccurred())

resolvConfBase := fs.GetFileTestStat("/etc/resolvconf/resolv.conf.d/base")
Expect(resolvConfBase).ToNot(BeNil())

expectedResolvConfBase := `# Generated by bosh-agent
nameserver 8.8.8.8
nameserver 9.9.9.9
`
Expect(resolvConfBase.StringContents()).To(Equal(expectedResolvConfBase))
})

Context("when writing to ../resolv.conf.d/base fails", func() {
It("fails reporting the error", func() {
fs.WriteFileError = errors.New("fake-write-file-error")

err := netManager.SetupNetworking(networks, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Writing to /etc/resolvconf/resolv.conf.d/base"))
})
})
})

Context("when no DNS servers are configured", func() {
BeforeEach(func() {
dhcpNetwork.DNS = []string{}
networks = boshsettings.Networks{
"first": dhcpNetwork,
"second": staticNetwork,
}
})

Context("when could not read link /etc/resolv.conf", func() {
It("fails reporting error", func() {
fs.ReadLinkError = errors.New("fake-read-link-error")

err := netManager.SetupNetworking(networks, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Reading /etc/resolv.conf symlink"))
})
})

Context("when /etc/resolv.conf is no symlink", func() {
BeforeEach(func() {
err := fs.Symlink("/etc/resolv.conf", "/etc/resolv.conf")
Expect(err).ToNot(HaveOccurred())

err = fs.WriteFileString("/etc/resolv.conf", "fake-content")
Expect(err).ToNot(HaveOccurred())
})

It("copies /etc/resolv.conf to .../resolv.conf.d/base", func() {
err := netManager.SetupNetworking(networks, nil)
Expect(err).ToNot(HaveOccurred())

contents, err := fs.ReadFile("/etc/resolvconf/resolv.conf.d/base")
Expect(err).ToNot(HaveOccurred())
Expect(string(contents)).To(Equal("fake-content"))
})

Context("when copying fails", func() {
It("fails reporting the error", func() {
fs.CopyFileError = errors.New("fake-copy-error")

err := netManager.SetupNetworking(networks, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Copying /etc/resolv.conf for backwards compat"))
})
})
})
})

It("writes dns servers in /etc/resolvconf/resolv.conf.d/head", func() {
It("forces /etc/resolv.conf to be a symlink", func() {
err := netManager.SetupNetworking(networks, nil)
Expect(err).ToNot(HaveOccurred())
Expect(fs.ReadLink("/etc/resolv.conf")).To(Equal("/run/resolvconf/resolv.conf"))
})

Context("when symlink command fails", func() {
BeforeEach(func() {
fs.SymlinkError = errors.New("fake-symlink-error")
})

It("fails reporting error", func() {
err := netManager.SetupNetworking(networks, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Setting up /etc/resolv.conf symlink"))
})
})

It("writes dns servers in /etc/resolvconf/resolv.conf.d/base", func() {
dhcpNetwork.Preconfigured = true
staticNetwork.Preconfigured = true
networks := boshsettings.Networks{
Expand All @@ -216,7 +326,7 @@ dns-nameservers 8.8.8.8 9.9.9.9`
err := netManager.SetupNetworking(networks, nil)
Expect(err).ToNot(HaveOccurred())

resolvConfHead := fs.GetFileTestStat("/etc/resolvconf/resolv.conf.d/head")
resolvConfHead := fs.GetFileTestStat("/etc/resolvconf/resolv.conf.d/base")
Expect(resolvConfHead).ToNot(BeNil())

expectedResolvConfHead = `# Generated by bosh-agent
Expand All @@ -240,7 +350,6 @@ nameserver 9.9.9.9
Expect(len(cmdRunner.RunCommands)).To(Equal(1))
Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"resolvconf", "-u"}))
})

})

It("writes interfaces in /etc/network/interfaces in alphabetic order", func() {
Expand Down

0 comments on commit b74fb97

Please sign in to comment.