[Tarantool-patches] [PATCHv2 1/2] core: add setting error injections via env

Leonid Vasiliev lvasiliev at tarantool.org
Mon Mar 1 17:47:24 MSK 2021


HI! Thank you for the patch.
Seems like write a test is more difficult than fixing the bug)
Generally OK.
I'll paste the last variation of the diff with comments.


 From c2ff4adb95eda297e61ed6836f391a541ab08fd3 Mon Sep 17 00:00:00 2001
Message-Id: 
<c2ff4adb95eda297e61ed6836f391a541ab08fd3.1614609240.git.lvasiliev at tarantool.org>
In-Reply-To: <cover.1614609240.git.lvasiliev at tarantool.org>
References: <cover.1614609240.git.lvasiliev at tarantool.org>
From: Artem Starshov <artemreyt at tarantool.org>
Date: Tue, 26 Jan 2021 20:15:22 +0300
Subject: [PATCH 1/2] core: add setting error injections via env
Cc: tarantool-patches at dev.tarantool.org

Sometimes, it's useful to set error injections via environment
variables and this commit adds this opportunity.

e.g: `ERRINJ_WAL_WRITE=true tarantool` will be launched with
ERRINJ_WAL_WRITE setted to true.

Errinjs with bool parameters can be set to "true", "false",
"True", "False", "TRUE", "FALSE", etc. (case-insensitive variable).

Errinjs with int or double parameters should be whole valid ("123s" is 
invalid).
e.g. for int or double: "123", "-1", "2.34", "+2.34".

Part of #5040
---
  src/lib/core/errinj.c                         | 31 +++++++++++++++++
  src/lib/core/errinj.h                         |  5 +++
  src/main.cc                                   |  1 +
  .../errinj_set_with_enviroment_vars.test.lua  | 18 ++++++++++
  ...errinj_set_with_enviroment_vars_script.lua | 34 +++++++++++++++++++
  5 files changed, 89 insertions(+)
  create mode 100755 test/box-tap/errinj_set_with_enviroment_vars.test.lua
  create mode 100644 test/box-tap/errinj_set_with_enviroment_vars_script.lua

diff --git a/src/lib/core/errinj.c b/src/lib/core/errinj.c
index d3aa0ca4f..983ead681 100644
--- a/src/lib/core/errinj.c
+++ b/src/lib/core/errinj.c
@@ -28,6 +28,7 @@
   * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   */
+#include <ctype.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
@@ -66,3 +67,33 @@ int errinj_foreach(errinj_cb cb, void *cb_ctx) {
  	}
  	return 0;
  }
