-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
integration_test.go
285 lines (248 loc) · 10.2 KB
/
integration_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package integration
import (
"bytes"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/docker/go-plugins-helpers/volume"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sapk/docker-volume-gluster/gluster"
"github.com/sapk/docker-volume-gluster/gluster/driver"
)
const timeInterval = 2 * time.Second
var randomTestName = "000000"
func TestMain(m *testing.M) {
//TODO check system for gluster, docker and docker-compose (install container version if needed)
//TODO need root to start plugin
randomTestName = strconv.Itoa(rand.Int())
//Setup cluster
setupGlusterCluster()
//Clean up
defer cleanGlusterCluster()
//Do tests
//retVal := m.Run()
m.Run()
//os.Exit(retVal)
}
func setupPlugin() {
gluster.PluginAlias = "gluster-local-integration"
gluster.BaseDir = filepath.Join(volume.DefaultDockerRootDirectory, gluster.PluginAlias)
driver.CfgFolder = "/etc/docker-volumes/" + gluster.PluginAlias
log.Print(cmd("rm", os.Environ(), "-rf", driver.CfgFolder))
zerolog.SetGlobalLevel(zerolog.DebugLevel)
gluster.DaemonStart(nil, []string{})
time.Sleep(timeInterval)
//log.Print(cmd("docker", "plugin", "ls"))
log.Print(cmd("docker", os.Environ(), "info", "-f", "{{.Plugins.Volume}}"))
}
func setupManagedPlugin() {
//Build custom integration docker plugin
env := os.Environ()
env = append(env, fmt.Sprintf("PLUGIN_USER=%s", "testing"))
log.Print(cmd("make", env, "--directory=../../", "docker-plugin"))
log.Print(cmd("make", env, "--directory=../../", "docker-plugin-enable"))
}
func cleanManagedPlugin() {
log.Print(cmd("docker", os.Environ(), "plugin", "disable", "testing/plugin-gluster"))
log.Print(cmd("docker", os.Environ(), "plugin", "rm", "testing/plugin-gluster"))
}
func setupGlusterCluster() {
log.Print(dockerCompose("up", "-d"))
time.Sleep(timeInterval)
nodes := []string{"node-1", "node-2", "node-3"}
for _, n := range nodes {
time.Sleep(timeInterval)
log.Print(dockerCompose("exec", "-T", n, "mkdir", "-p", "/brick"))
}
time.Sleep(timeInterval)
IPs := getGlusterClusterContainersIPs()
for _, ip := range IPs[1:] {
time.Sleep(timeInterval)
log.Print(dockerCompose("exec", "-T", "node-1", "gluster", "peer", "probe", ip))
}
time.Sleep(timeInterval)
log.Print(dockerCompose("exec", "-T", "node-1", "gluster", "pool", "list"))
log.Print(dockerCompose("exec", "-T", "node-1", "gluster", "peer", "status"))
time.Sleep(timeInterval)
log.Print(dockerCompose("exec", "-T", "node-1", "gluster", "volume", "create", "test-replica", "replica", "3", IPs[0]+":/brick/replica", IPs[1]+":/brick/replica", IPs[2]+":/brick/replica"))
time.Sleep(timeInterval)
log.Print(dockerCompose("exec", "-T", "node-1", "gluster", "volume", "create", "test-distributed", IPs[0]+":/brick/distributed", IPs[1]+":/brick/distributed", IPs[2]+":/brick/distributed"))
time.Sleep(timeInterval)
log.Print(dockerCompose("exec", "-T", "node-1", "gluster", "volume", "start", "test-replica"))
time.Sleep(timeInterval)
log.Print(dockerCompose("exec", "-T", "node-1", "gluster", "volume", "start", "test-distributed"))
time.Sleep(timeInterval)
}
func cleanGlusterCluster() {
log.Print("Cleaning up")
log.Print(dockerCompose("down"))
//log.Print(cmd("docker", "volume", "rm", "-f", "glustercluster_brick-node-2", "glustercluster_brick-node-1", "glustercluster_brick-node-3", "glustercluster_state-node-1", "glustercluster_state-node-2", "glustercluster_state-node-3"))
// extra clean up log.Print(cmd("docker", "volume", "prune", "-f"))
// full extra clean up log.Print(cmd("docker", "system", "prune", "-af"))
}
func dockerCompose(arg ...string) (string, error) {
pwd := currentPWD()
args := append([]string{"-f", pwd + "/docker/gluster-cluster/docker-compose.yml", "--project-name", "test_inte_" + randomTestName}, arg...)
return cmd("docker-compose", os.Environ(), args...)
}
func cmd(cmd string, env []string, arg ...string) (string, error) {
fmt.Println("Executing: " + cmd + " " + strings.Join(arg, " "))
c := exec.Command(cmd, arg...)
c.Env = env
var out bytes.Buffer
c.Stdout = &out
c.Stderr = &out
err := c.Run()
return out.String(), err
}
//TODO cache result ?
func currentPWD() string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Dir(filename)
}
func getGlusterClusterContainers() []string {
list, _ := dockerCompose("ps", "-q")
l := strings.Split(list, "\n")
return l[:len(l)-1]
}
func getContainerIP(cid string) string {
ips, _ := cmd("docker", os.Environ(), "inspect", "--format", "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'", cid)
return strings.Trim(strings.Split(ips, "\n")[0], "'")
}
func getGlusterClusterContainersIPs() []string {
containers := getGlusterClusterContainers()
IPs := make([]string, len(containers))
for i := range containers {
IPs[i] = getContainerIP(containers[i])
}
return IPs
}
func TestIntegration(t *testing.T) {
//Startplugin with empty config
go setupPlugin()
time.Sleep(3 * timeInterval)
if !testing.Short() {
//Setup managed plugin
setupManagedPlugin()
//Clean up
defer cleanManagedPlugin()
}
IPs := getGlusterClusterContainersIPs()
testCases := []struct {
id string
name string
driver string
volume string
servers []string
hostname string
}{
{strconv.Itoa(rand.Int()), "replica", gluster.PluginAlias, "test-replica", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "distributed", gluster.PluginAlias, "test-distributed", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "replica-double-server", gluster.PluginAlias, "test-replica", IPs[:2], ""},
{strconv.Itoa(rand.Int()), "distributed-double-server", gluster.PluginAlias, "test-distributed", IPs[:2], ""},
{strconv.Itoa(rand.Int()), "replica-subdir", gluster.PluginAlias, "test-replica/subdir", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "distributed-subdir", gluster.PluginAlias, "test-distributed/subdir", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "managed-replica", "testing/plugin-gluster", "test-replica", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "managed-distributed", "testing/plugin-gluster", "test-distributed", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "managed-replica-double-server", "testing/plugin-gluster", "test-replica", IPs[:2], ""},
{strconv.Itoa(rand.Int()), "managed-distributed-double-server", "testing/plugin-gluster", "test-distributed", IPs[:2], ""},
{strconv.Itoa(rand.Int()), "managed-replica-subdir", gluster.PluginAlias, "test-replica/subdir", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "managed-distributed-subdir", gluster.PluginAlias, "test-distributed/subdir", IPs[:1], ""},
}
if testing.Short() { //Disable managed volume
testCases = []struct {
id string
name string
driver string
volume string
servers []string
hostname string
}{
{strconv.Itoa(rand.Int()), "replica", gluster.PluginAlias, "test-replica", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "distributed", gluster.PluginAlias, "test-distributed", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "replica-double-server", gluster.PluginAlias, "test-replica", IPs[:2], ""},
{strconv.Itoa(rand.Int()), "distributed-double-server", gluster.PluginAlias, "test-distributed", IPs[:2], ""},
{strconv.Itoa(rand.Int()), "replica-subdir", gluster.PluginAlias, "test-replica/subdir", IPs[:1], ""},
{strconv.Itoa(rand.Int()), "distributed-subdir", gluster.PluginAlias, "test-distributed/subdir", IPs[:1], ""},
}
}
for _, tc := range testCases {
t.Run("Create volume for "+tc.name, func(t *testing.T) {
log.Print(cmd("docker", os.Environ(), "volume", "create", "--driver", tc.driver, "--opt", "voluri=\""+strings.Join(tc.servers, ",")+":"+tc.volume+"\"", tc.id))
time.Sleep(timeInterval)
})
time.Sleep(3 * timeInterval)
//TODO test volume exist
}
log.Print(cmd("docker", os.Environ(), "volume", "ls"))
for i, tc := range testCases {
t.Run("Test volume "+tc.name, func(t *testing.T) {
out, err := cmd("docker", os.Environ(), "run", "--rm", "-t", "-v", tc.id+":/mnt", "alpine", "/bin/ls", "/mnt")
log.Print(out)
if err != nil {
t.Errorf("Failed to list mounted volume : %v", err)
}
if !strings.Contains(tc.name, "double") && !strings.Contains(tc.name, "managed") {
out, err = cmd("docker", os.Environ(), "run", "--rm", "-t", "-v", tc.id+":/mnt", "alpine", "/bin/cp", "/etc/hostname", "/mnt/container")
log.Print(out)
if err != nil {
t.Errorf("Failed to write inside mounted volume : %v", err)
}
out, err = cmd("docker", os.Environ(), "run", "--rm", "-t", "-v", tc.id+":/mnt", "alpine", "/bin/mkdir", "/mnt/subdir")
log.Print(out)
if err != nil {
t.Errorf("Failed to create dir inside mounted volume : %v", err)
}
out, err = cmd("docker", os.Environ(), "run", "--rm", "-t", "-v", tc.id+":/mnt", "alpine", "/bin/cp", "/etc/hostname", "/mnt/subdir/container")
log.Print(out)
if err != nil {
t.Errorf("Failed to write inside mounted volume : %v", err)
}
}
testCases[i].hostname, err = cmd("docker", os.Environ(), "run", "--rm", "-t", "-v", tc.id+":/mnt", "alpine", "/bin/cat", "/mnt/container")
log.Print(out)
if err != nil {
t.Errorf("Failed to read from mounted volume : %v", err)
}
time.Sleep(3 * timeInterval)
//TODO check content is same
})
}
for _, tc := range testCases {
for _, td := range testCases {
if tc.volume == td.volume {
t.Run(fmt.Sprintf("Test same volume data between %s and %s", tc.name, td.name), func(t *testing.T) {
if tc.hostname != td.hostname {
t.Errorf("Content inside gluster %s volume in not the same : %s != %s", tc.volume, tc.hostname, td.hostname)
}
})
}
}
}
//TODO check persistence
for _, tc := range testCases {
out, err := cmd("docker", os.Environ(), "volume", "rm", tc.id)
if err != nil {
t.Errorf("Failed to remove mounted volume %s (%s) : %v", tc.name, tc.id, err)
}
if !strings.Contains(out, tc.id) { //TODO should be only "vol\n"
t.Errorf("Failed to remove mounted volume %s (%s)", tc.name, tc.id)
}
out, err = cmd("docker", os.Environ(), "volume", "ls", "-q")
if err != nil {
t.Errorf("Failed to list volume : %v", err)
}
if strings.Contains(out, tc.id) { //TODO should be "vol\n" to limit confussion ith other volume existing or generate name
t.Errorf("Failed to remove volume %s (%s) from volume list", tc.name, tc.id)
}
time.Sleep(3 * timeInterval)
}
}