Skip to content

Commit

Permalink
test: handle everything with go test
Browse files Browse the repository at this point in the history
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
  • Loading branch information
DifferentialOrange committed Jan 17, 2022
1 parent 8ad21b2 commit 510da5f
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 76 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/reusable_testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,21 @@ jobs:

- name: Run base tests
run: |
mkdir snap xlog
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
go clean -testcache && go test -v
kill $TNT_PID
# TODO(ylobankov): Uncomment this when tarantool/go-tarantool#115 is resolved.
# - name: Run queue tests
# working-directory: ./queue
# run: |
# mkdir snap xlog
# tarantoolctl rocks install queue 1.1.0
# TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
# go clean -testcache && go test -v
# kill $TNT_PID

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

- name: Run multi tests
working-directory: ./multi
run: |
mkdir -p m1/{snap,xlog} m2/{snap,xlog}
TNT_PID_1=$(tarantool ./config_m1.lua > tarantool_m1.log 2>&1 & echo $!)
TNT_PID_2=$(tarantool ./config_m2.lua > tarantool_m2.log 2>&1 & echo $!)
go clean -testcache && go test -v
kill $TNT_PID_1 $TNT_PID_2
14 changes: 0 additions & 14 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,21 @@ jobs:

- name: Run base tests
run: |
mkdir snap xlog
TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
go clean -testcache && go test -v
kill $TNT_PID
# TODO(ylobankov): Uncomment this when tarantool/go-tarantool#115 is resolved.
# - name: Run queue tests
# working-directory: ./queue
# run: |
# mkdir snap xlog
# tarantoolctl rocks install queue 1.1.0
# TNT_PID=$(tarantool ./config.lua > tarantool.log 2>&1 & echo $!)
# go clean -testcache && go test -v
# kill $TNT_PID

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

- name: Run multi tests
working-directory: ./multi
run: |
mkdir -p m1/{snap,xlog} m2/{snap,xlog}
TNT_PID_1=$(tarantool ./config_m1.lua > tarantool_m1.log 2>&1 & echo $!)
TNT_PID_2=$(tarantool ./config_m2.lua > tarantool_m2.log 2>&1 & echo $!)
go clean -testcache && go test -v
kill $TNT_PID_1 $TNT_PID_2
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*.DS_Store
*.swp
.idea/
snap
xlog
work_dir*
.rocks
13 changes: 8 additions & 5 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Do not set listen for now so connector won't be
-- able to send requests until everything is configured.
box.cfg{
listen = 3013,
wal_dir='xlog',
snap_dir='snap',
work_dir = os.getenv("TARANTOOL_WORK_DIR"),
}

box.once("init", function()
Expand Down Expand Up @@ -56,7 +56,10 @@ function simple_incr(a)
end

box.space.test:truncate()
local console = require 'console'
console.listen '0.0.0.0:33015'

--box.schema.user.revoke('guest', 'read,write,execute', 'universe')

-- Set listen only when every other thing is configured.
box.cfg{
listen = os.getenv("TARANTOOL_LISTEN"),
}
19 changes: 19 additions & 0 deletions multi/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local nodes_load = require("config_load_nodes")

-- Do not set listen for now so connector won't be
-- able to send requests until everything is configured.
box.cfg{
work_dir = os.getenv("TARANTOOL_WORK_DIR"),
}

get_cluster_nodes = nodes_load.get_cluster_nodes

box.once("init", function()
box.schema.user.create('test', { password = 'test' })
box.schema.user.grant('test', 'read,write,execute', 'universe')
end)

-- Set listen only when every other thing is configured.
box.cfg{
listen = os.getenv("TARANTOOL_LISTEN"),
}
14 changes: 0 additions & 14 deletions multi/config_m1.lua

This file was deleted.

14 changes: 0 additions & 14 deletions multi/config_m2.lua

This file was deleted.

50 changes: 50 additions & 0 deletions multi/multi_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package multi

import (
"log"
"os"
"os/exec"
"testing"
"time"

"github.com/tarantool/go-tarantool"
"github.com/tarantool/go-tarantool/test_helpers"
)

var server1 = "127.0.0.1:3013"
Expand Down Expand Up @@ -204,3 +208,49 @@ func TestRefresh(t *testing.T) {
t.Error("Expect to get data after reconnect")
}
}

