|
1 | 1 | local fun = require('fun')
|
| 2 | +local yaml = require('yaml') |
| 3 | +local fio = require('fio') |
| 4 | +local cluster_config = require('internal.config.cluster_config') |
2 | 5 | local t = require('luatest')
|
| 6 | +local treegen = require('test.treegen') |
| 7 | +local justrun = require('test.justrun') |
3 | 8 | local server = require('test.luatest_helpers.server')
|
4 | 9 |
|
| 10 | +local function group(name, params) |
| 11 | + local g = t.group(name, params) |
| 12 | + |
| 13 | + g.before_all(treegen.init) |
| 14 | + |
| 15 | + g.after_each(function(g) |
| 16 | + for k, v in pairs(table.copy(g)) do |
| 17 | + if k == 'server' or k:match('^server_%d+$') then |
| 18 | + v:stop() |
| 19 | + g[k] = nil |
| 20 | + end |
| 21 | + end |
| 22 | + end) |
| 23 | + |
| 24 | + g.after_all(treegen.clean) |
| 25 | + |
| 26 | + return g |
| 27 | +end |
| 28 | + |
5 | 29 | local function start_example_replicaset(g, dir, config_file, opts)
|
6 | 30 | local credentials = {
|
7 | 31 | user = 'client',
|
@@ -40,6 +64,217 @@ local function start_example_replicaset(g, dir, config_file, opts)
|
40 | 64 | t.assert_equals(info.cluster.name, 'group-001')
|
41 | 65 | end
|
42 | 66 |
|
| 67 | +-- A simple single instance configuration. |
| 68 | +local simple_config = { |
| 69 | + credentials = { |
| 70 | + users = { |
| 71 | + guest = { |
| 72 | + roles = {'super'}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + iproto = { |
| 77 | + listen = 'unix/:./{{ instance_name }}.iproto', |
| 78 | + }, |
| 79 | + groups = { |
| 80 | + ['group-001'] = { |
| 81 | + replicasets = { |
| 82 | + ['replicaset-001'] = { |
| 83 | + instances = { |
| 84 | + ['instance-001'] = {}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | +} |
| 91 | + |
| 92 | +local function prepare_case(g, opts) |
| 93 | + local dir = opts.dir |
| 94 | + local script = opts.script |
| 95 | + local options = opts.options |
| 96 | + |
| 97 | + if dir == nil then |
| 98 | + dir = treegen.prepare_directory(g, {}, {}) |
| 99 | + end |
| 100 | + |
| 101 | + if script ~= nil then |
| 102 | + treegen.write_script(dir, 'main.lua', script) |
| 103 | + end |
| 104 | + |
| 105 | + if options ~= nil then |
| 106 | + local config = table.deepcopy(simple_config) |
| 107 | + for path, value in pairs(options) do |
| 108 | + cluster_config:set(config, path, value) |
| 109 | + end |
| 110 | + treegen.write_script(dir, 'config.yaml', yaml.encode(config)) |
| 111 | + end |
| 112 | + |
| 113 | + local config_file = fio.pathjoin(dir, 'config.yaml') |
| 114 | + local server = { |
| 115 | + config_file = config_file, |
| 116 | + chdir = dir, |
| 117 | + alias = 'instance-001', |
| 118 | + } |
| 119 | + local justrun = { |
| 120 | + -- dir |
| 121 | + dir, |
| 122 | + -- env |
| 123 | + {}, |
| 124 | + -- args |
| 125 | + {'--name', 'instance-001', '--config', config_file}, |
| 126 | + -- opts |
| 127 | + {nojson = true, stderr = true}, |
| 128 | + } |
| 129 | + return { |
| 130 | + dir = dir, |
| 131 | + server = server, |
| 132 | + justrun = justrun, |
| 133 | + } |
| 134 | +end |
| 135 | + |
| 136 | +-- Start a server with the given script and the given |
| 137 | +-- configuration, run a verification function on it. |
| 138 | +-- |
| 139 | +-- * opts.script |
| 140 | +-- |
| 141 | +-- Code write into the main.lua file. |
| 142 | +-- |
| 143 | +-- * opts.options |
| 144 | +-- |
| 145 | +-- The configuration is expressed as a set of path:value pairs. |
| 146 | +-- It is merged into the simple config above. |
| 147 | +-- |
| 148 | +-- * opts.verify |
| 149 | +-- |
| 150 | +-- Function to run on the started server to verify some |
| 151 | +-- invariants. |
| 152 | +local function success_case(g, opts) |
| 153 | + local verify = assert(opts.verify) |
| 154 | + local prepared = prepare_case(g, opts) |
| 155 | + g.server = server:new(prepared.server) |
| 156 | + g.server:start() |
| 157 | + g.server:exec(verify) |
| 158 | + return prepared |
| 159 | +end |
| 160 | + |
| 161 | +-- Start tarantool process with the given script/config and check |
| 162 | +-- the error. |
| 163 | +-- |
| 164 | +-- * opts.script |
| 165 | +-- * opts.options |
| 166 | +-- |
| 167 | +-- Same as in success_case(). |
| 168 | +-- |
| 169 | +-- * opts.exp_err |
| 170 | +-- |
| 171 | +-- An error that must be written into stderr by tarantool |
| 172 | +-- process. |
| 173 | +local function failure_case(g, opts) |
| 174 | + local exp_err = assert(opts.exp_err) |
| 175 | + |
| 176 | + local prepared = prepare_case(g, opts) |
| 177 | + local res = justrun.tarantool(unpack(prepared.justrun)) |
| 178 | + t.assert_equals(res.exit_code, 1) |
| 179 | + t.assert_str_contains(res.stderr, exp_err) |
| 180 | +end |
| 181 | + |
| 182 | +-- Start a server, write a new script/config, reload, run a |
| 183 | +-- verification function. |
| 184 | +-- |
| 185 | +-- * opts.script |
| 186 | +-- * opts.options |
| 187 | +-- * opts.verify |
| 188 | +-- |
| 189 | +-- Same as in success_case(). |
| 190 | +-- |
| 191 | +-- * opts.script_2 |
| 192 | +-- |
| 193 | +-- A new script to write into the main.lua file before |
| 194 | +-- config:reload(). |
| 195 | +-- |
| 196 | +-- * opts.verify_2 |
| 197 | +-- |
| 198 | +-- Verify test invariants after config:reload(). |
| 199 | +local function reload_success_case(g, opts) |
| 200 | + local script_2 = assert(opts.script_2) |
| 201 | + local verify_2 = assert(opts.verify_2) |
| 202 | + |
| 203 | + local prepared = success_case(g, opts) |
| 204 | + |
| 205 | + prepare_case(g, { |
| 206 | + dir = prepared.dir, |
| 207 | + script = script_2, |
| 208 | + }) |
| 209 | + g.server:exec(function() |
| 210 | + local config = require('config') |
| 211 | + config:reload() |
| 212 | + end) |
| 213 | + g.server:exec(verify_2) |
| 214 | +end |
| 215 | + |
| 216 | +-- Start a server, write a new script/config, reload, run a |
| 217 | +-- verification function. |
| 218 | +-- |
| 219 | +-- * opts.script |
| 220 | +-- * opts.options |
| 221 | +-- * opts.verify |
| 222 | +-- |
| 223 | +-- Same as in success_case(). |
| 224 | +-- |
| 225 | +-- * opts.script_2 |
| 226 | +-- |
| 227 | +-- A new script to write into the main.lua file before |
| 228 | +-- config:reload(). |
| 229 | +-- |
| 230 | +-- * opts.exp_err |
| 231 | +-- |
| 232 | +-- An error that config:reload() must raise. |
| 233 | +local function reload_failure_case(g, opts) |
| 234 | + local script_2 = assert(opts.script_2) |
| 235 | + local exp_err = assert(opts.exp_err) |
| 236 | + |
| 237 | + local prepared = success_case(g, opts) |
| 238 | + |
| 239 | + prepare_case(g, { |
| 240 | + dir = prepared.dir, |
| 241 | + script = script_2, |
| 242 | + }) |
| 243 | + t.assert_error_msg_equals(exp_err, g.server.exec, g.server, function() |
| 244 | + local config = require('config') |
| 245 | + config:reload() |
| 246 | + end) |
| 247 | + g.server:exec(function(exp_err) |
| 248 | + local config = require('config') |
| 249 | + local info = config:info() |
| 250 | + t.assert_equals(info.status, 'check_errors') |
| 251 | + t.assert_equals(#info.alerts, 1) |
| 252 | + t.assert_equals(info.alerts[1].type, 'error') |
| 253 | + t.assert(info.alerts[1].timestamp ~= nil) |
| 254 | + t.assert_equals(info.alerts[1].message, exp_err) |
| 255 | + end, {exp_err}) |
| 256 | +end |
| 257 | + |
43 | 258 | return {
|
| 259 | + -- Setup a group of tests that are prepended/postpones with |
| 260 | + -- hooks to stop servers between tests and to remove temporary |
| 261 | + -- files after the testing. |
| 262 | + group = group, |
| 263 | + |
| 264 | + -- Start a three instance replicaset with the given config |
| 265 | + -- file. |
| 266 | + -- |
| 267 | + -- It assumes specific instance/replicaset/group names and |
| 268 | + -- net.box credentials. |
44 | 269 | start_example_replicaset = start_example_replicaset,
|
| 270 | + |
| 271 | + -- Run a single instance and verify some invariants. |
| 272 | + -- |
| 273 | + -- All the runs are based on the given simple config, |
| 274 | + -- but the options can be adjusted. |
| 275 | + simple_config = simple_config, |
| 276 | + success_case = success_case, |
| 277 | + failure_case = failure_case, |
| 278 | + reload_success_case = reload_success_case, |
| 279 | + reload_failure_case = reload_failure_case, |
45 | 280 | }
|
0 commit comments