Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2020 YADRO |
| 3 | |
| 4 | #include "config.hpp" |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | // Names of environment variables |
| 9 | static const char* SOCKET_ID = "SOCKET_ID"; |
| 10 | static const char* BUF_MAXSIZE = "BUF_MAXSIZE"; |
| 11 | static const char* BUF_MAXTIME = "BUF_MAXTIME"; |
| 12 | static const char* FLUSH_FULL = "FLUSH_FULL"; |
| 13 | static const char* HOST_STATE = "HOST_STATE"; |
| 14 | static const char* OUT_DIR = "OUT_DIR"; |
| 15 | static const char* MAX_FILES = "MAX_FILES"; |
| 16 | |
| 17 | /** |
| 18 | * @class ConfigTest |
| 19 | * @brief Configuration tests. |
| 20 | */ |
| 21 | class ConfigTest : public ::testing::Test |
| 22 | { |
| 23 | protected: |
| 24 | void SetUp() override |
| 25 | { |
| 26 | resetEnv(); |
| 27 | } |
| 28 | |
| 29 | void TearDown() override |
| 30 | { |
| 31 | resetEnv(); |
| 32 | } |
| 33 | |
| 34 | /** @brief Reset environment variables. */ |
| 35 | void resetEnv() const |
| 36 | { |
| 37 | unsetenv(SOCKET_ID); |
| 38 | unsetenv(BUF_MAXSIZE); |
| 39 | unsetenv(BUF_MAXTIME); |
| 40 | unsetenv(FLUSH_FULL); |
| 41 | unsetenv(HOST_STATE); |
| 42 | unsetenv(OUT_DIR); |
| 43 | unsetenv(MAX_FILES); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | TEST_F(ConfigTest, Defaults) |
| 48 | { |
| 49 | Config cfg; |
| 50 | EXPECT_STREQ(cfg.socketId, ""); |
| 51 | EXPECT_EQ(cfg.bufMaxSize, 3000); |
| 52 | EXPECT_EQ(cfg.bufMaxTime, 0); |
| 53 | EXPECT_EQ(cfg.bufFlushFull, false); |
| 54 | EXPECT_STREQ(cfg.hostState, "/xyz/openbmc_project/state/host0"); |
| 55 | EXPECT_STREQ(cfg.outDir, "/var/lib/obmc/hostlogs"); |
| 56 | EXPECT_EQ(cfg.maxFiles, 10); |
| 57 | } |
| 58 | |
| 59 | TEST_F(ConfigTest, Load) |
| 60 | { |
| 61 | setenv(SOCKET_ID, "id123", 1); |
| 62 | setenv(BUF_MAXSIZE, "1234", 1); |
| 63 | setenv(BUF_MAXTIME, "4321", 1); |
| 64 | setenv(FLUSH_FULL, "true", 1); |
| 65 | setenv(HOST_STATE, "host123", 1); |
| 66 | setenv(OUT_DIR, "path123", 1); |
| 67 | setenv(MAX_FILES, "1122", 1); |
| 68 | |
| 69 | Config cfg; |
| 70 | EXPECT_STREQ(cfg.socketId, "id123"); |
| 71 | EXPECT_EQ(cfg.bufMaxSize, 1234); |
| 72 | EXPECT_EQ(cfg.bufMaxTime, 4321); |
| 73 | EXPECT_EQ(cfg.bufFlushFull, true); |
| 74 | EXPECT_STREQ(cfg.hostState, "host123"); |
| 75 | EXPECT_STREQ(cfg.outDir, "path123"); |
| 76 | EXPECT_EQ(cfg.maxFiles, 1122); |
| 77 | } |
| 78 | |
| 79 | TEST_F(ConfigTest, InvalidNumeric) |
| 80 | { |
| 81 | setenv(BUF_MAXSIZE, "-1234", 1); |
| 82 | ASSERT_THROW(Config(), std::invalid_argument); |
| 83 | } |
| 84 | |
| 85 | TEST_F(ConfigTest, InvalidBoolean) |
| 86 | { |
| 87 | setenv(FLUSH_FULL, "invalid", 1); |
| 88 | ASSERT_THROW(Config(), std::invalid_argument); |
| 89 | } |
| 90 | |
| 91 | TEST_F(ConfigTest, InvalidConfig) |
| 92 | { |
| 93 | setenv(BUF_MAXSIZE, "0", 1); |
| 94 | setenv(BUF_MAXTIME, "0", 1); |
| 95 | setenv(FLUSH_FULL, "true", 1); |
| 96 | ASSERT_THROW(Config(), std::invalid_argument); |
| 97 | } |