// See https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
// on issue with defer + os.Exit.
func runTestMain(m *testing.M) int {
var err error
var cmd1, cmd2 *exec.Cmd

cmd1, err = test_helpers.StartTarantool(test_helpers.StartOpts{
Command: "config.lua",
Server: server1,
WorkDir: "work_dir1",
User: connOpts.User,
Pass: connOpts.Pass,
WaitStart: 100 * time.Millisecond,
ConnectRetry: 3,
RetryTimeout: 200 * time.Millisecond,
})
defer test_helpers.StopTarantool(cmd1)

if err != nil {
log.Panic("Failed to prepare test tarantool: ", err)
}

cmd2, err = test_helpers.StartTarantool(test_helpers.StartOpts{
Command: "config.lua",
Server: server2,
WorkDir: "work_dir2",
User: connOpts.User,
Pass: connOpts.Pass,
WaitStart: 100 * time.Millisecond,
ConnectRetry: 3,
RetryTimeout: 200 * time.Millisecond,
})
defer test_helpers.StopTarantool(cmd2)

if err != nil {
log.Panic("Failed to prepare test tarantool: ", err)
}

return m.Run()
}

func TestMain(m *testing.M) {
code := runTestMain(m)
os.Exit(code)
}
13 changes: 9 additions & 4 deletions queue/config.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
queue = require 'queue'
queue = require('queue')

-- Do not set listen for now so connector won't be
-- able to send requests until everything is configured.
box.cfg{
listen = 3013,
wal_dir='xlog',
snap_dir='snap',
work_dir = os.getenv("TARANTOOL_WORK_DIR"),
}

box.once("init", function()
Expand All @@ -27,3 +27,8 @@ box.schema.user.grant('test', 'read,write', 'space', '_queue_consumers')
box.schema.user.grant('test', 'read,write', 'space', '_priv')
box.schema.user.grant('test', 'read,write', 'space', '_queue_taken')
end)

-- Set listen only when every other thing is configured.
box.cfg{
listen = os.getenv("TARANTOOL_LISTEN"),
}
38 changes: 38 additions & 0 deletions queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package queue_test

import (
"fmt"
"log"
"math"
"os"
"testing"
"time"

. "github.com/tarantool/go-tarantool"
"github.com/tarantool/go-tarantool/queue"
"github.com/tarantool/go-tarantool/test_helpers"
"gopkg.in/vmihailenco/msgpack.v2"
)

Expand Down Expand Up @@ -818,3 +821,38 @@ func TestUtube_Put(t *testing.T) {
t.Fatalf("Blocking time is less than expected: actual = %.2fs, expected = 1s", end.Sub(start).Seconds())
}
}

// See https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
// on issue with defer + os.Exit.
func runTestMain(m *testing.M) int {
if err := os.RemoveAll(".rocks"); err != nil {
log.Panic(err)
}

if err := test_helpers.InstallRock("queue", "1.1.0"); err != nil {
log.Panic(err)
}

cmd, err := test_helpers.StartTarantool(test_helpers.StartOpts{
Command: "config.lua",
Server: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
ConnectRetry: 3,
RetryTimeout: 200 * time.Millisecond,
})
defer test_helpers.StopTarantool(cmd)

if err != nil {
log.Panic("Failed to prepare test tarantool: ", err)
}

return m.Run()
}

func TestMain(m *testing.M) {
code := runTestMain(m)
os.Exit(code)
}
30 changes: 30 additions & 0 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package tarantool_test

import (
"fmt"
"log"
"os"
"strings"
"sync"
"testing"
"time"

. "github.com/tarantool/go-tarantool"
"github.com/tarantool/go-tarantool/test_helpers"
"gopkg.in/vmihailenco/msgpack.v2"
)

Expand Down Expand Up @@ -1005,3 +1008,30 @@ func TestComplexStructs(t *testing.T) {
return
}
}

// See https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
// on issue with defer + os.Exit.
func runTestMain(m *testing.M) int {
cmd, err := test_helpers.StartTarantool(test_helpers.StartOpts{
Command: "config.lua",
Server: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
ConnectRetry: 3,
RetryTimeout: 200 * time.Millisecond,
})
defer test_helpers.StopTarantool(cmd)

if err != nil {
log.Panic("Failed to prepare test tarantool: ", err)
}

return m.Run()
}

func TestMain(m *testing.M) {
code := runTestMain(m)
os.Exit(code)
}
Loading

0 comments on commit 510da5f

Please sign in to comment.