Skip to content

Commit db28eb9

Browse files
test: handle everything with go test
Before this patch, it was required to set up test tarantool processes manually (and also handle their dependencies, like making working dir). You can see an example in CI scripts. This patch introduces go helpers for starting a tarantool process and validating Tarantool version. Helpers are based on `os/exec` calls. Retries to connect test tarantool instance handled explicitly, see #136. Setup scripts are reworked to use environment variables to configure `box.cfg`. Listen port is set in the end of script so it is possible to connect only if every other thing was set up already. Every test is reworked to start a tarantool process (or processes) in TestMain before test run. To run tests, install all dependencies with running root folder `deps.sh` and then run `go clean -testcache && go test ./... -v -p 1`. Flag `-p 1` means no parallel runs. If you run tests without this flag, several test tarantool instances will try to bind the same port, resulting in run fail. Closes #107
1 parent c84ec85 commit db28eb9

16 files changed

+497
-134
lines changed

.github/workflows/reusable_testing.yml

+4-32
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,8 @@ jobs:
3838
with:
3939
go-version: 1.13
4040

41-
- name: Run base tests
42-
run: |
43-
mkdir snap xlog
44-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
45-
go clean -testcache && go test -v
46-
kill $TNT_PID
47-
48-
- name: Run queue tests
49-
working-directory: ./queue
50-
run: |
51-
mkdir snap xlog
52-
tarantoolctl rocks install queue 1.1.0
53-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
54-
go clean -testcache && go test -v
55-
kill $TNT_PID
41+
- name: Install test dependencies
42+
run: ./deps.sh
5643

57-
- name: Run uuid tests
58-
working-directory: ./uuid
59-
run: |
60-
mkdir snap xlog
61-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
62-
go clean -testcache && go test -v
63-
kill $TNT_PID
64-
if: ${{ !startsWith(env.TNT_VERSION, 'Tarantool 1.10') }}
65-
66-
- name: Run multi tests
67-
working-directory: ./multi
68-
run: |
69-
mkdir -p m1/{snap,xlog} m2/{snap,xlog}
70-
TNT_PID_1=$(tarantool ./config_m1.lua > tarantool_m1.log 2>&1 & echo $!)
71-
TNT_PID_2=$(tarantool ./config_m2.lua > tarantool_m2.log 2>&1 & echo $!)
72-
go clean -testcache && go test -v
73-
kill $TNT_PID_1 $TNT_PID_2
44+
- name: Run tests
45+
run: go clean -testcache && go test ./... -v -p 1

.github/workflows/testing.yml

+4-33
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,9 @@ jobs:
3939
with:
4040
go-version: 1.13
4141

42-
- name: Run base tests
43-
run: |
44-
mkdir snap xlog
45-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
46-
go clean -testcache && go test -v
47-
kill $TNT_PID
48-
49-
- name: Run queue tests
42+
- name: Install test dependencies
5043
working-directory: ./queue
51-
run: |
52-
rm -rf snap
53-
rm -rf xlog
54-
mkdir snap xlog
55-
tarantoolctl rocks install queue 1.1.0
56-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
57-
go clean -testcache && go test -v
58-
kill $TNT_PID
59-
60-
- name: Run uuid tests
61-
working-directory: ./uuid
62-
run: |
63-
mkdir snap xlog
64-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
65-
go clean -testcache && go test -v
66-
kill $TNT_PID
67-
if: ${{ matrix.tarantool != 1.10 }}
44+
run: ./deps.sh
6845

69-
- name: Run multi tests
70-
working-directory: ./multi
71-
run: |
72-
mkdir -p m1/{snap,xlog} m2/{snap,xlog}
73-
TNT_PID_1=$(tarantool ./config_m1.lua > tarantool_m1.log 2>&1 & echo $!)
74-
TNT_PID_2=$(tarantool ./config_m2.lua > tarantool_m2.log 2>&1 & echo $!)
75-
go clean -testcache && go test -v
76-
kill $TNT_PID_1 $TNT_PID_2
46+
- name: Run tests
47+
run: go clean -testcache && go test ./... -v -p 1

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
*.DS_Store
22
*.swp
33
.idea/
4-
snap
5-
xlog
4+
work_dir*
5+
.rocks

