Tarantool development patches archive
 help / color / mirror / Atom feed
From: Artem Starshov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Alexander Turenko <alexander.turenko@tarantool.org>,
	Leonid Vasiliev <lvasiliev@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCHv4 1/2] core: add setting error injections via env
Date: Thu, 11 Mar 2021 15:05:48 +0300	[thread overview]
Message-ID: <804e084d08a18a48c0aeb29bf8c278f5893ea379.1615463815.git.artemreyt@tarantool.org> (raw)
In-Reply-To: <cover.1615463815.git.artemreyt@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".

NOTE: errinjs should work only in debug mode, so added `release_disabled` in
suite.ini. But there is a bug in test-run: `release_disable` disables tests at
each build type. Partially this problem is descripted in tarantool/test-run#199.

Part of #5040
---
 src/lib/core/errinj.c                         | 37 +++++++++++++++++++
 src/lib/core/errinj.h                         |  5 +++
 src/main.cc                                   |  3 ++
 .../errinj_set_with_enviroment_vars.test.lua  | 29 +++++++++++++++
 ...errinj_set_with_enviroment_vars_script.lua | 34 +++++++++++++++++
 test/box-tap/suite.ini                        |  1 +
 6 files changed, 109 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..5afbd1839 100644
--- a/src/lib/core/errinj.c
+++ b/src/lib/core/errinj.c
@@ -28,9 +28,11 @@
  * 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>
+#include <strings.h>
 #include <stdbool.h>
 
 #include "trivia/config.h"
@@ -66,3 +68,38 @@ int errinj_foreach(errinj_cb cb, void *cb_ctx) {
 	}
 	return 0;
 }
+
+void errinj_set_with_environment_vars(void) {
+	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 == NULL || *env_value == '\0')
+			continue;
+
+		if (inj->type == ERRINJ_INT) {
+			char *end;
+			int64_t int_value = strtoll(env_value, &end, 10);
+			if (*end == '\0')
+				inj->iparam = int_value;
+			else
+				panic("Incorrect value for integer %s: %s",
+				      inj->name, env_value);
+		} else if (inj->type == ERRINJ_BOOL) {
+			if (strcasecmp(env_value, "false") == 0)
+				inj->bparam = false;
+			else if (strcasecmp(env_value, "true") == 0)
+				inj->bparam = true;
+			else
+				panic("Incorrect value for boolean %s: %s",
+				      inj->name, env_value);
+		} else if (inj->type == ERRINJ_DOUBLE) {
+			char *end;
+			double double_value = strtod(env_value, &end);
+			if (*end == '\0')
+				inj->dparam = double_value;
+			else
+				panic("Incorrect value for double %s: %s",
+				      inj->name, env_value);
+		}
+	}
+}
diff --git a/src/lib/core/errinj.h b/src/lib/core/errinj.h
index 814c57c2e..10edbe2b9 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(void);
+
 #ifdef NDEBUG
 #  define ERROR_INJECT(ID, CODE)
 #  define ERROR_INJECT_WHILE(ID, CODE)
diff --git a/src/main.cc b/src/main.cc
index f3edc4305..c322d2d90 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -717,6 +717,9 @@ main(int argc, char **argv)
 	if (res != 0)
 		say_warn("No enough memory for setup ssl certificates paths");
 
+#ifndef NDEBUG
+	errinj_set_with_environment_vars();
+#endif
 	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..79f770ab7
--- /dev/null
+++ b/test/box-tap/errinj_set_with_enviroment_vars.test.lua
@@ -0,0 +1,29 @@
+#!/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'
+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)
diff --git a/test/box-tap/suite.ini b/test/box-tap/suite.ini
index a7c03baa8..8ed7fffb9 100644
--- a/test/box-tap/suite.ini
+++ b/test/box-tap/suite.ini
@@ -4,6 +4,7 @@ description = Database tests with #! using TAP
 is_parallel = True
 pretest_clean = True
 use_unix_sockets_iproto = True
+release_disabled = errinj_set_with_enviroment_vars.test.lua
 config = suite.cfg
 fragile = {
     "retries": 10,
-- 
2.28.0


  reply	other threads:[~2021-03-11 12:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-11 12:05 [Tarantool-patches] [PATCHv4 0/2] lua: fix tarantool -e always enters interactive Artem Starshov via Tarantool-patches
2021-03-11 12:05 ` Artem Starshov via Tarantool-patches [this message]
2021-03-11 12:05 ` [Tarantool-patches] [PATCHv4 2/2] lua: fix tarantool -e always enters interactive mode Artem Starshov via Tarantool-patches
2021-03-11 12:34 ` [Tarantool-patches] [PATCHv4 0/2] lua: fix tarantool -e always enters interactive Leonid Vasiliev via Tarantool-patches
2021-03-12 16:40 ` Kirill Yukhin 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=804e084d08a18a48c0aeb29bf8c278f5893ea379.1615463815.git.artemreyt@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=alexander.turenko@tarantool.org \
    --cc=artemreyt@tarantool.org \
    --cc=lvasiliev@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCHv4 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