forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_api_test.go
110 lines (100 loc) · 4.43 KB
/
docker_api_test.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"io/ioutil"
"net/http"
"runtime"
"strconv"
"strings"
"github.com/docker/docker/api"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/internal/test/request"
"github.com/go-check/check"
)
func (s *DockerSuite) TestAPIOptionsRoute(c *check.C) {
resp, _, err := request.Do("/", request.Method(http.MethodOptions))
c.Assert(err, checker.IsNil)
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
}
func (s *DockerSuite) TestAPIGetEnabledCORS(c *check.C) {
res, body, err := request.Get("/version")
c.Assert(err, checker.IsNil)
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
body.Close()
// TODO: @runcom incomplete tests, why old integration tests had this headers
// and here none of the headers below are in the response?
//c.Log(res.Header)
//c.Assert(res.Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
//c.Assert(res.Header.Get("Access-Control-Allow-Headers"), check.Equals, "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
}
func (s *DockerSuite) TestAPIClientVersionOldNotSupported(c *check.C) {
if testEnv.OSType != runtime.GOOS {
c.Skip("Daemon platform doesn't match test platform")
}
if api.MinVersion == api.DefaultVersion {
c.Skip("API MinVersion==DefaultVersion")
}
v := strings.Split(api.MinVersion, ".")
vMinInt, err := strconv.Atoi(v[1])
c.Assert(err, checker.IsNil)
vMinInt--
v[1] = strconv.Itoa(vMinInt)
version := strings.Join(v, ".")
resp, body, err := request.Get("/v" + version + "/version")
c.Assert(err, checker.IsNil)
defer body.Close()
c.Assert(resp.StatusCode, checker.Equals, http.StatusBadRequest)
expected := fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", version, api.MinVersion)
content, err := ioutil.ReadAll(body)
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(string(content)), checker.Contains, expected)
}
func (s *DockerSuite) TestAPIErrorJSON(c *check.C) {
httpResp, body, err := request.Post("/containers/create", request.JSONBody(struct{}{}))
c.Assert(err, checker.IsNil)
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
c.Assert(httpResp.StatusCode, checker.Equals, http.StatusInternalServerError)
} else {
c.Assert(httpResp.StatusCode, checker.Equals, http.StatusBadRequest)
}
c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json")
b, err := request.ReadBody(body)
c.Assert(err, checker.IsNil)
c.Assert(getErrorMessage(c, b), checker.Equals, "Config cannot be empty in order to create a container")
}
func (s *DockerSuite) TestAPIErrorPlainText(c *check.C) {
// Windows requires API 1.25 or later. This test is validating a behaviour which was present
// in v1.23, but changed in 1.24, hence not applicable on Windows. See apiVersionSupportsJSONErrors
testRequires(c, DaemonIsLinux)
httpResp, body, err := request.Post("/v1.23/containers/create", request.JSONBody(struct{}{}))
c.Assert(err, checker.IsNil)
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
c.Assert(httpResp.StatusCode, checker.Equals, http.StatusInternalServerError)
} else {
c.Assert(httpResp.StatusCode, checker.Equals, http.StatusBadRequest)
}
c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain")
b, err := request.ReadBody(body)
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(string(b)), checker.Equals, "Config cannot be empty in order to create a container")
}
func (s *DockerSuite) TestAPIErrorNotFoundJSON(c *check.C) {
// 404 is a different code path to normal errors, so test separately
httpResp, body, err := request.Get("/notfound", request.JSON)
c.Assert(err, checker.IsNil)
c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound)
c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json")
b, err := request.ReadBody(body)
c.Assert(err, checker.IsNil)
c.Assert(getErrorMessage(c, b), checker.Equals, "page not found")
}
func (s *DockerSuite) TestAPIErrorNotFoundPlainText(c *check.C) {
httpResp, body, err := request.Get("/v1.23/notfound", request.JSON)
c.Assert(err, checker.IsNil)
c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound)
c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain")
b, err := request.ReadBody(body)
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(string(b)), checker.Equals, "page not found")
}