config.lua

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
-- Do not set listen for now so connector won't be
2+
-- able to send requests until everything is configured.
13
box.cfg{
2-
listen = 3013,
3-
wal_dir='xlog',
4-
snap_dir='snap',
4+
work_dir = os.getenv("TEST_TNT_WORK_DIR"),
55
}
66

77
box.once("init", function()
@@ -56,7 +56,10 @@ function simple_incr(a)
5656
end
5757

5858
box.space.test:truncate()
59-
local console = require 'console'
60-
console.listen '0.0.0.0:33015'
6159

6260
--box.schema.user.revoke('guest', 'read,write,execute', 'universe')
61+
62+
-- Set listen only when every other thing is configured.
63+
box.cfg{
64+
listen = os.getenv("TEST_TNT_LISTEN"),
65+
}

deps.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
# Call this script to install test dependencies for all subpackages.
3+
4+
set -e
5+
6+
# We should install rock dependencies to corresponding directory.
7+
# If you run ./queue/deps.sh instead of cd ./queue; ./deps.sh,
8+
# rock will be installed to current directory instead of ./queue.
9+
cd ./queue; ./deps.sh; cd ..

multi/config.lua

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local nodes_load = require("config_load_nodes")
2+
3+
-- Do not set listen for now so connector won't be
4+
-- able to send requests until everything is configured.
5+
box.cfg{
6+
work_dir = os.getenv("TEST_TNT_WORK_DIR"),
7+
}
8+
9+
get_cluster_nodes = nodes_load.get_cluster_nodes
10+
11+
box.once("init", function()
12+
box.schema.user.create('test', { password = 'test' })
13+
box.schema.user.grant('test', 'read,write,execute', 'universe')
14+
end)
15+
16+
-- Set listen only when every other thing is configured.
17+
box.cfg{
18+
listen = os.getenv("TEST_TNT_LISTEN"),
19+
}

multi/config_m1.lua

-14
This file was deleted.

multi/config_m2.lua

-14
This file was deleted.

multi/multi_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package multi
22

33
import (
4+
"log"
5+
"os"
46
"testing"
57
"time"
68

79
"github.com/tarantool/go-tarantool"
10+
"github.com/tarantool/go-tarantool/test_helpers"
811
)
912

1013
var server1 = "127.0.0.1:3013"
@@ -204,3 +207,57 @@ func TestRefresh(t *testing.T) {
204207
t.Error("Expect to get data after reconnect")
205208
}
206209
}
210+
211+
// runTestMain is a body of TestMain function
212+
// (see https://pkg.go.dev/testing#hdr-Main).
213+
// Using defer + os.Exit is not works so TestMain body
214+
// is a separate function, see
215+
// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
216+
func runTestMain(m *testing.M) int {
217+
var err error
218+
var inst1, inst2 test_helpers.TarantoolInstance
219+
220+
initScript := "config.lua"
221+
waitStart := 100 * time.Millisecond
222+
var connectRetry uint = 3
223+
retryTimeout := 200 * time.Millisecond
224+
225+
inst1, err = test_helpers.StartTarantool(test_helpers.StartOpts{
226+
InitScript: initScript,
227+
Listen: server1,
228+
WorkDir: "work_dir1",
229+
User: connOpts.User,
230+
Pass: connOpts.Pass,
231+
WaitStart: waitStart,
232+
ConnectRetry: connectRetry,
233+
RetryTimeout: retryTimeout,
234+
})
235+
defer test_helpers.StopTarantoolWithCleanup(inst1)
236+
237+
if err != nil {
238+
log.Fatalf("Failed to prepare test tarantool: %s", err)
239+
}
240+
241+
inst2, err = test_helpers.StartTarantool(test_helpers.StartOpts{
242+
InitScript: initScript,
243+
Listen: server2,
244+
WorkDir: "work_dir2",
245+
User: connOpts.User,
246+
Pass: connOpts.Pass,
247+
WaitStart: waitStart,
248+
ConnectRetry: connectRetry,
249+
RetryTimeout: retryTimeout,
250+
})
251+
defer test_helpers.StopTarantoolWithCleanup(inst2)
252+
253+
if err != nil {
254+
log.Fatalf("Failed to prepare test tarantool: %s", err)
255+
}
256+
257+
return m.Run()
258+
}
259+
260+
func TestMain(m *testing.M) {
261+
code := runTestMain(m)
262+
os.Exit(code)
263+
}

