Signed-off-by: Daniel Nephin <dnephin@docker.com>
| ... | ... |
@@ -6,18 +6,19 @@ import ( |
| 6 | 6 |
"net/url" |
| 7 | 7 |
"os" |
| 8 | 8 |
"runtime" |
| 9 |
- "strings" |
|
| 10 | 9 |
"testing" |
| 11 | 10 |
|
| 12 | 11 |
"github.com/docker/docker/api" |
| 13 | 12 |
"github.com/docker/docker/api/types" |
| 14 | 13 |
"github.com/docker/docker/internal/testutil" |
| 14 |
+ "github.com/gotestyourself/gotestyourself/env" |
|
| 15 | 15 |
"github.com/gotestyourself/gotestyourself/skip" |
| 16 | 16 |
"github.com/stretchr/testify/assert" |
| 17 |
+ "github.com/stretchr/testify/require" |
|
| 17 | 18 |
) |
| 18 | 19 |
|
| 19 | 20 |
func TestNewEnvClient(t *testing.T) {
|
| 20 |
- skip.IfCondition(t, runtime.GOOS == "windows") |
|
| 21 |
+ skip.If(t, runtime.GOOS == "windows") |
|
| 21 | 22 |
|
| 22 | 23 |
testcases := []struct {
|
| 23 | 24 |
doc string |
| ... | ... |
@@ -83,10 +84,9 @@ func TestNewEnvClient(t *testing.T) {
|
| 83 | 83 |
}, |
| 84 | 84 |
} |
| 85 | 85 |
|
| 86 |
- env := envToMap() |
|
| 87 |
- defer mapToEnv(env) |
|
| 86 |
+ defer env.PatchAll(t, nil)() |
|
| 88 | 87 |
for _, c := range testcases {
|
| 89 |
- mapToEnv(c.envs) |
|
| 88 |
+ env.PatchAll(t, c.envs) |
|
| 90 | 89 |
apiclient, err := NewEnvClient() |
| 91 | 90 |
if c.expectedError != "" {
|
| 92 | 91 |
assert.Error(t, err, c.doc) |
| ... | ... |
@@ -196,16 +196,12 @@ func TestParseHostURL(t *testing.T) {
|
| 196 | 196 |
} |
| 197 | 197 |
|
| 198 | 198 |
func TestNewEnvClientSetsDefaultVersion(t *testing.T) {
|
| 199 |
- env := envToMap() |
|
| 200 |
- defer mapToEnv(env) |
|
| 201 |
- |
|
| 202 |
- envMap := map[string]string{
|
|
| 199 |
+ defer env.PatchAll(t, map[string]string{
|
|
| 203 | 200 |
"DOCKER_HOST": "", |
| 204 | 201 |
"DOCKER_API_VERSION": "", |
| 205 | 202 |
"DOCKER_TLS_VERIFY": "", |
| 206 | 203 |
"DOCKER_CERT_PATH": "", |
| 207 |
- } |
|
| 208 |
- mapToEnv(envMap) |
|
| 204 |
+ })() |
|
| 209 | 205 |
|
| 210 | 206 |
client, err := NewEnvClient() |
| 211 | 207 |
if err != nil {
|
| ... | ... |
@@ -225,18 +221,10 @@ func TestNewEnvClientSetsDefaultVersion(t *testing.T) {
|
| 225 | 225 |
// TestNegotiateAPIVersionEmpty asserts that client.Client can |
| 226 | 226 |
// negotiate a compatible APIVersion when omitted |
| 227 | 227 |
func TestNegotiateAPIVersionEmpty(t *testing.T) {
|
| 228 |
- env := envToMap() |
|
| 229 |
- defer mapToEnv(env) |
|
| 230 |
- |
|
| 231 |
- envMap := map[string]string{
|
|
| 232 |
- "DOCKER_API_VERSION": "", |
|
| 233 |
- } |
|
| 234 |
- mapToEnv(envMap) |
|
| 228 |
+ defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": ""})
|
|
| 235 | 229 |
|
| 236 | 230 |
client, err := NewEnvClient() |
| 237 |
- if err != nil {
|
|
| 238 |
- t.Fatal(err) |
|
| 239 |
- } |
|
| 231 |
+ require.NoError(t, err) |
|
| 240 | 232 |
|
| 241 | 233 |
ping := types.Ping{
|
| 242 | 234 |
APIVersion: "", |
| ... | ... |
@@ -260,12 +248,9 @@ func TestNegotiateAPIVersionEmpty(t *testing.T) {
|
| 260 | 260 |
// negotiate a compatible APIVersion with the server |
| 261 | 261 |
func TestNegotiateAPIVersion(t *testing.T) {
|
| 262 | 262 |
client, err := NewEnvClient() |
| 263 |
- if err != nil {
|
|
| 264 |
- t.Fatal(err) |
|
| 265 |
- } |
|
| 263 |
+ require.NoError(t, err) |
|
| 266 | 264 |
|
| 267 | 265 |
expected := "1.21" |
| 268 |
- |
|
| 269 | 266 |
ping := types.Ping{
|
| 270 | 267 |
APIVersion: expected, |
| 271 | 268 |
OSType: "linux", |
| ... | ... |
@@ -291,18 +276,11 @@ func TestNegotiateAPIVersion(t *testing.T) {
|
| 291 | 291 |
// TestNegotiateAPIVersionOverride asserts that we honor |
| 292 | 292 |
// the environment variable DOCKER_API_VERSION when negotianing versions |
| 293 | 293 |
func TestNegotiateAPVersionOverride(t *testing.T) {
|
| 294 |
- env := envToMap() |
|
| 295 |
- defer mapToEnv(env) |
|
| 296 |
- |
|
| 297 |
- envMap := map[string]string{
|
|
| 298 |
- "DOCKER_API_VERSION": "9.99", |
|
| 299 |
- } |
|
| 300 |
- mapToEnv(envMap) |
|
| 294 |
+ expected := "9.99" |
|
| 295 |
+ defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": expected})()
|
|
| 301 | 296 |
|
| 302 | 297 |
client, err := NewEnvClient() |
| 303 |
- if err != nil {
|
|
| 304 |
- t.Fatal(err) |
|
| 305 |
- } |
|
| 298 |
+ require.NoError(t, err) |
|
| 306 | 299 |
|
| 307 | 300 |
ping := types.Ping{
|
| 308 | 301 |
APIVersion: "1.24", |
| ... | ... |
@@ -310,31 +288,11 @@ func TestNegotiateAPVersionOverride(t *testing.T) {
|
| 310 | 310 |
Experimental: false, |
| 311 | 311 |
} |
| 312 | 312 |
|
| 313 |
- expected := envMap["DOCKER_API_VERSION"] |
|
| 314 |
- |
|
| 315 | 313 |
// test that we honored the env var |
| 316 | 314 |
client.NegotiateAPIVersionPing(ping) |
| 317 | 315 |
assert.Equal(t, expected, client.version) |
| 318 | 316 |
} |
| 319 | 317 |
|
| 320 |
-// mapToEnv takes a map of environment variables and sets them |
|
| 321 |
-func mapToEnv(env map[string]string) {
|
|
| 322 |
- for k, v := range env {
|
|
| 323 |
- os.Setenv(k, v) |
|
| 324 |
- } |
|
| 325 |
-} |
|
| 326 |
- |
|
| 327 |
-// envToMap returns a map of environment variables |
|
| 328 |
-func envToMap() map[string]string {
|
|
| 329 |
- env := make(map[string]string) |
|
| 330 |
- for _, e := range os.Environ() {
|
|
| 331 |
- kv := strings.SplitAfterN(e, "=", 2) |
|
| 332 |
- env[kv[0]] = kv[1] |
|
| 333 |
- } |
|
| 334 |
- |
|
| 335 |
- return env |
|
| 336 |
-} |
|
| 337 |
- |
|
| 338 | 318 |
type roundTripFunc func(*http.Request) (*http.Response, error) |
| 339 | 319 |
|
| 340 | 320 |
func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|