From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH 2/5] libtracefs: Unit tests for tracing options APIs
Date: Fri, 15 Jan 2021 07:04:07 +0200 [thread overview]
Message-ID: <20210115050410.1194011-3-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20210115050410.1194011-1-tz.stoyanov@gmail.com>
Unit tests for these new APIs:
tracefs_options_get_supported();
tracefs_option_is_supported();
tracefs_options_get_enabled();
tracefs_option_is_enabled();
tracefs_options_set();
tracefs_options_clear();
tracefs_option_string();
Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
utest/tracefs-utest.c | 125 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 125 insertions(+)
diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index 0520f49..1ade5dd 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -452,6 +452,129 @@ static void test_tracing_onoff(void)
close(fd);
}
+static bool check_option(long long id, bool exist, int enabled)
+{
+ const char *name = tracefs_option_string(id);
+ char file[PATH_MAX];
+ char *path = NULL;
+ bool ret = false;
+ bool supported;
+ struct stat st;
+ char buf[10];
+ int fd;
+ int r;
+ int rstat;
+
+ CU_TEST(name != NULL);
+ supported = tracefs_option_is_supported(test_instance, id);
+ CU_TEST(supported == exist);
+ if (supported != exist)
+ goto out;
+ snprintf(file, PATH_MAX, "options/%s", name);
+ path = tracefs_instance_get_file(test_instance, file);
+ CU_TEST(path != NULL);
+ rstat = stat(path, &st);
+ if (exist) {
+ CU_TEST(rstat == 0);
+ if (rstat != 0)
+ goto out;
+ } else {
+ CU_TEST(stat(path, &st) == -1);
+ if (rstat != -1)
+ goto out;
+ }
+
+ fd = open(path, O_RDONLY);
+ if (exist) {
+ CU_TEST(fd >= 0);
+ if (fd < 0)
+ goto out;
+ } else {
+ CU_TEST(fd < 0);
+ if (fd >= 0)
+ goto out;
+ }
+
+ if (exist && enabled >= 0) {
+ int val = enabled ? '1' : '0';
+
+ r = read(fd, buf, 10);
+ CU_TEST(r == 2);
+ CU_TEST(buf[0] == val);
+ if (buf[0] != val)
+ goto out;
+ }
+
+ ret = true;
+out:
+ tracefs_put_tracing_file(path);
+ if (fd >= 0)
+ close(fd);
+ return ret;
+}
+
+static void test_tracing_options(void)
+{
+ unsigned long long options_enabled = 0;
+ unsigned long long options_all = 0;
+ unsigned long long i = 1;
+ char file[PATH_MAX];
+ const char *name;
+
+ options_all = tracefs_options_get_supported(test_instance);
+ options_enabled = tracefs_options_get_enabled(test_instance);
+ CU_TEST(options_all > 0);
+
+ /* Invalid parameters test */
+ CU_TEST(!tracefs_option_is_supported(test_instance, TRACEFS_OPTION_INVALID << 1));
+ CU_TEST(!tracefs_option_is_enabled(test_instance, TRACEFS_OPTION_INVALID << 1));
+ CU_TEST(tracefs_options_set(test_instance, TRACEFS_OPTION_INVALID << 1) == -1);
+ CU_TEST(tracefs_options_clear(test_instance, TRACEFS_OPTION_INVALID << 1) == -1);
+ name = tracefs_option_string(TRACEFS_OPTION_INVALID << 1);
+ CU_TEST(!strcmp(name, "unknown"));
+
+ /* Test all valid options */
+ do {
+ name = tracefs_option_string(i);
+ CU_TEST(name != NULL);
+ CU_TEST(strcmp(name, "unknown"));
+ snprintf(file, PATH_MAX, "options/%s", name);
+
+ if (options_all & i) {
+ options_all &= ~i;
+ CU_TEST(check_option(i, true, -1));
+ CU_TEST(tracefs_option_is_supported(test_instance, i));
+ } else {
+ CU_TEST(check_option(i, false, -1));
+ CU_TEST(!tracefs_option_is_supported(test_instance, i));
+ }
+
+ if (options_enabled & i) {
+ options_enabled &= ~i;
+ CU_TEST(check_option(i, true, 1));
+ CU_TEST(tracefs_option_is_supported(test_instance, i));
+ CU_TEST(tracefs_option_is_enabled(test_instance, i));
+ CU_TEST(tracefs_options_clear(test_instance, i) == 0);
+ CU_TEST(check_option(i, true, 0));
+ CU_TEST(tracefs_options_set(test_instance, i) == 0);
+ CU_TEST(check_option(i, true, 1));
+ } else if (options_all & i) {
+ CU_TEST(check_option(i, true, 0));
+ CU_TEST(tracefs_option_is_supported(test_instance, i));
+ CU_TEST(!tracefs_option_is_enabled(test_instance, i));
+ CU_TEST(tracefs_options_set(test_instance, i) == 0);
+ CU_TEST(check_option(i, true, 1));
+ CU_TEST(tracefs_options_clear(test_instance, i) == 0);
+ CU_TEST(check_option(i, true, 0));
+ }
+ i <<= 1;
+ } while (i < TRACEFS_OPTION_INVALID);
+
+ CU_TEST(options_all == 0);
+ CU_TEST(options_enabled == 0);
+
+}
+
static void exclude_string(char **strings, char *name)
{
int i;
@@ -761,5 +884,7 @@ void test_tracefs_lib(void)
test_get_clock);
CU_add_test(suite, "tracing on / off",
test_tracing_onoff);
+ CU_add_test(suite, "tracing options",
+ test_tracing_options);
}
--
2.29.2
next prev parent reply other threads:[~2021-01-15 5:04 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-15 5:04 [PATCH 0/5] New libtracefs APIs for trace options and trace dir Tzvetomir Stoyanov (VMware)
2021-01-15 5:04 ` [PATCH 1/5] libtracefs: New APIs for trace options Tzvetomir Stoyanov (VMware)
2021-01-19 21:12 ` Steven Rostedt
2021-01-15 5:04 ` Tzvetomir Stoyanov (VMware) [this message]
2021-01-15 5:04 ` [PATCH 3/5] libtracefs: Add information about top tracing directory in instance structure Tzvetomir Stoyanov (VMware)
2021-01-15 5:04 ` [PATCH 4/5] libtracefs: New APIs for getting existing trace instance Tzvetomir Stoyanov (VMware)
2021-01-19 22:18 ` Steven Rostedt
2021-01-15 5:04 ` [PATCH 5/5] libtracefs: Unit tests for working with non default tracing dir Tzvetomir Stoyanov (VMware)
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=20210115050410.1194011-3-tz.stoyanov@gmail.com \
--to=tz.stoyanov@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).