Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 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 "action_environment.hpp" |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 17 | #include "action_error.hpp" |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 18 | #include "compare_vpd_action.hpp" |
| 19 | #include "id_map.hpp" |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 20 | #include "mock_services.hpp" |
| 21 | #include "mock_vpd.hpp" |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 22 | |
| 23 | #include <exception> |
| 24 | #include <memory> |
| 25 | #include <stdexcept> |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 26 | #include <string> |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 27 | #include <utility> |
| 28 | |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 29 | #include <gmock/gmock.h> |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 30 | #include <gtest/gtest.h> |
| 31 | |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 32 | using ::testing::Return; |
| 33 | using ::testing::Throw; |
| 34 | |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 35 | using namespace phosphor::power::regulators; |
| 36 | |
| 37 | TEST(CompareVPDActionTests, Constructor) |
| 38 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 39 | CompareVPDAction action{ |
| 40 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
| 41 | "2D35"}; |
| 42 | EXPECT_EQ(action.getFRU(), |
| 43 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane"); |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 44 | EXPECT_EQ(action.getKeyword(), "CCIN"); |
| 45 | EXPECT_EQ(action.getValue(), "2D35"); |
| 46 | } |
| 47 | |
| 48 | TEST(CompareVPDActionTests, Execute) |
| 49 | { |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 50 | // Test where works: Actual VPD value is not an empty string |
| 51 | { |
| 52 | std::string fru{"/xyz/openbmc_project/inventory/system"}; |
| 53 | std::string keyword{"Model"}; |
| 54 | |
| 55 | // Create MockServices object. VPD service will return "ABCD" as VPD |
| 56 | // value 4 times. |
| 57 | MockServices services{}; |
| 58 | MockVPD& vpd = services.getMockVPD(); |
| 59 | EXPECT_CALL(vpd, getValue(fru, keyword)) |
| 60 | .Times(4) |
| 61 | .WillRepeatedly(Return("ABCD")); |
| 62 | |
| 63 | IDMap idMap{}; |
| 64 | ActionEnvironment environment{idMap, "", services}; |
| 65 | |
| 66 | // Test where returns true: actual value == expected value |
| 67 | { |
| 68 | CompareVPDAction action{fru, keyword, "ABCD"}; |
| 69 | EXPECT_TRUE(action.execute(environment)); |
| 70 | } |
| 71 | |
| 72 | // Test where returns false: actual value != expected value |
| 73 | { |
| 74 | CompareVPDAction action{fru, keyword, "BEEF"}; |
| 75 | EXPECT_FALSE(action.execute(environment)); |
| 76 | } |
| 77 | |
| 78 | // Test where returns false: expected value differs by case |
| 79 | { |
| 80 | CompareVPDAction action{fru, keyword, "abcd"}; |
| 81 | EXPECT_FALSE(action.execute(environment)); |
| 82 | } |
| 83 | |
| 84 | // Test where returns false: expected value is an empty string |
| 85 | { |
| 86 | CompareVPDAction action{fru, keyword, ""}; |
| 87 | EXPECT_FALSE(action.execute(environment)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Test where works: Actual VPD value is an empty string |
| 92 | { |
| 93 | std::string fru{"/xyz/openbmc_project/inventory/system"}; |
| 94 | std::string keyword{"Model"}; |
| 95 | |
| 96 | // Create MockServices object. VPD service will return "" as VPD value |
| 97 | // 2 times. |
| 98 | MockServices services{}; |
| 99 | MockVPD& vpd = services.getMockVPD(); |
| 100 | EXPECT_CALL(vpd, getValue(fru, keyword)) |
| 101 | .Times(2) |
| 102 | .WillRepeatedly(Return("")); |
| 103 | |
| 104 | IDMap idMap{}; |
| 105 | ActionEnvironment environment{idMap, "", services}; |
| 106 | |
| 107 | // Test where returns true: actual value == expected value |
| 108 | { |
| 109 | CompareVPDAction action{fru, keyword, ""}; |
| 110 | EXPECT_TRUE(action.execute(environment)); |
| 111 | } |
| 112 | |
| 113 | // Test where returns false: actual value != expected value |
| 114 | { |
| 115 | CompareVPDAction action{fru, keyword, "ABCD"}; |
| 116 | EXPECT_FALSE(action.execute(environment)); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Test where fails: Exception thrown when trying to get actual VPD value |
| 121 | { |
| 122 | std::string fru{"/xyz/openbmc_project/inventory/system"}; |
| 123 | std::string keyword{"Model"}; |
| 124 | |
| 125 | // Create MockServices object. VPD service will throw an exception. |
| 126 | MockServices services{}; |
| 127 | MockVPD& vpd = services.getMockVPD(); |
| 128 | EXPECT_CALL(vpd, getValue(fru, keyword)) |
| 129 | .Times(1) |
| 130 | .WillOnce( |
| 131 | Throw(std::runtime_error{"D-Bus error: Invalid object path"})); |
| 132 | |
| 133 | IDMap idMap{}; |
| 134 | ActionEnvironment environment{idMap, "", services}; |
| 135 | |
| 136 | try |
| 137 | { |
| 138 | CompareVPDAction action{fru, keyword, "ABCD"}; |
| 139 | action.execute(environment); |
| 140 | ADD_FAILURE() << "Should not have reached this line."; |
| 141 | } |
| 142 | catch (const ActionError& e) |
| 143 | { |
| 144 | EXPECT_STREQ(e.what(), "ActionError: compare_vpd: { fru: " |
| 145 | "/xyz/openbmc_project/inventory/system, " |
| 146 | "keyword: Model, value: ABCD }"); |
| 147 | try |
| 148 | { |
| 149 | // Re-throw inner exception |
| 150 | std::rethrow_if_nested(e); |
| 151 | ADD_FAILURE() << "Should not have reached this line."; |
| 152 | } |
| 153 | catch (const std::runtime_error& re) |
| 154 | { |
| 155 | EXPECT_STREQ(re.what(), "D-Bus error: Invalid object path"); |
| 156 | } |
| 157 | catch (...) |
| 158 | { |
| 159 | ADD_FAILURE() << "Should not have caught exception."; |
| 160 | } |
| 161 | } |
| 162 | catch (...) |
| 163 | { |
| 164 | ADD_FAILURE() << "Should not have caught exception."; |
| 165 | } |
| 166 | } |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | TEST(CompareVPDActionTests, GetFRU) |
| 170 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 171 | CompareVPDAction action{ |
| 172 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
| 173 | "2D35"}; |
| 174 | EXPECT_EQ(action.getFRU(), |
| 175 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane"); |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST(CompareVPDActionTests, GetKeyword) |
| 179 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 180 | CompareVPDAction action{ |
| 181 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
| 182 | "2D35"}; |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 183 | EXPECT_EQ(action.getKeyword(), "CCIN"); |
| 184 | } |
| 185 | |
| 186 | TEST(CompareVPDActionTests, GetValue) |
| 187 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 188 | CompareVPDAction action{ |
| 189 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
| 190 | "2D35"}; |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 191 | EXPECT_EQ(action.getValue(), "2D35"); |
| 192 | } |
| 193 | |
| 194 | TEST(CompareVPDActionTests, ToString) |
| 195 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 196 | CompareVPDAction action{ |
| 197 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
| 198 | "2D35"}; |
| 199 | EXPECT_EQ(action.toString(), "compare_vpd: { fru: " |
| 200 | "/xyz/openbmc_project/inventory/system/" |
| 201 | "chassis/disk_backplane, keyword: " |
| 202 | "CCIN, value: 2D35 }"); |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 203 | } |