Skip to content

Commit 45def97

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 56cb3ef commit 45def97

14 files changed

+326
-76
lines changed

.github/workflows/reusable_testing.yml

-14
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,21 @@ 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 $!)
5549
# go clean -testcache && go test -v
56-
# kill $TNT_PID
5750

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

6757
- name: Run multi tests
6858
working-directory: ./multi
6959
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 $!)
7360
go clean -testcache && go test -v
74-
kill $TNT_PID_1 $TNT_PID_2

.github/workflows/testing.yml

-14
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,21 @@ 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 $!)
5650
# go clean -testcache && go test -v
57-
# kill $TNT_PID
5851

5952
- name: Run uuid tests
6053
working-directory: ./uuid
6154
run: |
62-
mkdir snap xlog
63-
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
6455
go clean -testcache && go test -v
65-
kill $TNT_PID
6656
if: ${{ matrix.tarantool != 1.10 }}
6757

6858
- name: Run multi tests
6959
working-directory: ./multi
7060
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 $!)
7461
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("TARANTOOL_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("TARANTOOL_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("TARANTOOL_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("TARANTOOL_LISTEN"),
19+
}

multi/config_m1.lua

-14
This file was deleted.

multi/config_m2.lua

-14
This file was deleted.

multi/multi_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package multi
22

33
import (
4+
"log"
5+
"os"
6+
"os/exec"
47
"testing"
58
"time"
69

710
"github.com/tarantool/go-tarantool"
11+
"github.com/tarantool/go-tarantool/test_helpers"
812
)
913

1014
var server1 = "127.0.0.1:3013"
@@ -204,3 +208,49 @@ func TestRefresh(t *testing.T) {
204208
t.Error("Expect to get data after reconnect")
205209
}
206210
}
211+
212+
// See https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
213+
// on issue with defer + os.Exit.
214+
func runTestMain(m *testing.M) int {
215+
var err error
216+
var cmd1, cmd2 *exec.Cmd
217+
218+
cmd1, err = test_helpers.StartTarantool(test_helpers.StartOpts{
219+
Command: "config.lua",
220+
Server: server1,
221+
WorkDir: "work_dir1",
222+
User: connOpts.User,
223+
Pass: connOpts.Pass,
224+
WaitStart: 100 * time.Millisecond,
225+
ConnectRetry: 3,
226+
RetryTimeout: 200 * time.Millisecond,
227+
})
228+
defer test_helpers.StopTarantool(cmd1)
229+
230+
if err != nil {
231+
log.Panic("Failed to prepare test tarantool: ", err)
232+
}
233+
234+
cmd2, err = test_helpers.StartTarantool(test_helpers.StartOpts{
235+
Command: "config.lua",
236+
Server: server2,
237+
WorkDir: "work_dir2",
238+
User: connOpts.User,
239+
Pass: connOpts.Pass,
240+
WaitStart: 100 * time.Millisecond,
241+
ConnectRetry: 3,
242+
RetryTimeout: 200 * time.Millisecond,
243+
})
244+
defer test_helpers.StopTarantool(cmd2)
245+
246+
if err != nil {
247+
log.Panic("Failed to prepare test tarantool: ", err)
248+
}
249+
250+
return m.Run()
251+
}
252+
253+
func TestMain(m *testing.M) {
254+
code := runTestMain(m)
255+
os.Exit(code)
256+
}

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("TARANTOOL_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("TARANTOOL_LISTEN"),
34+
}

queue/queue_test.go

+38
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,38 @@ 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+
// See https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
826+
// on issue with defer + os.Exit.
827+
func runTestMain(m *testing.M) int {
828+
if err := os.RemoveAll(".rocks"); err != nil {
829+
log.Panic(err)
830+
}
831+
832+
if err := test_helpers.InstallRock("queue", "1.1.0"); err != nil {
833+
log.Panic(err)
834+
}
835+
836+
cmd, err := test_helpers.StartTarantool(test_helpers.StartOpts{
837+
Command: "config.lua",
838+
Server: server,
839+
WorkDir: "work_dir",
840+
User: opts.User,
841+
Pass: opts.Pass,
842+
WaitStart: 100 * time.Millisecond,
843+
ConnectRetry: 3,
844+
RetryTimeout: 200 * time.Millisecond,
845+
})
846+
defer test_helpers.StopTarantool(cmd)
847+
848+
if err != nil {
849+
log.Panic("Failed to prepare test tarantool: ", err)
850+
}
851+
852+
return m.Run()
853+
}
854+
855+
func TestMain(m *testing.M) {
856+
code := runTestMain(m)
857+
os.Exit(code)
858+
}

tarantool_test.go

+30
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,30 @@ func TestComplexStructs(t *testing.T) {
10051008
return
10061009
}
10071010
}
1011+
1012+
// See https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
1013+
// on issue with defer + os.Exit.
1014+
func runTestMain(m *testing.M) int {
1015+
cmd, err := test_helpers.StartTarantool(test_helpers.StartOpts{
1016+
Command: "config.lua",
1017+
Server: server,
1018+
WorkDir: "work_dir",
1019+
User: opts.User,
1020+
Pass: opts.Pass,
1021+
WaitStart: 100 * time.Millisecond,
1022+
ConnectRetry: 3,
1023+
RetryTimeout: 200 * time.Millisecond,
1024+
})
1025+
defer test_helpers.StopTarantool(cmd)
1026+
1027+
if err != nil {
1028+
log.Panic("Failed to prepare test tarantool: ", err)
1029+
}
1030+
1031+
return m.Run()
1032+
}
1033+
1034+
func TestMain(m *testing.M) {
1035+
code := runTestMain(m)
1036+
os.Exit(code)
1037+
}

0 commit comments

Comments
 (0)