+
+void errinj_set_with_environment_vars() {

Comment:
void errinj_set_with_environment_vars(void) see 
https://github.com/tarantool/tarantool/issues/4718

+    for (enum errinj_id i = 0; i < errinj_id_MAX; i++) {
+        struct errinj *inj = &errinjs[i];
+        const char *env_value = getenv(inj->name);
+        if (!env_value || *env_value == '\0')

Comment:
env_value == NULL 
(https://github.com/tarantool/tarantool/wiki/Code-review-procedure#code-style)

+            continue;
+
+        if (inj->type == ERRINJ_INT) {
+            char *end;
+            int64_t int_value = strtoll(env_value, &end, 10);
+            if (*end == '\0')
+                inj->iparam = int_value;

Comment:
Seems like the `panic()` should be called in the case of `else()`. Here 
and in
other cases of parsing.

+        } else if (inj->type == ERRINJ_BOOL) {
+            char *lower_env_value = (char *)calloc(strlen(env_value) + 
1, sizeof(char));

Comment:
`(char *)` - unneeded in C.
Maximal width of a code line is 80 symbols.

+            for (size_t i = 0; env_value[i]; ++i)
+                lower_env_value[i] = tolower(env_value[i]);

Comment:
I suggest to use `strcasecmp()`.

+            if (strcmp(lower_env_value, "false") == 0)
+                inj->bparam = false;
+            else if (strcmp(lower_env_value, "true") == 0)
+                inj->bparam = true;
+            free(lower_env_value);
+        } else if (inj->type == ERRINJ_DOUBLE) {
+            char *end;
+            double double_value = strtod(env_value, &end);
+            if (*end == '\0')
+                inj->dparam = double_value;
+        }
+    }
+}
diff --git a/src/lib/core/errinj.h b/src/lib/core/errinj.h
index 814c57c2e..d76aedf7a 100644
--- a/src/lib/core/errinj.h
+++ b/src/lib/core/errinj.h
@@ -168,6 +168,11 @@ typedef int (*errinj_cb)(struct errinj *e, void 
*cb_ctx);
  int
  errinj_foreach(errinj_cb cb, void *cb_ctx);

+/**
+ * Set injections by scanning ERRINJ_$(NAME) in environment variables
+ */
+void errinj_set_with_environment_vars();
+
  #ifdef NDEBUG
  #  define ERROR_INJECT(ID, CODE)
  #  define ERROR_INJECT_WHILE(ID, CODE)
diff --git a/src/main.cc b/src/main.cc
index 2fce81bb3..58a660689 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -710,6 +710,7 @@ main(int argc, char **argv)
  	memtx_tx_manager_init();
  	crypto_init();
  	systemd_init();
+	errinj_set_with_environment_vars();
  	tarantool_lua_init(tarantool_bin, main_argc, main_argv);

  	start_time = ev_monotonic_time();
diff --git a/test/box-tap/errinj_set_with_enviroment_vars.test.lua 
b/test/box-tap/errinj_set_with_enviroment_vars.test.lua
new file mode 100755
index 000000000..bffcc31e7
--- /dev/null
+++ b/test/box-tap/errinj_set_with_enviroment_vars.test.lua
@@ -0,0 +1,18 @@
+#!/usr/bin/env tarantool
+local fio = require('fio')
+
+-- Execute errinj_set_with_enviroment_vars_script.lua
+-- via tarantool with presetted environment variables.
+local TARANTOOL_PATH = arg[-1]

+local bool_env = 'ERRINJ_TESTING=true ERRINJ_WAL_IO=True 
ERRINJ_WAL_SYNC=TRUE ' ..
+                 'ERRINJ_WAL_WRITE=false ERRINJ_INDEX_ALLOC=False 
ERRINJ_WAL_WRITE_DISK=FALSE'
+local integer_env = 'ERRINJ_WAL_WRITE_PARTIAL=2 ERRINJ_WAL_FALLOCATE=+2 
ERRINJ_WAL_WRITE_COUNT=-2'
+local double_env = 'ERRINJ_VY_READ_PAGE_TIMEOUT=2.5 
ERRINJ_VY_SCHED_TIMEOUT=+2.5 ERRINJ_RELAY_TIMEOUT=-2.5'

Comment:
Maximal width of a code line is 80 symbols.

+local set_env_str = ('%s %s %s'):format(bool_env, integer_env, double_env)
+local path_to_test_file = fio.pathjoin(
+        os.getenv('PWD'),
+        'box-tap',
+        'errinj_set_with_enviroment_vars_script.lua')
+local shell_command = ('%s %s %s'):format(set_env_str, TARANTOOL_PATH, 
path_to_test_file)
+
+os.exit(os.execute(shell_command))
diff --git a/test/box-tap/errinj_set_with_enviroment_vars_script.lua 
b/test/box-tap/errinj_set_with_enviroment_vars_script.lua
new file mode 100644
index 000000000..f15085aa0
--- /dev/null
+++ b/test/box-tap/errinj_set_with_enviroment_vars_script.lua
@@ -0,0 +1,34 @@
+-- Script for box-tap/errinj_set_with_enviroment_vars.test.lua test.
+
+local tap = require('tap')
+local errinj = box.error.injection
+
+local test = tap.test('set errinjs via environment variables')
+
+test:plan(3)
+
+test:test('Set boolean error injections', function(test)
+    test:plan(6)
+    test:is(errinj.get('ERRINJ_TESTING'), true, 'true')
+    test:is(errinj.get('ERRINJ_WAL_IO'), true, 'True')
+    test:is(errinj.get('ERRINJ_WAL_SYNC'), true, 'TRUE')
+    test:is(errinj.get('ERRINJ_WAL_WRITE'), false, 'false')
+    test:is(errinj.get('ERRINJ_INDEX_ALLOC'), false, 'False')
+    test:is(errinj.get('ERRINJ_WAL_WRITE_DISK'), false, 'FALSE')
+end)
+
+test:test('Set integer error injections', function(test)
+    test:plan(3)
+    test:is(errinj.get('ERRINJ_WAL_WRITE_PARTIAL'), 2, '2')
+    test:is(errinj.get('ERRINJ_WAL_FALLOCATE'), 2, '+2')
+    test:is(errinj.get('ERRINJ_WAL_WRITE_COUNT'), -2, '-2')
+end)
+
+test:test('Set double error injections', function(test)
+    test:plan(3)
+    test:is(errinj.get('ERRINJ_VY_READ_PAGE_TIMEOUT'), 2.5, "2.5")
+    test:is(errinj.get('ERRINJ_VY_SCHED_TIMEOUT'), 2.5, "+2.5")
+    test:is(errinj.get('ERRINJ_RELAY_TIMEOUT'), -2.5, "-2.5")
+end)
+
+os.exit(test:check() and 0 or 1)
-- 
2.30.0


More information about the Tarantool-patches mailing list