tests: unit: add firing policy tests
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Tue, 6 Apr 2021 15:09:09 +0000 (11:09 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 17 Apr 2021 21:15:03 +0000 (17:15 -0400)
Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: Ib92bd40b2c93f4e90ec430f2ac70ef2a5ce8f891

.gitignore
tests/unit/Makefile.am
tests/unit/test_firing_policy.c [new file with mode: 0644]

index dca36c75ffde9794e0dfa5890840eaa0e02b88a2..0fe7c47742827549e9070885ef6953788062a4e7 100644 (file)
@@ -146,6 +146,7 @@ compile_commands.json
 /tests/unit/test_buffer_view
 /tests/unit/test_kernel_probe
 /tests/unit/test_event_expr_to_bytecode
+/tests/unit/test_firing_policy
 /tests/unit/test_log_level_rule
 /tests/utils/testapp/gen-ust-nevents-str/gen-ust-nevents-str
 /tests/utils/testapp/userspace-probe-elf-binary/userspace-probe-elf-binary
index b972c358533fa6f1d27778435fb58b16a4b47d18..5cf4d31ca37fa7fd1541bc75912c295a1377bc78 100644 (file)
@@ -15,6 +15,7 @@ TESTS = \
        test_event_expr_to_bytecode \
        test_event_rule \
        test_fd_tracker \
+       test_firing_policy \
        test_kernel_data \
        test_kernel_probe \
        test_log_level_rule \
@@ -50,6 +51,7 @@ noinst_PROGRAMS = \
        test_event_expr_to_bytecode \
        test_event_rule \
        test_fd_tracker \
+       test_firing_policy \
        test_kernel_data \
        test_kernel_probe \
        test_log_level_rule \
@@ -230,6 +232,11 @@ test_relayd_backward_compat_group_by_session_SOURCES = test_relayd_backward_comp
 test_relayd_backward_compat_group_by_session_LDADD = $(LIBTAP) $(LIBCOMMON) $(RELAYD_OBJS)
 test_relayd_backward_compat_group_by_session_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src/bin/lttng-relayd
 
+# firing policy object unit test
+test_firing_policy_SOURCES = test_firing_policy.c
+test_firing_policy_LDADD = $(LIBTAP) $(LIBCOMMON) $(LIBLTTNG_CTL) $(DL_LIBS) \
+                     $(top_builddir)/src/bin/lttng/lttng-loglevel.$(OBJEXT)
+
 # fd tracker unit test
 test_fd_tracker_SOURCES = test_fd_tracker.c
 test_fd_tracker_LDADD = $(LIBTAP) $(LIBFDTRACKER) $(DL_LIBS) $(URCU_LIBS) $(LIBCOMMON) $(LIBHASHTABLE)
diff --git a/tests/unit/test_firing_policy.c b/tests/unit/test_firing_policy.c
new file mode 100644 (file)
index 0000000..e7504c2
--- /dev/null
@@ -0,0 +1,250 @@
+/*
+ * Unit tests for the firing policy object API.
+ *
+ * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <tap/tap.h>
+
+#include <common/payload-view.h>
+#include <common/payload.h>
+#include <lttng/action/firing-policy-internal.h>
+#include <lttng/action/firing-policy.h>
+
+/* For error.h. */
+int lttng_opt_quiet = 1;
+int lttng_opt_verbose;
+int lttng_opt_mi;
+
+#define NUM_TESTS 42
+
+static void test_firing_policy_every_n(void)
+{
+       enum lttng_firing_policy_status status;
+       struct lttng_firing_policy *policy_a = NULL; /* Interval of 100. */
+       struct lttng_firing_policy *policy_b = NULL; /* Interval of 100. */
+       struct lttng_firing_policy *policy_c = NULL; /* Interval of 1. */
+       struct lttng_firing_policy *policy_from_buffer = NULL;
+       uint64_t interval_a_b = 100;
+       uint64_t interval_c = 1;
+       uint64_t interval_query = 0;
+       struct lttng_payload payload;
+
+       lttng_payload_init(&payload);
+
+       policy_a = lttng_firing_policy_every_n_create(interval_a_b);
+       policy_b = lttng_firing_policy_every_n_create(interval_a_b);
+       policy_c = lttng_firing_policy_every_n_create(interval_c);
+       ok(policy_a != NULL,
+                       "Firing policy 'every n' A created: interval: %" PRIu64,
+                       interval_a_b);
+       ok(policy_b != NULL,
+                       "Firing policy 'every n' B created: interval: %" PRIu64,
+                       interval_a_b);
+       ok(policy_c != NULL,
+                       "Firing policy 'every n' C created: interval: %" PRIu64,
+                       interval_c);
+
+       ok(LTTNG_FIRING_POLICY_TYPE_EVERY_N ==
+                                       lttng_firing_policy_get_type(policy_a),
+                       "Type is LTTNG_FIRING_POLICY_TYPE_EVERY_N");
+
+       /* Getter tests */
+       status = lttng_firing_policy_every_n_get_interval(NULL, NULL);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID,
+                       "Get interval returns INVALID");
+
+       status = lttng_firing_policy_every_n_get_interval(
+                       NULL, &interval_query);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID,
+                       "Get interval returns INVALID");
+
+       status = lttng_firing_policy_every_n_get_interval(policy_a, NULL);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID,
+                       "Get interval returns INVALID");
+
+       status = lttng_firing_policy_every_n_get_interval(
+                       policy_a, &interval_query);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_OK &&
+                                       interval_query == interval_a_b,
+                       "Getting interval A");
+
+       status = lttng_firing_policy_every_n_get_interval(
+                       policy_b, &interval_query);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_OK &&
+                                       interval_query == interval_a_b,
+                       "Getting interval B");
+
+       status = lttng_firing_policy_every_n_get_interval(
+                       policy_c, &interval_query);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_OK &&
+                                       interval_query == interval_c,
+                       "Getting interval C");
+
+       /* is_equal tests */
+       ok(false == lttng_firing_policy_is_equal(NULL, NULL),
+                       "is equal (NULL,NULL)");
+       ok(false == lttng_firing_policy_is_equal(policy_a, NULL),
+                       "is equal (object, NULL)");
+       ok(false == lttng_firing_policy_is_equal(NULL, policy_a),
+                       "is equal (NULL, object)");
+       ok(true == lttng_firing_policy_is_equal(policy_a, policy_a),
+                       "is equal (object A, object A)");
+
+       ok(true == lttng_firing_policy_is_equal(policy_a, policy_b),
+                       "is equal (object A, object B");
+       ok(true == lttng_firing_policy_is_equal(policy_b, policy_a),
+                       "is equal (object B, object A");
+
+       ok(false == lttng_firing_policy_is_equal(policy_a, policy_c),
+                       "is equal (object A, object C)");
+       ok(false == lttng_firing_policy_is_equal(policy_c, policy_a),
+                       "is equal (object C, object A)");
+
+       /* Serialization and create_from buffer. */
+       ok(lttng_firing_policy_serialize(policy_a, &payload) == 0,
+                       "Serializing firing policy");
+       {
+               struct lttng_payload_view view =
+                               lttng_payload_view_from_payload(
+                                               &payload, 0, -1);
+
+               ok(lttng_firing_policy_create_from_payload(
+                                  &view, &policy_from_buffer) > 0 &&
+                                               policy_from_buffer != NULL,
+                               "Deserializing firing policy");
+       }
+
+       ok(lttng_firing_policy_is_equal(policy_a, policy_from_buffer),
+                       "Original and deserialized instances are equal");
+
+       lttng_firing_policy_destroy(policy_a);
+       lttng_firing_policy_destroy(policy_b);
+       lttng_firing_policy_destroy(policy_c);
+       lttng_firing_policy_destroy(policy_from_buffer);
+       lttng_payload_reset(&payload);
+}
+
+static void test_firing_policy_once_after_n(void)
+{
+       enum lttng_firing_policy_status status;
+       struct lttng_firing_policy *policy_a = NULL; /* Threshold of 100. */
+       struct lttng_firing_policy *policy_b = NULL; /* threshold of 100 */
+       struct lttng_firing_policy *policy_c = NULL; /* threshold of 1 */
+       struct lttng_firing_policy *policy_from_buffer = NULL;
+       uint64_t threshold_a_b = 100;
+       uint64_t threshold_c = 1;
+       uint64_t threshold_query = 0;
+       struct lttng_payload payload;
+
+       lttng_payload_init(&payload);
+
+       policy_a = lttng_firing_policy_once_after_n_create(threshold_a_b);
+       policy_b = lttng_firing_policy_once_after_n_create(threshold_a_b);
+       policy_c = lttng_firing_policy_once_after_n_create(threshold_c);
+       ok(policy_a != NULL,
+                       "Firing policy every n A created: threshold: %" PRIu64,
+                       threshold_a_b);
+       ok(policy_b != NULL,
+                       "Firing policy every n B created: threshold: %" PRIu64,
+                       threshold_a_b);
+       ok(policy_c != NULL,
+                       "Firing policy every n C created: threshold: %" PRIu64,
+                       threshold_c);
+
+       ok(LTTNG_FIRING_POLICY_TYPE_ONCE_AFTER_N ==
+                                       lttng_firing_policy_get_type(policy_a),
+                       "Type is LTTNG_FIRING_POLICY_TYPE_once_after_n");
+
+       /* Getter tests */
+       status = lttng_firing_policy_once_after_n_get_threshold(NULL, NULL);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID,
+                       "Get threshold returns INVALID");
+
+       status = lttng_firing_policy_once_after_n_get_threshold(
+                       NULL, &threshold_query);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID,
+                       "Get threshold returns INVALID");
+
+       status = lttng_firing_policy_once_after_n_get_threshold(policy_a, NULL);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID,
+                       "Get threshold returns INVALID");
+
+       status = lttng_firing_policy_once_after_n_get_threshold(
+                       policy_a, &threshold_query);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_OK &&
+                                       threshold_query == threshold_a_b,
+                       "Getting threshold A");
+
+       status = lttng_firing_policy_once_after_n_get_threshold(
+                       policy_b, &threshold_query);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_OK &&
+                                       threshold_query == threshold_a_b,
+                       "Getting threshold B");
+
+       status = lttng_firing_policy_once_after_n_get_threshold(
+                       policy_c, &threshold_query);
+       ok(status == LTTNG_FIRING_POLICY_STATUS_OK &&
+                                       threshold_query == threshold_c,
+                       "Getting threshold C");
+
+       /* is_equal tests */
+       ok(false == lttng_firing_policy_is_equal(NULL, NULL),
+                       "is equal (NULL,NULL)");
+       ok(false == lttng_firing_policy_is_equal(policy_a, NULL),
+                       "is equal (object, NULL)");
+       ok(false == lttng_firing_policy_is_equal(NULL, policy_a),
+                       "is equal (NULL, object)");
+       ok(true == lttng_firing_policy_is_equal(policy_a, policy_a),
+                       "is equal (object A, object A)");
+
+       ok(true == lttng_firing_policy_is_equal(policy_a, policy_b),
+                       "is equal (object A, object B");
+       ok(true == lttng_firing_policy_is_equal(policy_b, policy_a),
+                       "is equal (object B, object A");
+
+       ok(false == lttng_firing_policy_is_equal(policy_a, policy_c),
+                       "is equal (object A, object C)");
+       ok(false == lttng_firing_policy_is_equal(policy_c, policy_a),
+                       "is equal (object C, object A)");
+
+       /* Serialization and create_from buffer. */
+       ok(lttng_firing_policy_serialize(policy_a, &payload) == 0,
+                       "Serializing firing policy");
+       {
+               struct lttng_payload_view view =
+                               lttng_payload_view_from_payload(
+                                               &payload, 0, -1);
+
+               ok(lttng_firing_policy_create_from_payload(
+                                  &view, &policy_from_buffer) > 0 &&
+                                               policy_from_buffer != NULL,
+                               "Deserializing firing policy");
+       }
+
+       ok(lttng_firing_policy_is_equal(policy_a, policy_from_buffer),
+                       "Original and deserialized instances are equal");
+
+       lttng_firing_policy_destroy(policy_a);
+       lttng_firing_policy_destroy(policy_b);
+       lttng_firing_policy_destroy(policy_c);
+       lttng_firing_policy_destroy(policy_from_buffer);
+       lttng_payload_reset(&payload);
+}
+
+int main(int argc, const char *argv[])
+{
+       plan_tests(NUM_TESTS);
+       test_firing_policy_every_n();
+       test_firing_policy_once_after_n();
+       return exit_status();
+}
This page took 0.041071 seconds and 4 git commands to generate.