|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/docker/go-connections/tlsconfig" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_buildDockerClientOptions(t *testing.T) { |
| 12 | + // Tell Docker where its config is located, so that we have repeatable paths in the tests |
| 13 | + os.Setenv("DOCKER_CONFIG", "/home/me/.docker") |
| 14 | + defer os.Unsetenv("DOCKER_CONFIG") |
| 15 | + |
| 16 | + defaultTLSOptions := &tlsconfig.Options{ |
| 17 | + CAFile: "/home/me/.docker/ca.pem", |
| 18 | + CertFile: "/home/me/.docker/cert.pem", |
| 19 | + KeyFile: "/home/me/.docker/key.pem", |
| 20 | + } |
| 21 | + |
| 22 | + customTLSOptions := &tlsconfig.Options{ |
| 23 | + CAFile: "/mycerts/ca.pem", |
| 24 | + CertFile: "/mycerts/cert.pem", |
| 25 | + KeyFile: "/mycerts/key.pem", |
| 26 | + } |
| 27 | + |
| 28 | + t.Run("tls disabled", func(t *testing.T) { |
| 29 | + os.Unsetenv(DockerTLSVerifyEnvVar) |
| 30 | + opts := buildDockerClientOptions() |
| 31 | + assert.False(t, opts.Common.TLS, "expected TLS to be disabled") |
| 32 | + assert.False(t, opts.Common.TLSVerify, "expected TLSVerify to be disabled") |
| 33 | + assert.Nil(t, opts.Common.TLSOptions, "expected TLSOptions to be unset") |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("tls enabled without certs", func(t *testing.T) { |
| 37 | + os.Setenv(DockerTLSVerifyEnvVar, "true") |
| 38 | + os.Unsetenv(DockerCertPathEnvVar) |
| 39 | + defer func() { |
| 40 | + os.Unsetenv(DockerTLSVerifyEnvVar) |
| 41 | + }() |
| 42 | + |
| 43 | + opts := buildDockerClientOptions() |
| 44 | + assert.True(t, opts.Common.TLS, "expected TLS to be enabled") |
| 45 | + assert.True(t, opts.Common.TLSVerify, "expected the certs to be verified") |
| 46 | + assert.Equal(t, defaultTLSOptions, opts.Common.TLSOptions, "expected TLSOptions to be initialized to the default TLS settings") |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("tls enabled with custom certs", func(t *testing.T) { |
| 50 | + os.Setenv(DockerTLSVerifyEnvVar, "true") |
| 51 | + os.Setenv(DockerCertPathEnvVar, "/mycerts") |
| 52 | + defer func() { |
| 53 | + os.Unsetenv(DockerTLSVerifyEnvVar) |
| 54 | + os.Unsetenv(DockerCertPathEnvVar) |
| 55 | + }() |
| 56 | + |
| 57 | + opts := buildDockerClientOptions() |
| 58 | + assert.True(t, opts.Common.TLS, "expected TLS to be enabled") |
| 59 | + assert.True(t, opts.Common.TLSVerify, "expected the certs to be verified") |
| 60 | + assert.Equal(t, customTLSOptions, opts.Common.TLSOptions, "expected TLSOptions to use the custom DOCKER_CERT_PATH set") |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("tls enabled with self-signed certs", func(t *testing.T) { |
| 64 | + os.Setenv(DockerTLSVerifyEnvVar, "false") |
| 65 | + os.Setenv(DockerCertPathEnvVar, "/mycerts") |
| 66 | + defer func() { |
| 67 | + os.Unsetenv(DockerTLSVerifyEnvVar) |
| 68 | + os.Unsetenv(DockerCertPathEnvVar) |
| 69 | + }() |
| 70 | + |
| 71 | + opts := buildDockerClientOptions() |
| 72 | + assert.True(t, opts.Common.TLS, "expected TLS to be enabled") |
| 73 | + assert.False(t, opts.Common.TLSVerify, "expected TLSVerify to be false") |
| 74 | + assert.Equal(t, customTLSOptions, opts.Common.TLSOptions, "expected TLSOptions to use the custom DOCKER_CERT_PATH set") |
| 75 | + }) |
| 76 | +} |
0 commit comments