queue/config.lua

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
queue = require 'queue'
1+
queue = require('queue')
22

3+
-- Do not set listen for now so connector won't be
4+
-- able to send requests until everything is configured.
35
box.cfg{
4-
listen = 3013,
5-
wal_dir='xlog',
6-
snap_dir='snap',
6+
work_dir = os.getenv("TEST_TNT_WORK_DIR"),
77
}
88

99
box.once("init", function()
@@ -45,3 +45,8 @@ if box.space._func_index ~= nil then
4545
box.schema.user.grant('test', 'read', 'space', '_func_index')
4646
end
4747
end)
48+
49+
-- Set listen only when every other thing is configured.
50+
box.cfg{
51+
listen = os.getenv("TEST_TNT_LISTEN"),
52+
}

queue/deps.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
# Call this script to install test dependencies.
3+
4+
set -e
5+
6+
tarantoolctl rocks install queue 1.1.0

queue/queue_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package queue_test
22

33
import (
44
"fmt"
5+
"log"
56
"math"
7+
"os"
68
"testing"
79
"time"
810

911
. "github.com/tarantool/go-tarantool"
1012
"github.com/tarantool/go-tarantool/queue"
13+
"github.com/tarantool/go-tarantool/test_helpers"
1114
"gopkg.in/vmihailenco/msgpack.v2"
1215
)
1316

@@ -818,3 +821,33 @@ func TestUtube_Put(t *testing.T) {
818821
t.Fatalf("Blocking time is less than expected: actual = %.2fs, expected = 1s", end.Sub(start).Seconds())
819822
}
820823
}
824+
825+
// runTestMain is a body of TestMain function
826+
// (see https://pkg.go.dev/testing#hdr-Main).
827+
// Using defer + os.Exit is not works so TestMain body
828+
// is a separate function, see
829+
// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
830+
func runTestMain(m *testing.M) int {
831+
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
832+
InitScript: "config.lua",
833+
Listen: server,
834+
WorkDir: "work_dir",
835+
User: opts.User,
836+
Pass: opts.Pass,
837+
WaitStart: 100 * time.Millisecond,
838+
ConnectRetry: 3,
839+
RetryTimeout: 200 * time.Millisecond,
840+
})
841+
defer test_helpers.StopTarantoolWithCleanup(inst)
842+
843+
if err != nil {
844+
log.Fatalf("Failed to prepare test tarantool: %s", err)
845+
}
846+
847+
return m.Run()
848+
}
849+
850+
func TestMain(m *testing.M) {
851+
code := runTestMain(m)
852+
os.Exit(code)
853+
}

tarantool_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package tarantool_test
22

33
import (
44
"fmt"
5+
"log"
6+
"os"
57
"strings"
68
"sync"
79
"testing"
810
"time"
911

1012
. "github.com/tarantool/go-tarantool"
13+
"github.com/tarantool/go-tarantool/test_helpers"
1114
"gopkg.in/vmihailenco/msgpack.v2"
1215
)
1316

@@ -1005,3 +1008,33 @@ func TestComplexStructs(t *testing.T) {
10051008
return
10061009
}
10071010
}
1011+
1012+
// runTestMain is a body of TestMain function
1013+
// (see https://pkg.go.dev/testing#hdr-Main).
1014+
// Using defer + os.Exit is not works so TestMain body
1015+
// is a separate function, see
1016+
// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
1017+
func runTestMain(m *testing.M) int {
1018+
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
1019+
InitScript: "config.lua",
1020+
Listen: server,
1021+
WorkDir: "work_dir",
1022+
User: opts.User,
1023+
Pass: opts.Pass,
1024+
WaitStart: 100 * time.Millisecond,
1025+
ConnectRetry: 3,
1026+
RetryTimeout: 200 * time.Millisecond,
1027+
})
1028+
defer test_helpers.StopTarantoolWithCleanup(inst)
1029+
1030+
if err != nil {
1031+
log.Fatalf("Failed to prepare test tarantool: %s", err)
1032+
}
1033+
1034+
return m.Run()
1035+
}
1036+
1037+
func TestMain(m *testing.M) {
1038+
code := runTestMain(m)
1039+
os.Exit(code)
1040+
}

0 commit comments

Comments
 (0)