From 7d296522f68a80d540b20753ea0648171ee12825 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 7 Feb 2018 11:22:55 -0500 Subject: [PATCH 1/2] update vendor Signed-off-by: Daniel Nephin --- .../gotestyourself/gotestyourself/env/env.go | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 vendor/github.com/gotestyourself/gotestyourself/env/env.go diff --git a/vendor/github.com/gotestyourself/gotestyourself/env/env.go b/vendor/github.com/gotestyourself/gotestyourself/env/env.go new file mode 100644 index 0000000000..95a53bee9c --- /dev/null +++ b/vendor/github.com/gotestyourself/gotestyourself/env/env.go @@ -0,0 +1,92 @@ +/*Package env provides functions to test code that read environment variables +or the current working directory. +*/ +package env + +import ( + "os" + "strings" + + "github.com/gotestyourself/gotestyourself/assert" +) + +type helperT interface { + Helper() +} + +// Patch changes the value of an environment variable, and returns a +// function which will reset the the value of that variable back to the +// previous state. +func Patch(t assert.TestingT, key, value string) func() { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + oldValue, ok := os.LookupEnv(key) + assert.NilError(t, os.Setenv(key, value)) + return func() { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + if !ok { + assert.NilError(t, os.Unsetenv(key)) + return + } + assert.NilError(t, os.Setenv(key, oldValue)) + } +} + +// PatchAll sets the environment to env, and returns a function which will +// reset the environment back to the previous state. +func PatchAll(t assert.TestingT, env map[string]string) func() { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + oldEnv := os.Environ() + os.Clearenv() + + for key, value := range env { + assert.NilError(t, os.Setenv(key, value)) + } + return func() { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + os.Clearenv() + for key, oldVal := range ToMap(oldEnv) { + assert.NilError(t, os.Setenv(key, oldVal)) + } + } +} + +// ToMap takes a list of strings in the format returned by os.Environ() and +// returns a mapping of keys to values. +func ToMap(env []string) map[string]string { + result := map[string]string{} + for _, raw := range env { + parts := strings.SplitN(raw, "=", 2) + switch len(parts) { + case 1: + result[raw] = "" + case 2: + result[parts[0]] = parts[1] + } + } + return result +} + +// ChangeWorkingDir to the directory, and return a function which restores the +// previous working directory. +func ChangeWorkingDir(t assert.TestingT, dir string) func() { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + cwd, err := os.Getwd() + assert.NilError(t, err) + assert.NilError(t, os.Chdir(dir)) + return func() { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + assert.NilError(t, os.Chdir(cwd)) + } +} From 3c4537d5b33d951237ea5e4cc123953eda7a37e7 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 6 Feb 2018 12:33:18 -0500 Subject: [PATCH 2/2] Support a proxy in splunk log driver Signed-off-by: Daniel Nephin --- daemon/logger/splunk/splunk.go | 1 + daemon/logger/splunk/splunk_test.go | 32 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/daemon/logger/splunk/splunk.go b/daemon/logger/splunk/splunk.go index 30ab7b7414..ada88d3559 100644 --- a/daemon/logger/splunk/splunk.go +++ b/daemon/logger/splunk/splunk.go @@ -218,6 +218,7 @@ func New(info logger.Info) (logger.Logger, error) { transport := &http.Transport{ TLSClientConfig: tlsConfig, + Proxy: http.ProxyFromEnvironment, } client := &http.Client{ Transport: transport, diff --git a/daemon/logger/splunk/splunk_test.go b/daemon/logger/splunk/splunk_test.go index 97613771d2..4ffdd853c5 100644 --- a/daemon/logger/splunk/splunk_test.go +++ b/daemon/logger/splunk/splunk_test.go @@ -4,12 +4,14 @@ import ( "compress/gzip" "context" "fmt" + "net/http" "os" "runtime" "testing" "time" "github.com/docker/docker/daemon/logger" + "github.com/gotestyourself/gotestyourself/env" "github.com/stretchr/testify/require" ) @@ -82,6 +84,36 @@ func TestNewMissedToken(t *testing.T) { } } +func TestNewWithProxy(t *testing.T) { + proxy := "http://proxy.testing:8888" + reset := env.Patch(t, "HTTP_PROXY", proxy) + defer reset() + + // must not be localhost + splunkURL := "http://example.com:12345" + logger, err := New(logger.Info{ + Config: map[string]string{ + splunkURLKey: splunkURL, + splunkTokenKey: "token", + splunkVerifyConnectionKey: "false", + }, + ContainerID: "containeriid", + }) + require.NoError(t, err) + splunkLogger := logger.(*splunkLoggerInline) + + proxyFunc := splunkLogger.transport.Proxy + require.NotNil(t, proxyFunc) + + req, err := http.NewRequest("GET", splunkURL, nil) + require.NoError(t, err) + + proxyURL, err := proxyFunc(req) + require.NoError(t, err) + require.NotNil(t, proxyURL) + require.Equal(t, proxy, proxyURL.String()) +} + // Test default settings func TestDefault(t *testing.T) { hec := NewHTTPEventCollectorMock(t)