log_ut.cpp 5.5 KB
Newer Older
H
haozi007 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/******************************************************************************
 * iSula-libutils: utils library for iSula
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
 *
 * Authors:
 * Haozi007 <liuhao27@huawei.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ********************************************************************************/
#include <gtest/gtest.h>

#include <iostream>

#include <string.h>
#include <unistd.h>

#include "log.h"
#include "utils.h"

TEST(log_testcases, test_isula_libutils_default_log_config)
{
    struct isula_libutils_log_config tconf = {0};
    const char *name = "test";

    // quiet configs check
    tconf.quiet = true;
    isula_libutils_default_log_config(name, &tconf);

    ASSERT_EQ(tconf.file, nullptr);
    ASSERT_EQ(tconf.driver, nullptr);
    EXPECT_STREQ(name, tconf.name);
H
haozi007 已提交
45
    EXPECT_STREQ("FATAL", tconf.priority);
H
haozi007 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    // not quiet configs check
    tconf.quiet = false;
    isula_libutils_default_log_config(name, &tconf);

    ASSERT_EQ(tconf.file, nullptr);
    ASSERT_NE(tconf.driver, nullptr);
    EXPECT_STREQ(name, tconf.name);
    EXPECT_STREQ(ISULA_LOG_DRIVER_STDOUT, tconf.driver);
}

static void check_log(int fd, bool exist, bool subcheck, const char *target)
{
    char buf[LXC_LOG_BUFFER_SIZE] = {0};
    ssize_t rlen;
    char *found = nullptr;

    rlen = lcr_util_read_nointr(fd, buf, LXC_LOG_BUFFER_SIZE);
    if (exist) {
        ASSERT_GT(rlen, 0);
    } else {
        ASSERT_LT(rlen, 0);
    }
    found = strstr(buf, target);
    if (subcheck) {
        ASSERT_NE(found, nullptr);
    } else {
        ASSERT_EQ(found, nullptr);
    }
}

TEST(log_testcases, test_isula_libutils_log_enable)
{
    struct isula_libutils_log_config tconf = {0};
    const char *prefix = "fake";
    const char *prio = "INFO";
H
haozi007 已提交
82
    const char *invalid_prio = "INVALID";
H
haozi007 已提交
83 84
    const char *fname = "/tmp/fake.fifo";
    int fd = -1;
H
haozi007 已提交
85 86 87 88 89 90
    int ret = 0;

    ret = isula_libutils_log_enable(nullptr);
    ASSERT_NE(ret, 0);
    fd = isula_libutils_get_log_fd();
    ASSERT_EQ(fd, -1);
H
haozi007 已提交
91 92 93 94

    tconf.driver = ISULA_LOG_DRIVER_FIFO;
    tconf.prefix = prefix;
    tconf.priority = prio;
H
haozi007 已提交
95 96 97 98 99 100 101 102 103
    tconf.file = nullptr;
    ret = isula_libutils_log_enable(&tconf);
    ASSERT_NE(ret, 0);
    fd = isula_libutils_get_log_fd();
    ASSERT_EQ(fd, -1);

    tconf.driver = nullptr;
    tconf.prefix = prefix;
    tconf.priority = prio;
H
haozi007 已提交
104
    tconf.file = fname;
H
haozi007 已提交
105 106 107 108
    ret = isula_libutils_log_enable(&tconf);
    ASSERT_EQ(ret, 0);
    fd = isula_libutils_get_log_fd();
    ASSERT_EQ(fd, -1);
H
haozi007 已提交
109

H
haozi007 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    tconf.driver = ISULA_LOG_DRIVER_STDOUT;
    tconf.prefix = prefix;
    tconf.priority = prio;
    tconf.file = fname;
    ret = isula_libutils_log_enable(&tconf);
    ASSERT_NE(ret, 0);
    isula_libutils_log_disable();

    tconf.driver = ISULA_LOG_DRIVER_STDOUT;
    tconf.prefix = prefix;
    tconf.priority = prio;
    tconf.file = nullptr;
    ret = isula_libutils_log_enable(&tconf);
    ASSERT_EQ(ret, 0);
    TRACE("trace log");
    DEBUG("debug log");
    INFO("info log");
    NOTICE("notice log");
    WARN("warn log");
    ERROR("error log");
    EVENT("event log");
    CRIT("crit log");
    FATAL("fatal log");
    isula_libutils_log_disable();

H
haozi007 已提交
135 136 137 138 139 140
    tconf.driver = ISULA_LOG_DRIVER_FIFO;
    tconf.prefix = prefix;
    tconf.priority = invalid_prio;
    tconf.file = fname;
    ret = isula_libutils_log_enable(&tconf);
    ASSERT_EQ(ret, 0);
H
haozi007 已提交
141 142
    fd = isula_libutils_get_log_fd();
    ASSERT_GE(fd, 0);
H
haozi007 已提交
143 144 145
    DEBUG("debug log");
    check_log(fd, false, false, "debug log");
    isula_libutils_log_disable();
H
haozi007 已提交
146

H
haozi007 已提交
147 148 149 150 151 152 153 154
    tconf.driver = ISULA_LOG_DRIVER_FIFO;
    tconf.prefix = prefix;
    tconf.priority = prio;
    tconf.file = fname;
    ret = isula_libutils_log_enable(&tconf);
    ASSERT_EQ(ret, 0);
    fd = isula_libutils_get_log_fd();
    ASSERT_GE(fd, 0);
H
haozi007 已提交
155 156 157 158
    INFO("info log");
    check_log(fd, true, true, "info log");
    DEBUG("debug log");
    check_log(fd, false, false, "debug log");
H
haozi007 已提交
159
    isula_libutils_log_disable();
H
haozi007 已提交
160 161 162 163 164
}

TEST(log_testcases, test_isula_libutils_log_prefix)
{
    struct isula_libutils_log_config tconf = {0};
H
haozi007 已提交
165
    const char *default_prefix = "iSula";
H
haozi007 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    const char *prefix = "prefix";
    const char *prio = "INFO";
    const char *fname = "/tmp/fake.fifo";
    int fd = -1;

    tconf.driver = ISULA_LOG_DRIVER_FIFO;
    tconf.priority = prio;
    tconf.file = fname;
    isula_libutils_log_enable(&tconf);

    fd = isula_libutils_get_log_fd();
    ASSERT_GE(fd, 0);

    isula_libutils_set_log_prefix(prefix);
    INFO("fake log");
    check_log(fd, true, true, prefix);

    isula_libutils_free_log_prefix();
    INFO("fake log");
    check_log(fd, true, false, prefix);
H
haozi007 已提交
186 187 188 189 190 191 192 193 194 195
    INFO("fake log");
    check_log(fd, true, true, default_prefix);

    isula_libutils_set_log_prefix(nullptr);
    INFO("fake log");
    check_log(fd, true, true, default_prefix);

    isula_libutils_set_log_prefix("");
    INFO("fake log");
    check_log(fd, true, true, default_prefix);
H
haozi007 已提交
196 197
}