Adding 05 new test case into a new testsuite 22
---
 src/log/Makefile.am                           |   7 +-
 src/log/apitest/log_server.cc                 |  43 +++
 src/log/apitest/log_server.h                  |  35 +++
 .../apitest/tet_saLogStreamConfigFacilityId.c | 273 ++++++++++++++++++
 src/log/tests/lgs_dest_test.cc                |   3 +
 5 files changed, 359 insertions(+), 2 deletions(-)
 create mode 100644 src/log/apitest/log_server.cc
 create mode 100644 src/log/apitest/log_server.h
 create mode 100644 src/log/apitest/tet_saLogStreamConfigFacilityId.c

diff --git a/src/log/Makefile.am b/src/log/Makefile.am
index a2a98baec..2dad3cfb1 100644
--- a/src/log/Makefile.am
+++ b/src/log/Makefile.am
@@ -190,7 +190,8 @@ bin_PROGRAMS += bin/logtest bin/saflogtest bin/logtestfr
 noinst_HEADERS += \
        src/log/apitest/logtest.h \
        src/log/apitest/logutil.h \
-       src/log/apitest/imm_tstutil.h
+       src/log/apitest/imm_tstutil.h \
+       src/log/apitest/log_server.h
 
 bin_logtest_CFLAGS = $(AM_CFLAGS) -Wformat=1
 
@@ -227,7 +228,9 @@ bin_logtest_SOURCES = \
        src/log/apitest/tet_Log_clm.c \
        src/log/apitest/tet_cfg_destination.c \
        src/log/apitest/tet_multiple_thread.c \
-       src/log/apitest/tet_saLogWriteLogAsync_cache.c
+       src/log/apitest/tet_saLogWriteLogAsync_cache.c \
+       src/log/apitest/tet_saLogStreamConfigFacilityId.c \
+       src/log/apitest/log_server.cc
 
 bin_logtest_LDADD = \
        lib/libapitest.la \
diff --git a/src/log/apitest/log_server.cc b/src/log/apitest/log_server.cc
new file mode 100644
index 000000000..25f7894e0
--- /dev/null
+++ b/src/log/apitest/log_server.cc
@@ -0,0 +1,43 @@
+/*      -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2020 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ * Author(s): Ericsson AB
+ *
+ */
+
+#include "log/apitest/log_server.h"
+#include "base/unix_server_socket.h"
+
+static base::UnixServerSocket* server = nullptr;
+
+void StartUnixServer() {
+  server = new base::UnixServerSocket("/tmp/test.sock",
+                                      base::UnixSocket::kBlocking, 0777);
+  server->fd();
+}
+
+bool FindPRI(const char* pri_field, const char* msg) {
+  char buf[1024];
+  bool found = false;
+  do {
+    memset(buf, 0, sizeof(buf));
+    size_t len = server->Recv(buf, 1024);
+    buf[len] = '\0';
+    if (!strncmp(buf, pri_field, strlen(pri_field))) {
+      found = true;
+    }
+  } while (!strstr(buf, msg));
+  return found;
+}
+
+void StopUnixServer() { delete server; }
diff --git a/src/log/apitest/log_server.h b/src/log/apitest/log_server.h
new file mode 100644
index 000000000..e36f885fa
--- /dev/null
+++ b/src/log/apitest/log_server.h
@@ -0,0 +1,35 @@
+/*      -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2020 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ * Author(s): Ericsson AB
+ *
+ */
+
+#ifndef LOG_APITEST_LOG_SERVER_H_
+#define LOG_APITEST_LOG_SERVER_H_
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void StartUnixServer();
+bool FindPRI(const char* pri_field, const char* msg);
+void StopUnixServer();
+
+#ifdef __cplusplus
+}  // closing brace for extern "C"
+#endif
+
+#endif  // LOG_APITEST_LOG_SERVER_H_
diff --git a/src/log/apitest/tet_saLogStreamConfigFacilityId.c 
b/src/log/apitest/tet_saLogStreamConfigFacilityId.c
new file mode 100644
index 000000000..276642d69
--- /dev/null
+++ b/src/log/apitest/tet_saLogStreamConfigFacilityId.c
@@ -0,0 +1,273 @@
+/*      -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2020 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ * Author(s): Ericsson AB
+ *
+ */
+
+#include "log/apitest/log_server.h"
+#include "log/apitest/logtest.h"
+
+typedef enum { ACTIVE_NODE = 0, STANDBY_NODE = 1, PAYLOAD = 2 } role;
+
+static bool set_facility_id(uint32_t value)
+{
+       char command[1000] = {0};
+       sprintf(command, "immcfg -a saLogStreamFacilityId=%d %s 2>/dev/null",
+               value, SA_LOG_STREAM_SYSTEM);
+       return systemCall(command) == EXIT_SUCCESS;
+}
+
+static void restore_facility_id()
+{
+       const char *command =
+           "immcfg -a saLogStreamFacilityId= " SA_LOG_STREAM_SYSTEM;
+       (void)systemCall(command);
+}
+
+static void enable_streaming()
+{
+       const char dest[] = "test;UNIX_SOCKET;/tmp/test.sock";
+       char command[1000];
+
+       memset(command, 0, sizeof(command));
+       sprintf(
+           command,
+           "immcfg -a logRecordDestinationConfiguration=\"%s\" %s 2>/dev/null",
+           dest, SA_LOG_CONFIGURATION_OBJECT);
+       (void)systemCall(command);
+
+       memset(command, 0, sizeof(command));
+       sprintf(command, "immcfg -a saLogRecordDestination=test %s 2>/dev/null",
+               SA_LOG_STREAM_SYSTEM);
+       (void)systemCall(command);
+}
+
+static void stop_streaming()
+{
+       char command[1000];
+       /* Restore the value back to the default value */
+       memset(command, 0, sizeof(command));
+       sprintf(command, "immcfg -a saLogRecordDestination= %s 2>/dev/null",
+               SA_LOG_STREAM_SYSTEM);
+       (void)systemCall(command);
+
+       memset(command, 0, sizeof(command));
+       sprintf(command,
+               "immcfg -a logRecordDestinationConfiguration= %s 2>/dev/null",
+               SA_LOG_CONFIGURATION_OBJECT);
+       (void)systemCall(command);
+       restore_facility_id();
+}
+
+static void switch_over()
+{
+       const char *command =
+           "amf-adm si-swap safSi=SC-2N,safApp=OpenSAF 2>/dev/null";
+       (void)systemCall(command);
+}
+
+static uint8_t get_role()
+{
+       char node[256];
+       sprintf(node, "SC-%d", get_active_sc());
+       if (strncmp("PL", hostname(), 2) == 0) {
+               return PAYLOAD;
+       } else {
+               if (strcmp(node, hostname()) == 0)
+                       return ACTIVE_NODE;
+               return STANDBY_NODE;
+       }
+}
+
+//>
+// 02 test cases about configuring `saLogStreamFacilityId`:
+// 1) try to set a valid value, expect getting OK.
+// 2) try to set an invalid value, expect getting NOK.
+//<
+
+// TC#1: Set a valid value to `saLogStreamFacilityId`
+void config_saLogStreamFacilityId_1()
+{
+       test_validate(set_facility_id(4), true);
+       restore_facility_id();
+}
+
+// TC#2: Set an invalid value to `saLogStreamFacilityId`
+void config_saLogStreamFacilityId_2()
+{
+       test_validate(set_facility_id(24), false);
+}
+
+//>
+// TC#3: Verify if PRI = 38 (saLogStreamFacilityId * 8 + severity) in package
+// RFC5424 format.The test must run on active node.
+//
+// Change `saLogStreamFacilityId = 4` and send the log record with severity = 6
+//<
+void streaming_log_record_then_verify_PRI_1()
+{
+       if (get_role() != ACTIVE_NODE) {
+               test_validate(true, true);
+               return;
+       }
+
+       enable_streaming();
+       set_facility_id(4);
+       StartUnixServer();
+
+       strcpy((char *)genLogRecord.logBuffer->logBuf, __FUNCTION__);
+       genLogRecord.logBuffer->logBufSize = strlen(__FUNCTION__);
+       genLogRecord.logHeader.genericHdr.logSeverity = SA_LOG_SEV_INFO;
+
+       rc = logInitialize();
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       rc = logStreamOpen(&systemStreamName);
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       rc = logWriteAsync(&genLogRecord);
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       test_validate(FindPRI("<38>", (char *)__FUNCTION__), true);
+
+done:
+       stop_streaming();
+       logStreamClose();
+       logFinalize();
+       StopUnixServer();
+}
+
+//>
+// TC#4: Verify if PRI = 134 (saLogStreamFacilityId (16)* 8 + severity) in
+// package RFC5424 format.The test must run on active node.
+//
+// Delete attribute `saLogStreamFacilityId` and send the log record with
+// severity = 6(information message)
+//<
+void streaming_log_record_then_verify_PRI_2()
+{
+       if (get_role() != ACTIVE_NODE) {
+               test_validate(true, true);
+               return;
+       }
+
+       enable_streaming();
+       restore_facility_id();
+       StartUnixServer();
+
+       strcpy((char *)genLogRecord.logBuffer->logBuf, __FUNCTION__);
+       genLogRecord.logBuffer->logBufSize = strlen(__FUNCTION__);
+       genLogRecord.logHeader.genericHdr.logSeverity = SA_LOG_SEV_INFO;
+
+       rc = logInitialize();
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       rc = logStreamOpen(&systemStreamName);
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       rc = logWriteAsync(&genLogRecord);
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       test_validate(FindPRI("<134>", (char *)__FUNCTION__), true);
+
+done:
+       stop_streaming();
+       logStreamClose();
+       logFinalize();
+       StopUnixServer();
+}
+
+//>
+// TC#5: This test case verify if the PRI=38 after trigger switch-over and
+// streaming log record ('PRI'field in package RFC5425 ).The test must run
+// on standby node.
+//<
+void streaming_log_record_then_verify_PRI_3()
+{
+       if (get_role() != STANDBY_NODE) {
+               test_validate(true, true);
+               return;
+       }
+
+       enable_streaming();
+       set_facility_id(4);
+       switch_over();
+       StartUnixServer();
+
+       strcpy((char *)genLogRecord.logBuffer->logBuf, __FUNCTION__);
+       genLogRecord.logBuffer->logBufSize = strlen(__FUNCTION__);
+       genLogRecord.logHeader.genericHdr.logSeverity = SA_LOG_SEV_INFO;
+
+       rc = logInitialize();
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       rc = logStreamOpen(&systemStreamName);
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       rc = logWriteAsync(&genLogRecord);
+       if (rc != SA_AIS_OK) {
+               test_validate(rc, SA_AIS_OK);
+               goto done;
+       }
+
+       test_validate(FindPRI("<38>", (char *)__FUNCTION__), true);
+
+done:
+       stop_streaming();
+       logStreamClose();
+       logFinalize();
+       StopUnixServer();
+}
+
+__attribute__((constructor)) static void saLibraryLifeCycle_constructor(void)
+{
+       test_suite_add(22, "Test log stream config facility id");
+       test_case_add(22, config_saLogStreamFacilityId_1,
+                     "Set a valid value to saLogStreamFacilityId");
+       test_case_add(22, config_saLogStreamFacilityId_2,
+                     "Set a invalid value to saLogStreamFacilityId");
+       test_case_add(22, streaming_log_record_then_verify_PRI_1,
+                     "Streaming with configure saLogStreamFacilityId=4 and "
+                     "verify PRI field");
+       test_case_add(
+           22, streaming_log_record_then_verify_PRI_2,
+           "Delelte attribute saLogStreamFacilityId and verify PRI field");
+       test_case_add(22, streaming_log_record_then_verify_PRI_3,
+                     "Trigger switch-over then verify PRI field in package "
+                     "RFC5424 format");
+}
diff --git a/src/log/tests/lgs_dest_test.cc b/src/log/tests/lgs_dest_test.cc
index 3f1b90e57..6ef27ac0d 100644
--- a/src/log/tests/lgs_dest_test.cc
+++ b/src/log/tests/lgs_dest_test.cc
@@ -147,6 +147,7 @@ static const char rec[] = "hello world";
 static const char hostname[] = "sc-1";
 static const char networkname[] = "networkA";
 static const uint16_t sev = 3;
+static const uint32_t facilityId = 4;
 static const char appname[] = "lgs_dest_test";
 static const char dn[] = "safLgStrCfg=test";
 static const char msgid[] = "testC";
@@ -162,6 +163,7 @@ void initData(RecordData* data) {
   data->recordId = 1;
   data->sev = 3;
   data->time = base::ReadRealtimeClock();
+  data->facilityId = facilityId;
 }
 
 // No destination name & no destination configuration exist
@@ -258,6 +260,7 @@ TEST(WriteToDestination, HaveDestNameAndDestCfg) {
   info.severity = data.sev;
   info.time = data.time;
   info.origin = origin.c_str();
+  info.facilityId = data.facilityId;
 
   WriteToDestination(data, {"test"});
 
-- 
2.17.1



_______________________________________________
Opensaf-devel mailing list
Opensaf-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to