Skip to content

Commit f65447a

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 installing rock requirements with tarantoolctl. 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. Now it is possible to run a test with plain `go test`. Queue tests changes may be broken because it is impossible to verify before #115 is fixed. Closes #107
1 parent d9c4090 commit f65447a

15 files changed

+389
-76
lines changed

.github/workflows/reusable_testing.yml

+1-14
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,22 @@ jobs:
4040

4141
- name: Run base tests
4242
run: |
43-
mkdir snap xlog
44-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
4543
go clean -testcache && go test -v
46-
kill $TNT_PID
4744
4845
# TODO(ylobankov): Uncomment this when tarantool/go-tarantool#115 is resolved.
4946
# - name: Run queue tests
5047
# working-directory: ./queue
5148
# run: |
52-
# mkdir snap xlog
53-
# tarantoolctl rocks install queue 1.1.0
54-
# TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
49+
# ./deps.sh
5550
# go clean -testcache && go test -v
56-
# kill $TNT_PID
5751

5852
- name: Run uuid tests
5953
working-directory: ./uuid
6054
run: |
61-
mkdir snap xlog
62-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
6355
go clean -testcache && go test -v
64-
kill $TNT_PID
6556
if: ${{ !startsWith(env.TNT_VERSION, 'Tarantool 1.10') }}
6657

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

.github/workflows/testing.yml

+1-14
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,22 @@ jobs:
4141

4242
- name: Run base tests
4343
run: |
44-
mkdir snap xlog
45-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
4644
go clean -testcache && go test -v
47-
kill $TNT_PID
4845
4946
# TODO(ylobankov): Uncomment this when tarantool/go-tarantool#115 is resolved.
5047
# - name: Run queue tests
5148
# working-directory: ./queue
5249
# run: |
53-
# mkdir snap xlog
54-
# tarantoolctl rocks install queue 1.1.0
55-
# TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
50+
# ./deps.sh
5651
# go clean -testcache && go test -v
57-
# kill $TNT_PID
5852

5953
- name: Run uuid tests
6054
working-directory: ./uuid
6155
run: |
62-
mkdir snap xlog
63-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
6456
go clean -testcache && go test -v
65-
kill $TNT_PID
6657
if: ${{ matrix.tarantool != 1.10 }}
6758

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

.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+
}

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.Panic("Failed to prepare test tarantool: ", 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.Panic("Failed to prepare test tarantool: ", 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()
@@ -27,3 +27,8 @@ box.schema.user.grant('test', 'read,write', 'space', '_queue_consumers')
2727
box.schema.user.grant('test', 'read,write', 'space', '_priv')
2828
box.schema.user.grant('test', 'read,write', 'space', '_queue_taken')
2929
end)
30+
31+
-- Set listen only when every other thing is configured.
32+
box.cfg{
33+
listen = os.getenv("TEST_TNT_LISTEN"),
34+
}

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.Panic("Failed to prepare test tarantool: ", 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.Panic("Failed to prepare test tarantool: ", 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)