Tarantool development patches archive
 help / color / mirror / Atom feed
From: Leonid Vasiliev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Artem Starshov <artemreyt@tarantool.org>,
	Alexander Turenko <alexander.turenko@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCHv2 1/2] core: add setting error injections via env
Date: Mon, 1 Mar 2021 17:47:24 +0300	[thread overview]
Message-ID: <1493e11a-e8ca-6dc0-c4cf-7f8bb4dd0d71@tarantool.org> (raw)
In-Reply-To: <7a1f0cb7a97b41b3cde25ac5d2c591a84aa13f0b.1613674486.git.artemreyt@tarantool.org>

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@tarantool.org>
In-Reply-To: <cover.1614609240.git.lvasiliev@tarantool.org>
References: <cover.1614609240.git.lvasiliev@tarantool.org>
From: Artem Starshov <artemreyt@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@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

  parent reply	other threads:[~2021-03-01 14:48 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-18 19:30 [Tarantool-patches] [PATCHv2 0/2] lua: fix tarantool -e always enters interactive mode Artem Starshov via Tarantool-patches
2021-02-18 19:30 ` [Tarantool-patches] [PATCHv2 1/2] core: add setting error injections via env Artem Starshov via Tarantool-patches
2021-02-20  9:03   ` Sergey Bronnikov via Tarantool-patches
2021-02-20 11:21     ` Artem via Tarantool-patches
2021-02-20 14:27       ` Sergey Bronnikov via Tarantool-patches
2021-02-24  9:38         ` Artem via Tarantool-patches
2021-02-25 14:36           ` Sergey Bronnikov via Tarantool-patches
2021-03-01 14:47   ` Leonid Vasiliev via Tarantool-patches [this message]
2021-03-02 17:00     ` Artem via Tarantool-patches
2021-02-18 19:30 ` [Tarantool-patches] [PATCHv2 2/2] lua: fix tarantool -e always enters interactive mode Artem Starshov via Tarantool-patches
2021-02-19  9:15   ` Konstantin Osipov via Tarantool-patches
2021-02-19 14:33     ` Artem via Tarantool-patches
2021-02-20 10:05   ` Sergey Bronnikov via Tarantool-patches
2021-02-20 11:27     ` Artem via Tarantool-patches
2021-03-01 15:10   ` Leonid Vasiliev via Tarantool-patches
2021-03-02 17:05     ` Artem via Tarantool-patches
2021-03-03 12:55       ` Leonid Vasiliev via Tarantool-patches
2021-02-25 14:37 ` [Tarantool-patches] [PATCHv2 0/2] " Sergey Bronnikov via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1493e11a-e8ca-6dc0-c4cf-7f8bb4dd0d71@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=alexander.turenko@tarantool.org \
    --cc=artemreyt@tarantool.org \
    --cc=lvasiliev@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCHv2 1/2] core: add setting error injections via env' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox