summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2018-08-14 14:59:24 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-08-15 11:38:18 +0000
commitcd7102a70e7f3555df832c4c1bcf958e121cbf4d (patch)
tree9c87128880bf9b82c6b2fe6afed0d4e054dabba4
parent77baae25ce473cac57d107715bebdf914f2567a0 (diff)
downloadpodman-cd7102a70e7f3555df832c4c1bcf958e121cbf4d.tar.gz
podman-cd7102a70e7f3555df832c4c1bcf958e121cbf4d.tar.bz2
podman-cd7102a70e7f3555df832c4c1bcf958e121cbf4d.zip
Fix handling of hostname in --net=host
Hostname should be set to the hosts hostname when network is none. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Closes: #1274 Approved by: giuseppe
-rw-r--r--pkg/spec/spec.go9
-rw-r--r--test/e2e/run_networking_test.go38
2 files changed, 44 insertions, 3 deletions
diff --git a/pkg/spec/spec.go b/pkg/spec/spec.go
index d9888e999..bceae4677 100644
--- a/pkg/spec/spec.go
+++ b/pkg/spec/spec.go
@@ -1,6 +1,7 @@
package createconfig
import (
+ "os"
"strings"
"github.com/docker/docker/daemon/caps"
@@ -73,6 +74,14 @@ func CreateConfigToOCISpec(config *CreateConfig) (*spec.Spec, error) { //nolint
g.AddAnnotation(key, val)
}
g.SetRootReadonly(config.ReadOnlyRootfs)
+ if config.Hostname == "" {
+ if config.NetMode.IsHost() {
+ config.Hostname, err = os.Hostname()
+ if err != nil {
+ return nil, errors.Wrap(err, "unable to retrieve hostname")
+ }
+ }
+ }
g.SetHostname(config.Hostname)
if config.Hostname != "" {
g.AddProcessEnv("HOSTNAME", config.Hostname)
diff --git a/test/e2e/run_networking_test.go b/test/e2e/run_networking_test.go
index f7a7f8d67..a4bdcdf89 100644
--- a/test/e2e/run_networking_test.go
+++ b/test/e2e/run_networking_test.go
@@ -10,9 +10,10 @@ import (
var _ = Describe("Podman rmi", func() {
var (
- tempdir string
- err error
- podmanTest PodmanTest
+ tempdir string
+ err error
+ podmanTest PodmanTest
+ hostname, _ = os.Hostname()
)
BeforeEach(func() {
@@ -98,4 +99,35 @@ var _ = Describe("Podman rmi", func() {
Expect(containerConfig[0].NetworkSettings.Ports[0].HostPort).ToNot(Equal("80"))
})
+ It("podman run hostname test", func() {
+ session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "printenv", "HOSTNAME"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+ match, _ := session.GrepString(hostname)
+ Expect(match).Should(BeFalse())
+ })
+
+ It("podman run --net host hostname test", func() {
+ session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", ALPINE, "printenv", "HOSTNAME"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+ match, _ := session.GrepString(hostname)
+ Expect(match).Should(BeTrue())
+ })
+
+ It("podman run --net host --hostname ... hostname test", func() {
+ session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+ match, _ := session.GrepString("foobar")
+ Expect(match).Should(BeTrue())
+ })
+
+ It("podman run --hostname ... hostname test", func() {
+ session := podmanTest.Podman([]string{"run", "--rm", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+ match, _ := session.GrepString("foobar")
+ Expect(match).Should(BeTrue())
+ })
})