File: client_unix_test.go

package info (click to toggle)
golang-github-fsouza-go-dockerclient 1.6.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,204 kB
  • sloc: makefile: 23
file content (69 lines) | stat: -rw-r--r-- 1,678 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// +build !windows
// Copyright 2016 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docker

import (
	"io/ioutil"
	"net"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"testing"
)

const (
	nativeProtocol     = unixProtocol
	nativeRealEndpoint = "unix:///var/run/docker.sock"
	nativeBadEndpoint  = "unix:///tmp/echo.sock"
)

func TestNewTSLAPIClientUnixEndpoint(t *testing.T) {
	t.Parallel()
	srv, cleanup, err := newNativeServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.Write([]byte("ok"))
	}))
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup()
	srv.Start()
	defer srv.Close()
	endpoint := nativeProtocol + "://" + srv.Listener.Addr().String()
	client, err := newTLSClient(endpoint)
	if err != nil {
		t.Fatal(err)
	}
	if client.endpoint != endpoint {
		t.Errorf("Expected endpoint %s. Got %s.", endpoint, client.endpoint)
	}
	rsp, err := client.do(http.MethodGet, "/", doOptions{})
	if err != nil {
		t.Fatal(err)
	}
	data, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		t.Fatal(err)
	}
	if string(data) != "ok" {
		t.Fatalf("Expected response to be %q, got: %q", "ok", string(data))
	}
}

func newNativeServer(handler http.Handler) (*httptest.Server, func(), error) {
	tmpdir, err := ioutil.TempDir("", "socket")
	if err != nil {
		return nil, nil, err
	}
	socketPath := filepath.Join(tmpdir, "docker_test_stress.sock")
	l, err := net.Listen("unix", socketPath)
	if err != nil {
		return nil, nil, err
	}
	srv := httptest.NewUnstartedServer(handler)
	srv.Listener = l
	return srv, func() { os.RemoveAll(tmpdir) }, nil
}