Matt Spinler | 18c42b0 | 2020-06-02 15:59:50 -0500 | [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 | |
| 17 | #include "extensions/openpower-pels/device_callouts.hpp" |
| 18 | #include "extensions/openpower-pels/paths.hpp" |
| 19 | |
| 20 | #include <fstream> |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | using namespace openpower::pels; |
| 25 | using namespace openpower::pels::device_callouts; |
| 26 | namespace fs = std::filesystem; |
| 27 | |
| 28 | // The callout JSON looks like: |
| 29 | // "I2C": |
| 30 | // "<bus>": |
| 31 | // "<address>": |
| 32 | // "Callouts": ... |
| 33 | // |
| 34 | // "FSI": |
| 35 | // "<fsi link>": |
| 36 | // "Callouts": ... |
| 37 | // |
| 38 | // "FSI-I2C": |
| 39 | // "<fsi link>": |
| 40 | // "<bus>": |
| 41 | // "<address>": |
| 42 | // "Callouts": ... |
| 43 | // |
| 44 | // "FSI-SPI": |
| 45 | // "<fsi link>": |
| 46 | // "<bus>": |
| 47 | // "Callouts": ... |
| 48 | |
| 49 | const auto calloutJSON = R"( |
| 50 | { |
| 51 | "I2C": |
| 52 | { |
| 53 | "0": |
| 54 | { |
| 55 | "32": |
| 56 | { |
| 57 | "Callouts":[ |
| 58 | { |
| 59 | "Name": "/chassis/motherboard/cpu0", |
| 60 | "LocationCode": "P1-C19", |
| 61 | "Priority": "H" |
| 62 | } |
| 63 | ], |
| 64 | "Dest": "proc-0 target" |
| 65 | }, |
| 66 | "81": |
| 67 | { |
| 68 | "Callouts":[ |
| 69 | { |
| 70 | "Name": "/chassis/motherboard/cpu0", |
| 71 | "LocationCode": "P1-C19", |
| 72 | "Priority": "H" |
| 73 | } |
| 74 | ], |
| 75 | "Dest": "proc-0 target" |
| 76 | } |
| 77 | }, |
| 78 | "14": |
| 79 | { |
| 80 | "112": |
| 81 | { |
| 82 | "Callouts":[ |
| 83 | { |
| 84 | "Name": "/chassis/motherboard/cpu0", |
| 85 | "LocationCode": "P1-C19", |
| 86 | "Priority": "H" |
| 87 | } |
| 88 | ], |
| 89 | "Dest": "proc-0 target" |
| 90 | }, |
| 91 | "114": |
| 92 | { |
| 93 | "Callouts":[ |
| 94 | { |
| 95 | "Name": "/chassis/motherboard/cpu0", |
| 96 | "LocationCode": "P1-C19", |
| 97 | "Priority": "H" |
| 98 | }, |
| 99 | { |
| 100 | "Name": "/chassis/motherboard", |
| 101 | "LocationCode": "P1", |
| 102 | "Priority": "M" |
| 103 | } |
| 104 | ], |
| 105 | "Dest": "proc-0 target" |
| 106 | } |
| 107 | } |
| 108 | }, |
| 109 | "FSI": |
| 110 | { |
| 111 | "0": |
| 112 | { |
| 113 | "Callouts":[ |
| 114 | { |
| 115 | "Name": "/chassis/motherboard/cpu0", |
| 116 | "LocationCode": "P1-C19", |
| 117 | "Priority": "H" |
| 118 | } |
| 119 | ], |
| 120 | "Dest": "proc-0 target" |
| 121 | }, |
| 122 | "0-1": |
| 123 | { |
| 124 | "Callouts":[ |
| 125 | { |
| 126 | "Name": "/chassis/motherboard/cpu0", |
| 127 | "LocationCode": "P1-C19", |
| 128 | "Priority": "H", |
| 129 | "MRU": "core" |
| 130 | } |
| 131 | ], |
| 132 | "Dest": "proc-0 target" |
| 133 | } |
| 134 | }, |
| 135 | "FSI-I2C": |
| 136 | { |
| 137 | "0-3": |
| 138 | { |
| 139 | "7": |
| 140 | { |
| 141 | "24": |
| 142 | { |
| 143 | "Callouts":[ |
| 144 | { |
| 145 | "Name": "/chassis/motherboard/cpu0", |
| 146 | "LocationCode": "P1-C19", |
| 147 | "Priority": "H" |
| 148 | } |
| 149 | ], |
| 150 | "Dest": "proc-0 target" |
| 151 | }, |
| 152 | "25": |
| 153 | { |
| 154 | "Callouts":[ |
| 155 | { |
| 156 | "Name": "/chassis/motherboard/cpu5", |
| 157 | "LocationCode": "P1-C25", |
| 158 | "Priority": "H" |
| 159 | }, |
| 160 | { |
| 161 | "Name": "/chassis/motherboard", |
| 162 | "LocationCode": "P1", |
| 163 | "Priority": "M" |
| 164 | }, |
| 165 | { |
| 166 | "Name": "/chassis/motherboard/bmc", |
| 167 | "LocationCode": "P2", |
| 168 | "Priority": "L" |
| 169 | } |
| 170 | ], |
| 171 | "Dest": "proc-5 target" |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | }, |
| 176 | "FSI-SPI": |
| 177 | { |
| 178 | "8": |
| 179 | { |
| 180 | "3": |
| 181 | { |
| 182 | "Callouts":[ |
| 183 | { |
| 184 | "Name": "/chassis/motherboard/cpu0", |
| 185 | "LocationCode": "P1-C19", |
| 186 | "Priority": "H" |
| 187 | } |
| 188 | ], |
| 189 | "Dest": "proc-0 target" |
| 190 | }, |
| 191 | "4": |
| 192 | { |
| 193 | "Callouts":[ |
| 194 | { |
| 195 | "Name": "/chassis/motherboard/cpu0", |
| 196 | "LocationCode": "P1-C19", |
| 197 | "Priority": "H" |
| 198 | } |
| 199 | ], |
| 200 | "Dest": "proc-0 target" |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | })"_json; |
| 205 | |
| 206 | class DeviceCalloutsTest : public ::testing::Test |
| 207 | { |
| 208 | public: |
| 209 | static void SetUpTestCase() |
| 210 | { |
| 211 | dataPath = getPELReadOnlyDataPath(); |
| 212 | std::ofstream file{dataPath / filename}; |
| 213 | file << calloutJSON.dump(); |
| 214 | } |
| 215 | |
| 216 | static void TearDownTestCase() |
| 217 | { |
| 218 | fs::remove_all(dataPath); |
| 219 | } |
| 220 | |
| 221 | static std::string filename; |
| 222 | static fs::path dataPath; |
| 223 | }; |
| 224 | |
| 225 | std::string DeviceCalloutsTest::filename = "systemA_dev_callouts.json"; |
| 226 | fs::path DeviceCalloutsTest::dataPath; |
| 227 | |
| 228 | // Test looking up the JSON file based on the system compatible names |
| 229 | TEST_F(DeviceCalloutsTest, getJSONFilenameTest) |
| 230 | { |
| 231 | { |
| 232 | std::vector<std::string> compatibles{"system1", "systemA", "system3"}; |
| 233 | EXPECT_EQ(util::getJSONFilename(compatibles), |
| 234 | fs::path{dataPath / filename}); |
| 235 | } |
| 236 | |
| 237 | // Actual filename not in compatibles |
| 238 | { |
| 239 | std::vector<std::string> compatibles{"system5", "system6"}; |
| 240 | EXPECT_THROW(util::getJSONFilename(compatibles), std::invalid_argument); |
| 241 | } |
| 242 | } |
Matt Spinler | a307089 | 2020-06-03 10:52:32 -0500 | [diff] [blame^] | 243 | |
| 244 | // Test determining the callout type from the device path |
| 245 | TEST_F(DeviceCalloutsTest, getCalloutTypeTest) |
| 246 | { |
| 247 | |
| 248 | // Invalid |
| 249 | { |
| 250 | EXPECT_EQ(util::getCalloutType("/some/bad/device/path"), |
| 251 | util::CalloutType::unknown); |
| 252 | } |
| 253 | |
| 254 | // I2C |
| 255 | { |
| 256 | EXPECT_EQ(util::getCalloutType( |
| 257 | "/sys/devices/platform/ahb/ahb:apb/ahb:apb:bus@1e78a000/" |
| 258 | "1e78a340.i2c-bus/i2c-10/10-0022"), |
| 259 | util::CalloutType::i2c); |
| 260 | } |
| 261 | |
| 262 | // FSI |
| 263 | { |
| 264 | EXPECT_EQ(util::getCalloutType( |
| 265 | "/sys/devices/platform/ahb/ahb:apb/1e79b000.fsi/" |
| 266 | "fsi-master/fsi0/slave@00:00/00:00:00:0a/fsi-master/fsi1/" |
| 267 | "slave@01:00/01:01:00:06/sbefifo2-dev0/occ-hwmon.2"), |
| 268 | util::CalloutType::fsi); |
| 269 | } |
| 270 | |
| 271 | // FSI-I2C |
| 272 | { |
| 273 | EXPECT_EQ(util::getCalloutType( |
| 274 | "/sys/devices/platform/ahb/ahb:apb/1e79b000.fsi/" |
| 275 | "fsi-master/fsi0/slave@00:00/00:00:00:0a/fsi-master/fsi1/" |
| 276 | "slave@01:00/01:01:00:03/i2c-211/211-0055"), |
| 277 | util::CalloutType::fsii2c); |
| 278 | } |
| 279 | |
| 280 | // FSI-SPI |
| 281 | { |
| 282 | |
| 283 | EXPECT_EQ( |
| 284 | util::getCalloutType( |
| 285 | "/sys/devices/platform/ahb/ahb:apb/1e79b000.fsi/fsi-master/" |
| 286 | "fsi0/slave@00:00/00:00:00:0a/fsi-master/fsi1/slave@08:00/" |
| 287 | "01:03:00:04/spi_master/spi9/spi9.0/spi9.00/nvmem"), |
| 288 | util::CalloutType::fsispi); |
| 289 | } |
| 290 | } |