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
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
  • Loading branch information
DifferentialOrange committed Feb 3, 2022
1 parent c84ec85 commit 91c4e2c
Show file tree
Hide file tree
Showing 16 changed files with 488 additions and 134 deletions.
36 changes: 4 additions & 32 deletions .github/workflows/reusable_testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,8 @@ jobs:
with:
go-version: 1.13

- 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
- 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: Install test dependencies
run: ./deps.sh

- 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
- name: Run tests
run: go clean -testcache && go test ./... -v -p 1
37 changes: 4 additions & 33 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,9 @@ jobs:
with:
go-version: 1.13

- 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
- name: Run queue tests
- name: Install test dependencies
working-directory: ./queue
run: |
rm -rf snap
rm -rf xlog
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 }}
run: ./deps.sh

- 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
- name: Run tests
run: go clean -testcache && go test ./... -v -p 1
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("TEST_TNT_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("TEST_TNT_LISTEN"),
}
9 changes: 9 additions & 0 deletions deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
# Call this script to install test dependencies for all subpackages.

set -eux

# We should install rock dependencies to corresponding directory.
# If you run ./queue/deps.sh instead of cd ./queue; ./deps.sh,
# rock will be installed to current directory instead of ./queue.
cd ./queue; ./deps.sh; cd ..
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("TEST_TNT_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("TEST_TNT_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.

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

import (
"log"
"os"
"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 +207,54 @@ func TestRefresh(t *testing.T) {
t.Error("Expect to get data after reconnect")
}
}

// runTestMain is a body of TestMain function
// (see https://pkg.go.dev/testing#hdr-Main).
// Using defer + os.Exit is not works so TestMain body
// is a separate function, see
// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
func runTestMain(m *testing.M) int {
initScript := "config.lua"
waitStart := 100 * time.Millisecond
var connectRetry uint = 3
retryTimeout := 500 * time.Millisecond

inst1, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: initScript,
Listen: server1,
WorkDir: "work_dir1",
User: connOpts.User,
Pass: connOpts.Pass,
WaitStart: waitStart,
ConnectRetry: connectRetry,
RetryTimeout: retryTimeout,
})
defer test_helpers.StopTarantoolWithCleanup(inst1)

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

inst2, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: initScript,
Listen: server2,
WorkDir: "work_dir2",
User: connOpts.User,
Pass: connOpts.Pass,
WaitStart: waitStart,
ConnectRetry: connectRetry,
RetryTimeout: retryTimeout,
})
defer test_helpers.StopTarantoolWithCleanup(inst2)

if err != nil {
log.Fatalf("Failed to prepare test tarantool: %s", 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("TEST_TNT_WORK_DIR"),
}

box.once("init", function()
Expand Down Expand Up @@ -45,3 +45,8 @@ if box.space._func_index ~= nil then
box.schema.user.grant('test', 'read', 'space', '_func_index')
end
end)

-- Set listen only when every other thing is configured.
box.cfg{
listen = os.getenv("TEST_TNT_LISTEN"),
}
6 changes: 6 additions & 0 deletions queue/deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# Call this script to install test dependencies.

set -eux

tarantoolctl rocks install queue 1.1.0
33 changes: 33 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,33 @@ func TestUtube_Put(t *testing.T) {
t.Fatalf("Blocking time is less than expected: actual = %.2fs, expected = 1s", end.Sub(start).Seconds())
}
}

// runTestMain is a body of TestMain function
// (see https://pkg.go.dev/testing#hdr-Main).
// Using defer + os.Exit is not works so TestMain body
// is a separate function, see
// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
func runTestMain(m *testing.M) int {
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: "config.lua",
Listen: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
ConnectRetry: 3,
RetryTimeout: 500 * time.Millisecond,
})
defer test_helpers.StopTarantoolWithCleanup(inst)

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

return m.Run()
}

func TestMain(m *testing.M) {
code := runTestMain(m)
os.Exit(code)
}
33 changes: 33 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,33 @@ func TestComplexStructs(t *testing.T) {
return
}
}

// runTestMain is a body of TestMain function
// (see https://pkg.go.dev/testing#hdr-Main).
// Using defer + os.Exit is not works so TestMain body
// is a separate function, see
// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls
func runTestMain(m *testing.M) int {
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: "config.lua",
Listen: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
ConnectRetry: 3,
RetryTimeout: 500 * time.Millisecond,
})
defer test_helpers.StopTarantoolWithCleanup(inst)

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

return m.Run()
}

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

0 comments on commit 91c4e2c

Please sign in to comment.