Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "extensions/openpower-pels/data_interface.hpp" |
| 17 | #include "extensions/openpower-pels/host_notifier.hpp" |
| 18 | #include "mocks.hpp" |
| 19 | #include "pel_utils.hpp" |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <chrono> |
| 26 | |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | using namespace openpower::pels; |
| 30 | using ::testing::_; |
| 31 | using ::testing::Invoke; |
| 32 | using ::testing::Return; |
| 33 | namespace fs = std::filesystem; |
| 34 | using namespace std::chrono; |
| 35 | |
| 36 | const size_t actionFlags0Offset = 66; |
| 37 | const size_t actionFlags1Offset = 67; |
| 38 | |
| 39 | class HostNotifierTest : public CleanPELFiles |
| 40 | { |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * @brief Create PEL with the specified action flags |
| 45 | * |
| 46 | * @param[in] actionFlagsMask - Optional action flags to use |
| 47 | * |
| 48 | * @return std::unique_ptr<PEL> |
| 49 | */ |
| 50 | std::unique_ptr<PEL> makePEL(uint16_t actionFlagsMask = 0) |
| 51 | { |
| 52 | static uint32_t obmcID = 1; |
| 53 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 54 | |
| 55 | data[actionFlags0Offset] |= actionFlagsMask >> 8; |
| 56 | data[actionFlags1Offset] |= actionFlagsMask & 0xFF; |
| 57 | |
| 58 | auto pel = std::make_unique<PEL>(data, obmcID++); |
| 59 | pel->assignID(); |
| 60 | pel->setCommitTime(); |
| 61 | return pel; |
| 62 | } |
| 63 | |
| 64 | // Test that host state change callbacks work |
| 65 | TEST_F(HostNotifierTest, TestHostStateChange) |
| 66 | { |
| 67 | MockDataInterface dataIface; |
| 68 | |
| 69 | bool hostState = false; |
| 70 | bool called = false; |
| 71 | DataInterfaceBase::HostStateChangeFunc func = [&hostState, |
| 72 | &called](bool state) { |
| 73 | hostState = state; |
| 74 | called = true; |
| 75 | }; |
| 76 | |
| 77 | dataIface.subscribeToHostStateChange("test", func); |
| 78 | |
| 79 | // callback called |
| 80 | dataIface.changeHostState(true); |
| 81 | EXPECT_TRUE(called); |
| 82 | EXPECT_TRUE(hostState); |
| 83 | |
| 84 | // No change, not called |
| 85 | called = false; |
| 86 | dataIface.changeHostState(true); |
| 87 | EXPECT_FALSE(called); |
| 88 | |
| 89 | // Called again |
| 90 | dataIface.changeHostState(false); |
| 91 | EXPECT_FALSE(hostState); |
| 92 | EXPECT_TRUE(called); |
| 93 | |
| 94 | // Shouldn't get called after an unsubscribe |
| 95 | dataIface.unsubscribeFromHostStateChange("test"); |
| 96 | |
| 97 | called = false; |
| 98 | |
| 99 | dataIface.changeHostState(true); |
| 100 | EXPECT_FALSE(called); |
| 101 | } |
| 102 | |
| 103 | // Test that PELs are enqueued on startup |
| 104 | TEST_F(HostNotifierTest, TestStartup) |
| 105 | { |
| 106 | Repository repo{repoPath}; |
| 107 | MockDataInterface dataIface; |
| 108 | |
| 109 | // Give the repo 10 PELs to start with |
| 110 | for (int i = 0; i < 10; i++) |
| 111 | { |
| 112 | auto pel = makePEL(); |
| 113 | repo.add(pel); |
| 114 | } |
| 115 | |
| 116 | sd_event* event = nullptr; |
| 117 | auto r = sd_event_default(&event); |
| 118 | ASSERT_TRUE(r >= 0); |
| 119 | |
| 120 | std::unique_ptr<HostInterface> hostIface = |
| 121 | std::make_unique<MockHostInterface>(event, dataIface); |
| 122 | |
| 123 | HostNotifier notifier{repo, dataIface, std::move(hostIface)}; |
| 124 | |
| 125 | ASSERT_EQ(notifier.queueSize(), 10); |
| 126 | |
| 127 | // Now add 10 more after the notifier is watching |
| 128 | for (int i = 0; i < 10; i++) |
| 129 | { |
| 130 | auto pel = makePEL(); |
| 131 | repo.add(pel); |
| 132 | } |
| 133 | |
| 134 | ASSERT_EQ(notifier.queueSize(), 20); |
| 135 | } |