|
| 1 | +local fio = require('fio') |
| 2 | + |
| 3 | +local t = require('luatest') |
| 4 | +local g_memtx = t.group('truncate_memtx') |
| 5 | +local g_vinyl = t.group('truncate_vinyl') |
| 6 | + |
| 7 | +local helpers = require('test.helper') |
| 8 | + |
| 9 | +math.randomseed(os.time()) |
| 10 | + |
| 11 | +local function before_all(g, engine) |
| 12 | + g.cluster = helpers.Cluster:new({ |
| 13 | + datadir = fio.tempdir(), |
| 14 | + server_command = helpers.entrypoint('srv_select'), |
| 15 | + use_vshard = true, |
| 16 | + replicasets = { |
| 17 | + { |
| 18 | + uuid = helpers.uuid('a'), |
| 19 | + alias = 'router', |
| 20 | + roles = { 'customers-router' }, |
| 21 | + servers = { |
| 22 | + { instance_uuid = helpers.uuid('a', 1), alias = 'router' }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + { |
| 26 | + uuid = helpers.uuid('b'), |
| 27 | + alias = 's-1', |
| 28 | + roles = { 'customers-storage' }, |
| 29 | + servers = { |
| 30 | + { instance_uuid = helpers.uuid('b', 1), alias = 's1-master' }, |
| 31 | + { instance_uuid = helpers.uuid('b', 2), alias = 's1-replica' }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + uuid = helpers.uuid('c'), |
| 36 | + alias = 's-2', |
| 37 | + roles = { 'customers-storage' }, |
| 38 | + servers = { |
| 39 | + { instance_uuid = helpers.uuid('c', 1), alias = 's2-master' }, |
| 40 | + { instance_uuid = helpers.uuid('c', 2), alias = 's2-replica' }, |
| 41 | + }, |
| 42 | + } |
| 43 | + }, |
| 44 | + env = { |
| 45 | + ['ENGINE'] = engine, |
| 46 | + }, |
| 47 | + }) |
| 48 | + g.engine = engine |
| 49 | + g.cluster:start() |
| 50 | + |
| 51 | + g.space_format = g.cluster.servers[2].net_box.space.customers:format() |
| 52 | +end |
| 53 | + |
| 54 | +g_memtx.before_all = function() before_all(g_memtx, 'memtx') end |
| 55 | +g_vinyl.before_all = function() before_all(g_vinyl, 'vinyl') end |
| 56 | + |
| 57 | +local function after_all(g) |
| 58 | + g.cluster:stop() |
| 59 | + fio.rmtree(g.cluster.datadir) |
| 60 | +end |
| 61 | + |
| 62 | +g_memtx.after_all = function() after_all(g_memtx) end |
| 63 | +g_vinyl.after_all = function() after_all(g_vinyl) end |
| 64 | + |
| 65 | +local function before_each(g) |
| 66 | + for _, server in ipairs(g.cluster.servers) do |
| 67 | + server.net_box:eval([[ |
| 68 | + local space = box.space.customers |
| 69 | + if space ~= nil and not box.cfg.read_only then |
| 70 | + space:truncate() |
| 71 | + end |
| 72 | + ]]) |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +g_memtx.before_each(function() before_each(g_memtx) end) |
| 77 | +g_vinyl.before_each(function() before_each(g_vinyl) end) |
| 78 | + |
| 79 | +local function add(name, fn) |
| 80 | + g_memtx[name] = fn |
| 81 | + g_vinyl[name] = fn |
| 82 | +end |
| 83 | + |
| 84 | +add('test_non_existent_space', function(g) |
| 85 | + -- insert |
| 86 | + local obj, err = g.cluster.main_server.net_box:call( |
| 87 | + 'crud.truncate', {'non_existent_space'} |
| 88 | + ) |
| 89 | + |
| 90 | + t.assert_equals(obj, nil) |
| 91 | + t.assert_str_contains(err.err, "Space \"non_existent_space\" doesn't exist") |
| 92 | +end) |
| 93 | + |
| 94 | +add('test_truncate', function(g) |
| 95 | + local customers = helpers.insert_objects(g, 'customers', { |
| 96 | + { |
| 97 | + id = 1, name = "Elizabeth", last_name = "Jackson", |
| 98 | + age = 12, city = "New York", |
| 99 | + }, { |
| 100 | + id = 2, name = "Mary", last_name = "Brown", |
| 101 | + age = 46, city = "Los Angeles", |
| 102 | + }, { |
| 103 | + id = 3, name = "David", last_name = "Smith", |
| 104 | + age = 33, city = "Los Angeles", |
| 105 | + }, { |
| 106 | + id = 4, name = "William", last_name = "White", |
| 107 | + age = 81, city = "Chicago", |
| 108 | + }, |
| 109 | + }) |
| 110 | + |
| 111 | + table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end) |
| 112 | + |
| 113 | + local result, err = g.cluster.main_server.net_box:call('crud.select', {'customers', nil}) |
| 114 | + t.assert_equals(err, nil) |
| 115 | + t.assert(#result.rows > 0) |
| 116 | + |
| 117 | + local result, err = g.cluster.main_server.net_box:call('crud.truncate', {'customers'}) |
| 118 | + t.assert_equals(err, nil) |
| 119 | + t.assert_equals(result, true) |
| 120 | + |
| 121 | + local result, err = g.cluster.main_server.net_box:call('crud.select', {'customers', nil}) |
| 122 | + t.assert_equals(err, nil) |
| 123 | + t.assert(#result.rows == 0) |
| 124 | +end) |
0 commit comments