This repository was archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcheck_docker_test.go
190 lines (152 loc) · 4.62 KB
/
check_docker_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"github.com/shenwei356/util/bytesize"
"os"
"testing"
)
func init() {
if os.Getenv("DOCKER_IMAGE") == "" || os.Getenv("DOCKER_CONTAINER_NAME") == "" {
println("You must set DOCKER_IMAGE and DOCKER_CONTAINER_NAME to test image related things.")
}
}
func TestByteSizeDoTheRightThing(t *testing.T) {
value, err := bytesize.Parse([]byte("1 kb"))
if err != nil {
t.Errorf("Failed to parse byte size. Error: %v", err)
}
if float64(value) != 1024 {
t.Errorf("Failed to parse byte size correctly. Value: %v", float64(value))
}
value, err = bytesize.Parse([]byte("1 mB"))
if err != nil {
t.Errorf("Failed to parse byte size. Error: %v", err)
}
if float64(value) != 1024*1024 {
t.Errorf("Failed to parse byte size correctly. Value: %v", float64(value))
}
value, err = bytesize.Parse([]byte("1 GB"))
if err != nil {
t.Errorf("Failed to parse byte size. Error: %v", err)
}
if float64(value) != 1024*1024*1024 {
t.Errorf("Failed to parse byte size correctly. Value: %v", float64(value))
}
}
func NewCheckDockerForTest(t *testing.T) *CheckDocker {
endpoint := os.Getenv("DOCKER_HOST")
if endpoint == "" {
endpoint = "http://localhost:2375"
}
cd, err := NewCheckDocker(endpoint)
if err != nil {
t.Fatalf("Failed to initialize CheckDocker. Error: %v", err)
}
if cd.dockerclient == nil {
t.Fatalf("Failed to initialize docker client. You must have a docker server (defined in DOCKER_HOST) to run test.")
}
err = cd.GetData()
if err != nil {
t.Fatalf("Unable to get data from docker server. Error: %v", err)
}
return cd
}
func TestNewCheckDocker(t *testing.T) {
NewCheckDockerForTest(t)
}
func TestGetByteSizeDriverStatus(t *testing.T) {
cd := NewCheckDockerForTest(t)
driver := cd.dockerInfoData.Get("Driver")
for _, key := range []string{"Data Space Used", "Data Space Total", "Metadata Space Used", "Metadata Space Total"} {
if driver == "aufs" {
_, err := cd.getByteSizeDriverStatus(key)
if err == nil {
t.Errorf("%v does not provide this information: %v.", driver, key)
}
} else if driver == "devicemapper" {
byteSizeValue, err := cd.getByteSizeDriverStatus(key)
if err != nil {
t.Errorf("%v should provide these this information: %v. Error: %v", driver, key, err)
}
if float64(byteSizeValue) <= 0 {
t.Errorf("%v byte value should never be empty.", key)
}
}
}
}
func TestIsContainerRunning(t *testing.T) {
imageId := os.Getenv("DOCKER_IMAGE")
if imageId != "" {
cd := NewCheckDockerForTest(t)
_, isRunning := cd.IsContainerRunning(imageId)
if !isRunning {
t.Errorf("Container for image: %v should be running.", imageId)
}
}
}
func TestIsNamedContainerRunning(t *testing.T) {
containerName := os.Getenv("DOCKER_CONTAINER_NAME")
if containerName == "" {
return
}
cd := NewCheckDockerForTest(t)
_, isRunning := cd.IsNamedContainerRunning(containerName)
if !isRunning {
t.Errorf("Container named: %v should be running.", containerName)
}
containerName = "no_container_with_this_name_should_exist"
_, isRunning = cd.IsNamedContainerRunning(containerName)
if isRunning {
t.Errorf("Container named: %v should not be running.", containerName)
}
}
func TestCountGhostsByImageId(t *testing.T) {
imageId := os.Getenv("DOCKER_IMAGE")
if imageId != "" {
cd := NewCheckDockerForTest(t)
_, isGhost := cd.IsContainerAGhost(imageId)
if isGhost {
t.Errorf("Container for image: %v should not be a ghost.", imageId)
}
}
}
func TestCheckMetaSpace(t *testing.T) {
cd := NewCheckDockerForTest(t)
status := cd.CheckMetaSpace(cd.WarnMetaSpace, cd.CritMetaSpace)
if status == nil {
t.Error("NagiosStatus struct should never be nil.")
}
}
func TestCheckDataSpace(t *testing.T) {
cd := NewCheckDockerForTest(t)
status := cd.CheckDataSpace(cd.WarnMetaSpace, cd.CritMetaSpace)
if status == nil {
t.Error("NagiosStatus struct should never be nil.")
}
}
func TestCheckImageContainerIsInGoodShape(t *testing.T) {
imageId := os.Getenv("DOCKER_IMAGE")
if imageId != "" {
cd := NewCheckDockerForTest(t)
status := cd.CheckImageContainerIsInGoodShape(imageId)
if status == nil {
t.Error("NagiosStatus struct should never be nil.")
}
if status.Value != 0 {
t.Errorf("Container of image: %v should be healthy.", imageId)
}
}
}
func TestCheckNamedContainerIsInGoodShape(t *testing.T) {
containerName := os.Getenv("DOCKER_CONTAINER_NAME")
if containerName == "" {
return
}
cd := NewCheckDockerForTest(t)
status := cd.CheckNamedContainerIsInGoodShape(containerName)
if status == nil {
t.Error("NagiosStatus struct should never be nil.")
}
if status.Value != 0 {
t.Errorf("Container named: %v should be healthy.", containerName)
}
}