From 78b8b7dd750cb09344f62e180e4721152af06d04 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Thu, 20 Nov 2014 15:18:12 +0500 Subject: [PATCH] Add hostname in docker section of .drone.yml --- shared/build/build.go | 1 + shared/build/script/docker.go | 15 +++++++++++++++ shared/build/script/docker_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/shared/build/build.go b/shared/build/build.go index be94904b5..b6cde2f64 100644 --- a/shared/build/build.go +++ b/shared/build/build.go @@ -319,6 +319,7 @@ func (b *Builder) teardown() error { func (b *Builder) run() error { // create and run the container conf := docker.Config{ + Hostname: script.DockerHostname(b.Build.Docker), Image: b.image.ID, AttachStdin: false, AttachStdout: true, diff --git a/shared/build/script/docker.go b/shared/build/script/docker.go index 6686045f1..dfceeeba3 100644 --- a/shared/build/script/docker.go +++ b/shared/build/script/docker.go @@ -10,6 +10,10 @@ type Docker struct { // NetworkMode (also known as `--net` option) // Could be set only if Docker is running in privileged mode NetworkMode *string `yaml:"net,omitempty"` + + // Hostname (also known as `--hostname` option) + // Could be set only if Docker is running in privileged mode + Hostname *string `yaml:"hostname,omitempty"` } // DockerNetworkMode returns DefaultNetworkMode @@ -22,3 +26,14 @@ func DockerNetworkMode(d *Docker) string { } return *d.NetworkMode } + +// DockerNetworkMode returns empty string +// when Docker.NetworkMode is empty. +// DockerNetworkMode returns Docker.NetworkMode +// when it is not empty. +func DockerHostname(d *Docker) string { + if d == nil || d.Hostname == nil { + return "" + } + return *d.Hostname +} diff --git a/shared/build/script/docker_test.go b/shared/build/script/docker_test.go index 5b6c1e94e..e089d9ea4 100644 --- a/shared/build/script/docker_test.go +++ b/shared/build/script/docker_test.go @@ -38,3 +38,32 @@ func TestDockerNetworkMode(t *testing.T) { t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) } } + +func TestDockerHostname(t *testing.T) { + var d *Docker + var expected string + + expected = "" + d = nil + if actual := DockerHostname(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } + + expected = "" + d = &Docker{} + if actual := DockerHostname(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } + + expected = "" + d = &Docker{Hostname: nil} + if actual := DockerHostname(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } + + expected = "host" + d = &Docker{Hostname: &expected} + if actual := DockerHostname(d); actual != expected { + t.Errorf("The result is invalid. [expected: %s][actual: %s]", expected, actual) + } +}