forked from fsouza/go-dockerclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
155 lines (146 loc) · 3.95 KB
/
misc_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2014 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 (
"net/http"
"net/url"
"reflect"
"testing"
)
type DockerVersion struct {
Version string
GitCommit string
GoVersion string
}
func TestVersion(t *testing.T) {
body := `{
"Version":"0.2.2",
"GitCommit":"5a2a5cc+CHANGES",
"GoVersion":"go1.0.3"
}`
fakeRT := FakeRoundTripper{message: body, status: http.StatusOK}
client := newTestClient(&fakeRT)
expected := DockerVersion{
Version: "0.2.2",
GitCommit: "5a2a5cc+CHANGES",
GoVersion: "go1.0.3",
}
version, err := client.Version()
if err != nil {
t.Fatal(err)
}
if result := version.Get("Version"); result != expected.Version {
t.Errorf("Version(): Wrong result. Want %#v. Got %#v.", expected.Version, version.Get("Version"))
}
if result := version.Get("GitCommit"); result != expected.GitCommit {
t.Errorf("GitCommit(): Wrong result. Want %#v. Got %#v.", expected.GitCommit, version.Get("GitCommit"))
}
if result := version.Get("GoVersion"); result != expected.GoVersion {
t.Errorf("GoVersion(): Wrong result. Want %#v. Got %#v.", expected.GoVersion, version.Get("GoVersion"))
}
req := fakeRT.requests[0]
if req.Method != "GET" {
t.Errorf("Version(): wrong request method. Want GET. Got %s.", req.Method)
}
u, _ := url.Parse(client.getURL("/version"))
if req.URL.Path != u.Path {
t.Errorf("Version(): wrong request path. Want %q. Got %q.", u.Path, req.URL.Path)
}
}
func TestVersionError(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "internal error", status: http.StatusInternalServerError}
client := newTestClient(fakeRT)
version, err := client.Version()
if version != nil {
t.Errorf("Version(): expected <nil> value, got %#v.", version)
}
if err == nil {
t.Error("Version(): unexpected <nil> error")
}
}
func TestInfo(t *testing.T) {
body := `{
"Containers":11,
"Images":16,
"Debug":false,
"NFd":11,
"NGoroutines":21,
"MemoryLimit":true,
"SwapLimit":false
}`
fakeRT := FakeRoundTripper{message: body, status: http.StatusOK}
client := newTestClient(&fakeRT)
expected := &DockerInfo{
Containers: 11,
Images: 16,
Debug: false,
NFd: 11,
NGoroutines: 21,
MemoryLimit: true,
SwapLimit: false,
}
info, err := client.Info()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, info) {
t.Errorf("Info(): Wrong result.\nWant %#v.\nGot %#v.", expected, info)
}
req := fakeRT.requests[0]
if req.Method != "GET" {
t.Errorf("Info(): Wrong HTTP method. Want GET. Got %s.", req.Method)
}
u, _ := url.Parse(client.getURL("/info"))
if req.URL.Path != u.Path {
t.Errorf("Info(): Wrong request path. Want %q. Got %q.", u.Path, req.URL.Path)
}
}
func TestInfoError(t *testing.T) {
fakeRT := &FakeRoundTripper{message: "internal error", status: http.StatusInternalServerError}
client := newTestClient(fakeRT)
version, err := client.Info()
if version != nil {
t.Errorf("Info(): expected <nil> value, got %#v.", version)
}
if err == nil {
t.Error("Info(): unexpected <nil> error")
}
}
func TestParseRepositoryTag(t *testing.T) {
var tests = []struct {
input string
expectedRepo string
expectedTag string
}{
{
"localhost.localdomain:5000/samalba/hipache:latest",
"localhost.localdomain:5000/samalba/hipache",
"latest",
},
{
"localhost.localdomain:5000/samalba/hipache",
"localhost.localdomain:5000/samalba/hipache",
"",
},
{
"tsuru/python",
"tsuru/python",
"",
},
{
"tsuru/python:2.7",
"tsuru/python",
"2.7",
},
}
for _, tt := range tests {
repo, tag := ParseRepositoryTag(tt.input)
if repo != tt.expectedRepo {
t.Errorf("ParseRepositoryTag(%q): wrong repository. Want %q. Got %q", tt.input, tt.expectedRepo, repo)
}
if tag != tt.expectedTag {
t.Errorf("ParseRepositoryTag(%q): wrong tag. Want %q. Got %q", tt.input, tt.expectedTag, tag)
}
}
}